aboutsummaryrefslogtreecommitdiff
path: root/src/comp/middle/metadata.rs
diff options
context:
space:
mode:
authorPatrick Walton <[email protected]>2011-05-03 14:36:04 -0700
committerPatrick Walton <[email protected]>2011-05-03 14:41:05 -0700
commite43729ccf2577fad114107a336099da75eff0ded (patch)
tree0ef063c28fc51eae95db8a0ebbb9c0c015ee864c /src/comp/middle/metadata.rs
parentrustc: Name type glue properly (diff)
downloadrust-e43729ccf2577fad114107a336099da75eff0ded.tar.xz
rust-e43729ccf2577fad114107a336099da75eff0ded.zip
rustc: Refactor metadata.Encode.* to not require a type abbreviation table if abbreviation isn't enabled
Diffstat (limited to 'src/comp/middle/metadata.rs')
-rw-r--r--src/comp/middle/metadata.rs83
1 files changed, 48 insertions, 35 deletions
diff --git a/src/comp/middle/metadata.rs b/src/comp/middle/metadata.rs
index bec62628..2b4ad2a2 100644
--- a/src/comp/middle/metadata.rs
+++ b/src/comp/middle/metadata.rs
@@ -53,59 +53,72 @@ const uint tag_index_table = 0x15u;
type ty_abbrev = rec(uint pos, uint len, str s);
+tag abbrev_ctxt {
+ ac_no_abbrevs;
+ ac_use_abbrevs(hashmap[ty.t, ty_abbrev]);
+}
+
mod Encode {
type ctxt = rec(
fn(ast.def_id) -> str ds, // Def -> str Callback.
ty.ctxt tcx, // The type context.
- bool use_abbrevs,
- hashmap[ty.t, ty_abbrev] abbrevs // Type abbrevs.
+ abbrev_ctxt abbrevs
);
+ fn cx_uses_abbrevs(@ctxt cx) -> bool {
+ alt (cx.abbrevs) {
+ case (ac_no_abbrevs) { ret false; }
+ case (ac_use_abbrevs(_)) { ret true; }
+ }
+ }
+
fn ty_str(@ctxt cx, ty.t t) -> str {
- assert (! cx.use_abbrevs);
+ assert (!cx_uses_abbrevs(cx));
auto sw = io.string_writer();
enc_ty(sw.get_writer(), cx, t);
ret sw.get_str();
}
fn enc_ty(io.writer w, @ctxt cx, ty.t t) {
- if (cx.use_abbrevs) {
- alt (cx.abbrevs.find(t)) {
- case (some[ty_abbrev](?a)) {
- w.write_str(a.s);
- ret;
- }
- case (none[ty_abbrev]) {
- auto pos = w.get_buf_writer().tell();
- auto ss = enc_sty(w, cx, ty.struct(cx.tcx, t));
- auto end = w.get_buf_writer().tell();
- auto len = end-pos;
- fn estimate_sz(uint u) -> uint {
- auto n = u;
- auto len = 0u;
- while (n != 0u) {
- len += 1u;
- n = n >> 4u;
- }
- ret len;
+ alt (cx.abbrevs) {
+ case (ac_no_abbrevs) { enc_sty(w, cx, ty.struct(cx.tcx, t)); }
+ case (ac_use_abbrevs(?abbrevs)) {
+ alt (abbrevs.find(t)) {
+ case (some[ty_abbrev](?a)) {
+ w.write_str(a.s);
+ ret;
}
- auto abbrev_len =
- 3u + estimate_sz(pos) + estimate_sz(len);
-
- if (abbrev_len < len) {
- // I.e. it's actually an abbreviation.
- auto s = ("#"
- + _uint.to_str(pos, 16u) + ":"
- + _uint.to_str(len, 16u) + "#");
- auto a = rec(pos=pos, len=len, s=s);
- cx.abbrevs.insert(t, a);
+ case (none[ty_abbrev]) {
+ auto pos = w.get_buf_writer().tell();
+ auto ss = enc_sty(w, cx, ty.struct(cx.tcx, t));
+ auto end = w.get_buf_writer().tell();
+ auto len = end-pos;
+ fn estimate_sz(uint u) -> uint {
+ auto n = u;
+ auto len = 0u;
+ while (n != 0u) {
+ len += 1u;
+ n = n >> 4u;
+ }
+ ret len;
+ }
+ auto abbrev_len =
+ 3u + estimate_sz(pos) + estimate_sz(len);
+
+ if (abbrev_len < len) {
+ // I.e. it's actually an abbreviation.
+ auto s = ("#"
+ + _uint.to_str(pos, 16u) + ":"
+ + _uint.to_str(len, 16u) + "#");
+ auto a = rec(pos=pos, len=len, s=s);
+ abbrevs.insert(t, a);
+ }
+ ret;
}
- ret;
}
}
}
- enc_sty(w, cx, ty.struct(cx.tcx, t));
}
fn enc_mt(io.writer w, @ctxt cx, &ty.mt mt) {
@@ -406,7 +419,7 @@ fn encode_type(@trans.crate_ctxt cx, &ebml.writer ebml_w, ty.t typ) {
auto f = def_to_str;
auto ty_str_ctxt = @rec(ds=f, tcx=cx.tcx,
- use_abbrevs=true, abbrevs=cx.type_abbrevs);
+ abbrevs=ac_use_abbrevs(cx.type_abbrevs));
Encode.enc_ty(io.new_writer_(ebml_w.writer), ty_str_ctxt, typ);
ebml.end_tag(ebml_w);
}