aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Walton <[email protected]>2011-04-22 14:48:25 -0700
committerPatrick Walton <[email protected]>2011-04-22 14:48:25 -0700
commit3d62c9adf35e8685f131be50afbcc711bb43cf60 (patch)
treefb670f7b0dbc4be175d04c004bb0e892edf4def4
parentrustc: Remove direct access to the cname field of types (diff)
downloadrust-3d62c9adf35e8685f131be50afbcc711bb43cf60.tar.xz
rust-3d62c9adf35e8685f131be50afbcc711bb43cf60.zip
rustc: Move the type serialization logic to an Encode module
-rw-r--r--src/comp/middle/metadata.rs200
-rw-r--r--src/comp/middle/trans.rs5
2 files changed, 108 insertions, 97 deletions
diff --git a/src/comp/middle/metadata.rs b/src/comp/middle/metadata.rs
index 5cefa83c..7f6ccc82 100644
--- a/src/comp/middle/metadata.rs
+++ b/src/comp/middle/metadata.rs
@@ -49,115 +49,120 @@ const uint tag_index_table = 0x15u;
// to/from def_ids in the string rep. Whatever format you choose should not
// contain pipe characters.
-// Callback to translate defs to strs or back.
-type def_str = fn(ast.def_id) -> str;
+mod Encode {
-fn ty_str(ty.t t, def_str ds) -> str {
- ret sty_str(ty.struct(t), ds);
-}
+ type ctxt = rec(
+ fn(ast.def_id) -> str ds // Callback to translate defs to strs.
+ );
-fn mt_str(&ty.mt mt, def_str ds) -> str {
- auto mut_str;
- alt (mt.mut) {
- case (ast.imm) { mut_str = ""; }
- case (ast.mut) { mut_str = "m"; }
- case (ast.maybe_mut) { mut_str = "?"; }
+ fn ty_str(@ctxt cx, ty.t t) -> str {
+ ret sty_str(cx, ty.struct(t));
}
- ret mut_str + ty_str(mt.ty, ds);
-}
-fn sty_str(ty.sty st, def_str ds) -> str {
- alt (st) {
- case (ty.ty_nil) {ret "n";}
- case (ty.ty_bool) {ret "b";}
- case (ty.ty_int) {ret "i";}
- case (ty.ty_uint) {ret "u";}
- case (ty.ty_float) {ret "l";}
- case (ty.ty_machine(?mach)) {
- alt (mach) {
- case (common.ty_u8) {ret "Mb";}
- case (common.ty_u16) {ret "Mw";}
- case (common.ty_u32) {ret "Ml";}
- case (common.ty_u64) {ret "Md";}
- case (common.ty_i8) {ret "MB";}
- case (common.ty_i16) {ret "MW";}
- case (common.ty_i32) {ret "ML";}
- case (common.ty_i64) {ret "MD";}
- case (common.ty_f32) {ret "Mf";}
- case (common.ty_f64) {ret "MF";}
- }
+ fn mt_str(@ctxt cx, &ty.mt mt) -> str {
+ auto mut_str;
+ alt (mt.mut) {
+ case (ast.imm) { mut_str = ""; }
+ case (ast.mut) { mut_str = "m"; }
+ case (ast.maybe_mut) { mut_str = "?"; }
}
- case (ty.ty_char) {ret "c";}
- case (ty.ty_str) {ret "s";}
- case (ty.ty_tag(?def,?tys)) { // TODO restore def_id
- auto acc = "t[" + ds(def) + "|";
- for (ty.t t in tys) {acc += ty_str(t, ds);}
- ret acc + "]";
- }
- case (ty.ty_box(?mt)) {ret "@" + mt_str(mt, ds);}
- case (ty.ty_vec(?mt)) {ret "V" + mt_str(mt, ds);}
- case (ty.ty_port(?t)) {ret "P" + ty_str(t, ds);}
- case (ty.ty_chan(?t)) {ret "C" + ty_str(t, ds);}
- case (ty.ty_tup(?mts)) {
- auto acc = "T[";
- for (ty.mt mt in mts) {acc += mt_str(mt, ds);}
- ret acc + "]";
- }
- case (ty.ty_rec(?fields)) {
- auto acc = "R[";
- for (ty.field field in fields) {
- acc += field.ident + "=";
- acc += mt_str(field.mt, ds);
+ ret mut_str + ty_str(cx, mt.ty);
+ }
+
+ fn sty_str(@ctxt cx, ty.sty st) -> str {
+ alt (st) {
+ case (ty.ty_nil) {ret "n";}
+ case (ty.ty_bool) {ret "b";}
+ case (ty.ty_int) {ret "i";}
+ case (ty.ty_uint) {ret "u";}
+ case (ty.ty_float) {ret "l";}
+ case (ty.ty_machine(?mach)) {
+ alt (mach) {
+ case (common.ty_u8) {ret "Mb";}
+ case (common.ty_u16) {ret "Mw";}
+ case (common.ty_u32) {ret "Ml";}
+ case (common.ty_u64) {ret "Md";}
+ case (common.ty_i8) {ret "MB";}
+ case (common.ty_i16) {ret "MW";}
+ case (common.ty_i32) {ret "ML";}
+ case (common.ty_i64) {ret "MD";}
+ case (common.ty_f32) {ret "Mf";}
+ case (common.ty_f64) {ret "MF";}
+ }
}
- ret acc + "]";
- }
- case (ty.ty_fn(?proto,?args,?out)) {
- ret proto_str(proto) + ty_fn_str(args, out, ds);
- }
- case (ty.ty_native_fn(?abi,?args,?out)) {
- auto abistr;
- alt (abi) {
- case (ast.native_abi_rust) {abistr = "r";}
- case (ast.native_abi_cdecl) {abistr = "c";}
- case (ast.native_abi_llvm) {abistr = "l";}
+ case (ty.ty_char) {ret "c";}
+ case (ty.ty_str) {ret "s";}
+ case (ty.ty_tag(?def,?tys)) { // TODO restore def_id
+ auto acc = "t[" + cx.ds(def) + "|";
+ for (ty.t t in tys) {acc += ty_str(cx, t);}
+ ret acc + "]";
}
- ret "N" + abistr + ty_fn_str(args, out, ds);
- }
- case (ty.ty_obj(?methods)) {
- auto acc = "O[";
- for (ty.method m in methods) {
- acc += proto_str(m.proto);
- acc += m.ident;
- acc += ty_fn_str(m.inputs, m.output, ds);
+ case (ty.ty_box(?mt)) {ret "@" + mt_str(cx, mt);}
+ case (ty.ty_vec(?mt)) {ret "V" + mt_str(cx, mt);}
+ case (ty.ty_port(?t)) {ret "P" + ty_str(cx, t);}
+ case (ty.ty_chan(?t)) {ret "C" + ty_str(cx, t);}
+ case (ty.ty_tup(?mts)) {
+ auto acc = "T[";
+ for (ty.mt mt in mts) {acc += mt_str(cx, mt);}
+ ret acc + "]";
+ }
+ case (ty.ty_rec(?fields)) {
+ auto acc = "R[";
+ for (ty.field field in fields) {
+ acc += field.ident + "=";
+ acc += mt_str(cx, field.mt);
+ }
+ ret acc + "]";
}
- ret acc + "]";
+ case (ty.ty_fn(?proto,?args,?out)) {
+ ret proto_str(proto) + ty_fn_str(cx, args, out);
+ }
+ case (ty.ty_native_fn(?abi,?args,?out)) {
+ auto abistr;
+ alt (abi) {
+ case (ast.native_abi_rust) {abistr = "r";}
+ case (ast.native_abi_cdecl) {abistr = "c";}
+ case (ast.native_abi_llvm) {abistr = "l";}
+ }
+ ret "N" + abistr + ty_fn_str(cx, args, out);
+ }
+ case (ty.ty_obj(?methods)) {
+ auto acc = "O[";
+ for (ty.method m in methods) {
+ acc += proto_str(m.proto);
+ acc += m.ident;
+ acc += ty_fn_str(cx, m.inputs, m.output);
+ }
+ ret acc + "]";
+ }
+ case (ty.ty_var(?id)) {ret "X" + common.istr(id);}
+ case (ty.ty_native) {ret "E";}
+ case (ty.ty_param(?id)) {ret "p" + common.uistr(id);}
+ case (ty.ty_type) {ret "Y";}
+
+ // These two don't appear in crate metadata, but are here because
+ // `hash_ty()` uses this function.
+ case (ty.ty_bound_param(?id)) {ret "o" + common.uistr(id);}
+ case (ty.ty_local(?def)) {ret "L" + cx.ds(def);}
}
- case (ty.ty_var(?id)) {ret "X" + common.istr(id);}
- case (ty.ty_native) {ret "E";}
- case (ty.ty_param(?id)) {ret "p" + common.uistr(id);}
- case (ty.ty_type) {ret "Y";}
-
- // These two don't appear in crate metadata, but are here because
- // `hash_ty()` uses this function.
- case (ty.ty_bound_param(?id)) {ret "o" + common.uistr(id);}
- case (ty.ty_local(?def)) {ret "L" + ds(def);}
}
-}
-fn proto_str(ast.proto proto) -> str {
- alt (proto) {
- case (ast.proto_iter) {ret "W";}
- case (ast.proto_fn) {ret "F";}
+ fn proto_str(ast.proto proto) -> str {
+ alt (proto) {
+ case (ast.proto_iter) {ret "W";}
+ case (ast.proto_fn) {ret "F";}
+ }
}
-}
-fn ty_fn_str(vec[ty.arg] args, ty.t out, def_str ds) -> str {
- auto acc = "[";
- for (ty.arg arg in args) {
- if (arg.mode == ast.alias) {acc += "&";}
- acc += ty_str(arg.ty, ds);
+ fn ty_fn_str(@ctxt cx, vec[ty.arg] args, ty.t out) -> str {
+ auto acc = "[";
+ for (ty.arg arg in args) {
+ if (arg.mode == ast.alias) {acc += "&";}
+ acc += ty_str(cx, arg.ty);
+ }
+ ret acc + "]" + ty_str(cx, out);
}
- ret acc + "]" + ty_str(out, ds);
+
}
@@ -329,8 +334,11 @@ fn encode_variant_id(&ebml.writer ebml_w, ast.def_id vid) {
fn encode_type(&ebml.writer ebml_w, ty.t typ) {
ebml.start_tag(ebml_w, tag_items_data_item_type);
+
auto f = def_to_str;
- ebml_w.writer.write(_str.bytes(ty_str(typ, f)));
+ auto ty_str_ctxt = @rec(ds=f);
+ ebml_w.writer.write(_str.bytes(Encode.ty_str(ty_str_ctxt, typ)));
+
ebml.end_tag(ebml_w);
}
diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs
index f995ee6e..8ab2c3c0 100644
--- a/src/comp/middle/trans.rs
+++ b/src/comp/middle/trans.rs
@@ -183,8 +183,11 @@ fn path_name(vec[str] path) -> str {
fn mangle_name_by_type(@crate_ctxt ccx, vec[str] path, ty.t t) -> str {
ccx.sha.reset();
+
auto f = metadata.def_to_str;
- ccx.sha.input_str(metadata.ty_str(t, f));
+ auto cx = @rec(ds=f);
+ ccx.sha.input_str(metadata.Encode.ty_str(cx, t));
+
ret sep() + "rust" + sep()
+ _str.substr(ccx.sha.result_str(), 0u, 16u) + sep()
+ path_name(path);