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.
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.
14 // tjc note: Would be great to have a `match check` macro equivalent
17 #![allow(non_camel_case_types)]
20 use middle::subst::VecPerParamSpace;
25 use std::string::String;
30 use syntax::parse::token;
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.
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`.
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.
50 // Identifies a type alias (`type X = ...`).
53 // Identifies a type parameter (`fn foo<X>() { ... }`).
56 // Identifies a region parameter (`fn foo<'X>() { ... }`).
59 pub type conv_did<'a> =
60 |source: DefIdSource, ast::DefId|: 'a -> ast::DefId;
62 pub struct PState<'a> {
69 fn peek(st: &PState) -> char {
70 st.data[st.pos] as char
73 fn next(st: &mut PState) -> char {
74 let ch = st.data[st.pos] as char;
79 fn next_byte(st: &mut PState) -> u8 {
80 let b = st.data[st.pos];
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) {
90 debug!("scan: '{}'", st.data[st.pos] as char);
94 return op(st.data.slice(start_pos, end_pos));
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) );
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())
108 pub fn parse_state_from_data<'a>(data: &'a [u8], crate_num: ast::CrateNum,
109 pos: uint, tcx: &'a ty::ctxt) -> PState<'a> {
118 fn data_log_string(data: &[u8], pos: uint) -> String {
119 let mut buf = String::new();
121 for i in range(pos, data.len()) {
123 if c > 0x20 && c <= 0x7F {
124 buf.push_char(c as char);
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)
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)
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)
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)
161 fn parse_size(st: &mut PState) -> Option<uint> {
162 assert_eq!(next(st), '/');
165 assert_eq!(next(st), '|');
168 let n = parse_uint(st);
169 assert_eq!(next(st), '|');
174 fn parse_trait_store(st: &mut PState, conv: conv_did) -> ty::TraitStore {
176 '~' => ty::UniqTraitStore,
177 '&' => ty::RegionTraitStore(parse_region(st, conv), parse_mutability(st)),
179 st.tcx.sess.bug(format!("parse_trait_store(): bad input '{}'",
185 fn parse_vec_per_param_space<T>(st: &mut PState,
186 f: |&mut PState| -> T)
187 -> VecPerParamSpace<T>
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));
195 assert_eq!(next(st), ']');
200 fn parse_substs(st: &mut PState, conv: conv_did) -> subst::Substs {
202 parse_region_substs(st, |x,y| conv(x,y));
205 parse_vec_per_param_space(st, |st| parse_ty(st, |x,y| conv(x,y)));
207 return subst::Substs { types: types,
211 fn parse_region_substs(st: &mut PState, conv: conv_did) -> subst::RegionSubsts {
213 'e' => subst::ErasedRegions,
215 subst::NonerasedRegions(
216 parse_vec_per_param_space(
217 st, |st| parse_region(st, |x,y| conv(x,y))))
219 _ => fail!("parse_bound_region: bad input")
223 fn parse_bound_region(st: &mut PState, conv: conv_did) -> ty::BoundRegion {
226 let id = parse_uint(st);
227 assert_eq!(next(st), '|');
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)
236 let id = parse_uint(st);
237 assert_eq!(next(st), '|');
240 _ => fail!("parse_bound_region: bad input")
244 fn parse_region(st: &mut PState, conv: conv_did) -> ty::Region {
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)
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)
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,
275 let id = parse_uint(st) as ast::NodeId;
276 assert_eq!(next(st), '|');
285 _ => fail!("parse_region: bad input")
289 fn parse_opt<T>(st: &mut PState, f: |&mut PState| -> T) -> Option<T> {
293 _ => fail!("parse_opt: bad input")
297 fn parse_str(st: &mut PState, term: char) -> String {
298 let mut result = String::new();
299 while peek(st) != term {
301 result.push_bytes([next_byte(st)])
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}
314 fn parse_ty(st: &mut PState, conv: conv_did) -> ty::t {
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(),
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")
337 'c' => return ty::mk_char(),
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);
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);
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);
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))),
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);
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);
376 return ty::mk_str(st.tcx);
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);
386 return ty::mk_closure(st.tcx, parse_closure_ty(st, |x,y| conv(x,y)));
389 return ty::mk_bare_fn(st.tcx, parse_bare_fn_ty(st, |x,y| conv(x,y)));
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,
400 match st.tcx.rcache.borrow().find_copy(&key) {
401 Some(tt) => return tt,
404 let mut ps = PState {
408 let tt = parse_ty(&mut ps, |x,y| conv(x,y));
409 st.tcx.rcache.borrow_mut().insert(key, tt);
413 let _ = parse_def(st, TypeWithId, |x,y| conv(x,y));
414 let inner = parse_ty(st, |x,y| conv(x,y));
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);
427 c => { fail!("unexpected char in type string: {}", c);}
431 fn parse_mutability(st: &mut PState) -> ast::Mutability {
433 'm' => { next(st); ast::MutMutable }
434 _ => { ast::MutImmutable }
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 }
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));
448 fn parse_uint(st: &mut PState) -> uint {
452 if cur < '0' || cur > '9' { return n; }
453 st.pos = st.pos + 1u;
455 n += (cur as uint) - ('0' as uint);
459 fn parse_param_space(st: &mut PState) -> subst::ParamSpace {
460 subst::ParamSpace::from_uint(parse_uint(st))
463 fn parse_hex(st: &mut PState) -> uint {
467 if (cur < '0' || cur > '9') && (cur < 'a' || cur > 'f') { return n; }
468 st.pos = st.pos + 1u;
470 if '0' <= cur && cur <= '9' {
471 n += (cur as uint) - ('0' as uint);
472 } else { n += 10u + (cur as uint) - ('a' as uint); }
476 fn parse_fn_style(c: char) -> FnStyle {
480 _ => fail!("parse_fn_style: bad fn_style {}", c)
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)
492 fn parse_onceness(c: char) -> ast::Onceness {
496 _ => fail!("parse_onceness: bad onceness")
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));
510 bounds: bounds.builtin_bounds,
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));
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)));
534 st.pos += 1u; // eat the ']'
535 let variadic = match next(st) {
538 r => fail!(format!("bad variadic: {}", r)),
540 let ret_ty = parse_ty(st, |x,y| conv(x,y));
541 ty::FnSig {binder_id: id,
547 // Rust metadata parsing
548 pub fn parse_def_id(buf: &[u8]) -> ast::DefId {
549 let mut colon_idx = 0u;
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");
557 let crate_part = buf.slice(0u, colon_idx);
558 let def_part = buf.slice(colon_idx + 1u, len);
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 {:?}",
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 {:?}",
570 ast::DefId { krate: crate_num, node: def_num }
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
577 let mut st = parse_state_from_data(data, crate_num, start, tcx);
578 parse_type_param_def(&mut st, conv)
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)));
591 ty::TypeParameterDef {
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()
609 param_bounds.builtin_bounds.add(ty::BoundSend);
612 param_bounds.builtin_bounds.add(ty::BoundStatic);
615 param_bounds.builtin_bounds.add(ty::BoundSized);
618 param_bounds.builtin_bounds.add(ty::BoundCopy);
621 param_bounds.builtin_bounds.add(ty::BoundShare);
624 param_bounds.trait_bounds.push(Rc::new(parse_trait_ref(st, |x,y| conv(x,y))));
630 fail!("parse_bounds: bad bounds ('{}')", c)