3 import core::{int, uint};
5 import std::map::hashmap;
6 import option::{some, none};
9 import syntax::print::pprust::*;
14 export ac_use_abbrevs;
19 // Def -> str Callback:
21 {ds: fn(def_id) -> str, tcx: ty::ctxt, abbrevs: abbrev_ctxt};
23 // Compact string representation for ty.t values. API ty_str & parse_from_str.
24 // Extra parameters are for converting to/from def_ids in the string rep.
25 // Whatever format you choose should not contain pipe characters.
26 type ty_abbrev = {pos: uint, len: uint, s: @str};
28 tag abbrev_ctxt { ac_no_abbrevs; ac_use_abbrevs(hashmap<ty::t, ty_abbrev>); }
30 fn cx_uses_abbrevs(cx: @ctxt) -> bool {
32 ac_no_abbrevs. { ret false; }
33 ac_use_abbrevs(_) { ret true; }
37 fn enc_ty(w: io::writer, cx: @ctxt, t: ty::t) {
41 alt cx.tcx.short_names_cache.find(t) {
42 some(s) { result_str = s; }
44 let sw = io::string_writer();
45 enc_sty(sw.get_writer(), cx, ty::struct(cx.tcx, t));
46 result_str = @sw.get_str();
47 cx.tcx.short_names_cache.insert(t, result_str);
50 w.write_str(*result_str);
52 ac_use_abbrevs(abbrevs) {
54 some(a) { w.write_str(*a.s); ret; }
56 let pos = w.get_buf_writer().tell();
57 enc_sty(w, cx, ty::struct(cx.tcx, t));
58 let end = w.get_buf_writer().tell();
60 fn estimate_sz(u: uint) -> uint {
63 while n != 0u { len += 1u; n = n >> 4u; }
66 let abbrev_len = 3u + estimate_sz(pos) + estimate_sz(len);
68 // I.e. it's actually an abbreviation.
69 let s = "#" + uint::to_str(pos, 16u) + ":" +
70 uint::to_str(len, 16u) + "#";
71 let a = {pos: pos, len: len, s: @s};
80 fn enc_mt(w: io::writer, cx: @ctxt, mt: ty::mt) {
83 mut. { w.write_char('m'); }
84 maybe_mut. { w.write_char('?'); }
88 fn enc_sty(w: io::writer, cx: @ctxt, st: ty::sty) {
90 ty::ty_nil. { w.write_char('n'); }
91 ty::ty_bot. { w.write_char('z'); }
92 ty::ty_bool. { w.write_char('b'); }
95 ty_i. { w.write_char('i'); }
96 ty_char. { w.write_char('c'); }
97 ty_i8. { w.write_str("MB"); }
98 ty_i16. { w.write_str("MW"); }
99 ty_i32. { w.write_str("ML"); }
100 ty_i64. { w.write_str("MD"); }
105 ty_u. { w.write_char('u'); }
106 ty_u8. { w.write_str("Mb"); }
107 ty_u16. { w.write_str("Mw"); }
108 ty_u32. { w.write_str("Ml"); }
109 ty_u64. { w.write_str("Md"); }
114 ty_f. { w.write_char('l'); }
115 ty_f32. { w.write_str("Mf"); }
116 ty_f64. { w.write_str("MF"); }
119 ty::ty_str. { w.write_char('S'); }
120 ty::ty_tag(def, tys) {
122 w.write_str(cx.ds(def));
124 for t: ty::t in tys { enc_ty(w, cx, t); }
127 ty::ty_iface(def, tys) {
129 w.write_str(cx.ds(def));
131 for t: ty::t in tys { enc_ty(w, cx, t); }
136 for t in ts { enc_ty(w, cx, t); }
139 ty::ty_box(mt) { w.write_char('@'); enc_mt(w, cx, mt); }
140 ty::ty_uniq(mt) { w.write_char('~'); enc_mt(w, cx, mt); }
141 ty::ty_ptr(mt) { w.write_char('*'); enc_mt(w, cx, mt); }
142 ty::ty_vec(mt) { w.write_char('I'); enc_mt(w, cx, mt); }
145 for field: ty::field in fields {
146 w.write_str(field.ident);
148 enc_mt(w, cx, field.mt);
153 enc_proto(w, f.proto);
156 ty::ty_native_fn(args, out) {
158 enc_ty_fn(w, cx, {proto: proto_bare, inputs: args, output: out,
159 ret_style: return_val, constraints: []});
161 ty::ty_obj(methods) {
163 for m: ty::method in methods {
164 enc_proto(w, m.fty.proto);
165 w.write_str(m.ident);
166 enc_ty_fn(w, cx, m.fty);
170 ty::ty_res(def, ty, tps) {
172 w.write_str(cx.ds(def));
175 for t: ty::t in tps { enc_ty(w, cx, t); }
178 ty::ty_var(id) { w.write_char('X'); w.write_str(int::str(id)); }
181 w.write_str(cx.ds(def));
184 ty::ty_param(id, did) {
186 w.write_str(cx.ds(did));
188 w.write_str(uint::str(id));
190 ty::ty_type. { w.write_char('Y'); }
191 ty::ty_send_type. { w.write_char('y'); }
192 ty::ty_opaque_closure_ptr(ty::closure_block.) { w.write_str("C&"); }
193 ty::ty_opaque_closure_ptr(ty::closure_shared.) { w.write_str("C@"); }
194 ty::ty_opaque_closure_ptr(ty::closure_send.) { w.write_str("C~"); }
195 ty::ty_constr(ty, cs) {
198 for tc: @ty::type_constr in cs { enc_ty_constr(w, cx, tc); }
203 fn enc_proto(w: io::writer, proto: proto) {
205 proto_send. { w.write_char('s'); }
206 proto_shared. { w.write_char('F'); }
207 proto_block. { w.write_char('B'); }
208 proto_bare. { w.write_char('f'); }
212 fn enc_ty_fn(w: io::writer, cx: @ctxt, ft: ty::fn_ty) {
214 for arg: ty::arg in ft.inputs {
216 by_mut_ref. { w.write_char('&'); }
217 by_move. { w.write_char('-'); }
218 by_copy. { w.write_char('+'); }
219 by_ref. { w.write_char('='); }
220 by_val. { w.write_char('#'); }
222 enc_ty(w, cx, arg.ty);
226 for c: @ty::constr in ft.constraints {
230 } else { w.write_char(';'); }
231 enc_constr(w, cx, c);
234 noreturn. { w.write_char('!'); }
235 _ { enc_ty(w, cx, ft.output); }
239 // FIXME less copy-and-paste
240 fn enc_constr(w: io::writer, cx: @ctxt, c: @ty::constr) {
241 w.write_str(path_to_str(c.node.path));
243 w.write_str(cx.ds(c.node.id));
246 for a: @constr_arg in c.node.args {
247 if semi { w.write_char(';'); } else { semi = true; }
249 carg_base. { w.write_char('*'); }
250 carg_ident(i) { w.write_uint(i); }
251 carg_lit(l) { w.write_str(lit_to_str(l)); }
257 fn enc_ty_constr(w: io::writer, cx: @ctxt, c: @ty::type_constr) {
258 w.write_str(path_to_str(c.node.path));
260 w.write_str(cx.ds(c.node.id));
263 for a: @ty::ty_constr_arg in c.node.args {
264 if semi { w.write_char(';'); } else { semi = true; }
266 carg_base. { w.write_char('*'); }
267 carg_ident(p) { w.write_str(path_to_str(p)); }
268 carg_lit(l) { w.write_str(lit_to_str(l)); }
274 fn enc_bounds(w: io::writer, cx: @ctxt, bs: @[ty::param_bound]) {
277 ty::bound_send. { w.write_char('S'); }
278 ty::bound_copy. { w.write_char('C'); }
279 ty::bound_iface(tp) {
292 // indent-tabs-mode: nil
294 // buffer-file-coding-system: utf-8-unix