about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorAriel Ben-Yehuda <ariel.byd@gmail.com>2015-11-25 00:00:26 +0200
committerAriel Ben-Yehuda <arielb1@mail.tau.ac.il>2015-11-26 18:22:40 +0200
commit0a8bb4c509e513a87a1bc0c04e0b7b8e13b82d2f (patch)
treeac16b45ccda05e01d32fa44377add76aa716325b /src
parent1430a3500076ad504a0b30be77fd2ad4468ea769 (diff)
downloadrust-0a8bb4c509e513a87a1bc0c04e0b7b8e13b82d2f.tar.gz
rust-0a8bb4c509e513a87a1bc0c04e0b7b8e13b82d2f.zip
split the metadata code into rustc_metadata
tests & rustdoc still broken
Diffstat (limited to 'src')
-rw-r--r--src/librustc/diagnostics.rs60
-rw-r--r--src/librustc/front/map/definitions.rs2
-rw-r--r--src/librustc/front/map/mod.rs4
-rw-r--r--src/librustc/lib.rs5
-rw-r--r--src/librustc/metadata/csearch.rs408
-rw-r--r--src/librustc/metadata/inline.rs60
-rw-r--r--src/librustc/metadata/mod.rs23
-rw-r--r--src/librustc/middle/const_eval.rs12
-rw-r--r--src/librustc/middle/cstore.rs277
-rw-r--r--src/librustc/middle/def_id.rs2
-rw-r--r--src/librustc/middle/dependency_format.rs4
-rw-r--r--src/librustc/middle/infer/error_reporting.rs2
-rw-r--r--src/librustc/middle/lang_items.rs2
-rw-r--r--src/librustc/middle/region.rs2
-rw-r--r--src/librustc/middle/stability.rs3
-rw-r--r--src/librustc/middle/traits/coherence.rs2
-rw-r--r--src/librustc/middle/ty/context.rs2
-rw-r--r--src/librustc/middle/ty/mod.rs3
-rw-r--r--src/librustc/middle/weak_lang_items.rs2
-rw-r--r--src/librustc/session/config.rs2
-rw-r--r--src/librustc/session/filesearch.rs (renamed from src/librustc/metadata/filesearch.rs)0
-rw-r--r--src/librustc/session/mod.rs4
-rw-r--r--src/librustc_driver/driver.rs8
-rw-r--r--src/librustc_driver/lib.rs9
-rw-r--r--src/librustc_driver/pretty.rs2
-rw-r--r--src/librustc_lint/builtin.rs2
-rw-r--r--src/librustc_lint/lib.rs1
-rw-r--r--src/librustc_metadata/astencode.rs (renamed from src/librustc/middle/astencode.rs)28
-rw-r--r--src/librustc_metadata/common.rs (renamed from src/librustc/metadata/common.rs)8
-rw-r--r--src/librustc_metadata/creader.rs (renamed from src/librustc/metadata/creader.rs)45
-rw-r--r--src/librustc_metadata/csearch.rs (renamed from src/librustc/metadata/util.rs)153
-rw-r--r--src/librustc_metadata/cstore.rs (renamed from src/librustc/metadata/cstore.rs)51
-rw-r--r--src/librustc_metadata/decoder.rs (renamed from src/librustc/metadata/decoder.rs)38
-rw-r--r--src/librustc_metadata/diagnostics.rs77
-rw-r--r--src/librustc_metadata/encoder.rs (renamed from src/librustc/metadata/encoder.rs)26
-rw-r--r--src/librustc_metadata/index.rs (renamed from src/librustc/metadata/index.rs)0
-rw-r--r--src/librustc_metadata/lib.rs61
-rw-r--r--src/librustc_metadata/loader.rs (renamed from src/librustc/metadata/loader.rs)24
-rw-r--r--src/librustc_metadata/macro_import.rs (renamed from src/librustc/metadata/macro_import.rs)7
-rw-r--r--src/librustc_metadata/macros.rs46
-rw-r--r--src/librustc_metadata/tydecode.rs (renamed from src/librustc/metadata/tydecode.rs)0
-rw-r--r--src/librustc_metadata/tyencode.rs (renamed from src/librustc/metadata/tyencode.rs)2
-rw-r--r--src/librustc_plugin/lib.rs1
-rw-r--r--src/librustc_plugin/load.rs4
-rw-r--r--src/librustc_resolve/build_reduced_graph.rs2
-rw-r--r--src/librustc_resolve/lib.rs2
-rw-r--r--src/librustc_trans/back/archive.rs2
-rw-r--r--src/librustc_trans/back/link.rs9
-rw-r--r--src/librustc_trans/back/linker.rs2
-rw-r--r--src/librustc_trans/lib.rs1
-rw-r--r--src/librustc_trans/save/recorder.rs2
-rw-r--r--src/librustc_trans/trans/base.rs2
-rw-r--r--src/librustc_trans/trans/callee.rs2
-rw-r--r--src/librustc_trans/trans/consts.rs2
-rw-r--r--src/librustc_trans/trans/context.rs2
-rw-r--r--src/librustc_trans/trans/inline.rs2
-rw-r--r--src/librustc_trans/trans/mod.rs2
-rw-r--r--src/librustc_typeck/check/callee.rs2
-rw-r--r--src/librustc_typeck/check/method/suggest.rs10
-rw-r--r--src/librustc_typeck/check/mod.rs2
-rw-r--r--src/librustc_typeck/coherence/orphan.rs2
-rw-r--r--src/librustc_typeck/coherence/overlap.rs3
-rw-r--r--src/librustc_typeck/lib.rs1
63 files changed, 623 insertions, 903 deletions
diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs
index a088567b419..b4e188c498d 100644
--- a/src/librustc/diagnostics.rs
+++ b/src/librustc/diagnostics.rs
@@ -1899,51 +1899,6 @@ contain references (with a maximum lifetime of `'a`).
 [1]: https://github.com/rust-lang/rfcs/pull/1156
 "##,
 
-E0454: r##"
-A link name was given with an empty name. Erroneous code example:
-
-```
-#[link(name = "")] extern {} // error: #[link(name = "")] given with empty name
-```
-
-The rust compiler cannot link to an external library if you don't give it its
-name. Example:
-
-```
-#[link(name = "some_lib")] extern {} // ok!
-```
-"##,
-
-E0458: r##"
-An unknown "kind" was specified for a link attribute. Erroneous code example:
-
-```
-#[link(kind = "wonderful_unicorn")] extern {}
-// error: unknown kind: `wonderful_unicorn`
-```
-
-Please specify a valid "kind" value, from one of the following:
- * static
- * dylib
- * framework
-"##,
-
-E0459: r##"
-A link was used without a name parameter. Erroneous code example:
-
-```
-#[link(kind = "dylib")] extern {}
-// error: #[link(...)] specified without `name = "foo"`
-```
-
-Please add the name parameter to allow the rust compiler to find the library
-you want. Example:
-
-```
-#[link(kind = "dylib", name = "some_lib")] extern {} // ok!
-```
-"##,
-
 E0493: r##"
 A type with a destructor was assigned to an invalid type of variable. Erroneous
 code example:
@@ -2144,20 +2099,6 @@ register_diagnostics! {
     E0400, // overloaded derefs are not allowed in constants
     E0452, // malformed lint attribute
     E0453, // overruled by outer forbid
-    E0455, // native frameworks are only available on OSX targets
-    E0456, // plugin `..` is not available for triple `..`
-    E0457, // plugin `..` only found in rlib format, but must be available...
-    E0460, // found possibly newer version of crate `..`
-    E0461, // couldn't find crate `..` with expected target triple ..
-    E0462, // found staticlib `..` instead of rlib or dylib
-    E0463, // can't find crate for `..`
-    E0464, // multiple matching crates for `..`
-    E0465, // multiple .. candidates for `..` found
-    E0466, // bad macro import
-    E0467, // bad macro reexport
-    E0468, // an `extern crate` loading macros must be at the crate root
-    E0469, // imported macro not found
-    E0470, // reexported macro not found
     E0471, // constant evaluation error: ..
     E0472, // asm! is unsupported on this target
     E0473, // dereference of reference outside its lifetime
@@ -2181,5 +2122,4 @@ register_diagnostics! {
     E0491, // in type `..`, reference has a longer lifetime than the data it...
     E0492, // cannot borrow a constant which contains interior mutability
     E0495, // cannot infer an appropriate lifetime due to conflicting requirements
-    E0514, // metadata version mismatch
 }
diff --git a/src/librustc/front/map/definitions.rs b/src/librustc/front/map/definitions.rs
index c1eb5f1f118..0f0d59e70b0 100644
--- a/src/librustc/front/map/definitions.rs
+++ b/src/librustc/front/map/definitions.rs
@@ -8,7 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-use metadata::cstore::LOCAL_CRATE;
+use middle::cstore::LOCAL_CRATE;
 use middle::def_id::{DefId, DefIndex};
 use rustc_data_structures::fnv::FnvHashMap;
 use rustc_front::hir;
diff --git a/src/librustc/front/map/mod.rs b/src/librustc/front/map/mod.rs
index 6ee6b070597..8c3da2cddd5 100644
--- a/src/librustc/front/map/mod.rs
+++ b/src/librustc/front/map/mod.rs
@@ -14,8 +14,8 @@ use self::MapEntry::*;
 use self::collector::NodeCollector;
 pub use self::definitions::{Definitions, DefKey, DefPath, DefPathData, DisambiguatedDefPathData};
 
-use metadata::inline::InlinedItem;
-use metadata::inline::InlinedItem as II;
+use middle::cstore::InlinedItem;
+use middle::cstore::InlinedItem as II;
 use middle::def_id::DefId;
 
 use syntax::abi;
diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs
index 62646f6bc56..6da4f174e3e 100644
--- a/src/librustc/lib.rs
+++ b/src/librustc/lib.rs
@@ -67,7 +67,6 @@ extern crate rustc_back;
 extern crate rustc_front;
 extern crate rustc_data_structures;
 extern crate serialize;
-extern crate rbml;
 extern crate collections;
 #[macro_use] extern crate log;
 #[macro_use] extern crate syntax;
@@ -101,7 +100,6 @@ pub mod front {
 pub mod middle {
     pub mod astconv_util;
     pub mod expr_use_visitor; // STAGE0: increase glitch immunity
-    pub mod astencode;
     pub mod cfg;
     pub mod check_const;
     pub mod check_static_recursion;
@@ -110,6 +108,7 @@ pub mod middle {
     pub mod check_no_asm;
     pub mod check_rvalues;
     pub mod const_eval;
+    pub mod cstore;
     pub mod dataflow;
     pub mod dead;
     pub mod def;
@@ -137,8 +136,6 @@ pub mod middle {
     pub mod weak_lang_items;
 }
 
-pub mod metadata;
-
 pub mod session;
 
 pub mod lint;
diff --git a/src/librustc/metadata/csearch.rs b/src/librustc/metadata/csearch.rs
deleted file mode 100644
index 40159b24fe7..00000000000
--- a/src/librustc/metadata/csearch.rs
+++ /dev/null
@@ -1,408 +0,0 @@
-// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-// Searching for information from the cstore
-
-use front::map as ast_map;
-use metadata::cstore;
-use metadata::decoder;
-use metadata::inline::InlinedItem;
-use middle::def_id::{DefId, DefIndex};
-use middle::lang_items;
-use middle::ty;
-use util::nodemap::FnvHashMap;
-
-use std::rc::Rc;
-use syntax::ast;
-use syntax::attr;
-use rustc_front::hir;
-
-#[derive(Copy, Clone)]
-pub struct MethodInfo {
-    pub name: ast::Name,
-    pub def_id: DefId,
-    pub vis: hir::Visibility,
-}
-
-pub fn get_symbol(cstore: &cstore::CStore, def: DefId) -> String {
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_symbol(&cdata, def.index)
-}
-
-/// Iterates over all the language items in the given crate.
-pub fn each_lang_item<F>(cstore: &cstore::CStore,
-                         cnum: ast::CrateNum,
-                         f: F)
-                         -> bool where
-    F: FnMut(DefIndex, usize) -> bool,
-{
-    let crate_data = cstore.get_crate_data(cnum);
-    decoder::each_lang_item(&*crate_data, f)
-}
-
-/// Iterates over each child of the given item.
-pub fn each_child_of_item<F>(cstore: &cstore::CStore,
-                             def_id: DefId,
-                             callback: F) where
-    F: FnMut(decoder::DefLike, ast::Name, hir::Visibility),
-{
-    let crate_data = cstore.get_crate_data(def_id.krate);
-    let get_crate_data = |cnum| {
-        cstore.get_crate_data(cnum)
-    };
-    decoder::each_child_of_item(cstore.intr.clone(),
-                                &*crate_data,
-                                def_id.index,
-                                get_crate_data,
-                                callback)
-}
-
-/// Iterates over each top-level crate item.
-pub fn each_top_level_item_of_crate<F>(cstore: &cstore::CStore,
-                                       cnum: ast::CrateNum,
-                                       callback: F) where
-    F: FnMut(decoder::DefLike, ast::Name, hir::Visibility),
-{
-    let crate_data = cstore.get_crate_data(cnum);
-    let get_crate_data = |cnum| {
-        cstore.get_crate_data(cnum)
-    };
-    decoder::each_top_level_item_of_crate(cstore.intr.clone(),
-                                          &*crate_data,
-                                          get_crate_data,
-                                          callback)
-}
-
-pub fn get_item_path(tcx: &ty::ctxt, def: DefId) -> Vec<ast_map::PathElem> {
-    let cstore = &tcx.sess.cstore;
-    let cdata = cstore.get_crate_data(def.krate);
-    let path = decoder::get_item_path(&*cdata, def.index);
-
-    cdata.with_local_path(|cpath| {
-        let mut r = Vec::with_capacity(cpath.len() + path.len());
-        r.push_all(cpath);
-        r.push_all(&path);
-        r
-    })
-}
-
-pub fn get_item_name(tcx: &ty::ctxt, def: DefId) -> ast::Name {
-    let cstore = &tcx.sess.cstore;
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_item_name(&cstore.intr, &cdata, def.index)
-}
-
-pub enum FoundAst<'ast> {
-    Found(&'ast InlinedItem),
-    FoundParent(DefId, &'ast InlinedItem),
-    NotFound,
-}
-
-// Finds the AST for this item in the crate metadata, if any.  If the item was
-// not marked for inlining, then the AST will not be present and hence none
-// will be returned.
-pub fn maybe_get_item_ast<'tcx>(tcx: &ty::ctxt<'tcx>, def: DefId,
-                                decode_inlined_item: decoder::DecodeInlinedItem)
-                                -> FoundAst<'tcx> {
-    let cstore = &tcx.sess.cstore;
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::maybe_get_item_ast(&*cdata, tcx, def.index, decode_inlined_item)
-}
-
-/// Returns information about the given implementation.
-pub fn get_impl_items(cstore: &cstore::CStore, impl_def_id: DefId)
-                      -> Vec<ty::ImplOrTraitItemId> {
-    let cdata = cstore.get_crate_data(impl_def_id.krate);
-    decoder::get_impl_items(&*cdata, impl_def_id.index)
-}
-
-pub fn get_impl_or_trait_item<'tcx>(tcx: &ty::ctxt<'tcx>, def: DefId)
-                                    -> ty::ImplOrTraitItem<'tcx> {
-    let cdata = tcx.sess.cstore.get_crate_data(def.krate);
-    decoder::get_impl_or_trait_item(tcx.sess.cstore.intr.clone(),
-                                    &*cdata,
-                                    def.index,
-                                    tcx)
-}
-
-pub fn get_trait_name(cstore: &cstore::CStore, def: DefId) -> ast::Name {
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_trait_name(cstore.intr.clone(),
-                            &*cdata,
-                            def.index)
-}
-
-pub fn is_static_method(cstore: &cstore::CStore, def: DefId) -> bool {
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::is_static_method(&*cdata, def.index)
-}
-
-pub fn get_trait_item_def_ids(cstore: &cstore::CStore, def: DefId)
-                              -> Vec<ty::ImplOrTraitItemId> {
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_trait_item_def_ids(&*cdata, def.index)
-}
-
-pub fn get_item_variances(cstore: &cstore::CStore,
-                          def: DefId) -> ty::ItemVariances {
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_item_variances(&*cdata, def.index)
-}
-
-pub fn get_provided_trait_methods<'tcx>(tcx: &ty::ctxt<'tcx>,
-                                        def: DefId)
-                                        -> Vec<Rc<ty::Method<'tcx>>> {
-    let cstore = &tcx.sess.cstore;
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_provided_trait_methods(cstore.intr.clone(), &*cdata, def.index, tcx)
-}
-
-pub fn get_associated_consts<'tcx>(tcx: &ty::ctxt<'tcx>, def: DefId)
-                                   -> Vec<Rc<ty::AssociatedConst<'tcx>>> {
-    let cstore = &tcx.sess.cstore;
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_associated_consts(cstore.intr.clone(), &*cdata, def.index, tcx)
-}
-
-pub fn get_methods_if_impl(cstore: &cstore::CStore,
-                                  def: DefId)
-                               -> Option<Vec<MethodInfo> > {
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_methods_if_impl(cstore.intr.clone(), &*cdata, def.index)
-}
-
-pub fn get_item_attrs(cstore: &cstore::CStore,
-                      def_id: DefId)
-                      -> Vec<ast::Attribute> {
-    let cdata = cstore.get_crate_data(def_id.krate);
-    decoder::get_item_attrs(&*cdata, def_id.index)
-}
-
-pub fn get_struct_field_names(cstore: &cstore::CStore, def: DefId) -> Vec<ast::Name> {
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_struct_field_names(&cstore.intr, &*cdata, def.index)
-}
-
-pub fn get_struct_field_attrs(cstore: &cstore::CStore, def: DefId)
-                              -> FnvHashMap<DefId, Vec<ast::Attribute>> {
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_struct_field_attrs(&*cdata)
-}
-
-pub fn get_type<'tcx>(tcx: &ty::ctxt<'tcx>,
-                      def: DefId)
-                      -> ty::TypeScheme<'tcx> {
-    let cstore = &tcx.sess.cstore;
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_type(&*cdata, def.index, tcx)
-}
-
-pub fn get_trait_def<'tcx>(tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::TraitDef<'tcx> {
-    let cstore = &tcx.sess.cstore;
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_trait_def(&*cdata, def.index, tcx)
-}
-
-pub fn get_adt_def<'tcx>(tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx> {
-    let cstore = &tcx.sess.cstore;
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_adt_def(&cstore.intr, &*cdata, def.index, tcx)
-}
-
-pub fn get_predicates<'tcx>(tcx: &ty::ctxt<'tcx>, def: DefId)
-                            -> ty::GenericPredicates<'tcx>
-{
-    let cstore = &tcx.sess.cstore;
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_predicates(&*cdata, def.index, tcx)
-}
-
-pub fn get_super_predicates<'tcx>(tcx: &ty::ctxt<'tcx>, def: DefId)
-                                  -> ty::GenericPredicates<'tcx>
-{
-    let cstore = &tcx.sess.cstore;
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_super_predicates(&*cdata, def.index, tcx)
-}
-
-pub fn get_impl_polarity<'tcx>(tcx: &ty::ctxt<'tcx>,
-                               def: DefId)
-                               -> Option<hir::ImplPolarity>
-{
-    let cstore = &tcx.sess.cstore;
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_impl_polarity(&*cdata, def.index)
-}
-
-pub fn get_custom_coerce_unsized_kind<'tcx>(
-    tcx: &ty::ctxt<'tcx>,
-    def: DefId)
-    -> Option<ty::adjustment::CustomCoerceUnsized>
-{
-    let cstore = &tcx.sess.cstore;
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_custom_coerce_unsized_kind(&*cdata, def.index)
-}
-
-// Given a def_id for an impl, return the trait it implements,
-// if there is one.
-pub fn get_impl_trait<'tcx>(tcx: &ty::ctxt<'tcx>,
-                            def: DefId)
-                            -> Option<ty::TraitRef<'tcx>> {
-    let cstore = &tcx.sess.cstore;
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_impl_trait(&*cdata, def.index, tcx)
-}
-
-pub fn get_native_libraries(cstore: &cstore::CStore, crate_num: ast::CrateNum)
-                            -> Vec<(cstore::NativeLibraryKind, String)> {
-    let cdata = cstore.get_crate_data(crate_num);
-    decoder::get_native_libraries(&*cdata)
-}
-
-pub fn each_inherent_implementation_for_type<F>(cstore: &cstore::CStore,
-                                                def_id: DefId,
-                                                callback: F) where
-    F: FnMut(DefId),
-{
-    let cdata = cstore.get_crate_data(def_id.krate);
-    decoder::each_inherent_implementation_for_type(&*cdata, def_id.index, callback)
-}
-
-pub fn each_implementation_for_trait<F>(cstore: &cstore::CStore,
-                                        def_id: DefId,
-                                        mut callback: F) where
-    F: FnMut(DefId),
-{
-    cstore.iter_crate_data(|_, cdata| {
-        decoder::each_implementation_for_trait(cdata, def_id, &mut callback)
-    })
-}
-
-/// If the given def ID describes an item belonging to a trait (either a
-/// default method or an implementation of a trait method), returns the ID of
-/// the trait that the method belongs to. Otherwise, returns `None`.
-pub fn get_trait_of_item(cstore: &cstore::CStore,
-                         def_id: DefId,
-                         tcx: &ty::ctxt)
-                         -> Option<DefId> {
-    let cdata = cstore.get_crate_data(def_id.krate);
-    decoder::get_trait_of_item(&*cdata, def_id.index, tcx)
-}
-
-pub fn get_tuple_struct_definition_if_ctor(cstore: &cstore::CStore,
-                                           def_id: DefId)
-    -> Option<DefId>
-{
-    let cdata = cstore.get_crate_data(def_id.krate);
-    decoder::get_tuple_struct_definition_if_ctor(&*cdata, def_id.index)
-}
-
-pub fn get_dylib_dependency_formats(cstore: &cstore::CStore,
-                                    cnum: ast::CrateNum)
-    -> Vec<(ast::CrateNum, cstore::LinkagePreference)>
-{
-    let cdata = cstore.get_crate_data(cnum);
-    decoder::get_dylib_dependency_formats(&*cdata)
-}
-
-pub fn get_missing_lang_items(cstore: &cstore::CStore, cnum: ast::CrateNum)
-    -> Vec<lang_items::LangItem>
-{
-    let cdata = cstore.get_crate_data(cnum);
-    decoder::get_missing_lang_items(&*cdata)
-}
-
-pub fn get_method_arg_names(cstore: &cstore::CStore, did: DefId)
-    -> Vec<String>
-{
-    let cdata = cstore.get_crate_data(did.krate);
-    decoder::get_method_arg_names(&*cdata, did.index)
-}
-
-pub fn get_reachable_ids(cstore: &cstore::CStore, cnum: ast::CrateNum)
-    -> Vec<DefId>
-{
-    let cdata = cstore.get_crate_data(cnum);
-    decoder::get_reachable_ids(&*cdata)
-}
-
-pub fn is_typedef(cstore: &cstore::CStore, did: DefId) -> bool {
-    let cdata = cstore.get_crate_data(did.krate);
-    decoder::is_typedef(&*cdata, did.index)
-}
-
-pub fn is_const_fn(cstore: &cstore::CStore, did: DefId) -> bool {
-    let cdata = cstore.get_crate_data(did.krate);
-    decoder::is_const_fn(&*cdata, did.index)
-}
-
-pub fn is_static(cstore: &cstore::CStore, did: DefId) -> bool {
-    let cdata = cstore.get_crate_data(did.krate);
-    decoder::is_static(&*cdata, did.index)
-}
-
-pub fn is_impl(cstore: &cstore::CStore, did: DefId) -> bool {
-    let cdata = cstore.get_crate_data(did.krate);
-    decoder::is_impl(&*cdata, did.index)
-}
-
-pub fn get_stability(cstore: &cstore::CStore,
-                     def: DefId)
-                     -> Option<attr::Stability> {
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_stability(&*cdata, def.index)
-}
-
-pub fn is_staged_api(cstore: &cstore::CStore, krate: ast::CrateNum) -> bool {
-    cstore.get_crate_data(krate).staged_api
-}
-
-pub fn get_repr_attrs(cstore: &cstore::CStore, def: DefId)
-                      -> Vec<attr::ReprAttr> {
-    let cdata = cstore.get_crate_data(def.krate);
-    decoder::get_repr_attrs(&*cdata, def.index)
-}
-
-pub fn is_defaulted_trait(cstore: &cstore::CStore, trait_def_id: DefId) -> bool {
-    let cdata = cstore.get_crate_data(trait_def_id.krate);
-    decoder::is_defaulted_trait(&*cdata, trait_def_id.index)
-}
-
-pub fn is_default_impl(cstore: &cstore::CStore, impl_did: DefId) -> bool {
-    let cdata = cstore.get_crate_data(impl_did.krate);
-    decoder::is_default_impl(&*cdata, impl_did.index)
-}
-
-pub fn is_extern_fn(cstore: &cstore::CStore, did: DefId,
-                    tcx: &ty::ctxt) -> bool {
-    let cdata = cstore.get_crate_data(did.krate);
-    decoder::is_extern_fn(&*cdata, did.index, tcx)
-}
-
-pub fn closure_kind<'tcx>(tcx: &ty::ctxt<'tcx>, def_id: DefId) -> ty::ClosureKind {
-    assert!(!def_id.is_local());
-    let cdata = tcx.sess.cstore.get_crate_data(def_id.krate);
-    decoder::closure_kind(&*cdata, def_id.index)
-}
-
-pub fn closure_ty<'tcx>(tcx: &ty::ctxt<'tcx>, def_id: DefId) -> ty::ClosureTy<'tcx> {
-    assert!(!def_id.is_local());
-    let cdata = tcx.sess.cstore.get_crate_data(def_id.krate);
-    decoder::closure_ty(&*cdata, def_id.index, tcx)
-}
-
-pub fn def_path(tcx: &ty::ctxt, def: DefId) -> ast_map::DefPath {
-    let cstore = &tcx.sess.cstore;
-    let cdata = cstore.get_crate_data(def.krate);
-    let path = decoder::def_path(&*cdata, def.index);
-    let local_path = cdata.local_def_path();
-    local_path.into_iter().chain(path).collect()
-}
diff --git a/src/librustc/metadata/inline.rs b/src/librustc/metadata/inline.rs
deleted file mode 100644
index e621a4166d7..00000000000
--- a/src/librustc/metadata/inline.rs
+++ /dev/null
@@ -1,60 +0,0 @@
-// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-use middle::def_id::DefId;
-use rustc_front::hir;
-use rustc_front::util::IdVisitor;
-use syntax::ast_util::{IdRange, IdRangeComputingVisitor, IdVisitingOperation};
-use syntax::ptr::P;
-use rustc_front::intravisit::Visitor;
-use self::InlinedItem::*;
-
-/// The data we save and restore about an inlined item or method.  This is not
-/// part of the AST that we parse from a file, but it becomes part of the tree
-/// that we trans.
-#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
-pub enum InlinedItem {
-    Item(P<hir::Item>),
-    TraitItem(DefId /* impl id */, P<hir::TraitItem>),
-    ImplItem(DefId /* impl id */, P<hir::ImplItem>),
-    Foreign(P<hir::ForeignItem>),
-}
-
-/// A borrowed version of `hir::InlinedItem`.
-pub enum InlinedItemRef<'a> {
-    Item(&'a hir::Item),
-    TraitItem(DefId, &'a hir::TraitItem),
-    ImplItem(DefId, &'a hir::ImplItem),
-    Foreign(&'a hir::ForeignItem)
-}
-
-impl InlinedItem {
-    pub fn visit<'ast,V>(&'ast self, visitor: &mut V)
-        where V: Visitor<'ast>
-    {
-        match *self {
-            Item(ref i) => visitor.visit_item(&**i),
-            Foreign(ref i) => visitor.visit_foreign_item(&**i),
-            TraitItem(_, ref ti) => visitor.visit_trait_item(ti),
-            ImplItem(_, ref ii) => visitor.visit_impl_item(ii),
-        }
-    }
-
-    pub fn visit_ids<O: IdVisitingOperation>(&self, operation: &mut O) {
-        let mut id_visitor = IdVisitor::new(operation);
-        self.visit(&mut id_visitor);
-    }
-
-    pub fn compute_id_range(&self) -> IdRange {
-        let mut visitor = IdRangeComputingVisitor::new();
-        self.visit_ids(&mut visitor);
-        visitor.result()
-    }
-}
diff --git a/src/librustc/metadata/mod.rs b/src/librustc/metadata/mod.rs
deleted file mode 100644
index 052ed81394b..00000000000
--- a/src/librustc/metadata/mod.rs
+++ /dev/null
@@ -1,23 +0,0 @@
-// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-pub mod common;
-pub mod tyencode;
-pub mod tydecode;
-pub mod encoder;
-pub mod decoder;
-pub mod creader;
-pub mod cstore;
-pub mod index;
-pub mod loader;
-pub mod filesearch;
-pub mod macro_import;
-pub mod inline;
-pub mod util;
diff --git a/src/librustc/middle/const_eval.rs b/src/librustc/middle/const_eval.rs
index 63134a8766d..21ece8f381e 100644
--- a/src/librustc/middle/const_eval.rs
+++ b/src/librustc/middle/const_eval.rs
@@ -16,7 +16,7 @@ use self::EvalHint::*;
 
 use front::map as ast_map;
 use front::map::blocks::FnLikeNode;
-use metadata::util::{self as mdutil, CrateStore, InlinedItem};
+use middle::cstore::{self, CrateStore, InlinedItem};
 use middle::{def, infer, subst, traits};
 use middle::def_id::DefId;
 use middle::pat_util::def_to_path;
@@ -145,11 +145,11 @@ pub fn lookup_const_by_id<'a, 'tcx: 'a>(tcx: &'a ty::ctxt<'tcx>,
         }
         let mut used_ref_id = false;
         let expr_id = match tcx.sess.cstore.maybe_get_item_ast(tcx, def_id) {
-            mdutil::FoundAst::Found(&InlinedItem::Item(ref item)) => match item.node {
+            cstore::FoundAst::Found(&InlinedItem::Item(ref item)) => match item.node {
                 hir::ItemConst(_, ref const_expr) => Some(const_expr.id),
                 _ => None
             },
-            mdutil::FoundAst::Found(&InlinedItem::TraitItem(trait_id, ref ti)) => match ti.node {
+            cstore::FoundAst::Found(&InlinedItem::TraitItem(trait_id, ref ti)) => match ti.node {
                 hir::ConstTraitItem(_, _) => {
                     used_ref_id = true;
                     match maybe_ref_id {
@@ -168,7 +168,7 @@ pub fn lookup_const_by_id<'a, 'tcx: 'a>(tcx: &'a ty::ctxt<'tcx>,
                 }
                 _ => None
             },
-            mdutil::FoundAst::Found(&InlinedItem::ImplItem(_, ref ii)) => match ii.node {
+            cstore::FoundAst::Found(&InlinedItem::ImplItem(_, ref ii)) => match ii.node {
                 hir::ImplItemKind::Const(_, ref expr) => Some(expr.id),
                 _ => None
             },
@@ -200,8 +200,8 @@ fn inline_const_fn_from_external_crate(tcx: &ty::ctxt, def_id: DefId)
     }
 
     let fn_id = match tcx.sess.cstore.maybe_get_item_ast(tcx, def_id) {
-        mdutil::FoundAst::Found(&InlinedItem::Item(ref item)) => Some(item.id),
-        mdutil::FoundAst::Found(&InlinedItem::ImplItem(_, ref item)) => Some(item.id),
+        cstore::FoundAst::Found(&InlinedItem::Item(ref item)) => Some(item.id),
+        cstore::FoundAst::Found(&InlinedItem::ImplItem(_, ref item)) => Some(item.id),
         _ => None
     };
     tcx.extern_const_fns.borrow_mut().insert(def_id,
diff --git a/src/librustc/middle/cstore.rs b/src/librustc/middle/cstore.rs
new file mode 100644
index 00000000000..ee337f02ffc
--- /dev/null
+++ b/src/librustc/middle/cstore.rs
@@ -0,0 +1,277 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// the rustc crate store interface. This also includes types that
+// are *mostly* used as a part of that interface, but these should
+// probably get a better home if someone can find one.
+
+use back::svh::Svh;
+use front::map as hir_map;
+use middle::def;
+use middle::lang_items;
+use middle::ty::{self, Ty};
+use middle::def_id::{DefId, DefIndex};
+use session::Session;
+use session::search_paths::PathKind;
+use util::nodemap::{FnvHashMap, NodeMap, NodeSet};
+use std::any::Any;
+use std::cell::RefCell;
+use std::rc::Rc;
+use std::path::PathBuf;
+use syntax::ast;
+use syntax::ast_util::{IdVisitingOperation};
+use syntax::attr;
+use syntax::codemap::Span;
+use syntax::ptr::P;
+use rustc_back::target::Target;
+use rustc_front::hir;
+use rustc_front::visit::Visitor;
+use rustc_front::util::IdVisitor;
+
+pub use self::DefLike::{DlDef, DlField, DlImpl};
+pub use self::NativeLibraryKind::{NativeStatic, NativeFramework, NativeUnknown};
+
+// lonely orphan structs and enums looking for a better home
+
+#[derive(Clone, Debug)]
+pub struct LinkMeta {
+    pub crate_name: String,
+    pub crate_hash: Svh,
+}
+
+// Where a crate came from on the local filesystem. One of these two options
+// must be non-None.
+#[derive(PartialEq, Clone, Debug)]
+pub struct CrateSource {
+    pub dylib: Option<(PathBuf, PathKind)>,
+    pub rlib: Option<(PathBuf, PathKind)>,
+    pub cnum: ast::CrateNum,
+}
+
+#[derive(Copy, Debug, PartialEq, Clone)]
+pub enum LinkagePreference {
+    RequireDynamic,
+    RequireStatic,
+}
+
+enum_from_u32! {
+    #[derive(Copy, Clone, PartialEq)]
+    pub enum NativeLibraryKind {
+        NativeStatic,    // native static library (.a archive)
+        NativeFramework, // OSX-specific
+        NativeUnknown,   // default way to specify a dynamic library
+    }
+}
+
+// Something that a name can resolve to.
+#[derive(Copy, Clone, Debug)]
+pub enum DefLike {
+    DlDef(def::Def),
+    DlImpl(DefId),
+    DlField
+}
+
+/// The data we save and restore about an inlined item or method.  This is not
+/// part of the AST that we parse from a file, but it becomes part of the tree
+/// that we trans.
+#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
+pub enum InlinedItem {
+    Item(P<hir::Item>),
+    TraitItem(DefId /* impl id */, P<hir::TraitItem>),
+    ImplItem(DefId /* impl id */, P<hir::ImplItem>),
+    Foreign(P<hir::ForeignItem>),
+}
+
+/// A borrowed version of `hir::InlinedItem`.
+pub enum InlinedItemRef<'a> {
+    Item(&'a hir::Item),
+    TraitItem(DefId, &'a hir::TraitItem),
+    ImplItem(DefId, &'a hir::ImplItem),
+    Foreign(&'a hir::ForeignItem)
+}
+
+/// Item definitions in the currently-compiled crate would have the CrateNum
+/// LOCAL_CRATE in their DefId.
+pub const LOCAL_CRATE: ast::CrateNum = 0;
+
+pub struct ChildItem {
+    pub def: DefLike,
+    pub name: ast::Name,
+    pub vis: hir::Visibility
+}
+
+pub enum FoundAst<'ast> {
+    Found(&'ast InlinedItem),
+    FoundParent(DefId, &'ast InlinedItem),
+    NotFound,
+}
+
+pub trait CrateStore<'tcx> : Any {
+    // item info
+    fn stability(&self, def: DefId) -> Option<attr::Stability>;
+    fn closure_kind(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId)
+                    -> ty::ClosureKind;
+    fn closure_ty(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId)
+                  -> ty::ClosureTy<'tcx>;
+    fn item_variances(&self, def: DefId) -> ty::ItemVariances;
+    fn repr_attrs(&self, def: DefId) -> Vec<attr::ReprAttr>;
+    fn item_type(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
+                 -> ty::TypeScheme<'tcx>;
+    fn item_path(&self, def: DefId) -> Vec<hir_map::PathElem>;
+    fn item_name(&self, def: DefId) -> ast::Name;
+    fn item_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
+                       -> ty::GenericPredicates<'tcx>;
+    fn item_super_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
+                             -> ty::GenericPredicates<'tcx>;
+    fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute>;
+    fn item_symbol(&self, def: DefId) -> String;
+    fn trait_def(&self, tcx: &ty::ctxt<'tcx>, def: DefId)-> ty::TraitDef<'tcx>;
+    fn adt_def(&self, tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>;
+    fn method_arg_names(&self, did: DefId) -> Vec<String>;
+    fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>;
+
+    // trait info
+    fn implementations_of_trait(&self, def_id: DefId) -> Vec<DefId>;
+    fn provided_trait_methods(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
+                              -> Vec<Rc<ty::Method<'tcx>>>;
+    fn trait_item_def_ids(&self, def: DefId)
+                          -> Vec<ty::ImplOrTraitItemId>;
+
+    // impl info
+    fn impl_items(&self, impl_def_id: DefId) -> Vec<ty::ImplOrTraitItemId>;
+    fn impl_trait_ref(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
+                      -> Option<ty::TraitRef<'tcx>>;
+    fn impl_polarity(&self, def: DefId) -> Option<hir::ImplPolarity>;
+    fn custom_coerce_unsized_kind(&self, def: DefId)
+                                  -> Option<ty::adjustment::CustomCoerceUnsized>;
+    fn associated_consts(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
+                         -> Vec<Rc<ty::AssociatedConst<'tcx>>>;
+
+    // trait/impl-item info
+    fn trait_of_item(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId)
+                     -> Option<DefId>;
+    fn impl_or_trait_item(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
+                          -> ty::ImplOrTraitItem<'tcx>;
+
+    // flags
+    fn is_const_fn(&self, did: DefId) -> bool;
+    fn is_defaulted_trait(&self, did: DefId) -> bool;
+    fn is_impl(&self, did: DefId) -> bool;
+    fn is_default_impl(&self, impl_did: DefId) -> bool;
+    fn is_extern_fn(&self, tcx: &ty::ctxt<'tcx>, did: DefId) -> bool;
+    fn is_static(&self, did: DefId) -> bool;
+    fn is_static_method(&self, did: DefId) -> bool;
+    fn is_statically_included_foreign_item(&self, id: ast::NodeId) -> bool;
+    fn is_typedef(&self, did: DefId) -> bool;
+
+    // crate metadata
+    fn dylib_dependency_formats(&self, cnum: ast::CrateNum)
+                                    -> Vec<(ast::CrateNum, LinkagePreference)>;
+    fn lang_items(&self, cnum: ast::CrateNum) -> Vec<(DefIndex, usize)>;
+    fn missing_lang_items(&self, cnum: ast::CrateNum) -> Vec<lang_items::LangItem>;
+    fn is_staged_api(&self, cnum: ast::CrateNum) -> bool;
+    fn is_explicitly_linked(&self, cnum: ast::CrateNum) -> bool;
+    fn is_allocator(&self, cnum: ast::CrateNum) -> bool;
+    fn crate_attrs(&self, cnum: ast::CrateNum) -> Vec<ast::Attribute>;
+    fn crate_name(&self, cnum: ast::CrateNum) -> String;
+    fn crate_hash(&self, cnum: ast::CrateNum) -> Svh;
+    fn crate_struct_field_attrs(&self, cnum: ast::CrateNum)
+                                -> FnvHashMap<DefId, Vec<ast::Attribute>>;
+    fn plugin_registrar_fn(&self, cnum: ast::CrateNum) -> Option<DefId>;
+    fn native_libraries(&self, cnum: ast::CrateNum) -> Vec<(NativeLibraryKind, String)>;
+    fn reachable_ids(&self, cnum: ast::CrateNum) -> Vec<DefId>;
+
+    // resolve
+    fn def_path(&self, def: DefId) -> hir_map::DefPath;
+    fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>;
+    fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
+    fn item_children(&self, did: DefId) -> Vec<ChildItem>;
+    fn crate_top_level_items(&self, cnum: ast::CrateNum) -> Vec<ChildItem>;
+
+    // misc. metadata
+    fn maybe_get_item_ast(&'tcx self, tcx: &ty::ctxt<'tcx>, def: DefId)
+                          -> FoundAst<'tcx>;
+    // This is basically a 1-based range of ints, which is a little
+    // silly - I may fix that.
+    fn crates(&self) -> Vec<ast::CrateNum>;
+    fn used_libraries(&self) -> Vec<(String, NativeLibraryKind)>;
+    fn used_link_args(&self) -> Vec<String>;
+
+    // utility functions
+    fn metadata_filename(&self) -> &str;
+    fn metadata_section_name(&self, target: &Target) -> &str;
+    fn encode_type(&self, tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> Vec<u8>;
+    fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option<PathBuf>)>;
+    fn used_crate_source(&self, cnum: ast::CrateNum) -> CrateSource;
+    fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<ast::CrateNum>;
+    fn encode_metadata(&self,
+                       tcx: &ty::ctxt<'tcx>,
+                       reexports: &def::ExportMap,
+                       item_symbols: &RefCell<NodeMap<String>>,
+                       link_meta: &LinkMeta,
+                       reachable: &NodeSet,
+                       krate: &hir::Crate) -> Vec<u8>;
+    fn metadata_encoding_version(&self) -> &[u8];
+}
+
+impl InlinedItem {
+    pub fn visit<'ast,V>(&'ast self, visitor: &mut V)
+        where V: Visitor<'ast>
+    {
+        match *self {
+            InlinedItem::Item(ref i) => visitor.visit_item(&**i),
+            InlinedItem::Foreign(ref i) => visitor.visit_foreign_item(&**i),
+            InlinedItem::TraitItem(_, ref ti) => visitor.visit_trait_item(ti),
+            InlinedItem::ImplItem(_, ref ii) => visitor.visit_impl_item(ii),
+        }
+    }
+
+    pub fn visit_ids<O: IdVisitingOperation>(&self, operation: &mut O) {
+        let mut id_visitor = IdVisitor {
+            operation: operation,
+            pass_through_items: true,
+            visited_outermost: false,
+        };
+        self.visit(&mut id_visitor);
+    }
+}
+
+// FIXME: find a better place for this?
+pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option<Span>) {
+    let say = |s: &str| {
+        match (sp, sess) {
+            (_, None) => panic!("{}", s),
+            (Some(sp), Some(sess)) => sess.span_err(sp, s),
+            (None, Some(sess)) => sess.err(s),
+        }
+    };
+    if s.is_empty() {
+        say("crate name must not be empty");
+    }
+    for c in s.chars() {
+        if c.is_alphanumeric() { continue }
+        if c == '_'  { continue }
+        say(&format!("invalid character `{}` in crate name: `{}`", c, s));
+    }
+    match sess {
+        Some(sess) => sess.abort_if_errors(),
+        None => {}
+    }
+}
diff --git a/src/librustc/middle/def_id.rs b/src/librustc/middle/def_id.rs
index 288eb01ebb4..4d0005f47c4 100644
--- a/src/librustc/middle/def_id.rs
+++ b/src/librustc/middle/def_id.rs
@@ -8,7 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-use metadata::cstore::LOCAL_CRATE;
+use middle::cstore::LOCAL_CRATE;
 use middle::ty;
 use syntax::ast::CrateNum;
 use std::fmt;
diff --git a/src/librustc/middle/dependency_format.rs b/src/librustc/middle/dependency_format.rs
index 229ed5984b0..ab5153e1a61 100644
--- a/src/librustc/middle/dependency_format.rs
+++ b/src/librustc/middle/dependency_format.rs
@@ -65,8 +65,8 @@ use syntax::ast;
 
 use session;
 use session::config;
-use metadata::util::CrateStore;
-use metadata::util::LinkagePreference::{self, RequireStatic, RequireDynamic};
+use middle::cstore::CrateStore;
+use middle::cstore::LinkagePreference::{self, RequireStatic, RequireDynamic};
 use util::nodemap::FnvHashMap;
 
 /// A list of dependencies for a certain crate type.
diff --git a/src/librustc/middle/infer/error_reporting.rs b/src/librustc/middle/infer/error_reporting.rs
index 5101e69204d..5563cd80438 100644
--- a/src/librustc/middle/infer/error_reporting.rs
+++ b/src/librustc/middle/infer/error_reporting.rs
@@ -76,7 +76,7 @@ use front::map as ast_map;
 use rustc_front::hir;
 use rustc_front::print::pprust;
 
-use metadata::util::CrateStore;
+use middle::cstore::CrateStore;
 use middle::def;
 use middle::def_id::DefId;
 use middle::infer::{self, TypeOrigin};
diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs
index c9be73892e4..ec55daca9ec 100644
--- a/src/librustc/middle/lang_items.rs
+++ b/src/librustc/middle/lang_items.rs
@@ -23,7 +23,7 @@ pub use self::LangItem::*;
 
 use front::map as hir_map;
 use session::Session;
-use metadata::util::CrateStore;
+use middle::cstore::CrateStore;
 use middle::def_id::DefId;
 use middle::ty;
 use middle::weak_lang_items;
diff --git a/src/librustc/middle/region.rs b/src/librustc/middle/region.rs
index 45b8ac4a16d..e3504b6a744 100644
--- a/src/librustc/middle/region.rs
+++ b/src/librustc/middle/region.rs
@@ -16,10 +16,10 @@
 //! Most of the documentation on regions can be found in
 //! `middle/typeck/infer/region_inference.rs`
 
-use metadata::inline::InlinedItem;
 use front::map as ast_map;
 use session::Session;
 use util::nodemap::{FnvHashMap, NodeMap, NodeSet};
+use middle::cstore::InlinedItem;
 use middle::ty::{self, Ty};
 
 use std::cell::RefCell;
diff --git a/src/librustc/middle/stability.rs b/src/librustc/middle/stability.rs
index f70dce90fed..0d92c3da83c 100644
--- a/src/librustc/middle/stability.rs
+++ b/src/librustc/middle/stability.rs
@@ -15,8 +15,7 @@ pub use self::StabilityLevel::*;
 
 use session::Session;
 use lint;
-use metadata::cstore::LOCAL_CRATE;
-use metadata::util::CrateStore;
+use middle::cstore::{CrateStore, LOCAL_CRATE};
 use middle::def;
 use middle::def_id::{CRATE_DEF_INDEX, DefId};
 use middle::ty;
diff --git a/src/librustc/middle/traits/coherence.rs b/src/librustc/middle/traits/coherence.rs
index 705cb97a898..56dc259b1c2 100644
--- a/src/librustc/middle/traits/coherence.rs
+++ b/src/librustc/middle/traits/coherence.rs
@@ -17,7 +17,7 @@ use super::PredicateObligation;
 use super::project;
 use super::util;
 
-use metadata::cstore::LOCAL_CRATE;
+use middle::cstore::LOCAL_CRATE;
 use middle::def_id::DefId;
 use middle::subst::{Subst, Substs, TypeSpace};
 use middle::ty::{self, Ty};
diff --git a/src/librustc/middle/ty/context.rs b/src/librustc/middle/ty/context.rs
index 105abe15509..4b614539921 100644
--- a/src/librustc/middle/ty/context.rs
+++ b/src/librustc/middle/ty/context.rs
@@ -16,8 +16,8 @@
 use front::map as ast_map;
 use session::Session;
 use lint;
-use metadata::util::CrateStore;
 use middle;
+use middle::cstore::CrateStore;
 use middle::def::DefMap;
 use middle::def_id::DefId;
 use middle::free_region::FreeRegionMap;
diff --git a/src/librustc/middle/ty/mod.rs b/src/librustc/middle/ty/mod.rs
index bedff8b9a8a..71ae8e40b45 100644
--- a/src/librustc/middle/ty/mod.rs
+++ b/src/librustc/middle/ty/mod.rs
@@ -21,9 +21,8 @@ pub use self::LvaluePreference::*;
 
 use front::map as ast_map;
 use front::map::LinkedPath;
-use metadata::cstore::LOCAL_CRATE;
-use metadata::util::CrateStore;
 use middle;
+use middle::cstore::{CrateStore, LOCAL_CRATE};
 use middle::def::{self, ExportMap};
 use middle::def_id::DefId;
 use middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem};
diff --git a/src/librustc/middle/weak_lang_items.rs b/src/librustc/middle/weak_lang_items.rs
index a6c2fd08e87..78cdc99f047 100644
--- a/src/librustc/middle/weak_lang_items.rs
+++ b/src/librustc/middle/weak_lang_items.rs
@@ -12,7 +12,7 @@
 
 use session::config;
 use session::Session;
-use metadata::util::CrateStore;
+use middle::cstore::CrateStore;
 use middle::lang_items;
 
 use syntax::ast;
diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs
index 4781992b987..5d691af49dc 100644
--- a/src/librustc/session/config.rs
+++ b/src/librustc/session/config.rs
@@ -22,7 +22,7 @@ use session::search_paths::SearchPaths;
 
 use rustc_back::target::Target;
 use lint;
-use metadata::cstore;
+use middle::cstore;
 
 use syntax::ast::{self, IntTy, UintTy};
 use syntax::attr;
diff --git a/src/librustc/metadata/filesearch.rs b/src/librustc/session/filesearch.rs
index 09c6b54d99c..09c6b54d99c 100644
--- a/src/librustc/metadata/filesearch.rs
+++ b/src/librustc/session/filesearch.rs
diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs
index bf9c4c8bca6..7bf96b41dce 100644
--- a/src/librustc/session/mod.rs
+++ b/src/librustc/session/mod.rs
@@ -9,8 +9,7 @@
 // except according to those terms.
 
 use lint;
-use metadata::filesearch;
-use metadata::util::CrateStore;
+use middle::cstore::CrateStore;
 use middle::dependency_format;
 use session::search_paths::PathKind;
 use util::nodemap::{NodeMap, FnvHashMap};
@@ -34,6 +33,7 @@ use std::env;
 use std::rc::Rc;
 
 pub mod config;
+pub mod filesearch;
 pub mod search_paths;
 
 // Represents the data associated with a compilation
diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs
index d00b08538d2..1429a6a54a6 100644
--- a/src/librustc_driver/driver.rs
+++ b/src/librustc_driver/driver.rs
@@ -16,9 +16,6 @@ use rustc::session::Session;
 use rustc::session::config::{self, Input, OutputFilenames, OutputType};
 use rustc::session::search_paths::PathKind;
 use rustc::lint;
-use rustc::metadata;
-use rustc::metadata::creader::LocalCrateReader;
-use rustc::metadata::cstore::CStore;
 use rustc::middle::{stability, ty, reachable};
 use rustc::middle::dependency_format;
 use rustc::middle;
@@ -26,6 +23,9 @@ use rustc::util::nodemap::NodeMap;
 use rustc::util::common::time;
 use rustc_borrowck as borrowck;
 use rustc_resolve as resolve;
+use rustc_metadata::macro_import;
+use rustc_metadata::creader::LocalCrateReader;
+use rustc_metadata::cstore::CStore;
 use rustc_trans::back::link;
 use rustc_trans::back::write;
 use rustc_trans::trans;
@@ -482,7 +482,7 @@ pub fn phase_2_configure_and_expand(sess: &Session,
 
     let macros = time(time_passes,
                       "macro loading",
-                      || metadata::macro_import::read_macro_defs(sess, &cstore, &krate));
+                      || macro_import::read_macro_defs(sess, &cstore, &krate));
 
     let mut addl_plugins = Some(addl_plugins);
     let registrars = time(time_passes, "plugin loading", || {
diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs
index fdbb093eab0..b77425e1809 100644
--- a/src/librustc_driver/lib.rs
+++ b/src/librustc_driver/lib.rs
@@ -47,6 +47,7 @@ extern crate rustc_front;
 extern crate rustc_lint;
 extern crate rustc_plugin;
 extern crate rustc_privacy;
+extern crate rustc_metadata;
 extern crate rustc_mir;
 extern crate rustc_resolve;
 extern crate rustc_trans;
@@ -68,11 +69,11 @@ use rustc_trans::back::link;
 use rustc_trans::save;
 use rustc::session::{config, Session, build_session};
 use rustc::session::config::{Input, PrintRequest, OutputType};
+use rustc::middle::cstore::CrateStore;
 use rustc::lint::Lint;
 use rustc::lint;
-use rustc::metadata;
-use rustc::metadata::cstore::CStore;
-use rustc::metadata::util::CrateStore;
+use rustc_metadata::loader;
+use rustc_metadata::cstore::CStore;
 use rustc::util::common::time;
 
 use std::cmp::Ordering::Equal;
@@ -448,7 +449,7 @@ impl RustcDefaultCalls {
                 &Input::File(ref ifile) => {
                     let path = &(*ifile);
                     let mut v = Vec::new();
-                    metadata::loader::list_file_metadata(&sess.target.target, path, &mut v)
+                    loader::list_file_metadata(&sess.target.target, path, &mut v)
                         .unwrap();
                     println!("{}", String::from_utf8(v).unwrap());
                 }
diff --git a/src/librustc_driver/pretty.rs b/src/librustc_driver/pretty.rs
index ddd5f931a80..630c42db68c 100644
--- a/src/librustc_driver/pretty.rs
+++ b/src/librustc_driver/pretty.rs
@@ -19,7 +19,6 @@ use rustc_trans::back::link;
 
 use driver;
 
-use rustc::metadata::cstore::CStore;
 use rustc::middle::ty;
 use rustc::middle::cfg;
 use rustc::middle::cfg::graphviz::LabelledCFG;
@@ -28,6 +27,7 @@ use rustc::session::config::Input;
 use rustc_borrowck as borrowck;
 use rustc_borrowck::graphviz as borrowck_dot;
 use rustc_resolve as resolve;
+use rustc_metadata::cstore::CStore;
 
 use syntax::ast;
 use syntax::codemap;
diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs
index 7327170c52b..739c5f12ecb 100644
--- a/src/librustc_lint/builtin.rs
+++ b/src/librustc_lint/builtin.rs
@@ -29,11 +29,11 @@
 //! a `pub fn new()`.
 
 use middle::{cfg, def, infer, stability, traits};
+use middle::cstore::CrateStore;
 use middle::def_id::DefId;
 use middle::subst::Substs;
 use middle::ty::{self, Ty};
 use middle::ty::adjustment;
-use rustc::metadata::util::CrateStore;
 use rustc::front::map as hir_map;
 use util::nodemap::{NodeSet};
 use lint::{Level, LateContext, LintContext, LintArray, Lint};
diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs
index d48dbdc5f51..1d7431404f5 100644
--- a/src/librustc_lint/lib.rs
+++ b/src/librustc_lint/lib.rs
@@ -50,7 +50,6 @@ extern crate rustc_front;
 extern crate rustc_back;
 
 pub use rustc::lint as lint;
-pub use rustc::metadata as metadata;
 pub use rustc::middle as middle;
 pub use rustc::session as session;
 pub use rustc::util as util;
diff --git a/src/librustc/middle/astencode.rs b/src/librustc_metadata/astencode.rs
index 752fdc23474..2ecf715424b 100644
--- a/src/librustc/middle/astencode.rs
+++ b/src/librustc_metadata/astencode.rs
@@ -12,19 +12,21 @@
 // FIXME: remove this after snapshot, and Results are handled
 #![allow(unused_must_use)]
 
-use front::map as ast_map;
+use rustc::front::map as ast_map;
+use rustc::session::Session;
+
 use rustc_front::hir;
 use rustc_front::fold;
 use rustc_front::fold::Folder;
 
-use metadata::common as c;
-use metadata::cstore as cstore;
-use session::Session;
-use metadata::decoder;
-use metadata::encoder as e;
-use metadata::inline::{InlinedItem, InlinedItemRef};
-use metadata::tydecode;
-use metadata::tyencode;
+use common as c;
+use cstore;
+use decoder;
+use encoder as e;
+use tydecode;
+use tyencode;
+
+use middle::cstore::{InlinedItem, InlinedItemRef};
 use middle::ty::adjustment;
 use middle::ty::cast;
 use middle::check_const::ConstQualif;
@@ -89,7 +91,7 @@ pub fn encode_inlined_item(ecx: &e::EncodeContext,
 
     // Folding could be avoided with a smarter encoder.
     let ii = simplify_ast(ii);
-    let id_range = ii.compute_id_range();
+    let id_range = inlined_item_id_range(&ii);
 
     rbml_w.start_tag(c::tag_ast as usize);
     id_range.encode(rbml_w);
@@ -1326,6 +1328,12 @@ fn copy_item_types(dcx: &DecodeContext, ii: &InlinedItem, orig_did: DefId) {
     }
 }
 
+fn inlined_item_id_range(v: &InlinedItem) -> ast_util::IdRange {
+    let mut visitor = ast_util::IdRangeComputingVisitor::new();
+    v.visit_ids(&mut visitor);
+    visitor.result()
+}
+
 // ______________________________________________________________________
 // Testing of astencode_gen
 
diff --git a/src/librustc/metadata/common.rs b/src/librustc_metadata/common.rs
index a4fee5b7aa8..b6454a4c81a 100644
--- a/src/librustc/metadata/common.rs
+++ b/src/librustc_metadata/common.rs
@@ -12,8 +12,6 @@
 
 pub use self::astencode_tag::*;
 
-use back::svh::Svh;
-
 // RBML enum definitions and utils shared by the encoder and decoder
 //
 // 0x00..0x1f: reserved for RBML generic type tags
@@ -191,12 +189,6 @@ pub const tag_items_data_item_stability: usize = 0x88;
 
 pub const tag_items_data_item_repr: usize = 0x89;
 
-#[derive(Clone, Debug)]
-pub struct LinkMeta {
-    pub crate_name: String,
-    pub crate_hash: Svh,
-}
-
 pub const tag_struct_fields: usize = 0x10d; // top-level only
 pub const tag_struct_field: usize = 0x8a;
 
diff --git a/src/librustc/metadata/creader.rs b/src/librustc_metadata/creader.rs
index 191b1705371..89aa3bf8661 100644
--- a/src/librustc/metadata/creader.rs
+++ b/src/librustc_metadata/creader.rs
@@ -12,17 +12,17 @@
 
 //! Validates all used crates and extern libraries and loads their metadata
 
-use back::svh::Svh;
-use session::{config, Session};
-use session::search_paths::PathKind;
-use metadata::common::rustc_version;
-use metadata::cstore;
-use metadata::cstore::{CStore, CrateSource, MetadataBlob};
-use metadata::decoder;
-use metadata::loader;
-use metadata::loader::CratePaths;
-use util::nodemap::FnvHashMap;
-use front::map as hir_map;
+use common::rustc_version;
+use cstore::{self, CStore, CrateSource, MetadataBlob};
+use decoder;
+use loader::{self, CratePaths};
+
+use rustc::back::svh::Svh;
+use rustc::session::{config, Session};
+use rustc::session::search_paths::PathKind;
+use rustc::middle::cstore::validate_crate_name;
+use rustc::util::nodemap::FnvHashMap;
+use rustc::front::map as hir_map;
 
 use std::cell::{RefCell, Cell};
 use std::path::PathBuf;
@@ -91,29 +91,6 @@ struct CrateInfo {
     should_link: bool,
 }
 
-pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option<Span>) {
-    let say = |s: &str| {
-        match (sp, sess) {
-            (_, None) => panic!("{}", s),
-            (Some(sp), Some(sess)) => sess.span_err(sp, s),
-            (None, Some(sess)) => sess.err(s),
-        }
-    };
-    if s.is_empty() {
-        say("crate name must not be empty");
-    }
-    for c in s.chars() {
-        if c.is_alphanumeric() { continue }
-        if c == '_'  { continue }
-        say(&format!("invalid character `{}` in crate name: `{}`", c, s));
-    }
-    match sess {
-        Some(sess) => sess.abort_if_errors(),
-        None => {}
-    }
-}
-
-
 fn register_native_lib(sess: &Session,
                        cstore: &CStore,
                        span: Option<Span>,
diff --git a/src/librustc/metadata/util.rs b/src/librustc_metadata/csearch.rs
index f00dd3b852c..99fa18837eb 100644
--- a/src/librustc/metadata/util.rs
+++ b/src/librustc_metadata/csearch.rs
@@ -8,160 +8,31 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-use back::svh::Svh;
-use front::map as ast_map;
-use metadata::cstore;
-use metadata::decoder;
-use metadata::encoder;
-use metadata::loader;
-use middle::astencode;
+use astencode;
+use cstore;
+use decoder;
+use encoder;
+use loader;
+
+use middle::cstore::{CrateStore, CrateSource, ChildItem, FoundAst};
+use middle::cstore::{NativeLibraryKind, LinkMeta, LinkagePreference};
 use middle::def;
 use middle::lang_items;
 use middle::ty::{self, Ty};
 use middle::def_id::{DefId, DefIndex};
-use util::nodemap::{FnvHashMap, NodeMap, NodeSet};
 
-use std::any::Any;
+use rustc::front::map as ast_map;
+use rustc::util::nodemap::{FnvHashMap, NodeMap, NodeSet};
+
 use std::cell::RefCell;
 use std::rc::Rc;
 use std::path::PathBuf;
 use syntax::ast;
 use syntax::attr;
+use rustc_back::svh::Svh;
 use rustc_back::target::Target;
 use rustc_front::hir;
 
-pub use metadata::common::LinkMeta;
-pub use metadata::creader::validate_crate_name;
-pub use metadata::cstore::CrateSource;
-pub use metadata::cstore::LinkagePreference;
-pub use metadata::cstore::NativeLibraryKind;
-pub use metadata::decoder::DecodeInlinedItem;
-pub use metadata::decoder::DefLike;
-pub use metadata::inline::InlinedItem;
-
-pub use self::DefLike::{DlDef, DlField, DlImpl};
-pub use self::NativeLibraryKind::{NativeStatic, NativeFramework, NativeUnknown};
-
-pub struct ChildItem {
-    pub def: DefLike,
-    pub name: ast::Name,
-    pub vis: hir::Visibility
-}
-
-pub enum FoundAst<'ast> {
-    Found(&'ast InlinedItem),
-    FoundParent(DefId, &'ast InlinedItem),
-    NotFound,
-}
-
-pub trait CrateStore<'tcx> : Any {
-    // item info
-    fn stability(&self, def: DefId) -> Option<attr::Stability>;
-    fn closure_kind(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId)
-                    -> ty::ClosureKind;
-    fn closure_ty(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId)
-                  -> ty::ClosureTy<'tcx>;
-    fn item_variances(&self, def: DefId) -> ty::ItemVariances;
-    fn repr_attrs(&self, def: DefId) -> Vec<attr::ReprAttr>;
-    fn item_type(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
-                 -> ty::TypeScheme<'tcx>;
-    fn item_path(&self, def: DefId) -> Vec<ast_map::PathElem>;
-    fn item_name(&self, def: DefId) -> ast::Name;
-    fn item_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
-                       -> ty::GenericPredicates<'tcx>;
-    fn item_super_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
-                             -> ty::GenericPredicates<'tcx>;
-    fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute>;
-    fn item_symbol(&self, def: DefId) -> String;
-    fn trait_def(&self, tcx: &ty::ctxt<'tcx>, def: DefId)-> ty::TraitDef<'tcx>;
-    fn adt_def(&self, tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>;
-    fn method_arg_names(&self, did: DefId) -> Vec<String>;
-    fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>;
-
-    // trait info
-    fn implementations_of_trait(&self, def_id: DefId) -> Vec<DefId>;
-    fn provided_trait_methods(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
-                              -> Vec<Rc<ty::Method<'tcx>>>;
-    fn trait_item_def_ids(&self, def: DefId)
-                          -> Vec<ty::ImplOrTraitItemId>;
-
-    // impl info
-    fn impl_items(&self, impl_def_id: DefId) -> Vec<ty::ImplOrTraitItemId>;
-    fn impl_trait_ref(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
-                      -> Option<ty::TraitRef<'tcx>>;
-    fn impl_polarity(&self, def: DefId) -> Option<hir::ImplPolarity>;
-    fn custom_coerce_unsized_kind(&self, def: DefId)
-                                  -> Option<ty::adjustment::CustomCoerceUnsized>;
-    fn associated_consts(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
-                         -> Vec<Rc<ty::AssociatedConst<'tcx>>>;
-
-    // trait/impl-item info
-    fn trait_of_item(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId)
-                     -> Option<DefId>;
-    fn impl_or_trait_item(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
-                          -> ty::ImplOrTraitItem<'tcx>;
-
-    // flags
-    fn is_const_fn(&self, did: DefId) -> bool;
-    fn is_defaulted_trait(&self, did: DefId) -> bool;
-    fn is_impl(&self, did: DefId) -> bool;
-    fn is_default_impl(&self, impl_did: DefId) -> bool;
-    fn is_extern_fn(&self, tcx: &ty::ctxt<'tcx>, did: DefId) -> bool;
-    fn is_static(&self, did: DefId) -> bool;
-    fn is_static_method(&self, did: DefId) -> bool;
-    fn is_statically_included_foreign_item(&self, id: ast::NodeId) -> bool;
-    fn is_typedef(&self, did: DefId) -> bool;
-
-    // crate metadata
-    fn dylib_dependency_formats(&self, cnum: ast::CrateNum)
-                                    -> Vec<(ast::CrateNum, LinkagePreference)>;
-    fn lang_items(&self, cnum: ast::CrateNum) -> Vec<(DefIndex, usize)>;
-    fn missing_lang_items(&self, cnum: ast::CrateNum) -> Vec<lang_items::LangItem>;
-    fn is_staged_api(&self, cnum: ast::CrateNum) -> bool;
-    fn is_explicitly_linked(&self, cnum: ast::CrateNum) -> bool;
-    fn is_allocator(&self, cnum: ast::CrateNum) -> bool;
-    fn crate_attrs(&self, cnum: ast::CrateNum) -> Vec<ast::Attribute>;
-    fn crate_name(&self, cnum: ast::CrateNum) -> String;
-    fn crate_hash(&self, cnum: ast::CrateNum) -> Svh;
-    fn crate_struct_field_attrs(&self, cnum: ast::CrateNum)
-                                -> FnvHashMap<DefId, Vec<ast::Attribute>>;
-    fn plugin_registrar_fn(&self, cnum: ast::CrateNum) -> Option<DefId>;
-    fn native_libraries(&self, cnum: ast::CrateNum) -> Vec<(NativeLibraryKind, String)>;
-    fn reachable_ids(&self, cnum: ast::CrateNum) -> Vec<DefId>;
-
-    // resolve
-    fn def_path(&self, def: DefId) -> ast_map::DefPath;
-    fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>;
-    fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
-    fn item_children(&self, did: DefId) -> Vec<ChildItem>;
-    fn crate_top_level_items(&self, cnum: ast::CrateNum) -> Vec<ChildItem>;
-
-    // misc. metadata
-    fn maybe_get_item_ast(&'tcx self, tcx: &ty::ctxt<'tcx>, def: DefId)
-                          -> FoundAst<'tcx>;
-    // This is basically a 1-based range of ints, which is a little
-    // silly - I may fix that.
-    fn crates(&self) -> Vec<ast::CrateNum>;
-    fn used_libraries(&self) -> Vec<(String, NativeLibraryKind)>;
-    fn used_link_args(&self) -> Vec<String>;
-
-    // utility functions
-    fn metadata_filename(&self) -> &str;
-    fn metadata_section_name(&self, target: &Target) -> &str;
-    fn encode_type(&self, tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> Vec<u8>;
-    fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option<PathBuf>)>;
-    fn used_crate_source(&self, cnum: ast::CrateNum) -> CrateSource;
-    fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<ast::CrateNum>;
-    fn encode_metadata(&self,
-                       tcx: &ty::ctxt<'tcx>,
-                       reexports: &def::ExportMap,
-                       item_symbols: &RefCell<NodeMap<String>>,
-                       link_meta: &LinkMeta,
-                       reachable: &NodeSet,
-                       krate: &hir::Crate) -> Vec<u8>;
-    fn metadata_encoding_version(&self) -> &[u8];
-}
-
 impl<'tcx> CrateStore<'tcx> for cstore::CStore {
     fn stability(&self, def: DefId) -> Option<attr::Stability>
     {
diff --git a/src/librustc/metadata/cstore.rs b/src/librustc_metadata/cstore.rs
index 706033f815c..6a1f9d16fe7 100644
--- a/src/librustc/metadata/cstore.rs
+++ b/src/librustc_metadata/cstore.rs
@@ -14,13 +14,15 @@
 // crates and libraries
 
 pub use self::MetadataBlob::*;
-pub use self::LinkagePreference::*;
-pub use self::NativeLibraryKind::*;
 
-use back::svh::Svh;
-use metadata::{creader, decoder, index, loader};
-use session::search_paths::PathKind;
-use util::nodemap::{FnvHashMap, NodeMap, NodeSet};
+use creader;
+use decoder;
+use index;
+use loader;
+
+use rustc::back::svh::Svh;
+use rustc::front::map as ast_map;
+use rustc::util::nodemap::{FnvHashMap, NodeMap, NodeSet};
 
 use std::cell::{RefCell, Ref, Cell};
 use std::rc::Rc;
@@ -32,7 +34,10 @@ use syntax::codemap;
 use syntax::parse::token;
 use syntax::parse::token::IdentInterner;
 use syntax::util::small_vector::SmallVector;
-use front::map as ast_map;
+
+pub use middle::cstore::{NativeLibraryKind, LinkagePreference};
+pub use middle::cstore::{NativeStatic, NativeFramework, NativeUnknown};
+pub use middle::cstore::{CrateSource, LinkMeta};
 
 // A map from external crate numbers (as decoded from some crate file) to
 // local crate numbers (as generated during this session). Each external
@@ -77,30 +82,6 @@ pub struct crate_metadata {
     pub explicitly_linked: Cell<bool>,
 }
 
-#[derive(Copy, Debug, PartialEq, Clone)]
-pub enum LinkagePreference {
-    RequireDynamic,
-    RequireStatic,
-}
-
-enum_from_u32! {
-    #[derive(Copy, Clone, PartialEq)]
-    pub enum NativeLibraryKind {
-        NativeStatic,    // native static library (.a archive)
-        NativeFramework, // OSX-specific
-        NativeUnknown,   // default way to specify a dynamic library
-    }
-}
-
-// Where a crate came from on the local filesystem. One of these two options
-// must be non-None.
-#[derive(PartialEq, Clone, Debug)]
-pub struct CrateSource {
-    pub dylib: Option<(PathBuf, PathKind)>,
-    pub rlib: Option<(PathBuf, PathKind)>,
-    pub cnum: ast::CrateNum,
-}
-
 pub struct CStore {
     metas: RefCell<FnvHashMap<ast::CrateNum, Rc<crate_metadata>>>,
     /// Map from NodeId's of local extern crate statements to crate numbers
@@ -112,10 +93,6 @@ pub struct CStore {
     pub intr: Rc<IdentInterner>,
 }
 
-/// Item definitions in the currently-compiled crate would have the CrateNum
-/// LOCAL_CRATE in their DefId.
-pub const LOCAL_CRATE: ast::CrateNum = 0;
-
 impl CStore {
     pub fn new(intr: Rc<IdentInterner>) -> CStore {
         CStore {
@@ -218,8 +195,8 @@ impl CStore {
         let mut libs = self.used_crate_sources.borrow()
             .iter()
             .map(|src| (src.cnum, match prefer {
-                RequireDynamic => src.dylib.clone().map(|p| p.0),
-                RequireStatic => src.rlib.clone().map(|p| p.0),
+                LinkagePreference::RequireDynamic => src.dylib.clone().map(|p| p.0),
+                LinkagePreference::RequireStatic => src.rlib.clone().map(|p| p.0),
             }))
             .collect::<Vec<_>>();
         libs.sort_by(|&(a, _), &(b, _)| {
diff --git a/src/librustc/metadata/decoder.rs b/src/librustc_metadata/decoder.rs
index 84a95f04795..092f7849115 100644
--- a/src/librustc/metadata/decoder.rs
+++ b/src/librustc_metadata/decoder.rs
@@ -12,29 +12,27 @@
 
 #![allow(non_camel_case_types)]
 
-pub use self::DefLike::*;
 use self::Family::*;
 
-use front::map as hir_map;
+use cstore::{self, crate_metadata};
+use common::*;
+use encoder::def_to_u64;
+use index;
+use tydecode::TyDecoder;
+
+use rustc::back::svh::Svh;
+use rustc::front::map as hir_map;
+use rustc::util::nodemap::FnvHashMap;
 use rustc_front::hir;
 
-use back::svh::Svh;
-use metadata::cstore::crate_metadata;
-use metadata::cstore::LOCAL_CRATE;
-use metadata::common::*;
-use metadata::cstore;
-use metadata::encoder::def_to_u64;
-use metadata::index;
-use metadata::inline::InlinedItem;
-use metadata::tydecode::TyDecoder;
-use metadata::util::FoundAst;
+use middle::cstore::{LOCAL_CRATE, FoundAst, InlinedItem, LinkagePreference};
+use middle::cstore::{DefLike, DlDef, DlField, DlImpl};
 use middle::def;
 use middle::def_id::{DefId, DefIndex};
 use middle::lang_items;
 use middle::subst;
 use middle::ty::{ImplContainer, TraitContainer};
 use middle::ty::{self, RegionEscape, Ty};
-use util::nodemap::FnvHashMap;
 
 use std::cell::{Cell, RefCell};
 use std::io::prelude::*;
@@ -590,14 +588,6 @@ pub fn get_symbol_from_buf(data: &[u8], id: DefIndex) -> String {
     item_symbol(doc)
 }
 
-// Something that a name can resolve to.
-#[derive(Copy, Clone, Debug)]
-pub enum DefLike {
-    DlDef(def::Def),
-    DlImpl(DefId),
-    DlField
-}
-
 /// Iterates over the language items in the given crate.
 pub fn each_lang_item<F>(cdata: Cmd, mut f: F) -> bool where
     F: FnMut(DefIndex, usize) -> bool,
@@ -1315,7 +1305,7 @@ pub fn each_exported_macro<F>(data: &[u8], intr: &IdentInterner, mut f: F) where
 }
 
 pub fn get_dylib_dependency_formats(cdata: Cmd)
-    -> Vec<(ast::CrateNum, cstore::LinkagePreference)>
+    -> Vec<(ast::CrateNum, LinkagePreference)>
 {
     let formats = reader::get_doc(rbml::Doc::new(cdata.data()),
                                   tag_dylib_dependency_formats);
@@ -1332,9 +1322,9 @@ pub fn get_dylib_dependency_formats(cdata: Cmd)
             None => panic!("didn't find a crate in the cnum_map")
         };
         result.push((cnum, if link == "d" {
-            cstore::RequireDynamic
+            LinkagePreference::RequireDynamic
         } else {
-            cstore::RequireStatic
+            LinkagePreference::RequireStatic
         }));
     }
     return result;
diff --git a/src/librustc_metadata/diagnostics.rs b/src/librustc_metadata/diagnostics.rs
new file mode 100644
index 00000000000..2340efd2cae
--- /dev/null
+++ b/src/librustc_metadata/diagnostics.rs
@@ -0,0 +1,77 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![allow(non_snake_case)]
+
+register_long_diagnostics! {
+E0454: r##"
+A link name was given with an empty name. Erroneous code example:
+
+```
+#[link(name = "")] extern {} // error: #[link(name = "")] given with empty name
+```
+
+The rust compiler cannot link to an external library if you don't give it its
+name. Example:
+
+```
+#[link(name = "some_lib")] extern {} // ok!
+```
+"##,
+
+E0458: r##"
+An unknown "kind" was specified for a link attribute. Erroneous code example:
+
+```
+#[link(kind = "wonderful_unicorn")] extern {}
+// error: unknown kind: `wonderful_unicorn`
+```
+
+Please specify a valid "kind" value, from one of the following:
+ * static
+ * dylib
+ * framework
+"##,
+
+E0459: r##"
+A link was used without a name parameter. Erroneous code example:
+
+```
+#[link(kind = "dylib")] extern {}
+// error: #[link(...)] specified without `name = "foo"`
+```
+
+Please add the name parameter to allow the rust compiler to find the library
+you want. Example:
+
+```
+#[link(kind = "dylib", name = "some_lib")] extern {} // ok!
+```
+"##,
+
+}
+
+register_diagnostics! {
+    E0455, // native frameworks are only available on OSX targets
+    E0456, // plugin `..` is not available for triple `..`
+    E0457, // plugin `..` only found in rlib format, but must be available...
+    E0514, // metadata version mismatch
+    E0460, // found possibly newer version of crate `..`
+    E0461, // couldn't find crate `..` with expected target triple ..
+    E0462, // found staticlib `..` instead of rlib or dylib
+    E0463, // can't find crate for `..`
+    E0464, // multiple matching crates for `..`
+    E0465, // multiple .. candidates for `..` found
+    E0466, // bad macro import
+    E0467, // bad macro reexport
+    E0468, // an `extern crate` loading macros must be at the crate root
+    E0469, // imported macro not found
+    E0470, // reexported macro not found
+}
diff --git a/src/librustc/metadata/encoder.rs b/src/librustc_metadata/encoder.rs
index d86d6c25cce..1d88fa4454b 100644
--- a/src/librustc/metadata/encoder.rs
+++ b/src/librustc_metadata/encoder.rs
@@ -13,23 +13,25 @@
 #![allow(unused_must_use)] // everything is just a MemWriter, can't fail
 #![allow(non_camel_case_types)]
 
-use back::svh::Svh;
-use session::config;
-use metadata::common::*;
-use metadata::cstore;
-use metadata::cstore::LOCAL_CRATE;
-use metadata::decoder;
-use metadata::tyencode;
-use metadata::index::{self, IndexData};
-use metadata::inline::InlinedItemRef;
-use metadata::util::CrateStore;
+use common::*;
+use cstore;
+use decoder;
+use tyencode;
+use index::{self, IndexData};
+
+use middle::cstore::{LOCAL_CRATE, CrateStore, InlinedItemRef, LinkMeta};
 use middle::def;
 use middle::def_id::{CRATE_DEF_INDEX, DefId};
 use middle::dependency_format::Linkage;
 use middle::stability;
 use middle::subst;
 use middle::ty::{self, Ty};
-use util::nodemap::{FnvHashMap, NodeMap, NodeSet};
+
+use rustc::back::svh::Svh;
+use rustc::front::map::{LinkedPath, PathElem, PathElems};
+use rustc::front::map as ast_map;
+use rustc::session::config;
+use rustc::util::nodemap::{FnvHashMap, NodeMap, NodeSet};
 
 use serialize::Encodable;
 use std::cell::RefCell;
@@ -49,8 +51,6 @@ use rbml::writer::Encoder;
 use rustc_front::hir;
 use rustc_front::intravisit::Visitor;
 use rustc_front::intravisit;
-use front::map::{LinkedPath, PathElem, PathElems};
-use front::map as ast_map;
 
 pub type EncodeInlinedItem<'a> =
     Box<FnMut(&EncodeContext, &mut Encoder, InlinedItemRef) + 'a>;
diff --git a/src/librustc/metadata/index.rs b/src/librustc_metadata/index.rs
index 60bbdaddd75..60bbdaddd75 100644
--- a/src/librustc/metadata/index.rs
+++ b/src/librustc_metadata/index.rs
diff --git a/src/librustc_metadata/lib.rs b/src/librustc_metadata/lib.rs
new file mode 100644
index 00000000000..6affbd2b593
--- /dev/null
+++ b/src/librustc_metadata/lib.rs
@@ -0,0 +1,61 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![cfg_attr(stage0, feature(custom_attribute))]
+#![crate_name = "rustc_metadata"]
+#![unstable(feature = "rustc_private", issue = "27812")]
+#![cfg_attr(stage0, staged_api)]
+#![crate_type = "dylib"]
+#![crate_type = "rlib"]
+#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
+      html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
+      html_root_url = "https://doc.rust-lang.org/nightly/")]
+
+#![feature(box_patterns)]
+#![feature(duration_span)]
+#![feature(enumset)]
+#![feature(quote)]
+#![feature(staged_api)]
+#![feature(vec_push_all)]
+#![feature(rustc_diagnostic_macros)]
+#![feature(rustc_private)]
+
+#[macro_use] extern crate log;
+#[macro_use] extern crate syntax;
+#[macro_use] #[no_link] extern crate rustc_bitflags;
+
+extern crate flate;
+extern crate rbml;
+extern crate serialize;
+
+extern crate rustc;
+extern crate rustc_back;
+extern crate rustc_front;
+extern crate rustc_llvm;
+
+pub use rustc::middle;
+
+#[macro_use]
+mod macros;
+
+pub mod diagnostics;
+
+pub mod astencode;
+pub mod common;
+pub mod tyencode;
+pub mod tydecode;
+pub mod encoder;
+pub mod decoder;
+pub mod creader;
+pub mod csearch;
+pub mod cstore;
+pub mod index;
+pub mod loader;
+pub mod macro_import;
diff --git a/src/librustc/metadata/loader.rs b/src/librustc_metadata/loader.rs
index ca1562683ef..72938a7660a 100644
--- a/src/librustc/metadata/loader.rs
+++ b/src/librustc_metadata/loader.rs
@@ -212,19 +212,21 @@
 //! no means all of the necessary details. Take a look at the rest of
 //! metadata::loader or metadata::creader for all the juicy details!
 
-use back::svh::Svh;
-use session::Session;
-use session::search_paths::PathKind;
-use llvm;
-use llvm::{False, ObjectFile, mk_section_iter};
-use llvm::archive_ro::ArchiveRO;
-use metadata::cstore::{MetadataBlob, MetadataVec, MetadataArchive};
-use metadata::decoder;
-use metadata::encoder;
-use metadata::filesearch::{FileSearch, FileMatches, FileDoesntMatch};
+use cstore::{MetadataBlob, MetadataVec, MetadataArchive};
+use decoder;
+use encoder;
+
+use rustc::back::svh::Svh;
+use rustc::session::Session;
+use rustc::session::filesearch::{FileSearch, FileMatches, FileDoesntMatch};
+use rustc::session::search_paths::PathKind;
+use rustc::util::common;
+
+use rustc_llvm as llvm;
+use rustc_llvm::{False, ObjectFile, mk_section_iter};
+use rustc_llvm::archive_ro::ArchiveRO;
 use syntax::codemap::Span;
 use syntax::diagnostic::SpanHandler;
-use util::common;
 use rustc_back::target::Target;
 
 use std::cmp;
diff --git a/src/librustc/metadata/macro_import.rs b/src/librustc_metadata/macro_import.rs
index b08f5d0640e..d67fc3a0eab 100644
--- a/src/librustc/metadata/macro_import.rs
+++ b/src/librustc_metadata/macro_import.rs
@@ -10,9 +10,10 @@
 
 //! Used by `rustc` when loading a crate with exported macros.
 
-use session::Session;
-use metadata::creader::CrateReader;
-use metadata::cstore::CStore;
+use creader::CrateReader;
+use cstore::CStore;
+
+use rustc::session::Session;
 
 use std::collections::{HashSet, HashMap};
 use syntax::codemap::Span;
diff --git a/src/librustc_metadata/macros.rs b/src/librustc_metadata/macros.rs
new file mode 100644
index 00000000000..ed764ebd9f9
--- /dev/null
+++ b/src/librustc_metadata/macros.rs
@@ -0,0 +1,46 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+macro_rules! enum_from_u32 {
+    ($(#[$attr:meta])* pub enum $name:ident {
+        $($variant:ident = $e:expr,)*
+    }) => {
+        $(#[$attr])*
+        pub enum $name {
+            $($variant = $e),*
+        }
+
+        impl $name {
+            pub fn from_u32(u: u32) -> Option<$name> {
+                $(if u == $name::$variant as u32 {
+                    return Some($name::$variant)
+                })*
+                None
+            }
+        }
+    };
+    ($(#[$attr:meta])* pub enum $name:ident {
+        $($variant:ident,)*
+    }) => {
+        $(#[$attr])*
+        pub enum $name {
+            $($variant,)*
+        }
+
+        impl $name {
+            pub fn from_u32(u: u32) -> Option<$name> {
+                $(if u == $name::$variant as u32 {
+                    return Some($name::$variant)
+                })*
+                None
+            }
+        }
+    }
+}
diff --git a/src/librustc/metadata/tydecode.rs b/src/librustc_metadata/tydecode.rs
index d03af6b6722..d03af6b6722 100644
--- a/src/librustc/metadata/tydecode.rs
+++ b/src/librustc_metadata/tydecode.rs
diff --git a/src/librustc/metadata/tyencode.rs b/src/librustc_metadata/tyencode.rs
index 1b993a00e28..bc1edd5c767 100644
--- a/src/librustc/metadata/tyencode.rs
+++ b/src/librustc_metadata/tyencode.rs
@@ -23,7 +23,7 @@ use middle::subst;
 use middle::subst::VecPerParamSpace;
 use middle::ty::ParamTy;
 use middle::ty::{self, Ty};
-use util::nodemap::FnvHashMap;
+use rustc::util::nodemap::FnvHashMap;
 
 use rustc_front::hir;
 
diff --git a/src/librustc_plugin/lib.rs b/src/librustc_plugin/lib.rs
index 33d63d833c7..5dedef7ab6c 100644
--- a/src/librustc_plugin/lib.rs
+++ b/src/librustc_plugin/lib.rs
@@ -71,6 +71,7 @@
 
 extern crate rustc;
 extern crate rustc_front;
+extern crate rustc_metadata;
 
 pub use self::registry::Registry;
 
diff --git a/src/librustc_plugin/load.rs b/src/librustc_plugin/load.rs
index d121a306c61..51eec07505a 100644
--- a/src/librustc_plugin/load.rs
+++ b/src/librustc_plugin/load.rs
@@ -11,8 +11,8 @@
 //! Used by `rustc` when loading a plugin.
 
 use rustc::session::Session;
-use rustc::metadata::creader::CrateReader;
-use rustc::metadata::cstore::CStore;
+use rustc_metadata::creader::CrateReader;
+use rustc_metadata::cstore::CStore;
 use registry::Registry;
 
 use std::borrow::ToOwned;
diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs
index 752e7e55f55..13f7e318163 100644
--- a/src/librustc_resolve/build_reduced_graph.rs
+++ b/src/librustc_resolve/build_reduced_graph.rs
@@ -28,7 +28,7 @@ use {resolve_error, ResolutionError};
 
 use self::DuplicateCheckingMode::*;
 
-use rustc::metadata::util::{CrateStore, ChildItem, DlDef, DlField, DlImpl};
+use rustc::middle::cstore::{CrateStore, ChildItem, DlDef, DlField, DlImpl};
 use rustc::middle::def::*;
 use rustc::middle::def_id::{CRATE_DEF_INDEX, DefId};
 
diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs
index 6bfe9cd393e..ef03ac520df 100644
--- a/src/librustc_resolve/lib.rs
+++ b/src/librustc_resolve/lib.rs
@@ -54,7 +54,7 @@ use self::FallbackChecks::*;
 use rustc::front::map as hir_map;
 use rustc::session::Session;
 use rustc::lint;
-use rustc::metadata::util::{CrateStore, DefLike, DlDef};
+use rustc::middle::cstore::{CrateStore, DefLike, DlDef};
 use rustc::middle::def::*;
 use rustc::middle::def_id::DefId;
 use rustc::middle::pat_util::pat_bindings_hygienic;
diff --git a/src/librustc_trans/back/archive.rs b/src/librustc_trans/back/archive.rs
index d7f9002a4f4..f5431554a75 100644
--- a/src/librustc_trans/back/archive.rs
+++ b/src/librustc_trans/back/archive.rs
@@ -21,7 +21,7 @@ use std::process::{Command, Output, Stdio};
 use std::ptr;
 use std::str;
 
-use metadata::util::CrateStore;
+use middle::cstore::CrateStore;
 
 use libc;
 use llvm::archive_ro::{ArchiveRO, Child};
diff --git a/src/librustc_trans/back/link.rs b/src/librustc_trans/back/link.rs
index cefe04f5e35..d7b4243afee 100644
--- a/src/librustc_trans/back/link.rs
+++ b/src/librustc_trans/back/link.rs
@@ -17,12 +17,11 @@ use super::svh::Svh;
 use session::config;
 use session::config::NoDebugInfo;
 use session::config::{OutputFilenames, Input, OutputType};
+use session::filesearch;
 use session::search_paths::PathKind;
 use session::Session;
-use metadata::{util as mdutil};
-use metadata::filesearch;
-use metadata::util::{CrateStore, LinkMeta};
-use metadata::util::{LinkagePreference, NativeLibraryKind};
+use middle::cstore::{self, CrateStore, LinkMeta};
+use middle::cstore::{LinkagePreference, NativeLibraryKind};
 use middle::dependency_format::Linkage;
 use middle::ty::{self, Ty};
 use rustc::front::map::DefPath;
@@ -138,7 +137,7 @@ pub fn find_crate_name(sess: Option<&Session>,
                        attrs: &[ast::Attribute],
                        input: &Input) -> String {
     let validate = |s: String, span: Option<Span>| {
-        mdutil::validate_crate_name(sess, &s[..], span);
+        cstore::validate_crate_name(sess, &s[..], span);
         s
     };
 
diff --git a/src/librustc_trans/back/linker.rs b/src/librustc_trans/back/linker.rs
index 8235df581c8..1ee1c9f1912 100644
--- a/src/librustc_trans/back/linker.rs
+++ b/src/librustc_trans/back/linker.rs
@@ -16,7 +16,7 @@ use std::path::{Path, PathBuf};
 use std::process::Command;
 
 use back::archive;
-use metadata::util::CrateStore;
+use middle::cstore::CrateStore;
 use middle::dependency_format::Linkage;
 use session::Session;
 use session::config::CrateTypeDylib;
diff --git a/src/librustc_trans/lib.rs b/src/librustc_trans/lib.rs
index 67a7f3efb27..b672c49bdca 100644
--- a/src/librustc_trans/lib.rs
+++ b/src/librustc_trans/lib.rs
@@ -62,7 +62,6 @@ extern crate serialize;
 #[macro_use] extern crate syntax;
 
 pub use rustc::session;
-pub use rustc::metadata;
 pub use rustc::middle;
 pub use rustc::lint;
 pub use rustc::util;
diff --git a/src/librustc_trans/save/recorder.rs b/src/librustc_trans/save/recorder.rs
index 34eb1d28263..a95a4c052fa 100644
--- a/src/librustc_trans/save/recorder.rs
+++ b/src/librustc_trans/save/recorder.rs
@@ -13,7 +13,7 @@ pub use self::Row::*;
 use super::escape;
 use super::span_utils::SpanUtils;
 
-use metadata::cstore::LOCAL_CRATE;
+use middle::cstore::LOCAL_CRATE;
 use middle::def_id::{CRATE_DEF_INDEX, DefId};
 use middle::ty;
 
diff --git a/src/librustc_trans/trans/base.rs b/src/librustc_trans/trans/base.rs
index 30ec1e662b0..d6b33672df0 100644
--- a/src/librustc_trans/trans/base.rs
+++ b/src/librustc_trans/trans/base.rs
@@ -35,6 +35,7 @@ use lint;
 use llvm::{BasicBlockRef, Linkage, ValueRef, Vector, get_param};
 use llvm;
 use middle::cfg;
+use middle::cstore::CrateStore;
 use middle::def_id::DefId;
 use middle::infer;
 use middle::lang_items::{LangItem, ExchangeMallocFnLangItem, StartFnLangItem};
@@ -42,7 +43,6 @@ use middle::weak_lang_items;
 use middle::pat_util::simple_name;
 use middle::subst::Substs;
 use middle::ty::{self, Ty, HasTypeFlags};
-use metadata::util::CrateStore;
 use rustc::front::map as hir_map;
 use rustc_mir::mir_map::MirMap;
 use session::config::{self, NoDebugInfo, FullDebugInfo};
diff --git a/src/librustc_trans/trans/callee.rs b/src/librustc_trans/trans/callee.rs
index a52c7f94c3a..0c0bda45d8c 100644
--- a/src/librustc_trans/trans/callee.rs
+++ b/src/librustc_trans/trans/callee.rs
@@ -22,7 +22,7 @@ use arena::TypedArena;
 use back::link;
 use session;
 use llvm::{self, ValueRef, get_params};
-use metadata::cstore::LOCAL_CRATE;
+use middle::cstore::LOCAL_CRATE;
 use middle::def;
 use middle::def_id::DefId;
 use middle::infer;
diff --git a/src/librustc_trans/trans/consts.rs b/src/librustc_trans/trans/consts.rs
index 91f17a50e2c..6f40283064b 100644
--- a/src/librustc_trans/trans/consts.rs
+++ b/src/librustc_trans/trans/consts.rs
@@ -13,8 +13,8 @@ use back::abi;
 use llvm;
 use llvm::{ConstFCmp, ConstICmp, SetLinkage, SetUnnamedAddr};
 use llvm::{InternalLinkage, ValueRef, Bool, True};
-use metadata::cstore::LOCAL_CRATE;
 use middle::{check_const, def};
+use middle::cstore::LOCAL_CRATE;
 use middle::const_eval::{self, ConstVal, ConstEvalErr};
 use middle::const_eval::{const_int_checked_neg, const_uint_checked_neg};
 use middle::const_eval::{const_int_checked_add, const_uint_checked_add};
diff --git a/src/librustc_trans/trans/context.rs b/src/librustc_trans/trans/context.rs
index 1f1d43feeb3..c6ca2e176aa 100644
--- a/src/librustc_trans/trans/context.rs
+++ b/src/librustc_trans/trans/context.rs
@@ -10,7 +10,7 @@
 
 use llvm;
 use llvm::{ContextRef, ModuleRef, ValueRef, BuilderRef};
-use metadata::common::LinkMeta;
+use middle::cstore::LinkMeta;
 use middle::def::ExportMap;
 use middle::def_id::DefId;
 use middle::traits;
diff --git a/src/librustc_trans/trans/inline.rs b/src/librustc_trans/trans/inline.rs
index 5be4892484b..29965755eac 100644
--- a/src/librustc_trans/trans/inline.rs
+++ b/src/librustc_trans/trans/inline.rs
@@ -9,7 +9,7 @@
 // except according to those terms.
 
 use llvm::{AvailableExternallyLinkage, InternalLinkage, SetLinkage};
-use metadata::util::{CrateStore, FoundAst, InlinedItem};
+use middle::cstore::{CrateStore, FoundAst, InlinedItem};
 use middle::def_id::DefId;
 use middle::subst::Substs;
 use trans::base::{push_ctxt, trans_item, get_item_val, trans_fn};
diff --git a/src/librustc_trans/trans/mod.rs b/src/librustc_trans/trans/mod.rs
index fa37b005539..b102e96af20 100644
--- a/src/librustc_trans/trans/mod.rs
+++ b/src/librustc_trans/trans/mod.rs
@@ -9,7 +9,7 @@
 // except according to those terms.
 
 use llvm::{ContextRef, ModuleRef};
-use metadata::common::LinkMeta;
+use middle::cstore::LinkMeta;
 
 pub use self::base::trans_crate;
 pub use self::context::CrateContext;
diff --git a/src/librustc_typeck/check/callee.rs b/src/librustc_typeck/check/callee.rs
index 988d5537e5d..6a23be682e9 100644
--- a/src/librustc_typeck/check/callee.rs
+++ b/src/librustc_typeck/check/callee.rs
@@ -25,7 +25,7 @@ use super::UnresolvedTypeAction;
 use super::write_call;
 
 use CrateCtxt;
-use metadata::cstore::LOCAL_CRATE;
+use middle::cstore::LOCAL_CRATE;
 use middle::def_id::DefId;
 use middle::infer;
 use middle::ty::{self, LvaluePreference, Ty};
diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs
index f4f4dc90feb..955bc92a8f3 100644
--- a/src/librustc_typeck/check/method/suggest.rs
+++ b/src/librustc_typeck/check/method/suggest.rs
@@ -17,12 +17,12 @@ use astconv::AstConv;
 use check::{self, FnCtxt};
 use front::map as hir_map;
 use middle::ty::{self, Ty, ToPolyTraitRef, ToPredicate, HasTypeFlags};
+use middle::cstore::{self, CrateStore, DefLike};
 use middle::def;
 use middle::def_id::DefId;
 use middle::lang_items::FnOnceTraitLangItem;
 use middle::subst::Substs;
 use middle::traits::{Obligation, SelectionContext};
-use metadata::util::{self as mdutil, CrateStore, DefLike};
 use util::nodemap::{FnvHashSet};
 
 use syntax::ast;
@@ -418,13 +418,13 @@ pub fn all_traits<'a>(ccx: &'a CrateCtxt) -> AllTraits<'a> {
         fn handle_external_def(traits: &mut AllTraitsVec,
                                external_mods: &mut FnvHashSet<DefId>,
                                ccx: &CrateCtxt,
-                               cstore: &for<'a> mdutil::CrateStore<'a>,
-                               dl: mdutil::DefLike) {
+                               cstore: &for<'a> cstore::CrateStore<'a>,
+                               dl: cstore::DefLike) {
             match dl {
-                mdutil::DlDef(def::DefTrait(did)) => {
+                cstore::DlDef(def::DefTrait(did)) => {
                     traits.push(TraitInfo::new(did));
                 }
-                mdutil::DlDef(def::DefMod(did)) => {
+                cstore::DlDef(def::DefMod(did)) => {
                     if !external_mods.insert(did) {
                         return;
                     }
diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs
index c375eb19a37..be60d2f3dcf 100644
--- a/src/librustc_typeck/check/mod.rs
+++ b/src/librustc_typeck/check/mod.rs
@@ -83,8 +83,8 @@ use self::TupleArgumentsFlag::*;
 use astconv::{self, ast_region_to_region, ast_ty_to_ty, AstConv, PathParamMode};
 use check::_match::pat_ctxt;
 use fmt_macros::{Parser, Piece, Position};
-use metadata::cstore::LOCAL_CRATE;
 use middle::astconv_util::prohibit_type_params;
+use middle::cstore::LOCAL_CRATE;
 use middle::def;
 use middle::def_id::DefId;
 use middle::infer;
diff --git a/src/librustc_typeck/coherence/orphan.rs b/src/librustc_typeck/coherence/orphan.rs
index 8eeafb9b432..e6e31ba0819 100644
--- a/src/librustc_typeck/coherence/orphan.rs
+++ b/src/librustc_typeck/coherence/orphan.rs
@@ -11,7 +11,7 @@
 //! Orphan checker: every impl either implements a trait defined in this
 //! crate or pertains to a type defined in this crate.
 
-use metadata::cstore::LOCAL_CRATE;
+use middle::cstore::LOCAL_CRATE;
 use middle::def_id::DefId;
 use middle::traits;
 use middle::ty;
diff --git a/src/librustc_typeck/coherence/overlap.rs b/src/librustc_typeck/coherence/overlap.rs
index 6a50ceba2f0..693c8716ab5 100644
--- a/src/librustc_typeck/coherence/overlap.rs
+++ b/src/librustc_typeck/coherence/overlap.rs
@@ -11,8 +11,7 @@
 //! Overlap: No two impls for the same trait are implemented for the
 //! same type.
 
-use metadata::cstore::LOCAL_CRATE;
-use metadata::util::CrateStore;
+use middle::cstore::{CrateStore, LOCAL_CRATE};
 use middle::def_id::DefId;
 use middle::traits;
 use middle::ty;
diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs
index 3d28a912179..4c09df41895 100644
--- a/src/librustc_typeck/lib.rs
+++ b/src/librustc_typeck/lib.rs
@@ -99,7 +99,6 @@ extern crate rustc_back;
 
 pub use rustc::front;
 pub use rustc::lint;
-pub use rustc::metadata;
 pub use rustc::middle;
 pub use rustc::session;
 pub use rustc::util;