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