CrateStore: Allow for custom def_id_to_string mappings in encode_type().

This commit is contained in:
Michael Woerister 2016-02-10 10:04:45 -05:00 committed by Niko Matsakis
parent 64a13a4660
commit 6fdeecf62f
5 changed files with 40 additions and 20 deletions

View file

@ -236,7 +236,11 @@ pub trait CrateStore<'tcx> : Any {
// utility functions
fn metadata_filename(&self) -> &str;
fn metadata_section_name(&self, target: &Target) -> &str;
fn encode_type(&self, tcx: &TyCtxt<'tcx>, ty: Ty<'tcx>) -> Vec<u8>;
fn encode_type(&self,
tcx: &TyCtxt<'tcx>,
ty: Ty<'tcx>,
def_id_to_string: fn(&TyCtxt<'tcx>, DefId) -> String)
-> 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>;
@ -419,8 +423,13 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
// utility functions
fn metadata_filename(&self) -> &str { unimplemented!() }
fn metadata_section_name(&self, target: &Target) -> &str { unimplemented!() }
fn encode_type(&self, tcx: &TyCtxt<'tcx>, ty: Ty<'tcx>) -> Vec<u8>
{ unimplemented!() }
fn encode_type(&self,
tcx: &TyCtxt<'tcx>,
ty: Ty<'tcx>,
def_id_to_string: fn(&TyCtxt<'tcx>, DefId) -> String)
-> Vec<u8> {
unimplemented!()
}
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option<PathBuf>)>
{ vec![] }
fn used_crate_source(&self, cnum: ast::CrateNum) -> CrateSource { unimplemented!() }

View file

@ -478,9 +478,13 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
{
loader::meta_section_name(target)
}
fn encode_type(&self, tcx: &TyCtxt<'tcx>, ty: Ty<'tcx>) -> Vec<u8>
fn encode_type(&self,
tcx: &TyCtxt<'tcx>,
ty: Ty<'tcx>,
def_id_to_string: fn(&TyCtxt<'tcx>, DefId) -> String)
-> Vec<u8>
{
encoder::encoded_ty(tcx, ty)
encoder::encoded_ty(tcx, ty, def_id_to_string)
}
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option<PathBuf>)>

View file

@ -143,7 +143,7 @@ pub fn def_to_u64(did: DefId) -> u64 {
(did.krate as u64) << 32 | (did.index.as_usize() as u64)
}
pub fn def_to_string(did: DefId) -> String {
pub fn def_to_string(_tcx: &TyCtxt, did: DefId) -> String {
format!("{}:{}", did.krate, did.index.as_usize())
}
@ -2078,11 +2078,14 @@ fn encode_metadata_inner(rbml_w: &mut Encoder,
}
// Get the encoded string for a type
pub fn encoded_ty<'tcx>(tcx: &TyCtxt<'tcx>, t: Ty<'tcx>) -> Vec<u8> {
pub fn encoded_ty<'tcx>(tcx: &TyCtxt<'tcx>,
t: Ty<'tcx>,
def_id_to_string: fn(&TyCtxt<'tcx>, DefId) -> String)
-> Vec<u8> {
let mut wr = Cursor::new(Vec::new());
tyencode::enc_ty(&mut wr, &tyencode::ctxt {
diag: tcx.sess.diagnostic(),
ds: def_to_string,
ds: def_id_to_string,
tcx: tcx,
abbrevs: &RefCell::new(FnvHashMap())
}, t);

View file

@ -37,7 +37,7 @@ use encoder;
pub struct ctxt<'a, 'tcx: 'a> {
pub diag: &'a Handler,
// Def -> str Callback:
pub ds: fn(DefId) -> String,
pub ds: fn(&TyCtxt<'tcx>, DefId) -> String,
// The type context.
pub tcx: &'a TyCtxt<'tcx>,
pub abbrevs: &'a abbrev_map<'tcx>
@ -99,7 +99,7 @@ pub fn enc_ty<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx
};
}
ty::TyEnum(def, substs) => {
write!(w, "t[{}|", (cx.ds)(def.did));
write!(w, "t[{}|", (cx.ds)(cx.tcx, def.did));
enc_substs(w, cx, substs);
write!(w, "]");
}
@ -137,7 +137,7 @@ pub fn enc_ty<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx
}
ty::TyFnDef(def_id, substs, f) => {
write!(w, "F");
write!(w, "{}|", (cx.ds)(def_id));
write!(w, "{}|", (cx.ds)(cx.tcx, def_id));
enc_substs(w, cx, substs);
enc_bare_fn_ty(w, cx, f);
}
@ -152,12 +152,12 @@ pub fn enc_ty<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx
write!(w, "p[{}|{}|{}]", idx, space.to_uint(), name);
}
ty::TyStruct(def, substs) => {
write!(w, "a[{}|", (cx.ds)(def.did));
write!(w, "a[{}|", (cx.ds)(cx.tcx, def.did));
enc_substs(w, cx, substs);
write!(w, "]");
}
ty::TyClosure(def, ref substs) => {
write!(w, "k[{}|", (cx.ds)(def));
write!(w, "k[{}|", (cx.ds)(cx.tcx, def));
enc_substs(w, cx, &substs.func_substs);
for ty in &substs.upvar_tys {
enc_ty(w, cx, ty);
@ -310,7 +310,7 @@ fn enc_bound_region(w: &mut Cursor<Vec<u8>>, cx: &ctxt, br: ty::BoundRegion) {
}
ty::BrNamed(d, name) => {
write!(w, "[{}|{}]",
(cx.ds)(d),
(cx.ds)(cx.tcx, d),
name);
}
ty::BrFresh(id) => {
@ -324,7 +324,7 @@ fn enc_bound_region(w: &mut Cursor<Vec<u8>>, cx: &ctxt, br: ty::BoundRegion) {
pub fn enc_trait_ref<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
s: ty::TraitRef<'tcx>) {
write!(w, "{}|", (cx.ds)(s.def_id));
write!(w, "{}|", (cx.ds)(cx.tcx, s.def_id));
enc_substs(w, cx, s.substs);
}
@ -408,8 +408,8 @@ pub fn enc_existential_bounds<'a,'tcx>(w: &mut Cursor<Vec<u8>>,
pub fn enc_type_param_def<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
v: &ty::TypeParameterDef<'tcx>) {
write!(w, "{}:{}|{}|{}|{}|",
v.name, (cx.ds)(v.def_id),
v.space.to_uint(), v.index, (cx.ds)(v.default_def_id));
v.name, (cx.ds)(cx.tcx, v.def_id),
v.space.to_uint(), v.index, (cx.ds)(cx.tcx, v.default_def_id));
enc_opt(w, v.default, |w, t| enc_ty(w, cx, t));
enc_object_lifetime_default(w, cx, v.object_lifetime_default);
}
@ -417,7 +417,7 @@ pub fn enc_type_param_def<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>
pub fn enc_region_param_def(w: &mut Cursor<Vec<u8>>, cx: &ctxt,
v: &ty::RegionParameterDef) {
write!(w, "{}:{}|{}|{}|",
v.name, (cx.ds)(v.def_id),
v.name, (cx.ds)(cx.tcx, v.def_id),
v.space.to_uint(), v.index);
for &r in &v.bounds {
write!(w, "R");
@ -477,7 +477,7 @@ pub fn enc_predicate<'a, 'tcx>(w: &mut Cursor<Vec<u8>>,
enc_ty(w, cx, data);
}
ty::Predicate::ObjectSafe(trait_def_id) => {
write!(w, "O{}|", (cx.ds)(trait_def_id));
write!(w, "O{}|", (cx.ds)(cx.tcx, trait_def_id));
}
}
}

View file

@ -22,6 +22,7 @@ use session::search_paths::PathKind;
use session::Session;
use middle::cstore::{self, CrateStore, LinkMeta};
use middle::cstore::{LinkagePreference, NativeLibraryKind};
use middle::def_id::DefId;
use middle::dependency_format::Linkage;
use middle::ty::{Ty, TyCtxt};
use rustc::front::map::DefPath;
@ -200,6 +201,9 @@ fn truncated_hash_result(symbol_hasher: &mut Sha256) -> String {
output[.. 8].to_hex().to_string()
}
pub fn def_to_string(_tcx: &ty::ctxt, did: DefId) -> String {
format!("{}:{}", did.krate, did.index.as_usize())
}
// This calculates STH for a symbol, as defined above
fn symbol_hash<'tcx>(tcx: &TyCtxt<'tcx>,
@ -218,7 +222,7 @@ fn symbol_hash<'tcx>(tcx: &TyCtxt<'tcx>,
symbol_hasher.input_str(&meta[..]);
}
symbol_hasher.input_str("-");
symbol_hasher.input(&tcx.sess.cstore.encode_type(tcx, t));
symbol_hasher.input(&tcx.sess.cstore.encode_type(tcx, t, def_to_string));
// Prefix with 'h' so that it never blends into adjacent digits
let mut hash = String::from("h");
hash.push_str(&truncated_hash_result(symbol_hasher));