]> git.lizzy.rs Git - rust.git/blob - src/librustc/metadata/tydecode.rs
4897117431bbdbc96ae13d9151e81013ac3c2632
[rust.git] / src / librustc / metadata / tydecode.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
12 // Type decoding
13
14 // tjc note: Would be great to have a `match check` macro equivalent
15 // for some of these
16
17 #![allow(non_camel_case_types)]
18
19 use middle::subst;
20 use middle::subst::VecPerParamSpace;
21 use middle::ty;
22
23 use std::rc::Rc;
24 use std::str;
25 use std::string::String;
26 use std::uint;
27 use syntax::abi;
28 use syntax::ast;
29 use syntax::ast::*;
30 use syntax::parse::token;
31
32 // Compact string representation for ty::t values. API ty_str &
33 // parse_from_str. Extra parameters are for converting to/from def_ids in the
34 // data buffer. Whatever format you choose should not contain pipe characters.
35
36 // Def id conversion: when we encounter def-ids, they have to be translated.
37 // For example, the crate number must be converted from the crate number used
38 // in the library we are reading from into the local crate numbers in use
39 // here.  To perform this translation, the type decoder is supplied with a
40 // conversion function of type `conv_did`.
41 //
42 // Sometimes, particularly when inlining, the correct translation of the
43 // def-id will depend on where it originated from.  Therefore, the conversion
44 // function is given an indicator of the source of the def-id.  See
45 // astencode.rs for more information.
46 pub enum DefIdSource {
47     // Identifies a struct, trait, enum, etc.
48     NominalType,
49
50     // Identifies a type alias (`type X = ...`).
51     TypeWithId,
52
53     // Identifies a type parameter (`fn foo<X>() { ... }`).
54     TypeParameter,
55
56     // Identifies a region parameter (`fn foo<'X>() { ... }`).
57     RegionParameter,
58 }
59 pub type conv_did<'a> =
60     |source: DefIdSource, ast::DefId|: 'a -> ast::DefId;
61
62 pub struct PState<'a> {
63     data: &'a [u8],
64     krate: ast::CrateNum,
65     pos: uint,
66     tcx: &'a ty::ctxt
67 }
68
69 fn peek(st: &PState) -> char {
70     st.data[st.pos] as char
71 }
72
73 fn next(st: &mut PState) -> char {
74     let ch = st.data[st.pos] as char;
75     st.pos = st.pos + 1u;
76     return ch;
77 }
78
79 fn next_byte(st: &mut PState) -> u8 {
80     let b = st.data[st.pos];
81     st.pos = st.pos + 1u;
82     return b;
83 }
84
85 fn scan<R>(st: &mut PState, is_last: |char| -> bool, op: |&[u8]| -> R) -> R {
86     let start_pos = st.pos;
87     debug!("scan: '{}' (start)", st.data[st.pos] as char);
88     while !is_last(st.data[st.pos] as char) {
89         st.pos += 1;
90         debug!("scan: '{}'", st.data[st.pos] as char);
91     }
92     let end_pos = st.pos;
93     st.pos += 1;
94     return op(st.data.slice(start_pos, end_pos));
95 }
96
97 pub fn parse_ident(st: &mut PState, last: char) -> ast::Ident {
98     fn is_last(b: char, c: char) -> bool { return c == b; }
99     return parse_ident_(st, |a| is_last(last, a) );
100 }
101
102 fn parse_ident_(st: &mut PState, is_last: |char| -> bool) -> ast::Ident {
103     scan(st, is_last, |bytes| {
104         token::str_to_ident(str::from_utf8(bytes).unwrap())
105     })
106 }
107
108 pub fn parse_state_from_data<'a>(data: &'a [u8], crate_num: ast::CrateNum,
109                              pos: uint, tcx: &'a ty::ctxt) -> PState<'a> {
110     PState {
111         data: data,
112         krate: crate_num,
113         pos: pos,
114         tcx: tcx
115     }
116 }
117
118 fn data_log_string(data: &[u8], pos: uint) -> String {
119     let mut buf = String::new();
120     buf.push_str("<<");
121     for i in range(pos, data.len()) {
122         let c = data[i];
123         if c > 0x20 && c <= 0x7F {
124             buf.push_char(c as char);
125         } else {
126             buf.push_char('.');
127         }
128     }
129     buf.push_str(">>");
130     buf
131 }
132
133 pub fn parse_ty_data(data: &[u8], crate_num: ast::CrateNum, pos: uint, tcx: &ty::ctxt,
134                      conv: conv_did) -> ty::t {
135     debug!("parse_ty_data {}", data_log_string(data, pos));
136     let mut st = parse_state_from_data(data, crate_num, pos, tcx);
137     parse_ty(&mut st, conv)
138 }
139
140 pub fn parse_bare_fn_ty_data(data: &[u8], crate_num: ast::CrateNum, pos: uint, tcx: &ty::ctxt,
141                              conv: conv_did) -> ty::BareFnTy {
142     debug!("parse_bare_fn_ty_data {}", data_log_string(data, pos));
143     let mut st = parse_state_from_data(data, crate_num, pos, tcx);
144     parse_bare_fn_ty(&mut st, conv)
145 }
146
147 pub fn parse_trait_ref_data(data: &[u8], crate_num: ast::CrateNum, pos: uint, tcx: &ty::ctxt,
148                             conv: conv_did) -> ty::TraitRef {
149     debug!("parse_trait_ref_data {}", data_log_string(data, pos));
150     let mut st = parse_state_from_data(data, crate_num, pos, tcx);
151     parse_trait_ref(&mut st, conv)
152 }
153
154 pub fn parse_substs_data(data: &[u8], crate_num: ast::CrateNum, pos: uint, tcx: &ty::ctxt,
155                          conv: conv_did) -> subst::Substs {
156     debug!("parse_substs_data {}", data_log_string(data, pos));
157     let mut st = parse_state_from_data(data, crate_num, pos, tcx);
158     parse_substs(&mut st, conv)
159 }
160
161 fn parse_size(st: &mut PState) -> Option<uint> {
162     assert_eq!(next(st), '/');
163
164     if peek(st) == '|' {
165         assert_eq!(next(st), '|');
166         None
167     } else {
168         let n = parse_uint(st);
169         assert_eq!(next(st), '|');
170         Some(n)
171     }
172 }
173
174 fn parse_trait_store(st: &mut PState, conv: conv_did) -> ty::TraitStore {
175     match next(st) {
176         '~' => ty::UniqTraitStore,
177         '&' => ty::RegionTraitStore(parse_region(st, conv), parse_mutability(st)),
178         c => {
179             st.tcx.sess.bug(format!("parse_trait_store(): bad input '{}'",
180                                     c).as_slice())
181         }
182     }
183 }
184
185 fn parse_vec_per_param_space<T>(st: &mut PState,
186                                 f: |&mut PState| -> T)
187                                 -> VecPerParamSpace<T>
188 {
189     let mut r = VecPerParamSpace::empty();
190     for &space in subst::ParamSpace::all().iter() {
191         assert_eq!(next(st), '[');
192         while peek(st) != ']' {
193             r.push(space, f(st));
194         }
195         assert_eq!(next(st), ']');
196     }
197     r
198 }
199
200 fn parse_substs(st: &mut PState, conv: conv_did) -> subst::Substs {
201     let regions =
202         parse_region_substs(st, |x,y| conv(x,y));
203
204     let types =
205         parse_vec_per_param_space(st, |st| parse_ty(st, |x,y| conv(x,y)));
206
207     return subst::Substs { types: types,
208                            regions: regions };
209 }
210
211 fn parse_region_substs(st: &mut PState, conv: conv_did) -> subst::RegionSubsts {
212     match next(st) {
213         'e' => subst::ErasedRegions,
214         'n' => {
215             subst::NonerasedRegions(
216                 parse_vec_per_param_space(
217                     st, |st| parse_region(st, |x,y| conv(x,y))))
218         }
219         _ => fail!("parse_bound_region: bad input")
220     }
221 }
222
223 fn parse_bound_region(st: &mut PState, conv: conv_did) -> ty::BoundRegion {
224     match next(st) {
225         'a' => {
226             let id = parse_uint(st);
227             assert_eq!(next(st), '|');
228             ty::BrAnon(id)
229         }
230         '[' => {
231             let def = parse_def(st, RegionParameter, |x,y| conv(x,y));
232             let ident = token::str_to_ident(parse_str(st, ']').as_slice());
233             ty::BrNamed(def, ident.name)
234         }
235         'f' => {
236             let id = parse_uint(st);
237             assert_eq!(next(st), '|');
238             ty::BrFresh(id)
239         }
240         _ => fail!("parse_bound_region: bad input")
241     }
242 }
243
244 fn parse_region(st: &mut PState, conv: conv_did) -> ty::Region {
245     match next(st) {
246       'b' => {
247         assert_eq!(next(st), '[');
248         let id = parse_uint(st) as ast::NodeId;
249         assert_eq!(next(st), '|');
250         let br = parse_bound_region(st, |x,y| conv(x,y));
251         assert_eq!(next(st), ']');
252         ty::ReLateBound(id, br)
253       }
254       'B' => {
255         assert_eq!(next(st), '[');
256         let node_id = parse_uint(st) as ast::NodeId;
257         assert_eq!(next(st), '|');
258         let space = parse_param_space(st);
259         assert_eq!(next(st), '|');
260         let index = parse_uint(st);
261         assert_eq!(next(st), '|');
262         let nm = token::str_to_ident(parse_str(st, ']').as_slice());
263         ty::ReEarlyBound(node_id, space, index, nm.name)
264       }
265       'f' => {
266         assert_eq!(next(st), '[');
267         let id = parse_uint(st) as ast::NodeId;
268         assert_eq!(next(st), '|');
269         let br = parse_bound_region(st, |x,y| conv(x,y));
270         assert_eq!(next(st), ']');
271         ty::ReFree(ty::FreeRegion {scope_id: id,
272                                     bound_region: br})
273       }
274       's' => {
275         let id = parse_uint(st) as ast::NodeId;
276         assert_eq!(next(st), '|');
277         ty::ReScope(id)
278       }
279       't' => {
280         ty::ReStatic
281       }
282       'e' => {
283         ty::ReStatic
284       }
285       _ => fail!("parse_region: bad input")
286     }
287 }
288
289 fn parse_opt<T>(st: &mut PState, f: |&mut PState| -> T) -> Option<T> {
290     match next(st) {
291       'n' => None,
292       's' => Some(f(st)),
293       _ => fail!("parse_opt: bad input")
294     }
295 }
296
297 fn parse_str(st: &mut PState, term: char) -> String {
298     let mut result = String::new();
299     while peek(st) != term {
300         unsafe {
301             result.push_bytes([next_byte(st)])
302         }
303     }
304     next(st);
305     result
306 }
307
308 fn parse_trait_ref(st: &mut PState, conv: conv_did) -> ty::TraitRef {
309     let def = parse_def(st, NominalType, |x,y| conv(x,y));
310     let substs = parse_substs(st, |x,y| conv(x,y));
311     ty::TraitRef {def_id: def, substs: substs}
312 }
313
314 fn parse_ty(st: &mut PState, conv: conv_did) -> ty::t {
315     match next(st) {
316       'n' => return ty::mk_nil(),
317       'z' => return ty::mk_bot(),
318       'b' => return ty::mk_bool(),
319       'i' => return ty::mk_int(),
320       'u' => return ty::mk_uint(),
321       'M' => {
322         match next(st) {
323           'b' => return ty::mk_mach_uint(ast::TyU8),
324           'w' => return ty::mk_mach_uint(ast::TyU16),
325           'l' => return ty::mk_mach_uint(ast::TyU32),
326           'd' => return ty::mk_mach_uint(ast::TyU64),
327           'B' => return ty::mk_mach_int(ast::TyI8),
328           'W' => return ty::mk_mach_int(ast::TyI16),
329           'L' => return ty::mk_mach_int(ast::TyI32),
330           'D' => return ty::mk_mach_int(ast::TyI64),
331           'f' => return ty::mk_mach_float(ast::TyF32),
332           'F' => return ty::mk_mach_float(ast::TyF64),
333           'Q' => return ty::mk_mach_float(ast::TyF128),
334           _ => fail!("parse_ty: bad numeric type")
335         }
336       }
337       'c' => return ty::mk_char(),
338       't' => {
339         assert_eq!(next(st), '[');
340         let def = parse_def(st, NominalType, |x,y| conv(x,y));
341         let substs = parse_substs(st, |x,y| conv(x,y));
342         assert_eq!(next(st), ']');
343         return ty::mk_enum(st.tcx, def, substs);
344       }
345       'x' => {
346         assert_eq!(next(st), '[');
347         let def = parse_def(st, NominalType, |x,y| conv(x,y));
348         let substs = parse_substs(st, |x,y| conv(x,y));
349         let bounds = parse_bounds(st, |x,y| conv(x,y));
350         assert_eq!(next(st), ']');
351         return ty::mk_trait(st.tcx, def, substs, bounds.builtin_bounds);
352       }
353       'p' => {
354         let did = parse_def(st, TypeParameter, |x,y| conv(x,y));
355         debug!("parsed ty_param: did={:?}", did);
356         let index = parse_uint(st);
357         assert_eq!(next(st), '|');
358         let space = parse_param_space(st);
359         assert_eq!(next(st), '|');
360         return ty::mk_param(st.tcx, space, index, did);
361       }
362       '@' => return ty::mk_box(st.tcx, parse_ty(st, |x,y| conv(x,y))),
363       '~' => return ty::mk_uniq(st.tcx, parse_ty(st, |x,y| conv(x,y))),
364       '*' => return ty::mk_ptr(st.tcx, parse_mt(st, |x,y| conv(x,y))),
365       '&' => {
366         let r = parse_region(st, |x,y| conv(x,y));
367         let mt = parse_mt(st, |x,y| conv(x,y));
368         return ty::mk_rptr(st.tcx, r, mt);
369       }
370       'V' => {
371         let mt = parse_mt(st, |x,y| conv(x,y));
372         let sz = parse_size(st);
373         return ty::mk_vec(st.tcx, mt, sz);
374       }
375       'v' => {
376         return ty::mk_str(st.tcx);
377       }
378       'T' => {
379         assert_eq!(next(st), '[');
380         let mut params = Vec::new();
381         while peek(st) != ']' { params.push(parse_ty(st, |x,y| conv(x,y))); }
382         st.pos = st.pos + 1u;
383         return ty::mk_tup(st.tcx, params);
384       }
385       'f' => {
386         return ty::mk_closure(st.tcx, parse_closure_ty(st, |x,y| conv(x,y)));
387       }
388       'F' => {
389         return ty::mk_bare_fn(st.tcx, parse_bare_fn_ty(st, |x,y| conv(x,y)));
390       }
391       '#' => {
392         let pos = parse_hex(st);
393         assert_eq!(next(st), ':');
394         let len = parse_hex(st);
395         assert_eq!(next(st), '#');
396         let key = ty::creader_cache_key {cnum: st.krate,
397                                          pos: pos,
398                                          len: len };
399
400         match st.tcx.rcache.borrow().find_copy(&key) {
401           Some(tt) => return tt,
402           None => {}
403         }
404         let mut ps = PState {
405             pos: pos,
406             .. *st
407         };
408         let tt = parse_ty(&mut ps, |x,y| conv(x,y));
409         st.tcx.rcache.borrow_mut().insert(key, tt);
410         return tt;
411       }
412       '"' => {
413         let _ = parse_def(st, TypeWithId, |x,y| conv(x,y));
414         let inner = parse_ty(st, |x,y| conv(x,y));
415         inner
416       }
417       'a' => {
418           assert_eq!(next(st), '[');
419           let did = parse_def(st, NominalType, |x,y| conv(x,y));
420           let substs = parse_substs(st, |x,y| conv(x,y));
421           assert_eq!(next(st), ']');
422           return ty::mk_struct(st.tcx, did, substs);
423       }
424       'e' => {
425           return ty::mk_err();
426       }
427       c => { fail!("unexpected char in type string: {}", c);}
428     }
429 }
430
431 fn parse_mutability(st: &mut PState) -> ast::Mutability {
432     match peek(st) {
433       'm' => { next(st); ast::MutMutable }
434       _ => { ast::MutImmutable }
435     }
436 }
437
438 fn parse_mt(st: &mut PState, conv: conv_did) -> ty::mt {
439     let m = parse_mutability(st);
440     ty::mt { ty: parse_ty(st, |x,y| conv(x,y)), mutbl: m }
441 }
442
443 fn parse_def(st: &mut PState, source: DefIdSource,
444              conv: conv_did) -> ast::DefId {
445     return conv(source, scan(st, |c| { c == '|' }, parse_def_id));
446 }
447
448 fn parse_uint(st: &mut PState) -> uint {
449     let mut n = 0;
450     loop {
451         let cur = peek(st);
452         if cur < '0' || cur > '9' { return n; }
453         st.pos = st.pos + 1u;
454         n *= 10;
455         n += (cur as uint) - ('0' as uint);
456     };
457 }
458
459 fn parse_param_space(st: &mut PState) -> subst::ParamSpace {
460     subst::ParamSpace::from_uint(parse_uint(st))
461 }
462
463 fn parse_hex(st: &mut PState) -> uint {
464     let mut n = 0u;
465     loop {
466         let cur = peek(st);
467         if (cur < '0' || cur > '9') && (cur < 'a' || cur > 'f') { return n; }
468         st.pos = st.pos + 1u;
469         n *= 16u;
470         if '0' <= cur && cur <= '9' {
471             n += (cur as uint) - ('0' as uint);
472         } else { n += 10u + (cur as uint) - ('a' as uint); }
473     };
474 }
475
476 fn parse_fn_style(c: char) -> FnStyle {
477     match c {
478         'u' => UnsafeFn,
479         'n' => NormalFn,
480         _ => fail!("parse_fn_style: bad fn_style {}", c)
481     }
482 }
483
484 fn parse_abi_set(st: &mut PState) -> abi::Abi {
485     assert_eq!(next(st), '[');
486     scan(st, |c| c == ']', |bytes| {
487         let abi_str = str::from_utf8(bytes).unwrap();
488         abi::lookup(abi_str.as_slice()).expect(abi_str)
489     })
490 }
491
492 fn parse_onceness(c: char) -> ast::Onceness {
493     match c {
494         'o' => ast::Once,
495         'm' => ast::Many,
496         _ => fail!("parse_onceness: bad onceness")
497     }
498 }
499
500 fn parse_closure_ty(st: &mut PState, conv: conv_did) -> ty::ClosureTy {
501     let fn_style = parse_fn_style(next(st));
502     let onceness = parse_onceness(next(st));
503     let store = parse_trait_store(st, |x,y| conv(x,y));
504     let bounds = parse_bounds(st, |x,y| conv(x,y));
505     let sig = parse_sig(st, |x,y| conv(x,y));
506     ty::ClosureTy {
507         fn_style: fn_style,
508         onceness: onceness,
509         store: store,
510         bounds: bounds.builtin_bounds,
511         sig: sig
512     }
513 }
514
515 fn parse_bare_fn_ty(st: &mut PState, conv: conv_did) -> ty::BareFnTy {
516     let fn_style = parse_fn_style(next(st));
517     let abi = parse_abi_set(st);
518     let sig = parse_sig(st, |x,y| conv(x,y));
519     ty::BareFnTy {
520         fn_style: fn_style,
521         abi: abi,
522         sig: sig
523     }
524 }
525
526 fn parse_sig(st: &mut PState, conv: conv_did) -> ty::FnSig {
527     assert_eq!(next(st), '[');
528     let id = parse_uint(st) as ast::NodeId;
529     assert_eq!(next(st), '|');
530     let mut inputs = Vec::new();
531     while peek(st) != ']' {
532         inputs.push(parse_ty(st, |x,y| conv(x,y)));
533     }
534     st.pos += 1u; // eat the ']'
535     let variadic = match next(st) {
536         'V' => true,
537         'N' => false,
538         r => fail!(format!("bad variadic: {}", r)),
539     };
540     let ret_ty = parse_ty(st, |x,y| conv(x,y));
541     ty::FnSig {binder_id: id,
542                inputs: inputs,
543                output: ret_ty,
544                variadic: variadic}
545 }
546
547 // Rust metadata parsing
548 pub fn parse_def_id(buf: &[u8]) -> ast::DefId {
549     let mut colon_idx = 0u;
550     let len = buf.len();
551     while colon_idx < len && buf[colon_idx] != ':' as u8 { colon_idx += 1u; }
552     if colon_idx == len {
553         error!("didn't find ':' when parsing def id");
554         fail!();
555     }
556
557     let crate_part = buf.slice(0u, colon_idx);
558     let def_part = buf.slice(colon_idx + 1u, len);
559
560     let crate_num = match uint::parse_bytes(crate_part, 10u) {
561        Some(cn) => cn as ast::CrateNum,
562        None => fail!("internal error: parse_def_id: crate number expected, but found {:?}",
563                      crate_part)
564     };
565     let def_num = match uint::parse_bytes(def_part, 10u) {
566        Some(dn) => dn as ast::NodeId,
567        None => fail!("internal error: parse_def_id: id expected, but found {:?}",
568                      def_part)
569     };
570     ast::DefId { krate: crate_num, node: def_num }
571 }
572
573 pub fn parse_type_param_def_data(data: &[u8], start: uint,
574                                  crate_num: ast::CrateNum, tcx: &ty::ctxt,
575                                  conv: conv_did) -> ty::TypeParameterDef
576 {
577     let mut st = parse_state_from_data(data, crate_num, start, tcx);
578     parse_type_param_def(&mut st, conv)
579 }
580
581 fn parse_type_param_def(st: &mut PState, conv: conv_did) -> ty::TypeParameterDef {
582     let ident = parse_ident(st, ':');
583     let def_id = parse_def(st, NominalType, |x,y| conv(x,y));
584     let space = parse_param_space(st);
585     assert_eq!(next(st), '|');
586     let index = parse_uint(st);
587     assert_eq!(next(st), '|');
588     let bounds = Rc::new(parse_bounds(st, |x,y| conv(x,y)));
589     let default = parse_opt(st, |st| parse_ty(st, |x,y| conv(x,y)));
590
591     ty::TypeParameterDef {
592         ident: ident,
593         def_id: def_id,
594         space: space,
595         index: index,
596         bounds: bounds,
597         default: default
598     }
599 }
600
601 fn parse_bounds(st: &mut PState, conv: conv_did) -> ty::ParamBounds {
602     let mut param_bounds = ty::ParamBounds {
603         builtin_bounds: ty::empty_builtin_bounds(),
604         trait_bounds: Vec::new()
605     };
606     loop {
607         match next(st) {
608             'S' => {
609                 param_bounds.builtin_bounds.add(ty::BoundSend);
610             }
611             'O' => {
612                 param_bounds.builtin_bounds.add(ty::BoundStatic);
613             }
614             'Z' => {
615                 param_bounds.builtin_bounds.add(ty::BoundSized);
616             }
617             'P' => {
618                 param_bounds.builtin_bounds.add(ty::BoundCopy);
619             }
620             'T' => {
621                 param_bounds.builtin_bounds.add(ty::BoundShare);
622             }
623             'I' => {
624                 param_bounds.trait_bounds.push(Rc::new(parse_trait_ref(st, |x,y| conv(x,y))));
625             }
626             '.' => {
627                 return param_bounds;
628             }
629             c => {
630                 fail!("parse_bounds: bad bounds ('{}')", c)
631             }
632         }
633     }
634 }