From c0a36b71be60e4dcb6fd5ef186a3fe9e694e4550 Mon Sep 17 00:00:00 2001
From: Brian Anderson <banderson@mozilla.com>
Date: Tue, 22 May 2012 17:48:04 -0700
Subject: [PATCH] rustc: Eliminate metadata's dependency on session

---
 src/rustc/driver/driver.rs    |  6 +++-
 src/rustc/metadata.rs         |  2 --
 src/rustc/metadata/creader.rs | 52 +++++++++++++++++++++--------------
 src/rustc/metadata/csearch.rs | 24 ++++++++--------
 src/rustc/metadata/loader.rs  | 29 +++++++++----------
 5 files changed, 64 insertions(+), 49 deletions(-)

diff --git a/src/rustc/driver/driver.rs b/src/rustc/driver/driver.rs
index d354292974c..655a6f82b78 100644
--- a/src/rustc/driver/driver.rs
+++ b/src/rustc/driver/driver.rs
@@ -162,7 +162,11 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg,
         time(time_passes, "ast indexing",
              bind syntax::ast_map::map_crate(sess.diagnostic(), *crate));
     time(time_passes, "external crate/lib resolution",
-         bind creader::read_crates(sess, *crate));
+         bind creader::read_crates(
+             sess.diagnostic(), *crate, sess.cstore,
+             sess.filesearch,
+             session::sess_os_to_meta_os(sess.targ_cfg.os),
+             sess.opts.static));
     let {def_map, exp_map, impl_map} =
         time(time_passes, "resolution",
              bind resolve::resolve_crate(sess, ast_map, crate));
diff --git a/src/rustc/metadata.rs b/src/rustc/metadata.rs
index 87a01d81829..272c3b0e561 100644
--- a/src/rustc/metadata.rs
+++ b/src/rustc/metadata.rs
@@ -14,8 +14,6 @@ mod back {
 }
 
 mod driver {
-    import session = driver_::session;
-    export session;
 }
 
 mod util {
diff --git a/src/rustc/metadata/creader.rs b/src/rustc/metadata/creader.rs
index 662471fd02d..0fadf1b1f68 100644
--- a/src/rustc/metadata/creader.rs
+++ b/src/rustc/metadata/creader.rs
@@ -4,22 +4,28 @@ Validates all used crates and native libraries and loads their metadata
 
 "];
 
-import driver::session;
-import session::session;
+import syntax::diagnostic::span_handler;
 import syntax::{ast, ast_util};
 import syntax::attr;
 import syntax::visit;
 import syntax::codemap::span;
 import std::map::{hashmap, int_hash};
 import syntax::print::pprust;
+import util::filesearch::filesearch;
 import common::*;
 
 export read_crates;
 
 // Traverses an AST, reading all the information about use'd crates and native
 // libraries necessary for later resolving, typechecking, linking, etc.
-fn read_crates(sess: session::session, crate: ast::crate) {
-    let e = @{sess: sess,
+fn read_crates(diag: span_handler, crate: ast::crate,
+               cstore: cstore::cstore, filesearch: filesearch,
+               os: loader::os, static: bool) {
+    let e = @{diag: diag,
+              filesearch: filesearch,
+              cstore: cstore,
+              os: os,
+              static: static,
               mut crate_cache: [],
               mut next_crate_num: 1};
     let v =
@@ -29,7 +35,7 @@ fn read_crates(sess: session::session, crate: ast::crate) {
                                       with *visit::default_simple_visitor()});
     visit::visit_crate(crate, (), v);
     dump_crates(e.crate_cache);
-    warn_if_multiple_versions(sess, copy e.crate_cache);
+    warn_if_multiple_versions(diag, copy e.crate_cache);
 }
 
 type cache_entry = {
@@ -54,7 +60,7 @@ fn dump_crates(crate_cache: [cache_entry]) {
     }
 }
 
-fn warn_if_multiple_versions(sess: session::session,
+fn warn_if_multiple_versions(diag: span_handler,
                              crate_cache: [cache_entry]) {
     import either::*;
 
@@ -73,21 +79,26 @@ fn warn_if_multiple_versions(sess: session::session,
         assert matches.is_not_empty();
 
         if matches.len() != 1u {
-            sess.warn(#fmt("using multiple versions of crate `%s`", name));
+            diag.handler().warn(
+                #fmt("using multiple versions of crate `%s`", name));
             for matches.each {|match|
-                sess.span_note(match.span, "used here");
+                diag.span_note(match.span, "used here");
                 let attrs = [
                     attr::mk_attr(attr::mk_list_item("link", *match.metas))
                 ];
-                loader::note_linkage_attrs(sess, attrs);
+                loader::note_linkage_attrs(diag, attrs);
             }
         }
 
-        warn_if_multiple_versions(sess, non_matches);
+        warn_if_multiple_versions(diag, non_matches);
     }
 }
 
-type env = @{sess: session::session,
+type env = @{diag: span_handler,
+             filesearch: filesearch,
+             cstore: cstore::cstore,
+             os: loader::os,
+             static: bool,
              mut crate_cache: [cache_entry],
              mut next_crate_num: ast::crate_num};
 
@@ -96,7 +107,7 @@ fn visit_view_item(e: env, i: @ast::view_item) {
       ast::view_item_use(ident, meta_items, id) {
         #debug("resolving use stmt. ident: %?, meta: %?", ident, meta_items);
         let cnum = resolve_crate(e, ident, meta_items, "", i.span);
-        cstore::add_use_stmt_cnum(e.sess.cstore, id, cnum);
+        cstore::add_use_stmt_cnum(e.cstore, id, cnum);
       }
       _ { }
     }
@@ -110,15 +121,15 @@ fn visit_item(e: env, i: @ast::item) {
             if abi != ast::native_abi_cdecl &&
                abi != ast::native_abi_stdcall { ret; }
           }
-          either::left(msg) { e.sess.span_fatal(i.span, msg); }
+          either::left(msg) { e.diag.span_fatal(i.span, msg); }
         }
 
-        let cstore = e.sess.cstore;
+        let cstore = e.cstore;
         let native_name =
             alt attr::first_attr_value_str_by_name(i.attrs, "link_name") {
               some(nn) {
                 if nn == "" {
-                    e.sess.span_fatal(
+                    e.diag.span_fatal(
                         i.span,
                         "empty #[link_name] not allowed; use #[nolink].");
                 }
@@ -132,7 +143,7 @@ fn visit_item(e: env, i: @ast::item) {
         }
         let link_args = attr::find_attrs_by_name(i.attrs, "link_args");
         if vec::len(link_args) > 0u && already_added {
-            e.sess.span_fatal(i.span, "library '" + native_name +
+            e.diag.span_fatal(i.span, "library '" + native_name +
                               "' already added: can't specify link_args.");
         }
         for link_args.each {|a|
@@ -180,13 +191,14 @@ fn resolve_crate(e: env, ident: ast::ident, metas: [@ast::meta_item],
     alt existing_match(e, metas, hash) {
       none {
         let load_ctxt: loader::ctxt = {
-            sess: e.sess,
+            diag: e.diag,
+            filesearch: e.filesearch,
             span: span,
             ident: ident,
             metas: metas,
             hash: hash,
-            os: session::sess_os_to_meta_os(e.sess.targ_cfg.os),
-            static: e.sess.opts.static
+            os: e.os,
+            static: e.static
         };
         let cinfo = loader::load_library_crate(load_ctxt);
 
@@ -214,7 +226,7 @@ fn resolve_crate(e: env, ident: ast::ident, metas: [@ast::meta_item],
         let cmeta = @{name: cname, data: cdata,
                       cnum_map: cnum_map, cnum: cnum};
 
-        let cstore = e.sess.cstore;
+        let cstore = e.cstore;
         cstore::set_crate_data(cstore, cnum, cmeta);
         cstore::add_used_crate_file(cstore, cfilename);
         ret cnum;
diff --git a/src/rustc/metadata/csearch.rs b/src/rustc/metadata/csearch.rs
index 4365e41e6eb..3994d9fa722 100644
--- a/src/rustc/metadata/csearch.rs
+++ b/src/rustc/metadata/csearch.rs
@@ -6,8 +6,8 @@ import syntax::ast_util;
 import syntax::ast_map;
 import middle::ty;
 import option::{some, none};
-import driver::session;
-import driver::session::expect;
+import syntax::diagnostic::span_handler;
+import syntax::diagnostic::expect;
 import common::*;
 import std::map::hashmap;
 
@@ -82,7 +82,7 @@ fn resolve_path(cstore: cstore::cstore, cnum: ast::crate_num,
 }
 
 fn get_item_path(tcx: ty::ctxt, def: ast::def_id) -> ast_map::path {
-    let cstore = tcx.sess.cstore;
+    let cstore = tcx.cstore;
     let cdata = cstore::get_crate_data(cstore, def.crate);
     let path = decoder::get_item_path(cdata, def.node);
 
@@ -103,14 +103,14 @@ enum found_ast {
 fn maybe_get_item_ast(tcx: ty::ctxt, def: ast::def_id,
                       decode_inlined_item: decoder::decode_inlined_item)
     -> found_ast {
-    let cstore = tcx.sess.cstore;
+    let cstore = tcx.cstore;
     let cdata = cstore::get_crate_data(cstore, def.crate);
     decoder::maybe_get_item_ast(cdata, tcx, def.node,
                                 decode_inlined_item)
 }
 
 fn get_enum_variants(tcx: ty::ctxt, def: ast::def_id) -> [ty::variant_info] {
-    let cstore = tcx.sess.cstore;
+    let cstore = tcx.cstore;
     let cdata = cstore::get_crate_data(cstore, def.crate);
     ret decoder::get_enum_variants(cdata, def.node, tcx)
 }
@@ -125,35 +125,35 @@ fn get_impls_for_mod(cstore: cstore::cstore, def: ast::def_id,
 }
 
 fn get_iface_methods(tcx: ty::ctxt, def: ast::def_id) -> @[ty::method] {
-    let cstore = tcx.sess.cstore;
+    let cstore = tcx.cstore;
     let cdata = cstore::get_crate_data(cstore, def.crate);
     decoder::get_iface_methods(cdata, def.node, tcx)
 }
 
 fn get_class_fields(tcx: ty::ctxt, def: ast::def_id) -> [ty::field_ty] {
-    let cstore = tcx.sess.cstore;
+    let cstore = tcx.cstore;
     let cdata = cstore::get_crate_data(cstore, def.crate);
     decoder::get_class_fields(cdata, def.node)
 }
 
 fn get_type(tcx: ty::ctxt, def: ast::def_id) -> ty::ty_param_bounds_and_ty {
-    let cstore = tcx.sess.cstore;
+    let cstore = tcx.cstore;
     let cdata = cstore::get_crate_data(cstore, def.crate);
     decoder::get_type(cdata, def.node, tcx)
 }
 
 fn get_field_type(tcx: ty::ctxt, class_id: ast::def_id,
                   def: ast::def_id) -> ty::ty_param_bounds_and_ty {
-    let cstore = tcx.sess.cstore;
+    let cstore = tcx.cstore;
     let cdata = cstore::get_crate_data(cstore, class_id.crate);
     let all_items = ebml::get_doc(ebml::doc(cdata.data), tag_items);
     #debug("Looking up %?", class_id);
-    let class_doc = expect(tcx.sess,
+    let class_doc = expect(tcx.diag,
                            decoder::maybe_find_item(class_id.node, all_items),
                            {|| #fmt("get_field_type: class ID %? not found",
                      class_id)});
     #debug("looking up %? : %?", def, class_doc);
-    let the_field = expect(tcx.sess,
+    let the_field = expect(tcx.diag,
         decoder::maybe_find_item(def.node, class_doc),
         {|| #fmt("get_field_type: in class %?, field ID %? not found",
                  class_id, def)});
@@ -165,7 +165,7 @@ fn get_field_type(tcx: ty::ctxt, class_id: ast::def_id,
 // Given a def_id for an impl or class, return the iface it implements,
 // or none if it's not for an impl or for a class that implements ifaces
 fn get_impl_iface(tcx: ty::ctxt, def: ast::def_id) -> option<ty::t> {
-    let cstore = tcx.sess.cstore;
+    let cstore = tcx.cstore;
     let cdata = cstore::get_crate_data(cstore, def.crate);
     decoder::get_impl_iface(cdata, def.node, tcx)
 }
diff --git a/src/rustc/metadata/loader.rs b/src/rustc/metadata/loader.rs
index 85b0db6da89..da991ec0c2a 100644
--- a/src/rustc/metadata/loader.rs
+++ b/src/rustc/metadata/loader.rs
@@ -4,13 +4,13 @@ Finds crate binaries and loads their metadata
 
 "];
 
-import driver::session;
-import session::session;
+import syntax::diagnostic::span_handler;
 import syntax::{ast, attr};
 import syntax::print::pprust;
 import syntax::codemap::span;
 import lib::llvm::{False, llvm, mk_object_file, mk_section_iter};
-import util::{filesearch};
+import util::filesearch;
+import filesearch::filesearch;
 import io::writer_util;
 
 export os;
@@ -30,7 +30,8 @@ enum os {
 }
 
 type ctxt = {
-    sess: session,
+    diag: span_handler,
+    filesearch: filesearch,
     span: span,
     ident: ast::ident,
     metas: [@ast::meta_item],
@@ -43,15 +44,15 @@ fn load_library_crate(cx: ctxt) -> {ident: str, data: @[u8]} {
     alt find_library_crate(cx) {
       some(t) { ret t; }
       none {
-        cx.sess.span_fatal(
+        cx.diag.span_fatal(
             cx.span, #fmt["can't find crate for '%s'", cx.ident]);
       }
     }
 }
 
 fn find_library_crate(cx: ctxt) -> option<{ident: str, data: @[u8]}> {
-    attr::require_unique_names(cx.sess.diagnostic(), cx.metas);
-    find_library_crate_aux(cx, libname(cx), cx.sess.filesearch)
+    attr::require_unique_names(cx.diag, cx.metas);
+    find_library_crate_aux(cx, libname(cx), cx.filesearch)
 }
 
 fn libname(cx: ctxt) -> {prefix: str, suffix: str} {
@@ -106,15 +107,15 @@ fn find_library_crate_aux(cx: ctxt,
     } else if matches.len() == 1u {
         some(matches[0])
     } else {
-        cx.sess.span_err(
+        cx.diag.span_err(
             cx.span, #fmt("multiple matching crates for `%s`", crate_name));
-        cx.sess.note("candidates:");
+        cx.diag.handler().note("candidates:");
         for matches.each {|match|
-            cx.sess.note(#fmt("path: %s", match.ident));
+            cx.diag.handler().note(#fmt("path: %s", match.ident));
             let attrs = decoder::get_crate_attributes(match.data);
-            note_linkage_attrs(cx.sess, attrs);
+            note_linkage_attrs(cx.diag, attrs);
         }
-        cx.sess.abort_if_errors();
+        cx.diag.handler().abort_if_errors();
         none
     }
 }
@@ -134,9 +135,9 @@ fn crate_name_from_metas(metas: [@ast::meta_item]) -> str {
     }
 }
 
-fn note_linkage_attrs(sess: session::session, attrs: [ast::attribute]) {
+fn note_linkage_attrs(diag: span_handler, attrs: [ast::attribute]) {
     for attr::find_linkage_attrs(attrs).each {|attr|
-        sess.note(#fmt("meta: %s", pprust::attr_to_str(attr)));
+        diag.handler().note(#fmt("meta: %s", pprust::attr_to_str(attr)));
     }
 }