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.
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 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
12 // file at the top-level directory of this distribution and at
13 // http://rust-lang.org/COPYRIGHT.
15 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
16 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
17 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
18 // option. This file may not be copied, modified, or distributed
19 // except according to those terms.
21 // the rustc crate store interface. This also includes types that
22 // are *mostly* used as a part of that interface, but these should
23 // probably get a better home if someone can find one.
26 use front::map as hir_map;
27 use middle::def::{self, Def};
28 use middle::lang_items;
29 use middle::ty::{self, Ty, TyCtxt, VariantKind};
30 use middle::def_id::{DefId, DefIndex};
32 use mir::mir_map::MirMap;
34 use session::search_paths::PathKind;
35 use util::nodemap::{FnvHashMap, NodeMap, NodeSet};
37 use std::cell::RefCell;
39 use std::path::PathBuf;
41 use syntax::ast_util::{IdVisitingOperation};
43 use syntax::codemap::Span;
45 use rustc_back::target::Target;
47 use rustc_front::intravisit::Visitor;
48 use rustc_front::util::IdVisitor;
50 pub use self::DefLike::{DlDef, DlField, DlImpl};
51 pub use self::NativeLibraryKind::{NativeStatic, NativeFramework, NativeUnknown};
53 // lonely orphan structs and enums looking for a better home
55 #[derive(Clone, Debug)]
57 pub crate_name: String,
61 // Where a crate came from on the local filesystem. One of these two options
63 #[derive(PartialEq, Clone, Debug)]
64 pub struct CrateSource {
65 pub dylib: Option<(PathBuf, PathKind)>,
66 pub rlib: Option<(PathBuf, PathKind)>,
67 pub cnum: ast::CrateNum,
70 #[derive(Copy, Debug, PartialEq, Clone)]
71 pub enum LinkagePreference {
77 #[derive(Copy, Clone, PartialEq)]
78 pub enum NativeLibraryKind {
79 NativeStatic, // native static library (.a archive)
80 NativeFramework, // OSX-specific
81 NativeUnknown, // default way to specify a dynamic library
85 // Something that a name can resolve to.
86 #[derive(Copy, Clone, Debug)]
93 /// The data we save and restore about an inlined item or method. This is not
94 /// part of the AST that we parse from a file, but it becomes part of the tree
96 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
97 pub enum InlinedItem {
99 TraitItem(DefId /* impl id */, P<hir::TraitItem>),
100 ImplItem(DefId /* impl id */, P<hir::ImplItem>),
101 Foreign(P<hir::ForeignItem>),
104 /// A borrowed version of `hir::InlinedItem`.
105 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
106 pub enum InlinedItemRef<'a> {
108 TraitItem(DefId, &'a hir::TraitItem),
109 ImplItem(DefId, &'a hir::ImplItem),
110 Foreign(&'a hir::ForeignItem)
113 /// Item definitions in the currently-compiled crate would have the CrateNum
114 /// LOCAL_CRATE in their DefId.
115 pub const LOCAL_CRATE: ast::CrateNum = 0;
117 pub struct ChildItem {
120 pub vis: hir::Visibility
123 pub enum FoundAst<'ast> {
124 Found(&'ast InlinedItem),
125 FoundParent(DefId, &'ast hir::Item),
129 /// A store of Rust crates, through with their metadata
132 /// The `: Any` bound is a temporary measure that allows access
133 /// to the backing `rustc_metadata::cstore::CStore` object. It
134 /// will be removed in the near future - if you need to access
135 /// internal APIs, please tell us.
136 pub trait CrateStore<'tcx> : Any {
138 fn stability(&self, def: DefId) -> Option<attr::Stability>;
139 fn deprecation(&self, def: DefId) -> Option<attr::Deprecation>;
140 fn visibility(&self, def: DefId) -> hir::Visibility;
141 fn closure_kind(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
143 fn closure_ty(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
144 -> ty::ClosureTy<'tcx>;
145 fn item_variances(&self, def: DefId) -> ty::ItemVariances;
146 fn repr_attrs(&self, def: DefId) -> Vec<attr::ReprAttr>;
147 fn item_type(&self, tcx: &TyCtxt<'tcx>, def: DefId)
148 -> ty::TypeScheme<'tcx>;
149 fn item_path(&self, def: DefId) -> Vec<hir_map::PathElem>;
150 fn extern_item_path(&self, def: DefId) -> Vec<hir_map::PathElem>;
151 fn item_name(&self, def: DefId) -> ast::Name;
152 fn item_predicates(&self, tcx: &TyCtxt<'tcx>, def: DefId)
153 -> ty::GenericPredicates<'tcx>;
154 fn item_super_predicates(&self, tcx: &TyCtxt<'tcx>, def: DefId)
155 -> ty::GenericPredicates<'tcx>;
156 fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute>;
157 fn item_symbol(&self, def: DefId) -> String;
158 fn trait_def(&self, tcx: &TyCtxt<'tcx>, def: DefId)-> ty::TraitDef<'tcx>;
159 fn adt_def(&self, tcx: &TyCtxt<'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>;
160 fn method_arg_names(&self, did: DefId) -> Vec<String>;
161 fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>;
164 fn implementations_of_trait(&self, def_id: DefId) -> Vec<DefId>;
165 fn provided_trait_methods(&self, tcx: &TyCtxt<'tcx>, def: DefId)
166 -> Vec<Rc<ty::Method<'tcx>>>;
167 fn trait_item_def_ids(&self, def: DefId)
168 -> Vec<ty::ImplOrTraitItemId>;
171 fn impl_items(&self, impl_def_id: DefId) -> Vec<ty::ImplOrTraitItemId>;
172 fn impl_trait_ref(&self, tcx: &TyCtxt<'tcx>, def: DefId)
173 -> Option<ty::TraitRef<'tcx>>;
174 fn impl_polarity(&self, def: DefId) -> Option<hir::ImplPolarity>;
175 fn custom_coerce_unsized_kind(&self, def: DefId)
176 -> Option<ty::adjustment::CustomCoerceUnsized>;
177 fn associated_consts(&self, tcx: &TyCtxt<'tcx>, def: DefId)
178 -> Vec<Rc<ty::AssociatedConst<'tcx>>>;
179 fn impl_parent(&self, impl_def_id: DefId) -> Option<DefId>;
181 // trait/impl-item info
182 fn trait_of_item(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
184 fn impl_or_trait_item(&self, tcx: &TyCtxt<'tcx>, def: DefId)
185 -> Option<ty::ImplOrTraitItem<'tcx>>;
188 fn is_const_fn(&self, did: DefId) -> bool;
189 fn is_defaulted_trait(&self, did: DefId) -> bool;
190 fn is_impl(&self, did: DefId) -> bool;
191 fn is_default_impl(&self, impl_did: DefId) -> bool;
192 fn is_extern_item(&self, tcx: &TyCtxt<'tcx>, did: DefId) -> bool;
193 fn is_static_method(&self, did: DefId) -> bool;
194 fn is_statically_included_foreign_item(&self, id: ast::NodeId) -> bool;
195 fn is_typedef(&self, did: DefId) -> bool;
198 fn dylib_dependency_formats(&self, cnum: ast::CrateNum)
199 -> Vec<(ast::CrateNum, LinkagePreference)>;
200 fn lang_items(&self, cnum: ast::CrateNum) -> Vec<(DefIndex, usize)>;
201 fn missing_lang_items(&self, cnum: ast::CrateNum) -> Vec<lang_items::LangItem>;
202 fn is_staged_api(&self, cnum: ast::CrateNum) -> bool;
203 fn is_explicitly_linked(&self, cnum: ast::CrateNum) -> bool;
204 fn is_allocator(&self, cnum: ast::CrateNum) -> bool;
205 fn crate_attrs(&self, cnum: ast::CrateNum) -> Vec<ast::Attribute>;
206 fn crate_name(&self, cnum: ast::CrateNum) -> String;
207 fn crate_hash(&self, cnum: ast::CrateNum) -> Svh;
208 fn crate_struct_field_attrs(&self, cnum: ast::CrateNum)
209 -> FnvHashMap<DefId, Vec<ast::Attribute>>;
210 fn plugin_registrar_fn(&self, cnum: ast::CrateNum) -> Option<DefId>;
211 fn native_libraries(&self, cnum: ast::CrateNum) -> Vec<(NativeLibraryKind, String)>;
212 fn reachable_ids(&self, cnum: ast::CrateNum) -> Vec<DefId>;
215 fn def_path(&self, def: DefId) -> hir_map::DefPath;
216 fn variant_kind(&self, def_id: DefId) -> Option<VariantKind>;
217 fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>;
218 fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>;
219 fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
220 fn item_children(&self, did: DefId) -> Vec<ChildItem>;
221 fn crate_top_level_items(&self, cnum: ast::CrateNum) -> Vec<ChildItem>;
224 fn maybe_get_item_ast(&'tcx self, tcx: &TyCtxt<'tcx>, def: DefId)
226 fn maybe_get_item_mir(&self, tcx: &TyCtxt<'tcx>, def: DefId)
227 -> Option<Mir<'tcx>>;
228 fn is_item_mir_available(&self, def: DefId) -> bool;
230 // This is basically a 1-based range of ints, which is a little
231 // silly - I may fix that.
232 fn crates(&self) -> Vec<ast::CrateNum>;
233 fn used_libraries(&self) -> Vec<(String, NativeLibraryKind)>;
234 fn used_link_args(&self) -> Vec<String>;
237 fn metadata_filename(&self) -> &str;
238 fn metadata_section_name(&self, target: &Target) -> &str;
239 fn encode_type(&self,
242 def_id_to_string: fn(&TyCtxt<'tcx>, DefId) -> String)
244 fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option<PathBuf>)>;
245 fn used_crate_source(&self, cnum: ast::CrateNum) -> CrateSource;
246 fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<ast::CrateNum>;
247 fn encode_metadata(&self,
249 reexports: &def::ExportMap,
250 item_symbols: &RefCell<NodeMap<String>>,
251 link_meta: &LinkMeta,
253 mir_map: &MirMap<'tcx>,
254 krate: &hir::Crate) -> Vec<u8>;
255 fn metadata_encoding_version(&self) -> &[u8];
259 pub fn visit<'ast,V>(&'ast self, visitor: &mut V)
260 where V: Visitor<'ast>
263 InlinedItem::Item(ref i) => visitor.visit_item(&i),
264 InlinedItem::Foreign(ref i) => visitor.visit_foreign_item(&i),
265 InlinedItem::TraitItem(_, ref ti) => visitor.visit_trait_item(ti),
266 InlinedItem::ImplItem(_, ref ii) => visitor.visit_impl_item(ii),
270 pub fn visit_ids<O: IdVisitingOperation>(&self, operation: &mut O) {
271 let mut id_visitor = IdVisitor::new(operation);
272 self.visit(&mut id_visitor);
276 // FIXME: find a better place for this?
277 pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option<Span>) {
278 let mut err_count = 0;
280 let mut say = |s: &str| {
282 (_, None) => panic!("{}", s),
283 (Some(sp), Some(sess)) => sess.span_err(sp, s),
284 (None, Some(sess)) => sess.err(s),
289 say("crate name must not be empty");
292 if c.is_alphanumeric() { continue }
293 if c == '_' { continue }
294 say(&format!("invalid character `{}` in crate name: `{}`", c, s));
299 sess.unwrap().abort_if_errors();
303 /// A dummy crate store that does not support any non-local crates,
304 /// for test purposes.
305 pub struct DummyCrateStore;
306 #[allow(unused_variables)]
307 impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
309 fn stability(&self, def: DefId) -> Option<attr::Stability> { unimplemented!() }
310 fn deprecation(&self, def: DefId) -> Option<attr::Deprecation> { unimplemented!() }
311 fn visibility(&self, def: DefId) -> hir::Visibility { unimplemented!() }
312 fn closure_kind(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
313 -> ty::ClosureKind { unimplemented!() }
314 fn closure_ty(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
315 -> ty::ClosureTy<'tcx> { unimplemented!() }
316 fn item_variances(&self, def: DefId) -> ty::ItemVariances { unimplemented!() }
317 fn repr_attrs(&self, def: DefId) -> Vec<attr::ReprAttr> { unimplemented!() }
318 fn item_type(&self, tcx: &TyCtxt<'tcx>, def: DefId)
319 -> ty::TypeScheme<'tcx> { unimplemented!() }
320 fn item_path(&self, def: DefId) -> Vec<hir_map::PathElem> { unimplemented!() }
321 fn extern_item_path(&self, def: DefId) -> Vec<hir_map::PathElem> { unimplemented!() }
322 fn item_name(&self, def: DefId) -> ast::Name { unimplemented!() }
323 fn item_predicates(&self, tcx: &TyCtxt<'tcx>, def: DefId)
324 -> ty::GenericPredicates<'tcx> { unimplemented!() }
325 fn item_super_predicates(&self, tcx: &TyCtxt<'tcx>, def: DefId)
326 -> ty::GenericPredicates<'tcx> { unimplemented!() }
327 fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute> { unimplemented!() }
328 fn item_symbol(&self, def: DefId) -> String { unimplemented!() }
329 fn trait_def(&self, tcx: &TyCtxt<'tcx>, def: DefId)-> ty::TraitDef<'tcx>
331 fn adt_def(&self, tcx: &TyCtxt<'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>
333 fn method_arg_names(&self, did: DefId) -> Vec<String> { unimplemented!() }
334 fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId> { vec![] }
337 fn implementations_of_trait(&self, def_id: DefId) -> Vec<DefId> { vec![] }
338 fn provided_trait_methods(&self, tcx: &TyCtxt<'tcx>, def: DefId)
339 -> Vec<Rc<ty::Method<'tcx>>> { unimplemented!() }
340 fn trait_item_def_ids(&self, def: DefId)
341 -> Vec<ty::ImplOrTraitItemId> { unimplemented!() }
344 fn impl_items(&self, impl_def_id: DefId) -> Vec<ty::ImplOrTraitItemId>
346 fn impl_trait_ref(&self, tcx: &TyCtxt<'tcx>, def: DefId)
347 -> Option<ty::TraitRef<'tcx>> { unimplemented!() }
348 fn impl_polarity(&self, def: DefId) -> Option<hir::ImplPolarity> { unimplemented!() }
349 fn custom_coerce_unsized_kind(&self, def: DefId)
350 -> Option<ty::adjustment::CustomCoerceUnsized>
352 fn associated_consts(&self, tcx: &TyCtxt<'tcx>, def: DefId)
353 -> Vec<Rc<ty::AssociatedConst<'tcx>>> { unimplemented!() }
354 fn impl_parent(&self, def: DefId) -> Option<DefId> { unimplemented!() }
356 // trait/impl-item info
357 fn trait_of_item(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
358 -> Option<DefId> { unimplemented!() }
359 fn impl_or_trait_item(&self, tcx: &TyCtxt<'tcx>, def: DefId)
360 -> Option<ty::ImplOrTraitItem<'tcx>> { unimplemented!() }
363 fn is_const_fn(&self, did: DefId) -> bool { unimplemented!() }
364 fn is_defaulted_trait(&self, did: DefId) -> bool { unimplemented!() }
365 fn is_impl(&self, did: DefId) -> bool { unimplemented!() }
366 fn is_default_impl(&self, impl_did: DefId) -> bool { unimplemented!() }
367 fn is_extern_item(&self, tcx: &TyCtxt<'tcx>, did: DefId) -> bool { unimplemented!() }
368 fn is_static_method(&self, did: DefId) -> bool { unimplemented!() }
369 fn is_statically_included_foreign_item(&self, id: ast::NodeId) -> bool { false }
370 fn is_typedef(&self, did: DefId) -> bool { unimplemented!() }
373 fn dylib_dependency_formats(&self, cnum: ast::CrateNum)
374 -> Vec<(ast::CrateNum, LinkagePreference)>
376 fn lang_items(&self, cnum: ast::CrateNum) -> Vec<(DefIndex, usize)>
378 fn missing_lang_items(&self, cnum: ast::CrateNum) -> Vec<lang_items::LangItem>
380 fn is_staged_api(&self, cnum: ast::CrateNum) -> bool { unimplemented!() }
381 fn is_explicitly_linked(&self, cnum: ast::CrateNum) -> bool { unimplemented!() }
382 fn is_allocator(&self, cnum: ast::CrateNum) -> bool { unimplemented!() }
383 fn crate_attrs(&self, cnum: ast::CrateNum) -> Vec<ast::Attribute>
385 fn crate_name(&self, cnum: ast::CrateNum) -> String { unimplemented!() }
386 fn crate_hash(&self, cnum: ast::CrateNum) -> Svh { unimplemented!() }
387 fn crate_struct_field_attrs(&self, cnum: ast::CrateNum)
388 -> FnvHashMap<DefId, Vec<ast::Attribute>>
390 fn plugin_registrar_fn(&self, cnum: ast::CrateNum) -> Option<DefId>
392 fn native_libraries(&self, cnum: ast::CrateNum) -> Vec<(NativeLibraryKind, String)>
394 fn reachable_ids(&self, cnum: ast::CrateNum) -> Vec<DefId> { unimplemented!() }
397 fn def_path(&self, def: DefId) -> hir_map::DefPath { unimplemented!() }
398 fn variant_kind(&self, def_id: DefId) -> Option<VariantKind> { unimplemented!() }
399 fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>
401 fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>
403 fn struct_field_names(&self, def: DefId) -> Vec<ast::Name> { unimplemented!() }
404 fn item_children(&self, did: DefId) -> Vec<ChildItem> { unimplemented!() }
405 fn crate_top_level_items(&self, cnum: ast::CrateNum) -> Vec<ChildItem>
409 fn maybe_get_item_ast(&'tcx self, tcx: &TyCtxt<'tcx>, def: DefId)
410 -> FoundAst<'tcx> { unimplemented!() }
411 fn maybe_get_item_mir(&self, tcx: &TyCtxt<'tcx>, def: DefId)
412 -> Option<Mir<'tcx>> { unimplemented!() }
413 fn is_item_mir_available(&self, def: DefId) -> bool {
417 // This is basically a 1-based range of ints, which is a little
418 // silly - I may fix that.
419 fn crates(&self) -> Vec<ast::CrateNum> { vec![] }
420 fn used_libraries(&self) -> Vec<(String, NativeLibraryKind)> { vec![] }
421 fn used_link_args(&self) -> Vec<String> { vec![] }
424 fn metadata_filename(&self) -> &str { unimplemented!() }
425 fn metadata_section_name(&self, target: &Target) -> &str { unimplemented!() }
426 fn encode_type(&self,
429 def_id_to_string: fn(&TyCtxt<'tcx>, DefId) -> String)
433 fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option<PathBuf>)>
435 fn used_crate_source(&self, cnum: ast::CrateNum) -> CrateSource { unimplemented!() }
436 fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<ast::CrateNum> { None }
437 fn encode_metadata(&self,
439 reexports: &def::ExportMap,
440 item_symbols: &RefCell<NodeMap<String>>,
441 link_meta: &LinkMeta,
443 mir_map: &MirMap<'tcx>,
444 krate: &hir::Crate) -> Vec<u8> { vec![] }
445 fn metadata_encoding_version(&self) -> &[u8] { unimplemented!() }
449 /// Metadata encoding and decoding can make use of thread-local encoding and
450 /// decoding contexts. These allow implementers of serialize::Encodable and
451 /// Decodable to access information and datastructures that would otherwise not
452 /// be available to them. For example, we can automatically translate def-id and
453 /// span information during decoding because the decoding context knows which
454 /// crate the data is decoded from. Or it allows to make ty::Ty decodable
455 /// because the context has access to the TyCtxt that is needed for creating
456 /// ty::Ty instances.
458 /// Note, however, that this only works for RBML-based encoding and decoding at
461 use rbml::opaque::Encoder as OpaqueEncoder;
462 use rbml::opaque::Decoder as OpaqueDecoder;
466 use middle::ty::{self, Ty, TyCtxt};
467 use middle::subst::Substs;
468 use middle::def_id::DefId;
470 pub trait EncodingContext<'tcx> {
471 fn tcx<'a>(&'a self) -> &'a TyCtxt<'tcx>;
472 fn encode_ty(&self, encoder: &mut OpaqueEncoder, t: Ty<'tcx>);
473 fn encode_substs(&self, encoder: &mut OpaqueEncoder, substs: &Substs<'tcx>);
476 /// Marker type used for the TLS slot.
477 /// The type context cannot be used directly because the TLS
478 /// in libstd doesn't allow types generic over lifetimes.
482 static TLS_ENCODING: Cell<Option<*const TlsPayload>> = Cell::new(None)
485 /// Execute f after pushing the given EncodingContext onto the TLS stack.
486 pub fn enter_encoding_context<'tcx, F, R>(ecx: &EncodingContext<'tcx>,
487 encoder: &mut OpaqueEncoder,
489 where F: FnOnce(&EncodingContext<'tcx>, &mut OpaqueEncoder) -> R
491 let tls_payload = (ecx as *const _, encoder as *mut _);
492 let tls_ptr = &tls_payload as *const _ as *const TlsPayload;
493 TLS_ENCODING.with(|tls| {
494 let prev = tls.get();
495 tls.set(Some(tls_ptr));
496 let ret = f(ecx, encoder);
502 /// Execute f with access to the thread-local encoding context and
503 /// rbml encoder. This function will panic if the encoder passed in and the
504 /// context encoder are not the same.
506 /// Note that this method is 'practically' safe due to its checking that the
507 /// encoder passed in is the same as the one in TLS, but it would still be
508 /// possible to construct cases where the EncodingContext is exchanged
509 /// while the same encoder is used, thus working with a wrong context.
510 pub fn with_encoding_context<'tcx, E, F, R>(encoder: &mut E, f: F) -> R
511 where F: FnOnce(&EncodingContext<'tcx>, &mut OpaqueEncoder) -> R,
512 E: serialize::Encoder
515 unsafe_with_encoding_context(|ecx, tls_encoder| {
516 assert!(encoder as *mut _ as usize == tls_encoder as *mut _ as usize);
518 let ecx: &EncodingContext<'tcx> = mem::transmute(ecx);
525 /// Execute f with access to the thread-local encoding context and
527 pub unsafe fn unsafe_with_encoding_context<F, R>(f: F) -> R
528 where F: FnOnce(&EncodingContext, &mut OpaqueEncoder) -> R
530 TLS_ENCODING.with(|tls| {
531 let tls = tls.get().unwrap();
532 let tls_payload = tls as *mut (&EncodingContext, &mut OpaqueEncoder);
533 f((*tls_payload).0, (*tls_payload).1)
537 pub trait DecodingContext<'tcx> {
538 fn tcx<'a>(&'a self) -> &'a TyCtxt<'tcx>;
539 fn decode_ty(&self, decoder: &mut OpaqueDecoder) -> ty::Ty<'tcx>;
540 fn decode_substs(&self, decoder: &mut OpaqueDecoder) -> Substs<'tcx>;
541 fn translate_def_id(&self, def_id: DefId) -> DefId;
545 static TLS_DECODING: Cell<Option<*const TlsPayload>> = Cell::new(None)
548 /// Execute f after pushing the given DecodingContext onto the TLS stack.
549 pub fn enter_decoding_context<'tcx, F, R>(dcx: &DecodingContext<'tcx>,
550 decoder: &mut OpaqueDecoder,
552 where F: FnOnce(&DecodingContext<'tcx>, &mut OpaqueDecoder) -> R
554 let tls_payload = (dcx as *const _, decoder as *mut _);
555 let tls_ptr = &tls_payload as *const _ as *const TlsPayload;
556 TLS_DECODING.with(|tls| {
557 let prev = tls.get();
558 tls.set(Some(tls_ptr));
559 let ret = f(dcx, decoder);
565 /// Execute f with access to the thread-local decoding context and
566 /// rbml decoder. This function will panic if the decoder passed in and the
567 /// context decoder are not the same.
569 /// Note that this method is 'practically' safe due to its checking that the
570 /// decoder passed in is the same as the one in TLS, but it would still be
571 /// possible to construct cases where the DecodingContext is exchanged
572 /// while the same decoder is used, thus working with a wrong context.
573 pub fn with_decoding_context<'decoder, 'tcx, D, F, R>(d: &'decoder mut D, f: F) -> R
574 where D: serialize::Decoder,
575 F: FnOnce(&DecodingContext<'tcx>,
576 &mut OpaqueDecoder) -> R,
580 unsafe_with_decoding_context(|dcx, decoder| {
581 assert!((d as *mut _ as usize) == (decoder as *mut _ as usize));
583 let dcx: &DecodingContext<'tcx> = mem::transmute(dcx);
590 /// Execute f with access to the thread-local decoding context and
592 pub unsafe fn unsafe_with_decoding_context<F, R>(f: F) -> R
593 where F: FnOnce(&DecodingContext, &mut OpaqueDecoder) -> R
595 TLS_DECODING.with(|tls| {
596 let tls = tls.get().unwrap();
597 let tls_payload = tls as *mut (&DecodingContext, &mut OpaqueDecoder);
598 f((*tls_payload).0, (*tls_payload).1)