]> git.lizzy.rs Git - rust.git/blob - src/librustc/metadata/tyencode.rs
doc: remove incomplete sentence
[rust.git] / src / librustc / metadata / tyencode.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Type encoding
12
13 #![allow(unused_must_use)] // as with encoding, everything is a no-fail MemWriter
14 #![allow(non_camel_case_types)]
15
16 use std::cell::RefCell;
17
18 use middle::region;
19 use middle::subst;
20 use middle::subst::VecPerParamSpace;
21 use middle::ty::ParamTy;
22 use middle::ty::{mod, Ty};
23 use util::nodemap::FnvHashMap;
24
25 use syntax::abi::Abi;
26 use syntax::ast;
27 use syntax::diagnostic::SpanHandler;
28 use syntax::parse::token;
29
30 use rbml::io::SeekableMemWriter;
31
32 macro_rules! mywrite { ($($arg:tt)*) => ({ write!($($arg)*); }) }
33
34 pub struct ctxt<'a, 'tcx: 'a> {
35     pub diag: &'a SpanHandler,
36     // Def -> str Callback:
37     pub ds: fn(ast::DefId) -> String,
38     // The type context.
39     pub tcx: &'a ty::ctxt<'tcx>,
40     pub abbrevs: &'a abbrev_map<'tcx>
41 }
42
43 // Compact string representation for Ty values. API ty_str & parse_from_str.
44 // Extra parameters are for converting to/from def_ids in the string rep.
45 // Whatever format you choose should not contain pipe characters.
46 pub struct ty_abbrev {
47     s: String
48 }
49
50 pub type abbrev_map<'tcx> = RefCell<FnvHashMap<Ty<'tcx>, ty_abbrev>>;
51
52 pub fn enc_ty<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx>) {
53     match cx.abbrevs.borrow_mut().get(&t) {
54         Some(a) => { w.write(a.s.as_bytes()); return; }
55         None => {}
56     }
57     let pos = w.tell().unwrap();
58
59     match t.sty {
60         ty::ty_bool => mywrite!(w, "b"),
61         ty::ty_char => mywrite!(w, "c"),
62         ty::ty_int(t) => {
63             match t {
64                 ast::TyI => mywrite!(w, "i"),
65                 ast::TyI8 => mywrite!(w, "MB"),
66                 ast::TyI16 => mywrite!(w, "MW"),
67                 ast::TyI32 => mywrite!(w, "ML"),
68                 ast::TyI64 => mywrite!(w, "MD")
69             }
70         }
71         ty::ty_uint(t) => {
72             match t {
73                 ast::TyU => mywrite!(w, "u"),
74                 ast::TyU8 => mywrite!(w, "Mb"),
75                 ast::TyU16 => mywrite!(w, "Mw"),
76                 ast::TyU32 => mywrite!(w, "Ml"),
77                 ast::TyU64 => mywrite!(w, "Md")
78             }
79         }
80         ty::ty_float(t) => {
81             match t {
82                 ast::TyF32 => mywrite!(w, "Mf"),
83                 ast::TyF64 => mywrite!(w, "MF"),
84             }
85         }
86         ty::ty_enum(def, substs) => {
87             mywrite!(w, "t[{}|", (cx.ds)(def));
88             enc_substs(w, cx, substs);
89             mywrite!(w, "]");
90         }
91         ty::ty_trait(box ty::TyTrait { ref principal,
92                                        ref bounds }) => {
93             mywrite!(w, "x[");
94             enc_trait_ref(w, cx, &*principal.0);
95             enc_existential_bounds(w, cx, bounds);
96             mywrite!(w, "]");
97         }
98         ty::ty_tup(ref ts) => {
99             mywrite!(w, "T[");
100             for t in ts.iter() { enc_ty(w, cx, *t); }
101             mywrite!(w, "]");
102         }
103         ty::ty_uniq(typ) => { mywrite!(w, "~"); enc_ty(w, cx, typ); }
104         ty::ty_ptr(mt) => { mywrite!(w, "*"); enc_mt(w, cx, mt); }
105         ty::ty_rptr(r, mt) => {
106             mywrite!(w, "&");
107             enc_region(w, cx, *r);
108             enc_mt(w, cx, mt);
109         }
110         ty::ty_vec(t, sz) => {
111             mywrite!(w, "V");
112             enc_ty(w, cx, t);
113             mywrite!(w, "/");
114             match sz {
115                 Some(n) => mywrite!(w, "{}|", n),
116                 None => mywrite!(w, "|"),
117             }
118         }
119         ty::ty_str => {
120             mywrite!(w, "v");
121         }
122         ty::ty_closure(ref f) => {
123             mywrite!(w, "f");
124             enc_closure_ty(w, cx, &**f);
125         }
126         ty::ty_bare_fn(Some(def_id), f) => {
127             mywrite!(w, "F");
128             mywrite!(w, "{}|", (cx.ds)(def_id));
129             enc_bare_fn_ty(w, cx, f);
130         }
131         ty::ty_bare_fn(None, f) => {
132             mywrite!(w, "G");
133             enc_bare_fn_ty(w, cx, f);
134         }
135         ty::ty_infer(_) => {
136             cx.diag.handler().bug("cannot encode inference variable types");
137         }
138         ty::ty_param(ParamTy {space, idx, name}) => {
139             mywrite!(w, "p[{}|{}|{}]", idx, space.to_uint(), token::get_name(name))
140         }
141         ty::ty_struct(def, substs) => {
142             mywrite!(w, "a[{}|", (cx.ds)(def));
143             enc_substs(w, cx, substs);
144             mywrite!(w, "]");
145         }
146         ty::ty_unboxed_closure(def, region, substs) => {
147             mywrite!(w, "k[{}|", (cx.ds)(def));
148             enc_region(w, cx, *region);
149             enc_substs(w, cx, substs);
150             mywrite!(w, "]");
151         }
152         ty::ty_projection(ref data) => {
153             mywrite!(w, "P[");
154             enc_trait_ref(w, cx, &*data.trait_ref);
155             mywrite!(w, "{}]", token::get_name(data.item_name));
156         }
157         ty::ty_err => {
158             mywrite!(w, "e");
159         }
160         ty::ty_open(_) => {
161             cx.diag.handler().bug("unexpected type in enc_sty (ty_open)");
162         }
163     }
164
165     let end = w.tell().unwrap();
166     let len = end - pos;
167     fn estimate_sz(u: u64) -> u64 {
168         let mut n = u;
169         let mut len = 0;
170         while n != 0 { len += 1; n = n >> 4; }
171         return len;
172     }
173     let abbrev_len = 3 + estimate_sz(pos) + estimate_sz(len);
174     if abbrev_len < len {
175         // I.e. it's actually an abbreviation.
176         cx.abbrevs.borrow_mut().insert(t, ty_abbrev {
177             s: format!("#{:x}:{:x}#", pos, len)
178         });
179     }
180 }
181
182 fn enc_mutability(w: &mut SeekableMemWriter, mt: ast::Mutability) {
183     match mt {
184         ast::MutImmutable => (),
185         ast::MutMutable => mywrite!(w, "m"),
186     }
187 }
188
189 fn enc_mt<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>,
190                     mt: ty::mt<'tcx>) {
191     enc_mutability(w, mt.mutbl);
192     enc_ty(w, cx, mt.ty);
193 }
194
195 fn enc_opt<T, F>(w: &mut SeekableMemWriter, t: Option<T>, enc_f: F) where
196     F: FnOnce(&mut SeekableMemWriter, T),
197 {
198     match t {
199         None => mywrite!(w, "n"),
200         Some(v) => {
201             mywrite!(w, "s");
202             enc_f(w, v);
203         }
204     }
205 }
206
207 fn enc_vec_per_param_space<'a, 'tcx, T, F>(w: &mut SeekableMemWriter,
208                                            cx: &ctxt<'a, 'tcx>,
209                                            v: &VecPerParamSpace<T>,
210                                            mut op: F) where
211     F: FnMut(&mut SeekableMemWriter, &ctxt<'a, 'tcx>, &T),
212 {
213     for &space in subst::ParamSpace::all().iter() {
214         mywrite!(w, "[");
215         for t in v.get_slice(space).iter() {
216             op(w, cx, t);
217         }
218         mywrite!(w, "]");
219     }
220 }
221
222 pub fn enc_substs<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>,
223                             substs: &subst::Substs<'tcx>) {
224     enc_region_substs(w, cx, &substs.regions);
225     enc_vec_per_param_space(w, cx, &substs.types,
226                             |w, cx, &ty| enc_ty(w, cx, ty));
227 }
228
229 fn enc_region_substs(w: &mut SeekableMemWriter, cx: &ctxt, substs: &subst::RegionSubsts) {
230     match *substs {
231         subst::ErasedRegions => {
232             mywrite!(w, "e");
233         }
234         subst::NonerasedRegions(ref regions) => {
235             mywrite!(w, "n");
236             enc_vec_per_param_space(w, cx, regions,
237                                     |w, cx, &r| enc_region(w, cx, r));
238         }
239     }
240 }
241
242 pub fn enc_region(w: &mut SeekableMemWriter, cx: &ctxt, r: ty::Region) {
243     match r {
244         ty::ReLateBound(id, br) => {
245             mywrite!(w, "b[{}|", id.depth);
246             enc_bound_region(w, cx, br);
247             mywrite!(w, "]");
248         }
249         ty::ReEarlyBound(node_id, space, index, name) => {
250             mywrite!(w, "B[{}|{}|{}|{}]",
251                      node_id,
252                      space.to_uint(),
253                      index,
254                      token::get_name(name));
255         }
256         ty::ReFree(ref fr) => {
257             mywrite!(w, "f[");
258             enc_scope(w, cx, fr.scope);
259             mywrite!(w, "|");
260             enc_bound_region(w, cx, fr.bound_region);
261             mywrite!(w, "]");
262         }
263         ty::ReScope(scope) => {
264             mywrite!(w, "s");
265             enc_scope(w, cx, scope);
266             mywrite!(w, "|");
267         }
268         ty::ReStatic => {
269             mywrite!(w, "t");
270         }
271         ty::ReEmpty => {
272             mywrite!(w, "e");
273         }
274         ty::ReInfer(_) => {
275             // these should not crop up after typeck
276             cx.diag.handler().bug("cannot encode region variables");
277         }
278     }
279 }
280
281 fn enc_scope(w: &mut SeekableMemWriter, _cx: &ctxt, scope: region::CodeExtent) {
282     match scope {
283         region::CodeExtent::Misc(node_id) => mywrite!(w, "M{}", node_id)
284     }
285 }
286
287 fn enc_bound_region(w: &mut SeekableMemWriter, cx: &ctxt, br: ty::BoundRegion) {
288     match br {
289         ty::BrAnon(idx) => {
290             mywrite!(w, "a{}|", idx);
291         }
292         ty::BrNamed(d, name) => {
293             mywrite!(w, "[{}|{}]",
294                      (cx.ds)(d),
295                      token::get_name(name));
296         }
297         ty::BrFresh(id) => {
298             mywrite!(w, "f{}|", id);
299         }
300         ty::BrEnv => {
301             mywrite!(w, "e|");
302         }
303     }
304 }
305
306 pub fn enc_trait_ref<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>,
307                                s: &ty::TraitRef<'tcx>) {
308     mywrite!(w, "{}|", (cx.ds)(s.def_id));
309     enc_substs(w, cx, s.substs);
310 }
311
312 pub fn enc_trait_store(w: &mut SeekableMemWriter, cx: &ctxt, s: ty::TraitStore) {
313     match s {
314         ty::UniqTraitStore => mywrite!(w, "~"),
315         ty::RegionTraitStore(re, m) => {
316             mywrite!(w, "&");
317             enc_region(w, cx, re);
318             enc_mutability(w, m);
319         }
320     }
321 }
322
323 fn enc_unsafety(w: &mut SeekableMemWriter, p: ast::Unsafety) {
324     match p {
325         ast::Unsafety::Normal => mywrite!(w, "n"),
326         ast::Unsafety::Unsafe => mywrite!(w, "u"),
327     }
328 }
329
330 fn enc_abi(w: &mut SeekableMemWriter, abi: Abi) {
331     mywrite!(w, "[");
332     mywrite!(w, "{}", abi.name());
333     mywrite!(w, "]")
334 }
335
336 fn enc_onceness(w: &mut SeekableMemWriter, o: ast::Onceness) {
337     match o {
338         ast::Once => mywrite!(w, "o"),
339         ast::Many => mywrite!(w, "m")
340     }
341 }
342
343 pub fn enc_bare_fn_ty<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>,
344                                 ft: &ty::BareFnTy<'tcx>) {
345     enc_unsafety(w, ft.unsafety);
346     enc_abi(w, ft.abi);
347     enc_fn_sig(w, cx, &ft.sig);
348 }
349
350 pub fn enc_closure_ty<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>,
351                                 ft: &ty::ClosureTy<'tcx>) {
352     enc_unsafety(w, ft.unsafety);
353     enc_onceness(w, ft.onceness);
354     enc_trait_store(w, cx, ft.store);
355     enc_existential_bounds(w, cx, &ft.bounds);
356     enc_fn_sig(w, cx, &ft.sig);
357     enc_abi(w, ft.abi);
358 }
359
360 fn enc_fn_sig<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>,
361                         fsig: &ty::PolyFnSig<'tcx>) {
362     mywrite!(w, "[");
363     for ty in fsig.0.inputs.iter() {
364         enc_ty(w, cx, *ty);
365     }
366     mywrite!(w, "]");
367     if fsig.0.variadic {
368         mywrite!(w, "V");
369     } else {
370         mywrite!(w, "N");
371     }
372     match fsig.0.output {
373         ty::FnConverging(result_type) => {
374             enc_ty(w, cx, result_type);
375         }
376         ty::FnDiverging => {
377             mywrite!(w, "z");
378         }
379     }
380 }
381
382 pub fn enc_builtin_bounds(w: &mut SeekableMemWriter, _cx: &ctxt, bs: &ty::BuiltinBounds) {
383     for bound in bs.iter() {
384         match bound {
385             ty::BoundSend => mywrite!(w, "S"),
386             ty::BoundSized => mywrite!(w, "Z"),
387             ty::BoundCopy => mywrite!(w, "P"),
388             ty::BoundSync => mywrite!(w, "T"),
389         }
390     }
391
392     mywrite!(w, ".");
393 }
394
395 pub fn enc_existential_bounds<'a,'tcx>(w: &mut SeekableMemWriter,
396                                        cx: &ctxt<'a,'tcx>,
397                                        bs: &ty::ExistentialBounds<'tcx>) {
398     let param_bounds = ty::ParamBounds { trait_bounds: vec!(),
399                                          region_bounds: vec!(bs.region_bound),
400                                          builtin_bounds: bs.builtin_bounds,
401                                          projection_bounds: bs.projection_bounds.clone() };
402     enc_bounds(w, cx, &param_bounds);
403 }
404
405 pub fn enc_bounds<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>,
406                             bs: &ty::ParamBounds<'tcx>) {
407     enc_builtin_bounds(w, cx, &bs.builtin_bounds);
408
409     for &r in bs.region_bounds.iter() {
410         mywrite!(w, "R");
411         enc_region(w, cx, r);
412     }
413
414     for tp in bs.trait_bounds.iter() {
415         mywrite!(w, "I");
416         enc_trait_ref(w, cx, &*tp.0);
417     }
418
419     for tp in bs.projection_bounds.iter() {
420         mywrite!(w, "P");
421         enc_projection_predicate(w, cx, &tp.0);
422     }
423
424     mywrite!(w, ".");
425 }
426
427 pub fn enc_type_param_def<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>,
428                                     v: &ty::TypeParameterDef<'tcx>) {
429     mywrite!(w, "{}:{}|{}|{}|",
430              token::get_name(v.name), (cx.ds)(v.def_id),
431              v.space.to_uint(), v.index);
432     enc_bounds(w, cx, &v.bounds);
433     enc_opt(w, v.default, |w, t| enc_ty(w, cx, t));
434 }
435
436 pub fn enc_predicate<'a, 'tcx>(w: &mut SeekableMemWriter,
437                                cx: &ctxt<'a, 'tcx>,
438                                p: &ty::Predicate<'tcx>)
439 {
440     match *p {
441         ty::Predicate::Trait(ref trait_ref) => {
442             mywrite!(w, "t");
443             enc_trait_ref(w, cx, &*trait_ref.0.trait_ref);
444         }
445         ty::Predicate::Equate(ty::Binder(ty::EquatePredicate(a, b))) => {
446             mywrite!(w, "e");
447             enc_ty(w, cx, a);
448             enc_ty(w, cx, b);
449         }
450         ty::Predicate::RegionOutlives(ty::Binder(ty::OutlivesPredicate(a, b))) => {
451             mywrite!(w, "r");
452             enc_region(w, cx, a);
453             enc_region(w, cx, b);
454         }
455         ty::Predicate::TypeOutlives(ty::Binder(ty::OutlivesPredicate(a, b))) => {
456             mywrite!(w, "o");
457             enc_ty(w, cx, a);
458             enc_region(w, cx, b);
459         }
460         ty::Predicate::Projection(ty::Binder(ref data)) => {
461             mywrite!(w, "p");
462             enc_projection_predicate(w, cx, data)
463         }
464     }
465 }
466
467 fn enc_projection_predicate<'a, 'tcx>(w: &mut SeekableMemWriter,
468                                       cx: &ctxt<'a, 'tcx>,
469                                       data: &ty::ProjectionPredicate<'tcx>) {
470     enc_trait_ref(w, cx, &*data.projection_ty.trait_ref);
471     mywrite!(w, "{}|", token::get_name(data.projection_ty.item_name));
472     enc_ty(w, cx, data.ty);
473 }