From f3c206c9f495180e5926a3e5d47f4e0f152d0cc6 Mon Sep 17 00:00:00 2001 From: Haitao Li Date: Sun, 11 Dec 2011 23:23:38 +0800 Subject: [PATCH] rustc: Encode crate hash into metadata --- src/comp/metadata/common.rs | 2 ++ src/comp/metadata/decoder.rs | 14 +++++++++++--- src/comp/metadata/encoder.rs | 8 ++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/comp/metadata/common.rs b/src/comp/metadata/common.rs index fdb2d72da6f..8002fddf3c9 100644 --- a/src/comp/metadata/common.rs +++ b/src/comp/metadata/common.rs @@ -64,6 +64,8 @@ const tag_crate_dep: uint = 0x26u; const tag_items_data_item_inlineness: uint = 0x27u; +const tag_crate_hash: uint = 0x28u; + // djb's cdb hashes. fn hash_node_id(&&node_id: int) -> uint { ret 177573u ^ (node_id as uint); } diff --git a/src/comp/metadata/decoder.rs b/src/comp/metadata/decoder.rs index b5c32efe66f..541b7c32635 100644 --- a/src/comp/metadata/decoder.rs +++ b/src/comp/metadata/decoder.rs @@ -19,6 +19,7 @@ export get_crate_attributes; export list_crate_metadata; export crate_dep; export get_crate_deps; +export get_crate_hash; export external_resolver; // A function that takes a def_id relative to the crate being searched and @@ -347,8 +348,8 @@ fn list_meta_items(meta_items: ebml::doc, out: io::writer) { } } -fn list_crate_attributes(md: ebml::doc, out: io::writer) { - out.write_str("=Crate Attributes=\n"); +fn list_crate_attributes(md: ebml::doc, hash: str, out: io::writer) { + out.write_str(#fmt("=Crate Attributes (%s)=\n", hash)); for attr: ast::attribute in get_attributes(md) { out.write_str(#fmt["%s\n", pprust::attribute_to_str(attr)]); @@ -386,6 +387,12 @@ fn list_crate_deps(data: @[u8], out: io::writer) { out.write_str("\n"); } +fn get_crate_hash(data: @[u8]) -> str { + let cratedoc = ebml::new_doc(data); + let hashdoc = ebml::get_doc(cratedoc, tag_crate_hash); + ret str::unsafe_from_bytes(ebml::doc_data(hashdoc)); +} + fn list_crate_items(bytes: @[u8], md: ebml::doc, out: io::writer) { out.write_str("=Items=\n"); let paths = ebml::get_doc(md, tag_paths); @@ -407,8 +414,9 @@ fn list_crate_items(bytes: @[u8], md: ebml::doc, out: io::writer) { } fn list_crate_metadata(bytes: @[u8], out: io::writer) { + let hash = get_crate_hash(bytes); let md = ebml::new_doc(bytes); - list_crate_attributes(md, out); + list_crate_attributes(md, hash, out); list_crate_deps(bytes, out); list_crate_items(bytes, md, out); } diff --git a/src/comp/metadata/encoder.rs b/src/comp/metadata/encoder.rs index 8eb0ea64e49..646c016d97d 100644 --- a/src/comp/metadata/encoder.rs +++ b/src/comp/metadata/encoder.rs @@ -576,6 +576,12 @@ fn encode_crate_deps(ebml_w: ebml::writer, cstore: cstore::cstore) { ebml::end_tag(ebml_w); } +fn encode_hash(ebml_w: ebml::writer, hash: str) { + ebml::start_tag(ebml_w, tag_crate_hash); + ebml_w.writer.write(str::bytes(hash)); + ebml::end_tag(ebml_w); +} + fn encode_metadata(cx: @crate_ctxt, crate: @crate) -> str { let abbrevs = map::mk_hashmap(ty::hash_ty, ty::eq_ty); @@ -585,6 +591,8 @@ fn encode_metadata(cx: @crate_ctxt, crate: @crate) -> str { let buf_w = string_w.get_writer().get_buf_writer(); let ebml_w = ebml::create_writer(buf_w); + encode_hash(ebml_w, cx.link_meta.extras_hash); + let crate_attrs = synthesize_crate_attrs(ecx, crate); encode_attributes(ebml_w, crate_attrs);