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.
11 // Decoding metadata from a single crate's metadata
13 #![allow(non_camel_case_types)]
15 pub use self::DefLike::*;
20 use metadata::cstore::crate_metadata;
21 use metadata::common::*;
22 use metadata::csearch::MethodInfo;
23 use metadata::csearch;
25 use metadata::encoder::def_to_u64;
26 use metadata::tydecode::{parse_ty_data, parse_region_data,
27 parse_type_param_def_data, parse_bare_fn_ty_data,
28 parse_trait_ref_data, parse_predicate_data};
30 use middle::lang_items;
32 use middle::ty::{ImplContainer, TraitContainer};
33 use middle::ty::{self, RegionEscape, Ty};
34 use util::nodemap::FnvHashMap;
36 use std::cell::{Cell, RefCell};
37 use std::collections::HashMap;
38 use std::hash::{Hash, SipHasher, Hasher};
39 use std::io::prelude::*;
42 use std::slice::bytes;
47 use serialize::Decodable;
50 use syntax::parse::token::{IdentInterner, special_idents};
51 use syntax::parse::token;
52 use syntax::print::pprust;
57 pub type Cmd<'a> = &'a crate_metadata;
59 // A function that takes a def_id relative to the crate being searched and
60 // returns a def_id relative to the compilation environment, i.e. if we hit a
61 // def_id for an item defined in another crate, somebody needs to figure out
62 // what crate that's in and give us a def_id that makes sense for the current
65 fn u32_from_be_bytes(bytes: &[u8]) -> u32 {
67 bytes::copy_memory(&bytes[..4], &mut b);
68 unsafe { (*(b.as_ptr() as *const u32)).to_be() }
71 fn lookup_hash<'a, F>(d: rbml::Doc<'a>, mut eq_fn: F, hash: u64) -> Option<rbml::Doc<'a>> where
72 F: FnMut(&[u8]) -> bool,
74 let index = reader::get_doc(d, tag_index);
75 let table = reader::get_doc(index, tag_index_table);
76 let hash_pos = table.start + (hash % 256 * 4) as usize;
77 let pos = u32_from_be_bytes(&d.data[hash_pos..]) as usize;
78 let tagged_doc = reader::doc_at(d.data, pos).unwrap();
80 reader::tagged_docs(tagged_doc.doc, tag_index_buckets_bucket_elt).find(|elt| {
81 eq_fn(&elt.data[elt.start + 4 .. elt.end])
83 let pos = u32_from_be_bytes(&elt.data[elt.start..]) as usize;
84 reader::doc_at(d.data, pos).unwrap().doc
88 pub fn maybe_find_item<'a>(item_id: ast::NodeId,
89 items: rbml::Doc<'a>) -> Option<rbml::Doc<'a>> {
90 fn eq_item(bytes: &[u8], item_id: ast::NodeId) -> bool {
91 u32_from_be_bytes(bytes) == item_id
93 let mut s = SipHasher::new_with_keys(0, 0);
94 (item_id as i64).hash(&mut s);
95 lookup_hash(items, |a| eq_item(a, item_id), s.finish())
98 fn find_item<'a>(item_id: ast::NodeId, items: rbml::Doc<'a>) -> rbml::Doc<'a> {
99 match maybe_find_item(item_id, items) {
100 None => panic!("lookup_item: id not found: {}", item_id),
105 // Looks up an item in the given metadata and returns an rbml doc pointing
107 fn lookup_item<'a>(item_id: ast::NodeId, data: &'a [u8]) -> rbml::Doc<'a> {
108 let items = reader::get_doc(rbml::Doc::new(data), tag_items);
109 find_item(item_id, items)
112 #[derive(Debug, PartialEq)]
135 fn item_family(item: rbml::Doc) -> Family {
136 let fam = reader::get_doc(item, tag_items_data_item_family);
137 match reader::doc_as_u8(fam) as char {
150 'V' => StructVariant,
156 'N' => InheritedField,
157 c => panic!("unexpected family char: {}", c)
161 fn item_visibility(item: rbml::Doc) -> ast::Visibility {
162 match reader::maybe_get_doc(item, tag_items_data_item_visibility) {
164 Some(visibility_doc) => {
165 match reader::doc_as_u8(visibility_doc) as char {
167 'i' => ast::Inherited,
168 _ => panic!("unknown visibility character")
174 fn fn_constness(item: rbml::Doc) -> ast::Constness {
175 match reader::maybe_get_doc(item, tag_items_data_item_constness) {
176 None => ast::Constness::NotConst,
177 Some(constness_doc) => {
178 match reader::doc_as_u8(constness_doc) as char {
179 'c' => ast::Constness::Const,
180 'n' => ast::Constness::NotConst,
181 _ => panic!("unknown constness character")
187 fn item_sort(item: rbml::Doc) -> Option<char> {
188 reader::tagged_docs(item, tag_item_trait_item_sort).nth(0).map(|doc| {
189 doc.as_str_slice().as_bytes()[0] as char
193 fn item_symbol(item: rbml::Doc) -> String {
194 reader::get_doc(item, tag_items_data_item_symbol).as_str().to_string()
197 fn translated_def_id(cdata: Cmd, d: rbml::Doc) -> ast::DefId {
198 let id = reader::doc_as_u64(d);
199 let def_id = ast::DefId { krate: (id >> 32) as u32, node: id as u32 };
200 translate_def_id(cdata, def_id)
203 fn item_parent_item(cdata: Cmd, d: rbml::Doc) -> Option<ast::DefId> {
204 reader::tagged_docs(d, tag_items_data_parent_item).nth(0).map(|did| {
205 translated_def_id(cdata, did)
209 fn item_require_parent_item(cdata: Cmd, d: rbml::Doc) -> ast::DefId {
210 translated_def_id(cdata, reader::get_doc(d, tag_items_data_parent_item))
213 fn item_def_id(d: rbml::Doc, cdata: Cmd) -> ast::DefId {
214 translated_def_id(cdata, reader::get_doc(d, tag_def_id))
217 fn get_provided_source(d: rbml::Doc, cdata: Cmd) -> Option<ast::DefId> {
218 reader::maybe_get_doc(d, tag_item_method_provided_source).map(|doc| {
219 translated_def_id(cdata, doc)
223 fn reexports<'a>(d: rbml::Doc<'a>) -> reader::TaggedDocsIterator<'a> {
224 reader::tagged_docs(d, tag_items_data_item_reexport)
227 fn variant_disr_val(d: rbml::Doc) -> Option<ty::Disr> {
228 reader::maybe_get_doc(d, tag_disr_val).and_then(|val_doc| {
229 reader::with_doc_data(val_doc, |data| {
230 str::from_utf8(data).ok().and_then(|s| s.parse().ok())
235 fn doc_type<'tcx>(doc: rbml::Doc, tcx: &ty::ctxt<'tcx>, cdata: Cmd) -> Ty<'tcx> {
236 let tp = reader::get_doc(doc, tag_items_data_item_type);
237 parse_ty_data(tp.data, cdata.cnum, tp.start, tcx,
238 |_, did| translate_def_id(cdata, did))
241 fn maybe_doc_type<'tcx>(doc: rbml::Doc, tcx: &ty::ctxt<'tcx>, cdata: Cmd) -> Option<Ty<'tcx>> {
242 reader::maybe_get_doc(doc, tag_items_data_item_type).map(|tp| {
243 parse_ty_data(tp.data, cdata.cnum, tp.start, tcx,
244 |_, did| translate_def_id(cdata, did))
248 fn doc_method_fty<'tcx>(doc: rbml::Doc, tcx: &ty::ctxt<'tcx>,
249 cdata: Cmd) -> ty::BareFnTy<'tcx> {
250 let tp = reader::get_doc(doc, tag_item_method_fty);
251 parse_bare_fn_ty_data(tp.data, cdata.cnum, tp.start, tcx,
252 |_, did| translate_def_id(cdata, did))
255 pub fn item_type<'tcx>(_item_id: ast::DefId, item: rbml::Doc,
256 tcx: &ty::ctxt<'tcx>, cdata: Cmd) -> Ty<'tcx> {
257 doc_type(item, tcx, cdata)
260 fn doc_trait_ref<'tcx>(doc: rbml::Doc, tcx: &ty::ctxt<'tcx>, cdata: Cmd)
261 -> ty::TraitRef<'tcx> {
262 parse_trait_ref_data(doc.data, cdata.cnum, doc.start, tcx,
263 |_, did| translate_def_id(cdata, did))
266 fn item_trait_ref<'tcx>(doc: rbml::Doc, tcx: &ty::ctxt<'tcx>, cdata: Cmd)
267 -> ty::TraitRef<'tcx> {
268 let tp = reader::get_doc(doc, tag_item_trait_ref);
269 doc_trait_ref(tp, tcx, cdata)
272 fn item_path(item_doc: rbml::Doc) -> Vec<ast_map::PathElem> {
273 let path_doc = reader::get_doc(item_doc, tag_path);
274 reader::docs(path_doc).filter_map(|(tag, elt_doc)| {
275 if tag == tag_path_elem_mod {
276 let s = elt_doc.as_str_slice();
277 Some(ast_map::PathMod(token::intern(s)))
278 } else if tag == tag_path_elem_name {
279 let s = elt_doc.as_str_slice();
280 Some(ast_map::PathName(token::intern(s)))
282 // ignore tag_path_len element
288 fn item_name(intr: &IdentInterner, item: rbml::Doc) -> ast::Name {
289 let name = reader::get_doc(item, tag_paths_data_name);
290 let string = name.as_str_slice();
291 match intr.find(string) {
292 None => token::intern(string),
297 fn item_to_def_like(cdata: Cmd, item: rbml::Doc, did: ast::DefId) -> DefLike {
298 let fam = item_family(item);
301 // Check whether we have an associated const item.
302 if item_sort(item) == Some('C') {
303 DlDef(def::DefAssociatedConst(did))
305 // Regular const item.
306 DlDef(def::DefConst(did))
309 ImmStatic => DlDef(def::DefStatic(did, false)),
310 MutStatic => DlDef(def::DefStatic(did, true)),
311 Struct => DlDef(def::DefStruct(did)),
312 Fn => DlDef(def::DefFn(did, false)),
313 CtorFn => DlDef(def::DefFn(did, true)),
314 Method | StaticMethod => {
315 DlDef(def::DefMethod(did))
318 if item_sort(item) == Some('t') {
319 let trait_did = item_require_parent_item(cdata, item);
320 DlDef(def::DefAssociatedTy(trait_did, did))
322 DlDef(def::DefTy(did, false))
325 Mod => DlDef(def::DefMod(did)),
326 ForeignMod => DlDef(def::DefForeignMod(did)),
328 let enum_did = item_require_parent_item(cdata, item);
329 DlDef(def::DefVariant(enum_did, did, true))
332 let enum_did = item_require_parent_item(cdata, item);
333 DlDef(def::DefVariant(enum_did, did, false))
335 Trait => DlDef(def::DefTrait(did)),
336 Enum => DlDef(def::DefTy(did, true)),
337 Impl | DefaultImpl => DlImpl(did),
338 PublicField | InheritedField => DlField,
342 fn parse_unsafety(item_doc: rbml::Doc) -> ast::Unsafety {
343 let unsafety_doc = reader::get_doc(item_doc, tag_unsafety);
344 if reader::doc_as_u8(unsafety_doc) != 0 {
345 ast::Unsafety::Unsafe
347 ast::Unsafety::Normal
351 fn parse_paren_sugar(item_doc: rbml::Doc) -> bool {
352 let paren_sugar_doc = reader::get_doc(item_doc, tag_paren_sugar);
353 reader::doc_as_u8(paren_sugar_doc) != 0
356 fn parse_polarity(item_doc: rbml::Doc) -> ast::ImplPolarity {
357 let polarity_doc = reader::get_doc(item_doc, tag_polarity);
358 if reader::doc_as_u8(polarity_doc) != 0 {
359 ast::ImplPolarity::Negative
361 ast::ImplPolarity::Positive
365 fn parse_associated_type_names(item_doc: rbml::Doc) -> Vec<ast::Name> {
366 let names_doc = reader::get_doc(item_doc, tag_associated_type_names);
367 reader::tagged_docs(names_doc, tag_associated_type_name)
368 .map(|name_doc| token::intern(name_doc.as_str_slice()))
372 pub fn get_trait_def<'tcx>(cdata: Cmd,
373 item_id: ast::NodeId,
374 tcx: &ty::ctxt<'tcx>) -> ty::TraitDef<'tcx>
376 let item_doc = lookup_item(item_id, cdata.data());
377 let generics = doc_generics(item_doc, tcx, cdata, tag_item_generics);
378 let unsafety = parse_unsafety(item_doc);
379 let associated_type_names = parse_associated_type_names(item_doc);
380 let paren_sugar = parse_paren_sugar(item_doc);
383 paren_sugar: paren_sugar,
386 trait_ref: item_trait_ref(item_doc, tcx, cdata),
387 associated_type_names: associated_type_names,
388 nonblanket_impls: RefCell::new(FnvHashMap()),
389 blanket_impls: RefCell::new(vec![]),
390 flags: Cell::new(ty::TraitFlags::NO_TRAIT_FLAGS)
394 pub fn get_adt_def<'tcx>(intr: &IdentInterner,
396 item_id: ast::NodeId,
397 tcx: &ty::ctxt<'tcx>) -> ty::AdtDefMaster<'tcx>
399 fn get_enum_variants<'tcx>(intr: &IdentInterner,
402 tcx: &ty::ctxt<'tcx>) -> Vec<ty::VariantDefData<'tcx, 'tcx>> {
403 let mut disr_val = 0;
404 reader::tagged_docs(doc, tag_items_data_item_variant).map(|p| {
405 let did = translated_def_id(cdata, p);
406 let item = lookup_item(did.node, cdata.data());
408 if let Some(disr) = variant_disr_val(item) {
412 disr_val = disr_val.wrapping_add(1);
416 name: item_name(intr, item),
417 fields: get_variant_fields(intr, cdata, item, tcx),
422 fn get_variant_fields<'tcx>(intr: &IdentInterner,
425 tcx: &ty::ctxt<'tcx>) -> Vec<ty::FieldDefData<'tcx, 'tcx>> {
426 reader::tagged_docs(doc, tag_item_field).map(|f| {
427 let ff = item_family(f);
429 PublicField | InheritedField => {},
430 _ => tcx.sess.bug(&format!("expected field, found {:?}", ff))
432 ty::FieldDefData::new(item_def_id(f, cdata),
434 struct_field_family_to_visibility(ff))
435 }).chain(reader::tagged_docs(doc, tag_item_unnamed_field).map(|f| {
436 let ff = item_family(f);
437 ty::FieldDefData::new(item_def_id(f, cdata),
438 special_idents::unnamed_field.name,
439 struct_field_family_to_visibility(ff))
442 fn get_struct_variant<'tcx>(intr: &IdentInterner,
446 tcx: &ty::ctxt<'tcx>) -> ty::VariantDefData<'tcx, 'tcx> {
449 name: item_name(intr, doc),
450 fields: get_variant_fields(intr, cdata, doc, tcx),
455 let doc = lookup_item(item_id, cdata.data());
456 let did = ast::DefId { krate: cdata.cnum, node: item_id };
457 let (kind, variants) = match item_family(doc) {
458 Enum => (ty::AdtKind::Enum,
459 get_enum_variants(intr, cdata, doc, tcx)),
460 Struct => (ty::AdtKind::Struct,
461 vec![get_struct_variant(intr, cdata, doc, did, tcx)]),
462 _ => tcx.sess.bug("get_adt_def called on a non-ADT")
465 let adt = tcx.intern_adt_def(did, kind, variants);
467 // this needs to be done *after* the variant is interned,
468 // to support recursive structures
469 for variant in &adt.variants {
470 if variant.kind() == ty::VariantKind::Tuple &&
471 adt.adt_kind() == ty::AdtKind::Enum {
472 // tuple-like enum variant fields aren't real items - get the types
474 debug!("evaluating the ctor-type of {:?}",
476 let ctor_ty = get_type(cdata, variant.did.node, tcx).ty;
477 debug!("evaluating the ctor-type of {:?}.. {:?}",
480 let field_tys = match ctor_ty.sty {
481 ty::TyBareFn(_, &ty::BareFnTy { sig: ty::Binder(ty::FnSig {
484 // tuple-struct constructors don't have escaping regions
485 assert!(!inputs.has_escaping_regions());
488 _ => tcx.sess.bug("tuple-variant ctor is not an ADT")
490 for (field, &ty) in variant.fields.iter().zip(field_tys.iter()) {
491 field.fulfill_ty(ty);
494 for field in &variant.fields {
495 debug!("evaluating the type of {:?}::{:?}", variant.name, field.name);
496 let ty = get_type(cdata, field.did.node, tcx).ty;
497 field.fulfill_ty(ty);
498 debug!("evaluating the type of {:?}::{:?}: {:?}",
499 variant.name, field.name, ty);
507 pub fn get_predicates<'tcx>(cdata: Cmd,
508 item_id: ast::NodeId,
509 tcx: &ty::ctxt<'tcx>)
510 -> ty::GenericPredicates<'tcx>
512 let item_doc = lookup_item(item_id, cdata.data());
513 doc_predicates(item_doc, tcx, cdata, tag_item_generics)
516 pub fn get_super_predicates<'tcx>(cdata: Cmd,
517 item_id: ast::NodeId,
518 tcx: &ty::ctxt<'tcx>)
519 -> ty::GenericPredicates<'tcx>
521 let item_doc = lookup_item(item_id, cdata.data());
522 doc_predicates(item_doc, tcx, cdata, tag_item_super_predicates)
525 pub fn get_type<'tcx>(cdata: Cmd, id: ast::NodeId, tcx: &ty::ctxt<'tcx>)
526 -> ty::TypeScheme<'tcx>
528 let item_doc = lookup_item(id, cdata.data());
529 let t = item_type(ast::DefId { krate: cdata.cnum, node: id }, item_doc, tcx,
531 let generics = doc_generics(item_doc, tcx, cdata, tag_item_generics);
538 pub fn get_stability(cdata: Cmd, id: ast::NodeId) -> Option<attr::Stability> {
539 let item = lookup_item(id, cdata.data());
540 reader::maybe_get_doc(item, tag_items_data_item_stability).map(|doc| {
541 let mut decoder = reader::Decoder::new(doc);
542 Decodable::decode(&mut decoder).unwrap()
546 pub fn get_repr_attrs(cdata: Cmd, id: ast::NodeId) -> Vec<attr::ReprAttr> {
547 let item = lookup_item(id, cdata.data());
548 match reader::maybe_get_doc(item, tag_items_data_item_repr).map(|doc| {
549 let mut decoder = reader::Decoder::new(doc);
550 Decodable::decode(&mut decoder).unwrap()
552 Some(attrs) => attrs,
557 pub fn get_impl_polarity<'tcx>(cdata: Cmd,
559 -> Option<ast::ImplPolarity>
561 let item_doc = lookup_item(id, cdata.data());
562 let fam = item_family(item_doc);
565 Some(parse_polarity(item_doc))
571 pub fn get_custom_coerce_unsized_kind<'tcx>(cdata: Cmd,
573 -> Option<ty::CustomCoerceUnsized> {
574 let item_doc = lookup_item(id, cdata.data());
575 reader::maybe_get_doc(item_doc, tag_impl_coerce_unsized_kind).map(|kind_doc| {
576 let mut decoder = reader::Decoder::new(kind_doc);
577 Decodable::decode(&mut decoder).unwrap()
581 pub fn get_impl_trait<'tcx>(cdata: Cmd,
583 tcx: &ty::ctxt<'tcx>)
584 -> Option<ty::TraitRef<'tcx>>
586 let item_doc = lookup_item(id, cdata.data());
587 let fam = item_family(item_doc);
589 Family::Impl | Family::DefaultImpl => {
590 reader::maybe_get_doc(item_doc, tag_item_trait_ref).map(|tp| {
591 doc_trait_ref(tp, tcx, cdata)
598 pub fn get_symbol(data: &[u8], id: ast::NodeId) -> String {
599 return item_symbol(lookup_item(id, data));
602 // Something that a name can resolve to.
603 #[derive(Copy, Clone, Debug)]
610 /// Iterates over the language items in the given crate.
611 pub fn each_lang_item<F>(cdata: Cmd, mut f: F) -> bool where
612 F: FnMut(ast::NodeId, usize) -> bool,
614 let root = rbml::Doc::new(cdata.data());
615 let lang_items = reader::get_doc(root, tag_lang_items);
616 reader::tagged_docs(lang_items, tag_lang_items_item).all(|item_doc| {
617 let id_doc = reader::get_doc(item_doc, tag_lang_items_item_id);
618 let id = reader::doc_as_u32(id_doc) as usize;
619 let node_id_doc = reader::get_doc(item_doc,
620 tag_lang_items_item_node_id);
621 let node_id = reader::doc_as_u32(node_id_doc) as ast::NodeId;
627 fn each_child_of_item_or_crate<F, G>(intr: Rc<IdentInterner>,
630 mut get_crate_data: G,
631 mut callback: F) where
632 F: FnMut(DefLike, ast::Name, ast::Visibility),
633 G: FnMut(ast::CrateNum) -> Rc<crate_metadata>,
635 // Iterate over all children.
636 for child_info_doc in reader::tagged_docs(item_doc, tag_mod_child) {
637 let child_def_id = translated_def_id(cdata, child_info_doc);
639 // This item may be in yet another crate if it was the child of a
641 let crate_data = if child_def_id.krate == cdata.cnum {
644 Some(get_crate_data(child_def_id.krate))
646 let crate_data = match crate_data {
647 Some(ref cdata) => &**cdata,
651 let other_crates_items = reader::get_doc(rbml::Doc::new(crate_data.data()), tag_items);
654 match maybe_find_item(child_def_id.node, other_crates_items) {
656 Some(child_item_doc) => {
657 // Hand off the item to the callback.
658 let child_name = item_name(&*intr, child_item_doc);
659 let def_like = item_to_def_like(crate_data, child_item_doc, child_def_id);
660 let visibility = item_visibility(child_item_doc);
661 callback(def_like, child_name, visibility);
666 // As a special case, iterate over all static methods of
667 // associated implementations too. This is a bit of a botch.
669 for inherent_impl_def_id_doc in reader::tagged_docs(item_doc,
670 tag_items_data_item_inherent_impl) {
671 let inherent_impl_def_id = item_def_id(inherent_impl_def_id_doc, cdata);
672 let items = reader::get_doc(rbml::Doc::new(cdata.data()), tag_items);
673 if let Some(inherent_impl_doc) = maybe_find_item(inherent_impl_def_id.node, items) {
674 for impl_item_def_id_doc in reader::tagged_docs(inherent_impl_doc,
675 tag_item_impl_item) {
676 let impl_item_def_id = item_def_id(impl_item_def_id_doc,
678 if let Some(impl_method_doc) = maybe_find_item(impl_item_def_id.node, items) {
679 if let StaticMethod = item_family(impl_method_doc) {
680 // Hand off the static method to the callback.
681 let static_method_name = item_name(&*intr, impl_method_doc);
682 let static_method_def_like = item_to_def_like(cdata, impl_method_doc,
684 callback(static_method_def_like,
686 item_visibility(impl_method_doc));
693 for reexport_doc in reexports(item_doc) {
694 let def_id_doc = reader::get_doc(reexport_doc,
695 tag_items_data_item_reexport_def_id);
696 let child_def_id = translated_def_id(cdata, def_id_doc);
698 let name_doc = reader::get_doc(reexport_doc,
699 tag_items_data_item_reexport_name);
700 let name = name_doc.as_str_slice();
702 // This reexport may be in yet another crate.
703 let crate_data = if child_def_id.krate == cdata.cnum {
706 Some(get_crate_data(child_def_id.krate))
708 let crate_data = match crate_data {
709 Some(ref cdata) => &**cdata,
713 let other_crates_items = reader::get_doc(rbml::Doc::new(crate_data.data()), tag_items);
716 if let Some(child_item_doc) = maybe_find_item(child_def_id.node, other_crates_items) {
717 // Hand off the item to the callback.
718 let def_like = item_to_def_like(crate_data, child_item_doc, child_def_id);
719 // These items have a public visibility because they're part of
720 // a public re-export.
721 callback(def_like, token::intern(name), ast::Public);
726 /// Iterates over each child of the given item.
727 pub fn each_child_of_item<F, G>(intr: Rc<IdentInterner>,
732 F: FnMut(DefLike, ast::Name, ast::Visibility),
733 G: FnMut(ast::CrateNum) -> Rc<crate_metadata>,
736 let root_doc = rbml::Doc::new(cdata.data());
737 let items = reader::get_doc(root_doc, tag_items);
738 let item_doc = match maybe_find_item(id, items) {
740 Some(item_doc) => item_doc,
743 each_child_of_item_or_crate(intr,
750 /// Iterates over all the top-level crate items.
751 pub fn each_top_level_item_of_crate<F, G>(intr: Rc<IdentInterner>,
755 F: FnMut(DefLike, ast::Name, ast::Visibility),
756 G: FnMut(ast::CrateNum) -> Rc<crate_metadata>,
758 let root_doc = rbml::Doc::new(cdata.data());
759 let misc_info_doc = reader::get_doc(root_doc, tag_misc_info);
760 let crate_items_doc = reader::get_doc(misc_info_doc,
761 tag_misc_info_crate_items);
763 each_child_of_item_or_crate(intr,
770 pub fn get_item_path(cdata: Cmd, id: ast::NodeId) -> Vec<ast_map::PathElem> {
771 item_path(lookup_item(id, cdata.data()))
774 pub type DecodeInlinedItem<'a> =
775 Box<for<'tcx> FnMut(Cmd,
777 Vec<ast_map::PathElem>,
779 -> Result<&'tcx ast::InlinedItem, Vec<ast_map::PathElem>> + 'a>;
781 pub fn maybe_get_item_ast<'tcx>(cdata: Cmd, tcx: &ty::ctxt<'tcx>, id: ast::NodeId,
782 mut decode_inlined_item: DecodeInlinedItem)
783 -> csearch::FoundAst<'tcx> {
784 debug!("Looking up item: {}", id);
785 let item_doc = lookup_item(id, cdata.data());
786 let path = item_path(item_doc).split_last().unwrap().1.to_vec();
787 match decode_inlined_item(cdata, tcx, path, item_doc) {
788 Ok(ii) => csearch::FoundAst::Found(ii),
790 match item_parent_item(cdata, item_doc) {
792 let parent_item = lookup_item(did.node, cdata.data());
793 match decode_inlined_item(cdata, tcx, path, parent_item) {
794 Ok(ii) => csearch::FoundAst::FoundParent(did, ii),
795 Err(_) => csearch::FoundAst::NotFound
798 None => csearch::FoundAst::NotFound
804 fn get_explicit_self(item: rbml::Doc) -> ty::ExplicitSelfCategory {
805 fn get_mutability(ch: u8) -> ast::Mutability {
807 'i' => ast::MutImmutable,
808 'm' => ast::MutMutable,
809 _ => panic!("unknown mutability character: `{}`", ch as char),
813 let explicit_self_doc = reader::get_doc(item, tag_item_trait_method_explicit_self);
814 let string = explicit_self_doc.as_str_slice();
816 let explicit_self_kind = string.as_bytes()[0];
817 match explicit_self_kind as char {
818 's' => ty::StaticExplicitSelfCategory,
819 'v' => ty::ByValueExplicitSelfCategory,
820 '~' => ty::ByBoxExplicitSelfCategory,
821 // FIXME(#4846) expl. region
823 ty::ByReferenceExplicitSelfCategory(
825 get_mutability(string.as_bytes()[1]))
827 _ => panic!("unknown self type code: `{}`", explicit_self_kind as char)
831 /// Returns the def IDs of all the items in the given implementation.
832 pub fn get_impl_items(cdata: Cmd, impl_id: ast::NodeId)
833 -> Vec<ty::ImplOrTraitItemId> {
834 reader::tagged_docs(lookup_item(impl_id, cdata.data()), tag_item_impl_item).map(|doc| {
835 let def_id = item_def_id(doc, cdata);
836 match item_sort(doc) {
837 Some('C') => ty::ConstTraitItemId(def_id),
838 Some('r') | Some('p') => ty::MethodTraitItemId(def_id),
839 Some('t') => ty::TypeTraitItemId(def_id),
840 _ => panic!("unknown impl item sort"),
845 pub fn get_trait_name(intr: Rc<IdentInterner>,
849 let doc = lookup_item(id, cdata.data());
850 item_name(&*intr, doc)
853 pub fn is_static_method(cdata: Cmd, id: ast::NodeId) -> bool {
854 let doc = lookup_item(id, cdata.data());
855 match item_sort(doc) {
856 Some('r') | Some('p') => {
857 get_explicit_self(doc) == ty::StaticExplicitSelfCategory
863 pub fn get_impl_or_trait_item<'tcx>(intr: Rc<IdentInterner>,
866 tcx: &ty::ctxt<'tcx>)
867 -> ty::ImplOrTraitItem<'tcx> {
868 let item_doc = lookup_item(id, cdata.data());
870 let def_id = item_def_id(item_doc, cdata);
872 let container_id = item_require_parent_item(cdata, item_doc);
873 let container_doc = lookup_item(container_id.node, cdata.data());
874 let container = match item_family(container_doc) {
875 Trait => TraitContainer(container_id),
876 _ => ImplContainer(container_id),
879 let name = item_name(&*intr, item_doc);
880 let vis = item_visibility(item_doc);
882 match item_sort(item_doc) {
884 let ty = doc_type(item_doc, tcx, cdata);
885 let default = get_provided_source(item_doc, cdata);
886 ty::ConstTraitItem(Rc::new(ty::AssociatedConst {
891 container: container,
895 Some('r') | Some('p') => {
896 let generics = doc_generics(item_doc, tcx, cdata, tag_method_ty_generics);
897 let predicates = doc_predicates(item_doc, tcx, cdata, tag_method_ty_generics);
898 let fty = doc_method_fty(item_doc, tcx, cdata);
899 let explicit_self = get_explicit_self(item_doc);
900 let provided_source = get_provided_source(item_doc, cdata);
902 ty::MethodTraitItem(Rc::new(ty::Method::new(name,
913 let ty = maybe_doc_type(item_doc, tcx, cdata);
914 ty::TypeTraitItem(Rc::new(ty::AssociatedType {
919 container: container,
922 _ => panic!("unknown impl/trait item sort"),
926 pub fn get_trait_item_def_ids(cdata: Cmd, id: ast::NodeId)
927 -> Vec<ty::ImplOrTraitItemId> {
928 let data = cdata.data();
929 let item = lookup_item(id, data);
930 reader::tagged_docs(item, tag_item_trait_item).map(|mth| {
931 let def_id = item_def_id(mth, cdata);
932 match item_sort(mth) {
933 Some('C') => ty::ConstTraitItemId(def_id),
934 Some('r') | Some('p') => ty::MethodTraitItemId(def_id),
935 Some('t') => ty::TypeTraitItemId(def_id),
936 _ => panic!("unknown trait item sort"),
941 pub fn get_item_variances(cdata: Cmd, id: ast::NodeId) -> ty::ItemVariances {
942 let data = cdata.data();
943 let item_doc = lookup_item(id, data);
944 let variance_doc = reader::get_doc(item_doc, tag_item_variances);
945 let mut decoder = reader::Decoder::new(variance_doc);
946 Decodable::decode(&mut decoder).unwrap()
949 pub fn get_provided_trait_methods<'tcx>(intr: Rc<IdentInterner>,
952 tcx: &ty::ctxt<'tcx>)
953 -> Vec<Rc<ty::Method<'tcx>>> {
954 let data = cdata.data();
955 let item = lookup_item(id, data);
957 reader::tagged_docs(item, tag_item_trait_item).filter_map(|mth_id| {
958 let did = item_def_id(mth_id, cdata);
959 let mth = lookup_item(did.node, data);
961 if item_sort(mth) == Some('p') {
962 let trait_item = get_impl_or_trait_item(intr.clone(),
966 if let ty::MethodTraitItem(ref method) = trait_item {
967 Some((*method).clone())
977 pub fn get_associated_consts<'tcx>(intr: Rc<IdentInterner>,
980 tcx: &ty::ctxt<'tcx>)
981 -> Vec<Rc<ty::AssociatedConst<'tcx>>> {
982 let data = cdata.data();
983 let item = lookup_item(id, data);
985 [tag_item_trait_item, tag_item_impl_item].iter().flat_map(|&tag| {
986 reader::tagged_docs(item, tag).filter_map(|ac_id| {
987 let did = item_def_id(ac_id, cdata);
988 let ac_doc = lookup_item(did.node, data);
990 if item_sort(ac_doc) == Some('C') {
991 let trait_item = get_impl_or_trait_item(intr.clone(),
995 if let ty::ConstTraitItem(ref ac) = trait_item {
1007 pub fn get_type_name_if_impl(cdata: Cmd,
1008 node_id: ast::NodeId) -> Option<ast::Name> {
1009 let item = lookup_item(node_id, cdata.data());
1010 if item_family(item) != Impl {
1014 reader::tagged_docs(item, tag_item_impl_type_basename).nth(0).map(|doc| {
1015 token::intern(doc.as_str_slice())
1019 pub fn get_methods_if_impl(intr: Rc<IdentInterner>,
1021 node_id: ast::NodeId)
1022 -> Option<Vec<MethodInfo> > {
1023 let item = lookup_item(node_id, cdata.data());
1024 if item_family(item) != Impl {
1028 // If this impl implements a trait, don't consider it.
1029 if reader::tagged_docs(item, tag_item_trait_ref).next().is_some() {
1033 let impl_method_ids = reader::tagged_docs(item, tag_item_impl_item)
1034 .map(|impl_method_doc| item_def_id(impl_method_doc, cdata));
1036 let mut impl_methods = Vec::new();
1037 for impl_method_id in impl_method_ids {
1038 let impl_method_doc = lookup_item(impl_method_id.node, cdata.data());
1039 let family = item_family(impl_method_doc);
1041 StaticMethod | Method => {
1042 impl_methods.push(MethodInfo {
1043 name: item_name(&*intr, impl_method_doc),
1044 def_id: item_def_id(impl_method_doc, cdata),
1045 vis: item_visibility(impl_method_doc),
1052 return Some(impl_methods);
1055 /// If node_id is the constructor of a tuple struct, retrieve the NodeId of
1056 /// the actual type definition, otherwise, return None
1057 pub fn get_tuple_struct_definition_if_ctor(cdata: Cmd,
1058 node_id: ast::NodeId)
1059 -> Option<ast::DefId>
1061 let item = lookup_item(node_id, cdata.data());
1062 reader::tagged_docs(item, tag_items_data_item_is_tuple_struct_ctor).next().map(|_| {
1063 item_require_parent_item(cdata, item)
1067 pub fn get_item_attrs(cdata: Cmd,
1068 orig_node_id: ast::NodeId)
1069 -> Vec<ast::Attribute> {
1070 // The attributes for a tuple struct are attached to the definition, not the ctor;
1071 // we assume that someone passing in a tuple struct ctor is actually wanting to
1072 // look at the definition
1073 let node_id = get_tuple_struct_definition_if_ctor(cdata, orig_node_id);
1074 let node_id = node_id.map(|x| x.node).unwrap_or(orig_node_id);
1075 let item = lookup_item(node_id, cdata.data());
1076 get_attributes(item)
1079 pub fn get_struct_field_attrs(cdata: Cmd) -> HashMap<ast::NodeId, Vec<ast::Attribute>> {
1080 let data = rbml::Doc::new(cdata.data());
1081 let fields = reader::get_doc(data, tag_struct_fields);
1082 reader::tagged_docs(fields, tag_struct_field).map(|field| {
1083 let id = reader::doc_as_u32(reader::get_doc(field, tag_struct_field_id));
1084 let attrs = get_attributes(field);
1089 fn struct_field_family_to_visibility(family: Family) -> ast::Visibility {
1091 PublicField => ast::Public,
1092 InheritedField => ast::Inherited,
1097 pub fn get_struct_field_names(intr: &IdentInterner, cdata: Cmd, id: ast::NodeId)
1099 let data = cdata.data();
1100 let item = lookup_item(id, data);
1101 reader::tagged_docs(item, tag_item_field).map(|an_item| {
1102 item_name(intr, an_item)
1103 }).chain(reader::tagged_docs(item, tag_item_unnamed_field).map(|_| {
1104 special_idents::unnamed_field.name
1108 fn get_meta_items(md: rbml::Doc) -> Vec<P<ast::MetaItem>> {
1109 reader::tagged_docs(md, tag_meta_item_word).map(|meta_item_doc| {
1110 let nd = reader::get_doc(meta_item_doc, tag_meta_item_name);
1111 let n = token::intern_and_get_ident(nd.as_str_slice());
1112 attr::mk_word_item(n)
1113 }).chain(reader::tagged_docs(md, tag_meta_item_name_value).map(|meta_item_doc| {
1114 let nd = reader::get_doc(meta_item_doc, tag_meta_item_name);
1115 let vd = reader::get_doc(meta_item_doc, tag_meta_item_value);
1116 let n = token::intern_and_get_ident(nd.as_str_slice());
1117 let v = token::intern_and_get_ident(vd.as_str_slice());
1118 // FIXME (#623): Should be able to decode MetaNameValue variants,
1119 // but currently the encoder just drops them
1120 attr::mk_name_value_item_str(n, v)
1121 })).chain(reader::tagged_docs(md, tag_meta_item_list).map(|meta_item_doc| {
1122 let nd = reader::get_doc(meta_item_doc, tag_meta_item_name);
1123 let n = token::intern_and_get_ident(nd.as_str_slice());
1124 let subitems = get_meta_items(meta_item_doc);
1125 attr::mk_list_item(n, subitems)
1129 fn get_attributes(md: rbml::Doc) -> Vec<ast::Attribute> {
1130 match reader::maybe_get_doc(md, tag_attributes) {
1132 reader::tagged_docs(attrs_d, tag_attribute).map(|attr_doc| {
1133 let is_sugared_doc = reader::doc_as_u8(
1134 reader::get_doc(attr_doc, tag_attribute_is_sugared_doc)
1136 let meta_items = get_meta_items(attr_doc);
1137 // Currently it's only possible to have a single meta item on
1139 assert_eq!(meta_items.len(), 1);
1140 let meta_item = meta_items.into_iter().nth(0).unwrap();
1142 node: ast::Attribute_ {
1143 id: attr::mk_attr_id(),
1144 style: ast::AttrOuter,
1146 is_sugared_doc: is_sugared_doc,
1148 span: codemap::DUMMY_SP
1156 fn list_crate_attributes(md: rbml::Doc, hash: &Svh,
1157 out: &mut io::Write) -> io::Result<()> {
1158 try!(write!(out, "=Crate Attributes ({})=\n", *hash));
1160 let r = get_attributes(md);
1162 try!(write!(out, "{}\n", pprust::attribute_to_string(attr)));
1168 pub fn get_crate_attributes(data: &[u8]) -> Vec<ast::Attribute> {
1169 get_attributes(rbml::Doc::new(data))
1173 pub struct CrateDep {
1174 pub cnum: ast::CrateNum,
1177 pub explicitly_linked: bool,
1180 pub fn get_crate_deps(data: &[u8]) -> Vec<CrateDep> {
1181 let cratedoc = rbml::Doc::new(data);
1182 let depsdoc = reader::get_doc(cratedoc, tag_crate_deps);
1184 fn docstr(doc: rbml::Doc, tag_: usize) -> String {
1185 let d = reader::get_doc(doc, tag_);
1186 d.as_str_slice().to_string()
1189 reader::tagged_docs(depsdoc, tag_crate_dep).enumerate().map(|(crate_num, depdoc)| {
1190 let name = docstr(depdoc, tag_crate_dep_crate_name);
1191 let hash = Svh::new(&docstr(depdoc, tag_crate_dep_hash));
1192 let doc = reader::get_doc(depdoc, tag_crate_dep_explicitly_linked);
1193 let explicitly_linked = reader::doc_as_u8(doc) != 0;
1195 cnum: crate_num as u32 + 1,
1198 explicitly_linked: explicitly_linked,
1203 fn list_crate_deps(data: &[u8], out: &mut io::Write) -> io::Result<()> {
1204 try!(write!(out, "=External Dependencies=\n"));
1205 for dep in &get_crate_deps(data) {
1206 try!(write!(out, "{} {}-{}\n", dep.cnum, dep.name, dep.hash));
1208 try!(write!(out, "\n"));
1212 pub fn maybe_get_crate_hash(data: &[u8]) -> Option<Svh> {
1213 let cratedoc = rbml::Doc::new(data);
1214 reader::maybe_get_doc(cratedoc, tag_crate_hash).map(|doc| {
1215 Svh::new(doc.as_str_slice())
1219 pub fn get_crate_hash(data: &[u8]) -> Svh {
1220 let cratedoc = rbml::Doc::new(data);
1221 let hashdoc = reader::get_doc(cratedoc, tag_crate_hash);
1222 Svh::new(hashdoc.as_str_slice())
1225 pub fn maybe_get_crate_name(data: &[u8]) -> Option<String> {
1226 let cratedoc = rbml::Doc::new(data);
1227 reader::maybe_get_doc(cratedoc, tag_crate_crate_name).map(|doc| {
1228 doc.as_str_slice().to_string()
1232 pub fn get_crate_triple(data: &[u8]) -> Option<String> {
1233 let cratedoc = rbml::Doc::new(data);
1234 let triple_doc = reader::maybe_get_doc(cratedoc, tag_crate_triple);
1235 triple_doc.map(|s| s.as_str().to_string())
1238 pub fn get_crate_name(data: &[u8]) -> String {
1239 maybe_get_crate_name(data).expect("no crate name in crate")
1242 pub fn list_crate_metadata(bytes: &[u8], out: &mut io::Write) -> io::Result<()> {
1243 let hash = get_crate_hash(bytes);
1244 let md = rbml::Doc::new(bytes);
1245 try!(list_crate_attributes(md, &hash, out));
1246 list_crate_deps(bytes, out)
1249 // Translates a def_id from an external crate to a def_id for the current
1250 // compilation environment. We use this when trying to load types from
1251 // external crates - if those types further refer to types in other crates
1252 // then we must translate the crate number from that encoded in the external
1253 // crate to the correct local crate number.
1254 pub fn translate_def_id(cdata: Cmd, did: ast::DefId) -> ast::DefId {
1255 if did.krate == ast::LOCAL_CRATE {
1256 return ast::DefId { krate: cdata.cnum, node: did.node };
1259 match cdata.cnum_map.borrow().get(&did.krate) {
1266 None => panic!("didn't find a crate in the cnum_map")
1270 // Translate a DefId from the current compilation environment to a DefId
1271 // for an external crate.
1272 fn reverse_translate_def_id(cdata: Cmd, did: ast::DefId) -> Option<ast::DefId> {
1273 if did.krate == cdata.cnum {
1274 return Some(ast::DefId { krate: ast::LOCAL_CRATE, node: did.node });
1277 for (&local, &global) in cdata.cnum_map.borrow().iter() {
1278 if global == did.krate {
1279 return Some(ast::DefId { krate: local, node: did.node });
1286 pub fn each_inherent_implementation_for_type<F>(cdata: Cmd,
1289 where F: FnMut(ast::DefId),
1291 let item_doc = lookup_item(id, cdata.data());
1292 for impl_doc in reader::tagged_docs(item_doc, tag_items_data_item_inherent_impl) {
1293 if reader::maybe_get_doc(impl_doc, tag_item_trait_ref).is_none() {
1294 callback(item_def_id(impl_doc, cdata));
1299 pub fn each_implementation_for_trait<F>(cdata: Cmd,
1301 mut callback: F) where
1302 F: FnMut(ast::DefId),
1304 if cdata.cnum == def_id.krate {
1305 let item_doc = lookup_item(def_id.node, cdata.data());
1306 for impl_doc in reader::tagged_docs(item_doc, tag_items_data_item_extension_impl) {
1307 callback(item_def_id(impl_doc, cdata));
1312 // Do a reverse lookup beforehand to avoid touching the crate_num
1313 // hash map in the loop below.
1314 if let Some(crate_local_did) = reverse_translate_def_id(cdata, def_id) {
1315 let def_id_u64 = def_to_u64(crate_local_did);
1317 let impls_doc = reader::get_doc(rbml::Doc::new(cdata.data()), tag_impls);
1318 for impl_doc in reader::tagged_docs(impls_doc, tag_impls_impl) {
1319 let impl_trait = reader::get_doc(impl_doc, tag_impls_impl_trait_def_id);
1320 if reader::doc_as_u64(impl_trait) == def_id_u64 {
1321 callback(item_def_id(impl_doc, cdata));
1327 pub fn get_trait_of_item(cdata: Cmd, id: ast::NodeId, tcx: &ty::ctxt)
1328 -> Option<ast::DefId> {
1329 let item_doc = lookup_item(id, cdata.data());
1330 let parent_item_id = match item_parent_item(cdata, item_doc) {
1331 None => return None,
1332 Some(item_id) => item_id,
1334 let parent_item_doc = lookup_item(parent_item_id.node, cdata.data());
1335 match item_family(parent_item_doc) {
1336 Trait => Some(item_def_id(parent_item_doc, cdata)),
1337 Impl | DefaultImpl => {
1338 reader::maybe_get_doc(parent_item_doc, tag_item_trait_ref)
1339 .map(|_| item_trait_ref(parent_item_doc, tcx, cdata).def_id)
1346 pub fn get_native_libraries(cdata: Cmd)
1347 -> Vec<(cstore::NativeLibraryKind, String)> {
1348 let libraries = reader::get_doc(rbml::Doc::new(cdata.data()),
1349 tag_native_libraries);
1350 reader::tagged_docs(libraries, tag_native_libraries_lib).map(|lib_doc| {
1351 let kind_doc = reader::get_doc(lib_doc, tag_native_libraries_kind);
1352 let name_doc = reader::get_doc(lib_doc, tag_native_libraries_name);
1353 let kind: cstore::NativeLibraryKind =
1354 cstore::NativeLibraryKind::from_u32(reader::doc_as_u32(kind_doc)).unwrap();
1355 let name = name_doc.as_str().to_string();
1360 pub fn get_plugin_registrar_fn(data: &[u8]) -> Option<ast::NodeId> {
1361 reader::maybe_get_doc(rbml::Doc::new(data), tag_plugin_registrar_fn)
1362 .map(|doc| reader::doc_as_u32(doc))
1365 pub fn each_exported_macro<F>(data: &[u8], intr: &IdentInterner, mut f: F) where
1366 F: FnMut(ast::Name, Vec<ast::Attribute>, String) -> bool,
1368 let macros = reader::get_doc(rbml::Doc::new(data), tag_macro_defs);
1369 for macro_doc in reader::tagged_docs(macros, tag_macro_def) {
1370 let name = item_name(intr, macro_doc);
1371 let attrs = get_attributes(macro_doc);
1372 let body = reader::get_doc(macro_doc, tag_macro_def_body);
1373 if !f(name, attrs, body.as_str().to_string()) {
1379 pub fn get_dylib_dependency_formats(cdata: Cmd)
1380 -> Vec<(ast::CrateNum, cstore::LinkagePreference)>
1382 let formats = reader::get_doc(rbml::Doc::new(cdata.data()),
1383 tag_dylib_dependency_formats);
1384 let mut result = Vec::new();
1386 debug!("found dylib deps: {}", formats.as_str_slice());
1387 for spec in formats.as_str_slice().split(',') {
1388 if spec.is_empty() { continue }
1389 let cnum = spec.split(':').nth(0).unwrap();
1390 let link = spec.split(':').nth(1).unwrap();
1391 let cnum: ast::CrateNum = cnum.parse().unwrap();
1392 let cnum = match cdata.cnum_map.borrow().get(&cnum) {
1394 None => panic!("didn't find a crate in the cnum_map")
1396 result.push((cnum, if link == "d" {
1397 cstore::RequireDynamic
1399 cstore::RequireStatic
1405 pub fn get_missing_lang_items(cdata: Cmd)
1406 -> Vec<lang_items::LangItem>
1408 let items = reader::get_doc(rbml::Doc::new(cdata.data()), tag_lang_items);
1409 reader::tagged_docs(items, tag_lang_items_missing).map(|missing_docs| {
1410 lang_items::LangItem::from_u32(reader::doc_as_u32(missing_docs)).unwrap()
1414 pub fn get_method_arg_names(cdata: Cmd, id: ast::NodeId) -> Vec<String> {
1415 let method_doc = lookup_item(id, cdata.data());
1416 match reader::maybe_get_doc(method_doc, tag_method_argument_names) {
1418 reader::tagged_docs(args_doc, tag_method_argument_name).map(|name_doc| {
1419 name_doc.as_str_slice().to_string()
1426 pub fn get_reachable_ids(cdata: Cmd) -> Vec<ast::DefId> {
1427 let items = reader::get_doc(rbml::Doc::new(cdata.data()),
1429 reader::tagged_docs(items, tag_reachable_id).map(|doc| {
1432 node: reader::doc_as_u32(doc),
1437 pub fn is_typedef(cdata: Cmd, id: ast::NodeId) -> bool {
1438 let item_doc = lookup_item(id, cdata.data());
1439 match item_family(item_doc) {
1445 pub fn is_const_fn(cdata: Cmd, id: ast::NodeId) -> bool {
1446 let item_doc = lookup_item(id, cdata.data());
1447 match fn_constness(item_doc) {
1448 ast::Constness::Const => true,
1449 ast::Constness::NotConst => false,
1453 pub fn is_impl(cdata: Cmd, id: ast::NodeId) -> bool {
1454 let item_doc = lookup_item(id, cdata.data());
1455 match item_family(item_doc) {
1461 fn doc_generics<'tcx>(base_doc: rbml::Doc,
1462 tcx: &ty::ctxt<'tcx>,
1465 -> ty::Generics<'tcx>
1467 let doc = reader::get_doc(base_doc, tag);
1469 let mut types = subst::VecPerParamSpace::empty();
1470 for p in reader::tagged_docs(doc, tag_type_param_def) {
1471 let bd = parse_type_param_def_data(
1472 p.data, p.start, cdata.cnum, tcx,
1473 |_, did| translate_def_id(cdata, did));
1474 types.push(bd.space, bd);
1477 let mut regions = subst::VecPerParamSpace::empty();
1478 for rp_doc in reader::tagged_docs(doc, tag_region_param_def) {
1479 let ident_str_doc = reader::get_doc(rp_doc,
1480 tag_region_param_def_ident);
1481 let name = item_name(&*token::get_ident_interner(), ident_str_doc);
1482 let def_id_doc = reader::get_doc(rp_doc,
1483 tag_region_param_def_def_id);
1484 let def_id = translated_def_id(cdata, def_id_doc);
1486 let doc = reader::get_doc(rp_doc, tag_region_param_def_space);
1487 let space = subst::ParamSpace::from_uint(reader::doc_as_u64(doc) as usize);
1489 let doc = reader::get_doc(rp_doc, tag_region_param_def_index);
1490 let index = reader::doc_as_u64(doc) as u32;
1492 let bounds = reader::tagged_docs(rp_doc, tag_items_data_region).map(|p| {
1493 parse_region_data(p.data, cdata.cnum, p.start, tcx,
1494 |_, did| translate_def_id(cdata, did))
1497 regions.push(space, ty::RegionParameterDef { name: name,
1504 ty::Generics { types: types, regions: regions }
1507 fn doc_predicates<'tcx>(base_doc: rbml::Doc,
1508 tcx: &ty::ctxt<'tcx>,
1511 -> ty::GenericPredicates<'tcx>
1513 let doc = reader::get_doc(base_doc, tag);
1515 let mut predicates = subst::VecPerParamSpace::empty();
1516 for predicate_doc in reader::tagged_docs(doc, tag_predicate) {
1517 let space_doc = reader::get_doc(predicate_doc, tag_predicate_space);
1518 let space = subst::ParamSpace::from_uint(reader::doc_as_u8(space_doc) as usize);
1520 let data_doc = reader::get_doc(predicate_doc, tag_predicate_data);
1521 let data = parse_predicate_data(data_doc.data, data_doc.start, cdata.cnum, tcx,
1522 |_, did| translate_def_id(cdata, did));
1524 predicates.push(space, data);
1527 ty::GenericPredicates { predicates: predicates }
1530 pub fn is_defaulted_trait(cdata: Cmd, trait_id: ast::NodeId) -> bool {
1531 let trait_doc = lookup_item(trait_id, cdata.data());
1532 assert!(item_family(trait_doc) == Family::Trait);
1533 let defaulted_doc = reader::get_doc(trait_doc, tag_defaulted_trait);
1534 reader::doc_as_u8(defaulted_doc) != 0
1537 pub fn is_default_impl(cdata: Cmd, impl_id: ast::NodeId) -> bool {
1538 let impl_doc = lookup_item(impl_id, cdata.data());
1539 item_family(impl_doc) == Family::DefaultImpl
1542 pub fn get_imported_filemaps(metadata: &[u8]) -> Vec<codemap::FileMap> {
1543 let crate_doc = rbml::Doc::new(metadata);
1544 let cm_doc = reader::get_doc(crate_doc, tag_codemap);
1546 reader::tagged_docs(cm_doc, tag_codemap_filemap).map(|filemap_doc| {
1547 let mut decoder = reader::Decoder::new(filemap_doc);
1548 Decodable::decode(&mut decoder).unwrap()
1552 pub fn is_extern_fn(cdata: Cmd, id: ast::NodeId, tcx: &ty::ctxt) -> bool {
1553 let root_doc = rbml::Doc::new(cdata.data());
1554 let items = reader::get_doc(root_doc, tag_items);
1555 let item_doc = match maybe_find_item(id, items) {
1557 None => return false,
1559 if let Fn = item_family(item_doc) {
1560 let ty::TypeScheme { generics, ty } = get_type(cdata, id, tcx);
1561 generics.types.is_empty() && match ty.sty {
1562 ty::TyBareFn(_, fn_ty) => fn_ty.abi != abi::Rust,