]>
Commit | Line | Data |
---|---|---|
c34b1796 | 1 | // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT |
223e47cc LB |
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 | ||
223e47cc LB |
11 | // Type encoding |
12 | ||
1a4d82fc JJ |
13 | #![allow(unused_must_use)] // as with encoding, everything is a no-fail MemWriter |
14 | #![allow(non_camel_case_types)] | |
223e47cc | 15 | |
1a4d82fc | 16 | use std::cell::RefCell; |
b039eaaf | 17 | use std::io::Cursor; |
c34b1796 | 18 | use std::io::prelude::*; |
223e47cc | 19 | |
54a0048b | 20 | use rustc::hir::def_id::DefId; |
1a4d82fc | 21 | use middle::region; |
54a0048b SL |
22 | use rustc::ty::subst; |
23 | use rustc::ty::subst::VecPerParamSpace; | |
24 | use rustc::ty::ParamTy; | |
25 | use rustc::ty::{self, Ty, TyCtxt}; | |
92a42be0 | 26 | use rustc::util::nodemap::FnvHashMap; |
1a4d82fc | 27 | |
54a0048b | 28 | use rustc::hir; |
e9174d1e | 29 | |
1a4d82fc | 30 | use syntax::abi::Abi; |
b039eaaf | 31 | use syntax::ast; |
3157f602 | 32 | use errors::Handler; |
1a4d82fc | 33 | |
9cc50fc6 SL |
34 | use rbml::leb128; |
35 | use encoder; | |
223e47cc | 36 | |
1a4d82fc | 37 | pub struct ctxt<'a, 'tcx: 'a> { |
9cc50fc6 | 38 | pub diag: &'a Handler, |
223e47cc | 39 | // Def -> str Callback: |
a7813a04 | 40 | pub ds: for<'b> fn(TyCtxt<'b, 'tcx, 'tcx>, DefId) -> String, |
223e47cc | 41 | // The type context. |
a7813a04 | 42 | pub tcx: TyCtxt<'a, 'tcx, 'tcx>, |
1a4d82fc | 43 | pub abbrevs: &'a abbrev_map<'tcx> |
223e47cc LB |
44 | } |
45 | ||
9cc50fc6 SL |
46 | impl<'a, 'tcx> encoder::EncodeContext<'a, 'tcx> { |
47 | pub fn ty_str_ctxt<'b>(&'b self) -> ctxt<'b, 'tcx> { | |
48 | ctxt { | |
49 | diag: self.tcx.sess.diagnostic(), | |
50 | ds: encoder::def_to_string, | |
51 | tcx: self.tcx, | |
52 | abbrevs: &self.type_abbrevs | |
53 | } | |
54 | } | |
55 | } | |
56 | ||
62682a34 | 57 | // Compact string representation for Ty values. API TyStr & parse_from_str. |
223e47cc LB |
58 | // Extra parameters are for converting to/from def_ids in the string rep. |
59 | // Whatever format you choose should not contain pipe characters. | |
60 | pub struct ty_abbrev { | |
b039eaaf | 61 | s: Vec<u8> |
223e47cc LB |
62 | } |
63 | ||
1a4d82fc | 64 | pub type abbrev_map<'tcx> = RefCell<FnvHashMap<Ty<'tcx>, ty_abbrev>>; |
223e47cc | 65 | |
9cc50fc6 | 66 | pub fn enc_ty<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx>) { |
3157f602 XL |
67 | if let Some(a) = cx.abbrevs.borrow_mut().get(&t) { |
68 | w.write_all(&a.s); | |
69 | return; | |
1a4d82fc | 70 | } |
c34b1796 | 71 | |
9cc50fc6 | 72 | let pos = w.position(); |
1a4d82fc JJ |
73 | |
74 | match t.sty { | |
9cc50fc6 SL |
75 | ty::TyBool => { write!(w, "b"); } |
76 | ty::TyChar => { write!(w, "c"); } | |
5bcae85e | 77 | ty::TyNever => { write!(w, "!"); } |
62682a34 | 78 | ty::TyInt(t) => { |
1a4d82fc | 79 | match t { |
7453a54e SL |
80 | ast::IntTy::Is => write!(w, "is"), |
81 | ast::IntTy::I8 => write!(w, "MB"), | |
82 | ast::IntTy::I16 => write!(w, "MW"), | |
83 | ast::IntTy::I32 => write!(w, "ML"), | |
84 | ast::IntTy::I64 => write!(w, "MD") | |
9cc50fc6 | 85 | }; |
1a4d82fc | 86 | } |
62682a34 | 87 | ty::TyUint(t) => { |
1a4d82fc | 88 | match t { |
7453a54e SL |
89 | ast::UintTy::Us => write!(w, "us"), |
90 | ast::UintTy::U8 => write!(w, "Mb"), | |
91 | ast::UintTy::U16 => write!(w, "Mw"), | |
92 | ast::UintTy::U32 => write!(w, "Ml"), | |
93 | ast::UintTy::U64 => write!(w, "Md") | |
9cc50fc6 | 94 | }; |
1a4d82fc | 95 | } |
62682a34 | 96 | ty::TyFloat(t) => { |
1a4d82fc | 97 | match t { |
7453a54e SL |
98 | ast::FloatTy::F32 => write!(w, "Mf"), |
99 | ast::FloatTy::F64 => write!(w, "MF"), | |
9cc50fc6 | 100 | }; |
1a4d82fc | 101 | } |
62682a34 | 102 | ty::TyEnum(def, substs) => { |
54a0048b | 103 | write!(w, "t[{}|", (cx.ds)(cx.tcx, def.did)); |
1a4d82fc | 104 | enc_substs(w, cx, substs); |
9cc50fc6 | 105 | write!(w, "]"); |
1a4d82fc | 106 | } |
62682a34 | 107 | ty::TyTrait(box ty::TraitTy { ref principal, |
1a4d82fc | 108 | ref bounds }) => { |
9cc50fc6 | 109 | write!(w, "x["); |
d9579d0f | 110 | enc_trait_ref(w, cx, principal.0); |
1a4d82fc | 111 | enc_existential_bounds(w, cx, bounds); |
9cc50fc6 | 112 | write!(w, "]"); |
1a4d82fc | 113 | } |
a7813a04 | 114 | ty::TyTuple(ts) => { |
9cc50fc6 | 115 | write!(w, "T["); |
85aaf69f | 116 | for t in ts { enc_ty(w, cx, *t); } |
9cc50fc6 | 117 | write!(w, "]"); |
1a4d82fc | 118 | } |
9cc50fc6 SL |
119 | ty::TyBox(typ) => { write!(w, "~"); enc_ty(w, cx, typ); } |
120 | ty::TyRawPtr(mt) => { write!(w, "*"); enc_mt(w, cx, mt); } | |
62682a34 | 121 | ty::TyRef(r, mt) => { |
9cc50fc6 | 122 | write!(w, "&"); |
1a4d82fc JJ |
123 | enc_region(w, cx, *r); |
124 | enc_mt(w, cx, mt); | |
125 | } | |
62682a34 | 126 | ty::TyArray(t, sz) => { |
9cc50fc6 | 127 | write!(w, "V"); |
1a4d82fc | 128 | enc_ty(w, cx, t); |
9cc50fc6 | 129 | write!(w, "/{}|", sz); |
1a4d82fc | 130 | } |
62682a34 | 131 | ty::TySlice(t) => { |
9cc50fc6 | 132 | write!(w, "V"); |
62682a34 | 133 | enc_ty(w, cx, t); |
9cc50fc6 | 134 | write!(w, "/|"); |
62682a34 SL |
135 | } |
136 | ty::TyStr => { | |
9cc50fc6 | 137 | write!(w, "v"); |
1a4d82fc | 138 | } |
54a0048b | 139 | ty::TyFnDef(def_id, substs, f) => { |
9cc50fc6 | 140 | write!(w, "F"); |
54a0048b SL |
141 | write!(w, "{}|", (cx.ds)(cx.tcx, def_id)); |
142 | enc_substs(w, cx, substs); | |
1a4d82fc JJ |
143 | enc_bare_fn_ty(w, cx, f); |
144 | } | |
54a0048b | 145 | ty::TyFnPtr(f) => { |
9cc50fc6 | 146 | write!(w, "G"); |
1a4d82fc JJ |
147 | enc_bare_fn_ty(w, cx, f); |
148 | } | |
62682a34 | 149 | ty::TyInfer(_) => { |
54a0048b | 150 | bug!("cannot encode inference variable types"); |
1a4d82fc | 151 | } |
62682a34 | 152 | ty::TyParam(ParamTy {space, idx, name}) => { |
9cc50fc6 | 153 | write!(w, "p[{}|{}|{}]", idx, space.to_uint(), name); |
1a4d82fc | 154 | } |
62682a34 | 155 | ty::TyStruct(def, substs) => { |
54a0048b | 156 | write!(w, "a[{}|", (cx.ds)(cx.tcx, def.did)); |
1a4d82fc | 157 | enc_substs(w, cx, substs); |
9cc50fc6 | 158 | write!(w, "]"); |
1a4d82fc | 159 | } |
a7813a04 | 160 | ty::TyClosure(def, substs) => { |
54a0048b | 161 | write!(w, "k[{}|", (cx.ds)(cx.tcx, def)); |
a7813a04 XL |
162 | enc_substs(w, cx, substs.func_substs); |
163 | for ty in substs.upvar_tys { | |
c1a9b12d SL |
164 | enc_ty(w, cx, ty); |
165 | } | |
9cc50fc6 SL |
166 | write!(w, "."); |
167 | write!(w, "]"); | |
1a4d82fc | 168 | } |
62682a34 | 169 | ty::TyProjection(ref data) => { |
9cc50fc6 | 170 | write!(w, "P["); |
d9579d0f | 171 | enc_trait_ref(w, cx, data.trait_ref); |
9cc50fc6 | 172 | write!(w, "{}]", data.item_name); |
1a4d82fc | 173 | } |
5bcae85e SL |
174 | ty::TyAnon(def_id, substs) => { |
175 | write!(w, "A[{}|", (cx.ds)(cx.tcx, def_id)); | |
176 | enc_substs(w, cx, substs); | |
177 | write!(w, "]"); | |
178 | } | |
62682a34 | 179 | ty::TyError => { |
9cc50fc6 | 180 | write!(w, "e"); |
1a4d82fc | 181 | } |
223e47cc | 182 | } |
223e47cc | 183 | |
9cc50fc6 | 184 | let end = w.position(); |
1a4d82fc | 185 | let len = end - pos; |
b039eaaf | 186 | |
9cc50fc6 | 187 | let mut abbrev = Cursor::new(Vec::with_capacity(16)); |
b039eaaf | 188 | abbrev.write_all(b"#"); |
9cc50fc6 SL |
189 | { |
190 | let start_position = abbrev.position() as usize; | |
191 | let bytes_written = leb128::write_unsigned_leb128(abbrev.get_mut(), | |
192 | start_position, | |
193 | pos); | |
194 | abbrev.set_position((start_position + bytes_written) as u64); | |
195 | } | |
b039eaaf SL |
196 | |
197 | cx.abbrevs.borrow_mut().insert(t, ty_abbrev { | |
198 | s: if abbrev.position() < len { | |
199 | abbrev.get_ref()[..abbrev.position() as usize].to_owned() | |
200 | } else { | |
201 | // if the abbreviation is longer than the real type, | |
202 | // don't use #-notation. However, insert it here so | |
203 | // other won't have to `mark_stable_position` | |
9cc50fc6 | 204 | w.get_ref()[pos as usize .. end as usize].to_owned() |
b039eaaf SL |
205 | } |
206 | }); | |
223e47cc | 207 | } |
970d7e83 | 208 | |
9cc50fc6 | 209 | fn enc_mutability(w: &mut Cursor<Vec<u8>>, mt: hir::Mutability) { |
970d7e83 | 210 | match mt { |
e9174d1e | 211 | hir::MutImmutable => (), |
9cc50fc6 SL |
212 | hir::MutMutable => { |
213 | write!(w, "m"); | |
214 | } | |
215 | }; | |
970d7e83 LB |
216 | } |
217 | ||
9cc50fc6 | 218 | fn enc_mt<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>, |
c1a9b12d | 219 | mt: ty::TypeAndMut<'tcx>) { |
970d7e83 | 220 | enc_mutability(w, mt.mutbl); |
223e47cc LB |
221 | enc_ty(w, cx, mt.ty); |
222 | } | |
223 | ||
9cc50fc6 SL |
224 | fn enc_opt<T, F>(w: &mut Cursor<Vec<u8>>, t: Option<T>, enc_f: F) where |
225 | F: FnOnce(&mut Cursor<Vec<u8>>, T), | |
1a4d82fc | 226 | { |
970d7e83 | 227 | match t { |
9cc50fc6 SL |
228 | None => { |
229 | write!(w, "n"); | |
230 | } | |
1a4d82fc | 231 | Some(v) => { |
9cc50fc6 | 232 | write!(w, "s"); |
1a4d82fc JJ |
233 | enc_f(w, v); |
234 | } | |
223e47cc LB |
235 | } |
236 | } | |
237 | ||
9cc50fc6 | 238 | fn enc_vec_per_param_space<'a, 'tcx, T, F>(w: &mut Cursor<Vec<u8>>, |
1a4d82fc JJ |
239 | cx: &ctxt<'a, 'tcx>, |
240 | v: &VecPerParamSpace<T>, | |
241 | mut op: F) where | |
9cc50fc6 | 242 | F: FnMut(&mut Cursor<Vec<u8>>, &ctxt<'a, 'tcx>, &T), |
1a4d82fc | 243 | { |
85aaf69f | 244 | for &space in &subst::ParamSpace::all() { |
9cc50fc6 | 245 | write!(w, "["); |
85aaf69f | 246 | for t in v.get_slice(space) { |
1a4d82fc JJ |
247 | op(w, cx, t); |
248 | } | |
9cc50fc6 | 249 | write!(w, "]"); |
1a4d82fc | 250 | } |
223e47cc LB |
251 | } |
252 | ||
9cc50fc6 | 253 | pub fn enc_substs<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>, |
1a4d82fc | 254 | substs: &subst::Substs<'tcx>) { |
54a0048b SL |
255 | enc_vec_per_param_space(w, cx, &substs.regions, |
256 | |w, cx, &r| enc_region(w, cx, r)); | |
1a4d82fc JJ |
257 | enc_vec_per_param_space(w, cx, &substs.types, |
258 | |w, cx, &ty| enc_ty(w, cx, ty)); | |
223e47cc LB |
259 | } |
260 | ||
9cc50fc6 | 261 | pub fn enc_region(w: &mut Cursor<Vec<u8>>, cx: &ctxt, r: ty::Region) { |
1a4d82fc JJ |
262 | match r { |
263 | ty::ReLateBound(id, br) => { | |
9cc50fc6 | 264 | write!(w, "b[{}|", id.depth); |
1a4d82fc | 265 | enc_bound_region(w, cx, br); |
9cc50fc6 | 266 | write!(w, "]"); |
1a4d82fc | 267 | } |
9346a6ac | 268 | ty::ReEarlyBound(ref data) => { |
9cc50fc6 SL |
269 | write!(w, "B[{}|{}|{}]", |
270 | data.space.to_uint(), | |
271 | data.index, | |
272 | data.name); | |
1a4d82fc JJ |
273 | } |
274 | ty::ReFree(ref fr) => { | |
9cc50fc6 | 275 | write!(w, "f["); |
e9174d1e | 276 | enc_scope(w, cx, fr.scope); |
9cc50fc6 | 277 | write!(w, "|"); |
1a4d82fc | 278 | enc_bound_region(w, cx, fr.bound_region); |
9cc50fc6 | 279 | write!(w, "]"); |
1a4d82fc JJ |
280 | } |
281 | ty::ReScope(scope) => { | |
9cc50fc6 | 282 | write!(w, "s"); |
1a4d82fc | 283 | enc_scope(w, cx, scope); |
9cc50fc6 | 284 | write!(w, "|"); |
1a4d82fc JJ |
285 | } |
286 | ty::ReStatic => { | |
9cc50fc6 | 287 | write!(w, "t"); |
1a4d82fc JJ |
288 | } |
289 | ty::ReEmpty => { | |
9cc50fc6 | 290 | write!(w, "e"); |
1a4d82fc | 291 | } |
3157f602 XL |
292 | ty::ReErased => { |
293 | write!(w, "E"); | |
294 | } | |
e9174d1e | 295 | ty::ReVar(_) | ty::ReSkolemized(..) => { |
1a4d82fc | 296 | // these should not crop up after typeck |
54a0048b | 297 | bug!("cannot encode region variables"); |
1a4d82fc | 298 | } |
223e47cc LB |
299 | } |
300 | } | |
301 | ||
9cc50fc6 | 302 | fn enc_scope(w: &mut Cursor<Vec<u8>>, cx: &ctxt, scope: region::CodeExtent) { |
e9174d1e | 303 | match cx.tcx.region_maps.code_extent_data(scope) { |
9cc50fc6 SL |
304 | region::CodeExtentData::CallSiteScope { |
305 | fn_id, body_id } => write!(w, "C[{}|{}]", fn_id, body_id), | |
e9174d1e | 306 | region::CodeExtentData::ParameterScope { |
9cc50fc6 SL |
307 | fn_id, body_id } => write!(w, "P[{}|{}]", fn_id, body_id), |
308 | region::CodeExtentData::Misc(node_id) => write!(w, "M{}", node_id), | |
e9174d1e | 309 | region::CodeExtentData::Remainder(region::BlockRemainder { |
9cc50fc6 SL |
310 | block: b, first_statement_index: i }) => write!(w, "B[{}|{}]", b, i), |
311 | region::CodeExtentData::DestructionScope(node_id) => write!(w, "D{}", node_id), | |
312 | }; | |
970d7e83 LB |
313 | } |
314 | ||
9cc50fc6 | 315 | fn enc_bound_region(w: &mut Cursor<Vec<u8>>, cx: &ctxt, br: ty::BoundRegion) { |
1a4d82fc JJ |
316 | match br { |
317 | ty::BrAnon(idx) => { | |
9cc50fc6 | 318 | write!(w, "a{}|", idx); |
1a4d82fc | 319 | } |
3157f602 XL |
320 | ty::BrNamed(d, name, issue32330) => { |
321 | write!(w, "[{}|{}|", | |
322 | (cx.ds)(cx.tcx, d), | |
323 | name); | |
324 | ||
325 | match issue32330 { | |
326 | ty::Issue32330::WontChange => | |
327 | write!(w, "n]"), | |
328 | ty::Issue32330::WillChange { fn_def_id, region_name } => | |
329 | write!(w, "y{}|{}]", (cx.ds)(cx.tcx, fn_def_id), region_name), | |
330 | }; | |
1a4d82fc JJ |
331 | } |
332 | ty::BrFresh(id) => { | |
9cc50fc6 | 333 | write!(w, "f{}|", id); |
1a4d82fc JJ |
334 | } |
335 | ty::BrEnv => { | |
9cc50fc6 | 336 | write!(w, "e|"); |
223e47cc LB |
337 | } |
338 | } | |
339 | } | |
340 | ||
9cc50fc6 | 341 | pub fn enc_trait_ref<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>, |
d9579d0f | 342 | s: ty::TraitRef<'tcx>) { |
54a0048b | 343 | write!(w, "{}|", (cx.ds)(cx.tcx, s.def_id)); |
1a4d82fc | 344 | enc_substs(w, cx, s.substs); |
223e47cc LB |
345 | } |
346 | ||
9cc50fc6 | 347 | fn enc_unsafety(w: &mut Cursor<Vec<u8>>, p: hir::Unsafety) { |
223e47cc | 348 | match p { |
9cc50fc6 SL |
349 | hir::Unsafety::Normal => write!(w, "n"), |
350 | hir::Unsafety::Unsafe => write!(w, "u"), | |
351 | }; | |
223e47cc LB |
352 | } |
353 | ||
9cc50fc6 SL |
354 | fn enc_abi(w: &mut Cursor<Vec<u8>>, abi: Abi) { |
355 | write!(w, "["); | |
356 | write!(w, "{}", abi.name()); | |
357 | write!(w, "]"); | |
223e47cc LB |
358 | } |
359 | ||
9cc50fc6 | 360 | pub fn enc_bare_fn_ty<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>, |
1a4d82fc JJ |
361 | ft: &ty::BareFnTy<'tcx>) { |
362 | enc_unsafety(w, ft.unsafety); | |
363 | enc_abi(w, ft.abi); | |
223e47cc LB |
364 | enc_fn_sig(w, cx, &ft.sig); |
365 | } | |
366 | ||
9cc50fc6 | 367 | pub fn enc_closure_ty<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>, |
1a4d82fc JJ |
368 | ft: &ty::ClosureTy<'tcx>) { |
369 | enc_unsafety(w, ft.unsafety); | |
223e47cc | 370 | enc_fn_sig(w, cx, &ft.sig); |
1a4d82fc | 371 | enc_abi(w, ft.abi); |
223e47cc LB |
372 | } |
373 | ||
9cc50fc6 | 374 | fn enc_fn_sig<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>, |
1a4d82fc | 375 | fsig: &ty::PolyFnSig<'tcx>) { |
9cc50fc6 | 376 | write!(w, "["); |
85aaf69f | 377 | for ty in &fsig.0.inputs { |
970d7e83 | 378 | enc_ty(w, cx, *ty); |
223e47cc | 379 | } |
9cc50fc6 | 380 | write!(w, "]"); |
1a4d82fc | 381 | if fsig.0.variadic { |
9cc50fc6 | 382 | write!(w, "V"); |
1a4d82fc | 383 | } else { |
9cc50fc6 | 384 | write!(w, "N"); |
1a4d82fc | 385 | } |
5bcae85e | 386 | enc_ty(w, cx, fsig.0.output); |
223e47cc LB |
387 | } |
388 | ||
9cc50fc6 | 389 | pub fn enc_builtin_bounds(w: &mut Cursor<Vec<u8>>, _cx: &ctxt, bs: &ty::BuiltinBounds) { |
85aaf69f | 390 | for bound in bs { |
970d7e83 | 391 | match bound { |
9cc50fc6 SL |
392 | ty::BoundSend => write!(w, "S"), |
393 | ty::BoundSized => write!(w, "Z"), | |
394 | ty::BoundCopy => write!(w, "P"), | |
395 | ty::BoundSync => write!(w, "T"), | |
396 | }; | |
223e47cc | 397 | } |
970d7e83 | 398 | |
9cc50fc6 | 399 | write!(w, "."); |
1a4d82fc JJ |
400 | } |
401 | ||
9cc50fc6 | 402 | pub fn enc_existential_bounds<'a,'tcx>(w: &mut Cursor<Vec<u8>>, |
1a4d82fc JJ |
403 | cx: &ctxt<'a,'tcx>, |
404 | bs: &ty::ExistentialBounds<'tcx>) { | |
1a4d82fc JJ |
405 | enc_builtin_bounds(w, cx, &bs.builtin_bounds); |
406 | ||
62682a34 | 407 | enc_region(w, cx, bs.region_bound); |
1a4d82fc | 408 | |
3157f602 XL |
409 | // Encode projection_bounds in a stable order |
410 | let mut projection_bounds: Vec<_> = bs.projection_bounds | |
411 | .iter() | |
412 | .map(|b| (b.item_name().as_str(), b)) | |
413 | .collect(); | |
414 | projection_bounds.sort_by_key(|&(ref name, _)| name.clone()); | |
415 | ||
416 | for tp in projection_bounds.iter().map(|&(_, tp)| tp) { | |
9cc50fc6 | 417 | write!(w, "P"); |
1a4d82fc | 418 | enc_projection_predicate(w, cx, &tp.0); |
970d7e83 LB |
419 | } |
420 | ||
9cc50fc6 | 421 | write!(w, "."); |
1a4d82fc JJ |
422 | } |
423 | ||
9cc50fc6 | 424 | pub fn enc_type_param_def<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>, |
1a4d82fc | 425 | v: &ty::TypeParameterDef<'tcx>) { |
9cc50fc6 | 426 | write!(w, "{}:{}|{}|{}|{}|", |
54a0048b SL |
427 | v.name, (cx.ds)(cx.tcx, v.def_id), |
428 | v.space.to_uint(), v.index, (cx.ds)(cx.tcx, v.default_def_id)); | |
1a4d82fc | 429 | enc_opt(w, v.default, |w, t| enc_ty(w, cx, t)); |
85aaf69f SL |
430 | enc_object_lifetime_default(w, cx, v.object_lifetime_default); |
431 | } | |
432 | ||
9cc50fc6 | 433 | pub fn enc_region_param_def(w: &mut Cursor<Vec<u8>>, cx: &ctxt, |
e9174d1e | 434 | v: &ty::RegionParameterDef) { |
9cc50fc6 | 435 | write!(w, "{}:{}|{}|{}|", |
54a0048b | 436 | v.name, (cx.ds)(cx.tcx, v.def_id), |
e9174d1e SL |
437 | v.space.to_uint(), v.index); |
438 | for &r in &v.bounds { | |
9cc50fc6 | 439 | write!(w, "R"); |
e9174d1e SL |
440 | enc_region(w, cx, r); |
441 | } | |
9cc50fc6 | 442 | write!(w, "."); |
e9174d1e SL |
443 | } |
444 | ||
9cc50fc6 | 445 | fn enc_object_lifetime_default<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, |
85aaf69f | 446 | cx: &ctxt<'a, 'tcx>, |
62682a34 | 447 | default: ty::ObjectLifetimeDefault) |
85aaf69f SL |
448 | { |
449 | match default { | |
9cc50fc6 SL |
450 | ty::ObjectLifetimeDefault::Ambiguous => { |
451 | write!(w, "a"); | |
452 | } | |
453 | ty::ObjectLifetimeDefault::BaseDefault => { | |
454 | write!(w, "b"); | |
455 | } | |
62682a34 | 456 | ty::ObjectLifetimeDefault::Specific(r) => { |
9cc50fc6 | 457 | write!(w, "s"); |
85aaf69f SL |
458 | enc_region(w, cx, r); |
459 | } | |
460 | } | |
1a4d82fc JJ |
461 | } |
462 | ||
9cc50fc6 | 463 | pub fn enc_predicate<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, |
1a4d82fc JJ |
464 | cx: &ctxt<'a, 'tcx>, |
465 | p: &ty::Predicate<'tcx>) | |
466 | { | |
467 | match *p { | |
a7813a04 XL |
468 | ty::Predicate::Rfc1592(..) => { |
469 | bug!("RFC1592 predicate in metadata `{:?}`", p); | |
470 | } | |
1a4d82fc | 471 | ty::Predicate::Trait(ref trait_ref) => { |
9cc50fc6 | 472 | write!(w, "t"); |
d9579d0f | 473 | enc_trait_ref(w, cx, trait_ref.0.trait_ref); |
1a4d82fc JJ |
474 | } |
475 | ty::Predicate::Equate(ty::Binder(ty::EquatePredicate(a, b))) => { | |
9cc50fc6 | 476 | write!(w, "e"); |
1a4d82fc JJ |
477 | enc_ty(w, cx, a); |
478 | enc_ty(w, cx, b); | |
479 | } | |
480 | ty::Predicate::RegionOutlives(ty::Binder(ty::OutlivesPredicate(a, b))) => { | |
9cc50fc6 | 481 | write!(w, "r"); |
1a4d82fc JJ |
482 | enc_region(w, cx, a); |
483 | enc_region(w, cx, b); | |
484 | } | |
485 | ty::Predicate::TypeOutlives(ty::Binder(ty::OutlivesPredicate(a, b))) => { | |
9cc50fc6 | 486 | write!(w, "o"); |
1a4d82fc JJ |
487 | enc_ty(w, cx, a); |
488 | enc_region(w, cx, b); | |
489 | } | |
490 | ty::Predicate::Projection(ty::Binder(ref data)) => { | |
9cc50fc6 SL |
491 | write!(w, "p"); |
492 | enc_projection_predicate(w, cx, data); | |
1a4d82fc | 493 | } |
e9174d1e | 494 | ty::Predicate::WellFormed(data) => { |
9cc50fc6 | 495 | write!(w, "w"); |
e9174d1e SL |
496 | enc_ty(w, cx, data); |
497 | } | |
498 | ty::Predicate::ObjectSafe(trait_def_id) => { | |
54a0048b | 499 | write!(w, "O{}|", (cx.ds)(cx.tcx, trait_def_id)); |
e9174d1e | 500 | } |
a7813a04 XL |
501 | ty::Predicate::ClosureKind(closure_def_id, kind) => { |
502 | let kind_char = match kind { | |
503 | ty::ClosureKind::Fn => 'f', | |
504 | ty::ClosureKind::FnMut => 'm', | |
505 | ty::ClosureKind::FnOnce => 'o', | |
506 | }; | |
507 | write!(w, "c{}|{}|", (cx.ds)(cx.tcx, closure_def_id), kind_char); | |
508 | } | |
1a4d82fc | 509 | } |
223e47cc LB |
510 | } |
511 | ||
9cc50fc6 | 512 | fn enc_projection_predicate<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, |
1a4d82fc JJ |
513 | cx: &ctxt<'a, 'tcx>, |
514 | data: &ty::ProjectionPredicate<'tcx>) { | |
d9579d0f | 515 | enc_trait_ref(w, cx, data.projection_ty.trait_ref); |
9cc50fc6 | 516 | write!(w, "{}|", data.projection_ty.item_name); |
1a4d82fc | 517 | enc_ty(w, cx, data.ty); |
970d7e83 | 518 | } |