Skip to content

Commit

Permalink
unify the 2 impl indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ariel Ben-Yehuda committed Oct 2, 2015
1 parent 55d35f1 commit eae41d3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 76 deletions.
20 changes: 7 additions & 13 deletions src/librustc/metadata/common.rs
Expand Up @@ -133,8 +133,7 @@ enum_from_u32! {
tag_table_method_map = 0x5f,
// GAP 0x60
tag_table_adjustments = 0x61,
// GAP 0x62, 0x63
// GAP 0x64, 0x65
// GAP 0x62, 0x63, 0x64, 0x65
tag_table_upvar_capture_map = 0x66,
// GAP 0x67, 0x68
tag_table_const_qualif = 0x69,
Expand Down Expand Up @@ -163,22 +162,17 @@ pub const tag_lang_items_missing: usize = 0x76;

pub const tag_item_unnamed_field: usize = 0x77;
pub const tag_items_data_item_visibility: usize = 0x78;

// GAP 0x79, 0x7a

pub const tag_items_data_item_inherent_impl: usize = 0x79;
// GAP 0x7a
pub const tag_mod_child: usize = 0x7b;
pub const tag_misc_info: usize = 0x108; // top-level only
pub const tag_misc_info_crate_items: usize = 0x7c;

// GAP 0x7d
// GAP 0x7e

pub const tag_impls: usize = 0x109; // top-level only
pub const tag_impls_impl: usize = 0x7f;
pub const tag_impls_impl_trait_def_id: usize = 0x8d;
pub const tag_impls_trait: usize = 0x7d;
pub const tag_impls_trait_impl: usize = 0x7e;

pub const tag_items_data_item_inherent_impl: usize = 0x80;
pub const tag_items_data_item_extension_impl: usize = 0x81;
// GAP 0x7f, 0x80, 0x81

pub const tag_native_libraries: usize = 0x10a; // top-level only
pub const tag_native_libraries_lib: usize = 0x82;
Expand Down Expand Up @@ -208,7 +202,7 @@ pub const tag_struct_field: usize = 0x8a;

pub const tag_items_data_item_struct_ctor: usize = 0x8b;
pub const tag_attribute_is_sugared_doc: usize = 0x8c;

// GAP 0x8d
pub const tag_items_data_region: usize = 0x8e;

pub const tag_region_param_def: usize = 0x8f;
Expand Down
19 changes: 7 additions & 12 deletions src/librustc/metadata/decoder.rs
Expand Up @@ -1284,24 +1284,19 @@ pub fn each_implementation_for_trait<F>(cdata: Cmd,
mut callback: F) where
F: FnMut(DefId),
{
if cdata.cnum == def_id.krate {
let item_doc = cdata.lookup_item(def_id.index);
for impl_doc in reader::tagged_docs(item_doc, tag_items_data_item_extension_impl) {
callback(item_def_id(impl_doc, cdata));
}
return;
}

// Do a reverse lookup beforehand to avoid touching the crate_num
// hash map in the loop below.
if let Some(crate_local_did) = reverse_translate_def_id(cdata, def_id) {
let def_id_u64 = def_to_u64(crate_local_did);

let impls_doc = reader::get_doc(rbml::Doc::new(cdata.data()), tag_impls);
for impl_doc in reader::tagged_docs(impls_doc, tag_impls_impl) {
let impl_trait = reader::get_doc(impl_doc, tag_impls_impl_trait_def_id);
if reader::doc_as_u64(impl_trait) == def_id_u64 {
callback(item_def_id(impl_doc, cdata));
for trait_doc in reader::tagged_docs(impls_doc, tag_impls_trait) {
let trait_def_id = reader::get_doc(trait_doc, tag_def_id);
if reader::doc_as_u64(trait_def_id) != def_id_u64 {
continue;
}
for impl_doc in reader::tagged_docs(trait_doc, tag_impls_trait_impl) {
callback(translated_def_id(cdata, impl_doc));
}
}
}
Expand Down
74 changes: 24 additions & 50 deletions src/librustc/metadata/encoder.rs
Expand Up @@ -865,20 +865,6 @@ fn encode_inherent_implementations(ecx: &EncodeContext,
}
}

// Encodes the implementations of a trait defined in this crate.
fn encode_extension_implementations(ecx: &EncodeContext,
rbml_w: &mut Encoder,
trait_def_id: DefId) {
assert!(trait_def_id.is_local());
let def = ecx.tcx.lookup_trait_def(trait_def_id);

def.for_each_impl(ecx.tcx, |impl_def_id| {
rbml_w.start_tag(tag_items_data_item_extension_impl);
encode_def_id(rbml_w, impl_def_id);
rbml_w.end_tag();
});
}

fn encode_stability(rbml_w: &mut Encoder, stab_opt: Option<&attr::Stability>) {
stab_opt.map(|stab| {
rbml_w.start_tag(tag_items_data_item_stability);
Expand Down Expand Up @@ -1256,9 +1242,6 @@ fn encode_info_for_item<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
}
encode_path(rbml_w, path.clone());

// Encode the implementations of this trait.
encode_extension_implementations(ecx, rbml_w, def_id);

// Encode inherent implementations for this trait.
encode_inherent_implementations(ecx, rbml_w, def_id);

Expand Down Expand Up @@ -1763,53 +1746,44 @@ fn encode_struct_field_attrs(ecx: &EncodeContext,



struct ImplVisitor<'a, 'b:'a, 'c:'a, 'tcx:'b> {
ecx: &'a EncodeContext<'b, 'tcx>,
rbml_w: &'a mut Encoder<'c>,
struct ImplVisitor<'a, 'tcx:'a> {
tcx: &'a ty::ctxt<'tcx>,
impls: FnvHashMap<DefId, Vec<DefId>>
}

impl<'a, 'b, 'c, 'tcx, 'v> Visitor<'v> for ImplVisitor<'a, 'b, 'c, 'tcx> {
impl<'a, 'tcx, 'v> Visitor<'v> for ImplVisitor<'a, 'tcx> {
fn visit_item(&mut self, item: &hir::Item) {
if let hir::ItemImpl(_, _, _, Some(ref trait_ref), _, _) = item.node {
let def_id = self.ecx.tcx.def_map.borrow().get(&trait_ref.ref_id).unwrap().def_id();

// Load eagerly if this is an implementation of the Drop trait
// or if the trait is not defined in this crate.
if Some(def_id) == self.ecx.tcx.lang_items.drop_trait() ||
def_id.krate != LOCAL_CRATE {
self.rbml_w.start_tag(tag_impls_impl);
encode_def_id(self.rbml_w, self.ecx.tcx.map.local_def_id(item.id));
self.rbml_w.wr_tagged_u64(tag_impls_impl_trait_def_id, def_to_u64(def_id));
self.rbml_w.end_tag();
if let hir::ItemImpl(..) = item.node {
let impl_id = self.tcx.map.local_def_id(item.id);
if let Some(trait_ref) = self.tcx.impl_trait_ref(impl_id) {
self.impls.entry(trait_ref.def_id)
.or_insert(vec![])
.push(impl_id);
}
}
visit::walk_item(self, item);
}
}

/// Encodes implementations that are eagerly loaded.
///
/// None of this is necessary in theory; we can load all implementations
/// lazily. However, in two cases the optimizations to lazily load
/// implementations are not yet implemented. These two cases, which require us
/// to load implementations eagerly, are:
///
/// * Destructors (implementations of the Drop trait).
///
/// * Implementations of traits not defined in this crate.
/// Encodes an index, mapping each trait to its (local) implementations.
fn encode_impls<'a>(ecx: &'a EncodeContext,
krate: &hir::Crate,
rbml_w: &'a mut Encoder) {
rbml_w.start_tag(tag_impls);
let mut visitor = ImplVisitor {
tcx: ecx.tcx,
impls: FnvHashMap()
};
visit::walk_crate(&mut visitor, krate);

{
let mut visitor = ImplVisitor {
ecx: ecx,
rbml_w: rbml_w,
};
visit::walk_crate(&mut visitor, krate);
rbml_w.start_tag(tag_impls);
for (trait_, trait_impls) in visitor.impls {
rbml_w.start_tag(tag_impls_trait);
encode_def_id(rbml_w, trait_);
for impl_ in trait_impls {
rbml_w.wr_tagged_u64(tag_impls_trait_impl, def_to_u64(impl_));
}
rbml_w.end_tag();
}

rbml_w.end_tag();
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/astencode.rs
Expand Up @@ -1319,7 +1319,7 @@ fn copy_item_types(dcx: &DecodeContext, ii: &InlinedItem, orig_did: DefId) {
if let Some(ctor_id) = def.ctor_id {
let ctor_did = dcx.tcx.lookup_adt_def(orig_did)
.struct_variant().ctor_id;
println!("copying ctor {:?}", ctor_did);
debug!("copying ctor {:?}", ctor_did);
copy_item_type(dcx, ctor_id, ctor_did);
}
}
Expand Down

0 comments on commit eae41d3

Please sign in to comment.