|
| 1 | +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use front::map as ast_map; |
| 12 | +use metadata::cstore; |
| 13 | +use metadata::decoder; |
| 14 | +use middle::astencode; |
| 15 | +use middle::lang_items; |
| 16 | +use middle::ty; |
| 17 | +use middle::def_id::{DefId, DefIndex}; |
| 18 | + |
| 19 | +use std::rc::Rc; |
| 20 | +use syntax::ast; |
| 21 | +use syntax::attr; |
| 22 | +use rustc_front::hir; |
| 23 | + |
| 24 | +pub use metadata::csearch::FoundAst; |
| 25 | +pub use metadata::cstore::LinkagePreference; |
| 26 | +pub use metadata::decoder::DecodeInlinedItem; |
| 27 | +pub use metadata::inline::InlinedItem; |
| 28 | + |
| 29 | +pub trait CrateStore<'tcx> { |
| 30 | + // item info |
| 31 | + fn stability(&self, def: DefId) -> Option<attr::Stability>; |
| 32 | + fn closure_kind(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId) |
| 33 | + -> ty::ClosureKind; |
| 34 | + fn closure_ty(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId) |
| 35 | + -> ty::ClosureTy<'tcx>; |
| 36 | + fn item_variances(&self, def: DefId) -> ty::ItemVariances; |
| 37 | + fn repr_attrs(&self, def: DefId) -> Vec<attr::ReprAttr>; |
| 38 | + fn item_type(&self, tcx: &ty::ctxt<'tcx>, def: DefId) |
| 39 | + -> ty::TypeScheme<'tcx>; |
| 40 | + fn item_path(&self, def: DefId) -> Vec<ast_map::PathElem>; |
| 41 | + fn item_name(&self, def: DefId) -> ast::Name; |
| 42 | + fn item_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId) |
| 43 | + -> ty::GenericPredicates<'tcx>; |
| 44 | + fn item_super_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId) |
| 45 | + -> ty::GenericPredicates<'tcx>; |
| 46 | + fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute>; |
| 47 | + fn trait_def(&self, tcx: &ty::ctxt<'tcx>, def: DefId)-> ty::TraitDef<'tcx>; |
| 48 | + fn adt_def(&self, tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>; |
| 49 | + fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>; |
| 50 | + |
| 51 | + // trait info |
| 52 | + fn implementations_of_trait(&self, def_id: DefId) -> Vec<DefId>; |
| 53 | + fn provided_trait_methods(&self, tcx: &ty::ctxt<'tcx>, def: DefId) |
| 54 | + -> Vec<Rc<ty::Method<'tcx>>>; |
| 55 | + fn trait_item_def_ids(&self, def: DefId) |
| 56 | + -> Vec<ty::ImplOrTraitItemId>; |
| 57 | + |
| 58 | + // impl info |
| 59 | + fn impl_items(&self, impl_def_id: DefId) -> Vec<ty::ImplOrTraitItemId>; |
| 60 | + fn impl_trait_ref(&self, tcx: &ty::ctxt<'tcx>, def: DefId) |
| 61 | + -> Option<ty::TraitRef<'tcx>>; |
| 62 | + fn impl_polarity(&self, def: DefId) -> Option<hir::ImplPolarity>; |
| 63 | + fn custom_coerce_unsized_kind(&self, def: DefId) |
| 64 | + -> Option<ty::adjustment::CustomCoerceUnsized>; |
| 65 | + fn associated_consts(&self, tcx: &ty::ctxt<'tcx>, def: DefId) |
| 66 | + -> Vec<Rc<ty::AssociatedConst<'tcx>>>; |
| 67 | + |
| 68 | + // trait/impl-item info |
| 69 | + fn trait_of_item(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId) |
| 70 | + -> Option<DefId>; |
| 71 | + fn impl_or_trait_item(&self, tcx: &ty::ctxt<'tcx>, def: DefId) |
| 72 | + -> ty::ImplOrTraitItem<'tcx>; |
| 73 | + |
| 74 | + // flags |
| 75 | + fn is_const_fn(&self, did: DefId) -> bool; |
| 76 | + fn is_defaulted_trait(&self, did: DefId) -> bool; |
| 77 | + fn is_impl(&self, did: DefId) -> bool; |
| 78 | + |
| 79 | + // metadata |
| 80 | + fn dylib_dependency_formats(&self, cnum: ast::CrateNum) |
| 81 | + -> Vec<(ast::CrateNum, cstore::LinkagePreference)>; |
| 82 | + fn lang_items(&self, cnum: ast::CrateNum) -> Vec<(DefIndex, usize)>; |
| 83 | + fn missing_lang_items(&self, cnum: ast::CrateNum) |
| 84 | + -> Vec<lang_items::LangItem>; |
| 85 | + fn is_staged_api(&self, cnum: ast::CrateNum) -> bool; |
| 86 | + |
| 87 | + // misc. |
| 88 | + fn def_path(&self, def: DefId) -> ast_map::DefPath; |
| 89 | + fn maybe_get_item_ast(&'tcx self, tcx: &ty::ctxt<'tcx>, def: DefId) |
| 90 | + -> FoundAst<'tcx>; |
| 91 | +} |
| 92 | + |
| 93 | +impl<'tcx> CrateStore<'tcx> for cstore::CStore { |
| 94 | + fn stability(&self, def: DefId) -> Option<attr::Stability> |
| 95 | + { |
| 96 | + let cdata = self.get_crate_data(def.krate); |
| 97 | + decoder::get_stability(&*cdata, def.index) |
| 98 | + } |
| 99 | + |
| 100 | + fn closure_kind(&self, _tcx: &ty::ctxt<'tcx>, def_id: DefId) -> ty::ClosureKind |
| 101 | + { |
| 102 | + assert!(!def_id.is_local()); |
| 103 | + let cdata = self.get_crate_data(def_id.krate); |
| 104 | + decoder::closure_kind(&*cdata, def_id.index) |
| 105 | + } |
| 106 | + |
| 107 | + fn closure_ty(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId) -> ty::ClosureTy<'tcx> |
| 108 | + { |
| 109 | + assert!(!def_id.is_local()); |
| 110 | + let cdata = self.get_crate_data(def_id.krate); |
| 111 | + decoder::closure_ty(&*cdata, def_id.index, tcx) |
| 112 | + } |
| 113 | + |
| 114 | + fn item_variances(&self, def: DefId) -> ty::ItemVariances { |
| 115 | + let cdata = self.get_crate_data(def.krate); |
| 116 | + decoder::get_item_variances(&*cdata, def.index) |
| 117 | + } |
| 118 | + |
| 119 | + fn repr_attrs(&self, def: DefId) -> Vec<attr::ReprAttr> { |
| 120 | + let cdata = self.get_crate_data(def.krate); |
| 121 | + decoder::get_repr_attrs(&*cdata, def.index) |
| 122 | + } |
| 123 | + |
| 124 | + fn item_type(&self, tcx: &ty::ctxt<'tcx>, def: DefId) |
| 125 | + -> ty::TypeScheme<'tcx> |
| 126 | + { |
| 127 | + let cdata = self.get_crate_data(def.krate); |
| 128 | + decoder::get_type(&*cdata, def.index, tcx) |
| 129 | + } |
| 130 | + |
| 131 | + fn item_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId) |
| 132 | + -> ty::GenericPredicates<'tcx> |
| 133 | + { |
| 134 | + let cdata = self.get_crate_data(def.krate); |
| 135 | + decoder::get_predicates(&*cdata, def.index, tcx) |
| 136 | + } |
| 137 | + |
| 138 | + fn item_super_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId) |
| 139 | + -> ty::GenericPredicates<'tcx> |
| 140 | + { |
| 141 | + let cdata = self.get_crate_data(def.krate); |
| 142 | + decoder::get_super_predicates(&*cdata, def.index, tcx) |
| 143 | + } |
| 144 | + |
| 145 | + fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute> |
| 146 | + { |
| 147 | + let cdata = self.get_crate_data(def_id.krate); |
| 148 | + decoder::get_item_attrs(&*cdata, def_id.index) |
| 149 | + } |
| 150 | + |
| 151 | + fn trait_def(&self, tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::TraitDef<'tcx> |
| 152 | + { |
| 153 | + let cdata = self.get_crate_data(def.krate); |
| 154 | + decoder::get_trait_def(&*cdata, def.index, tcx) |
| 155 | + } |
| 156 | + |
| 157 | + fn adt_def(&self, tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx> |
| 158 | + { |
| 159 | + let cdata = self.get_crate_data(def.krate); |
| 160 | + decoder::get_adt_def(&self.intr, &*cdata, def.index, tcx) |
| 161 | + } |
| 162 | + |
| 163 | + fn item_path(&self, def: DefId) -> Vec<ast_map::PathElem> { |
| 164 | + let cdata = self.get_crate_data(def.krate); |
| 165 | + let path = decoder::get_item_path(&*cdata, def.index); |
| 166 | + |
| 167 | + cdata.with_local_path(|cpath| { |
| 168 | + let mut r = Vec::with_capacity(cpath.len() + path.len()); |
| 169 | + r.push_all(cpath); |
| 170 | + r.push_all(&path); |
| 171 | + r |
| 172 | + }) |
| 173 | + } |
| 174 | + |
| 175 | + fn item_name(&self, def: DefId) -> ast::Name { |
| 176 | + let cdata = self.get_crate_data(def.krate); |
| 177 | + decoder::get_item_name(&self.intr, &cdata, def.index) |
| 178 | + } |
| 179 | + |
| 180 | + |
| 181 | + fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId> |
| 182 | + { |
| 183 | + let mut result = vec![]; |
| 184 | + let cdata = self.get_crate_data(def_id.krate); |
| 185 | + decoder::each_inherent_implementation_for_type(&*cdata, def_id.index, |
| 186 | + |iid| result.push(iid)); |
| 187 | + result |
| 188 | + } |
| 189 | + |
| 190 | + fn implementations_of_trait(&self, def_id: DefId) -> Vec<DefId> |
| 191 | + { |
| 192 | + let mut result = vec![]; |
| 193 | + self.iter_crate_data(|_, cdata| { |
| 194 | + decoder::each_implementation_for_trait(cdata, def_id, &mut |iid| { |
| 195 | + result.push(iid) |
| 196 | + }) |
| 197 | + }); |
| 198 | + result |
| 199 | + } |
| 200 | + |
| 201 | + fn provided_trait_methods(&self, tcx: &ty::ctxt<'tcx>, def: DefId) |
| 202 | + -> Vec<Rc<ty::Method<'tcx>>> |
| 203 | + { |
| 204 | + let cdata = self.get_crate_data(def.krate); |
| 205 | + decoder::get_provided_trait_methods(self.intr.clone(), &*cdata, def.index, tcx) |
| 206 | + } |
| 207 | + |
| 208 | + fn trait_item_def_ids(&self, def: DefId) |
| 209 | + -> Vec<ty::ImplOrTraitItemId> |
| 210 | + { |
| 211 | + let cdata = self.get_crate_data(def.krate); |
| 212 | + decoder::get_trait_item_def_ids(&*cdata, def.index) |
| 213 | + } |
| 214 | + |
| 215 | + fn impl_items(&self, impl_def_id: DefId) -> Vec<ty::ImplOrTraitItemId> |
| 216 | + { |
| 217 | + let cdata = self.get_crate_data(impl_def_id.krate); |
| 218 | + decoder::get_impl_items(&*cdata, impl_def_id.index) |
| 219 | + } |
| 220 | + |
| 221 | + fn impl_polarity(&self, def: DefId) -> Option<hir::ImplPolarity> |
| 222 | + { |
| 223 | + let cdata = self.get_crate_data(def.krate); |
| 224 | + decoder::get_impl_polarity(&*cdata, def.index) |
| 225 | + } |
| 226 | + |
| 227 | + fn impl_trait_ref(&self, tcx: &ty::ctxt<'tcx>, def: DefId) |
| 228 | + -> Option<ty::TraitRef<'tcx>> |
| 229 | + { |
| 230 | + let cdata = self.get_crate_data(def.krate); |
| 231 | + decoder::get_impl_trait(&*cdata, def.index, tcx) |
| 232 | + } |
| 233 | + |
| 234 | + fn custom_coerce_unsized_kind(&self, def: DefId) |
| 235 | + -> Option<ty::adjustment::CustomCoerceUnsized> |
| 236 | + { |
| 237 | + let cdata = self.get_crate_data(def.krate); |
| 238 | + decoder::get_custom_coerce_unsized_kind(&*cdata, def.index) |
| 239 | + } |
| 240 | + |
| 241 | + // FIXME: killme |
| 242 | + fn associated_consts(&self, tcx: &ty::ctxt<'tcx>, def: DefId) |
| 243 | + -> Vec<Rc<ty::AssociatedConst<'tcx>>> { |
| 244 | + let cdata = self.get_crate_data(def.krate); |
| 245 | + decoder::get_associated_consts(self.intr.clone(), &*cdata, def.index, tcx) |
| 246 | + } |
| 247 | + |
| 248 | + fn trait_of_item(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId) -> Option<DefId> |
| 249 | + { |
| 250 | + let cdata = self.get_crate_data(def_id.krate); |
| 251 | + decoder::get_trait_of_item(&*cdata, def_id.index, tcx) |
| 252 | + } |
| 253 | + |
| 254 | + fn impl_or_trait_item(&self, tcx: &ty::ctxt<'tcx>, def: DefId) |
| 255 | + -> ty::ImplOrTraitItem<'tcx> |
| 256 | + { |
| 257 | + let cdata = self.get_crate_data(def.krate); |
| 258 | + decoder::get_impl_or_trait_item( |
| 259 | + self.intr.clone(), |
| 260 | + &*cdata, |
| 261 | + def.index, |
| 262 | + tcx) |
| 263 | + } |
| 264 | + |
| 265 | + fn is_const_fn(&self, did: DefId) -> bool |
| 266 | + { |
| 267 | + let cdata = self.get_crate_data(did.krate); |
| 268 | + decoder::is_const_fn(&cdata, did.index) |
| 269 | + } |
| 270 | + |
| 271 | + fn is_defaulted_trait(&self, trait_def_id: DefId) -> bool { |
| 272 | + let cdata = self.get_crate_data(trait_def_id.krate); |
| 273 | + decoder::is_defaulted_trait(&*cdata, trait_def_id.index) |
| 274 | + } |
| 275 | + |
| 276 | + fn is_impl(&self, did: DefId) -> bool { |
| 277 | + let cdata = self.get_crate_data(did.krate); |
| 278 | + decoder::is_impl(&*cdata, did.index) |
| 279 | + } |
| 280 | + |
| 281 | + fn dylib_dependency_formats(&self, cnum: ast::CrateNum) |
| 282 | + -> Vec<(ast::CrateNum, cstore::LinkagePreference)> |
| 283 | + { |
| 284 | + let cdata = self.get_crate_data(cnum); |
| 285 | + decoder::get_dylib_dependency_formats(&cdata) |
| 286 | + } |
| 287 | + |
| 288 | + fn lang_items(&self, cnum: ast::CrateNum) -> Vec<(DefIndex, usize)> |
| 289 | + { |
| 290 | + let mut result = vec![]; |
| 291 | + let crate_data = self.get_crate_data(cnum); |
| 292 | + decoder::each_lang_item(&*crate_data, |did, lid| { |
| 293 | + result.push((did, lid)); true |
| 294 | + }); |
| 295 | + result |
| 296 | + } |
| 297 | + |
| 298 | + fn missing_lang_items(&self, cnum: ast::CrateNum) |
| 299 | + -> Vec<lang_items::LangItem> |
| 300 | + { |
| 301 | + let cdata = self.get_crate_data(cnum); |
| 302 | + decoder::get_missing_lang_items(&*cdata) |
| 303 | + } |
| 304 | + |
| 305 | + fn is_staged_api(&self, cnum: ast::CrateNum) -> bool |
| 306 | + { |
| 307 | + self.get_crate_data(cnum).staged_api |
| 308 | + } |
| 309 | + |
| 310 | + fn def_path(&self, def: DefId) -> ast_map::DefPath { |
| 311 | + let cdata = self.get_crate_data(def.krate); |
| 312 | + let path = decoder::def_path(&*cdata, def.index); |
| 313 | + let local_path = cdata.local_def_path(); |
| 314 | + local_path.into_iter().chain(path).collect() |
| 315 | + } |
| 316 | + |
| 317 | + fn maybe_get_item_ast(&'tcx self, tcx: &ty::ctxt<'tcx>, def: DefId) |
| 318 | + -> FoundAst<'tcx> |
| 319 | + { |
| 320 | + let cdata = self.get_crate_data(def.krate); |
| 321 | + let decode_inlined_item = Box::new(astencode::decode_inlined_item); |
| 322 | + decoder::maybe_get_item_ast(&*cdata, tcx, def.index, decode_inlined_item) |
| 323 | + } |
| 324 | +} |
0 commit comments