]> git.lizzy.rs Git - rust.git/commitdiff
rustc: Implement re-export of renamed modules
authorHaitao Li <lihaitao@gmail.com>
Mon, 19 Dec 2011 19:30:23 +0000 (03:30 +0800)
committerHaitao Li <lihaitao@gmail.com>
Tue, 20 Dec 2011 07:38:40 +0000 (15:38 +0800)
Issue #1115

src/comp/metadata/csearch.rs
src/comp/metadata/decoder.rs
src/comp/metadata/encoder.rs
src/comp/middle/resolve.rs

index 81bd90a0a4199c8e68b2811b2fed429485f93947..0c50845de8b0461b2831436bf4b4ace073fecc48 100644 (file)
@@ -12,6 +12,7 @@
 export get_impls_for_mod;
 export get_impl_methods;
 export get_type;
+export get_item_name;
 
 fn get_symbol(cstore: cstore::cstore, def: ast::def_id) -> str {
     let cdata = cstore::get_crate_data(cstore, def.crate).data;
@@ -93,6 +94,11 @@ fn get_type(tcx: ty::ctxt, def: ast::def_id) -> ty::ty_param_kinds_and_ty {
     decoder::get_type(cdata, def, tcx, resolver)
 }
 
+fn get_item_name(cstore: cstore::cstore, cnum: int, id: int) -> ast::ident {
+    let cdata = cstore::get_crate_data(cstore, cnum).data;
+    ret decoder::lookup_item_name(cdata, id);
+}
+
 // Translates a def_id from an external crate to a def_id for the current
 // compilation environment. We use this when trying to load types from
 // external crates - if those types further refer to types in other crates
index b1cbb7a752b7819b4f7f0757efc2f218e9285fac..6c3066f4752b382f5adaae4a733c25fddc3a901f 100644 (file)
@@ -183,7 +183,6 @@ fn lookup_item_name(data: @[u8], id: ast::node_id) -> ast::ident {
     item_name(lookup_item(id, data))
 }
 
-// FIXME doesn't yet handle renamed re-exported externals
 fn lookup_def(cnum: ast::crate_num, data: @[u8], did_: ast::def_id) ->
    ast::def {
     let item = lookup_item(did_.node, data);
index 4bfcc7fb95d9b7ae605d38b590788b46be15153b..26f55caec684d8a7344c2009b3ab2be3e22e5399 100644 (file)
@@ -250,10 +250,11 @@ fn encode_tag_variant_info(ecx: @encode_ctxt, ebml_w: ebml::writer,
 }
 
 fn encode_info_for_mod(ebml_w: ebml::writer, md: _mod,
-                       id: node_id) {
+                       id: node_id, name: ident) {
     ebml::start_tag(ebml_w, tag_items_data_item);
     encode_def_id(ebml_w, local_def(id));
     encode_family(ebml_w, 'm' as u8);
+    encode_name(ebml_w, name);
     for i in md.items {
         alt i.node {
           item_impl(_, _, _) {
@@ -300,12 +301,13 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::writer, item: @item,
         ebml::end_tag(ebml_w);
       }
       item_mod(m) {
-        encode_info_for_mod(ebml_w, m, item.id);
+        encode_info_for_mod(ebml_w, m, item.id, item.ident);
       }
       item_native_mod(_) {
         ebml::start_tag(ebml_w, tag_items_data_item);
         encode_def_id(ebml_w, local_def(item.id));
         encode_family(ebml_w, 'n' as u8);
+        encode_name(ebml_w, item.ident);
         ebml::end_tag(ebml_w);
       }
       item_ty(_, tps) {
@@ -434,7 +436,7 @@ fn encode_info_for_items(ecx: @encode_ctxt, ebml_w: ebml::writer,
     let index: [entry<int>] = [];
     ebml::start_tag(ebml_w, tag_items_data);
     index += [{val: crate_node_id, pos: ebml_w.writer.tell()}];
-    encode_info_for_mod(ebml_w, crate_mod, crate_node_id);
+    encode_info_for_mod(ebml_w, crate_mod, crate_node_id, "");
     ecx.ccx.ast_map.items {|key, val|
         alt val {
           middle::ast_map::node_item(i) {
index 60ea72ea9a2afec7f49505a54edae64d34c5f9b3..9620931995b1de2baacf06997722619205343dc0 100644 (file)
@@ -1353,7 +1353,25 @@ fn ns_for_def(d: def) -> namespace {
 fn lookup_external(e: env, cnum: int, ids: [ident], ns: namespace) ->
    option::t<def> {
     for d: def in csearch::lookup_defs(e.sess.get_cstore(), cnum, ids) {
-        e.ext_map.insert(def_id_of_def(d), ids);
+        let did = def_id_of_def(d);
+        alt d {
+          def_mod(_) | def_native_mod(_) {
+            // The [native] module name might have renamed when importing,
+            // find the original name for further lookup of names inside the
+            // [native] module
+            if did.crate != ast::local_crate {
+                let cname = cstore::get_crate_data(e.cstore, did.crate).name;
+                let name =
+                    csearch::get_item_name(e.cstore, did.crate, did.node);
+                e.ext_map.insert(did, vec::init(ids) + [name]);
+            } else {
+                e.ext_map.insert(did, ids);
+            }
+          }
+          _ {
+            e.ext_map.insert(did, ids);
+          }
+        }
         if ns == ns_for_def(d) { ret some(d); }
     }
     ret none::<def>;