]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/cstore.rs
CrateStore: Allow for custom def_id_to_string mappings in encode_type().
[rust.git] / src / librustc / middle / cstore.rs
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 // 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.
14 //
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.
20
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.
24
25 use back::svh::Svh;
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};
31 use mir::repr::Mir;
32 use mir::mir_map::MirMap;
33 use session::Session;
34 use session::search_paths::PathKind;
35 use util::nodemap::{FnvHashMap, NodeMap, NodeSet};
36 use std::any::Any;
37 use std::cell::RefCell;
38 use std::rc::Rc;
39 use std::path::PathBuf;
40 use syntax::ast;
41 use syntax::ast_util::{IdVisitingOperation};
42 use syntax::attr;
43 use syntax::codemap::Span;
44 use syntax::ptr::P;
45 use rustc_back::target::Target;
46 use rustc_front::hir;
47 use rustc_front::intravisit::Visitor;
48 use rustc_front::util::IdVisitor;
49
50 pub use self::DefLike::{DlDef, DlField, DlImpl};
51 pub use self::NativeLibraryKind::{NativeStatic, NativeFramework, NativeUnknown};
52
53 // lonely orphan structs and enums looking for a better home
54
55 #[derive(Clone, Debug)]
56 pub struct LinkMeta {
57     pub crate_name: String,
58     pub crate_hash: Svh,
59 }
60
61 // Where a crate came from on the local filesystem. One of these two options
62 // must be non-None.
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,
68 }
69
70 #[derive(Copy, Debug, PartialEq, Clone)]
71 pub enum LinkagePreference {
72     RequireDynamic,
73     RequireStatic,
74 }
75
76 enum_from_u32! {
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
82     }
83 }
84
85 // Something that a name can resolve to.
86 #[derive(Copy, Clone, Debug)]
87 pub enum DefLike {
88     DlDef(Def),
89     DlImpl(DefId),
90     DlField
91 }
92
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
95 /// that we trans.
96 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
97 pub enum InlinedItem {
98     Item(P<hir::Item>),
99     TraitItem(DefId /* impl id */, P<hir::TraitItem>),
100     ImplItem(DefId /* impl id */, P<hir::ImplItem>),
101     Foreign(P<hir::ForeignItem>),
102 }
103
104 /// A borrowed version of `hir::InlinedItem`.
105 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
106 pub enum InlinedItemRef<'a> {
107     Item(&'a hir::Item),
108     TraitItem(DefId, &'a hir::TraitItem),
109     ImplItem(DefId, &'a hir::ImplItem),
110     Foreign(&'a hir::ForeignItem)
111 }
112
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;
116
117 pub struct ChildItem {
118     pub def: DefLike,
119     pub name: ast::Name,
120     pub vis: hir::Visibility
121 }
122
123 pub enum FoundAst<'ast> {
124     Found(&'ast InlinedItem),
125     FoundParent(DefId, &'ast hir::Item),
126     NotFound,
127 }
128
129 /// A store of Rust crates, through with their metadata
130 /// can be accessed.
131 ///
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 {
137     // item info
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)
142                     -> ty::ClosureKind;
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>;
162
163     // trait info
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>;
169
170     // impl info
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>;
180
181     // trait/impl-item info
182     fn trait_of_item(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
183                      -> Option<DefId>;
184     fn impl_or_trait_item(&self, tcx: &TyCtxt<'tcx>, def: DefId)
185                           -> Option<ty::ImplOrTraitItem<'tcx>>;
186
187     // flags
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;
196
197     // crate metadata
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>;
213
214     // resolve
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>;
222
223     // misc. metadata
224     fn maybe_get_item_ast(&'tcx self, tcx: &TyCtxt<'tcx>, def: DefId)
225                           -> FoundAst<'tcx>;
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;
229
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>;
235
236     // utility functions
237     fn metadata_filename(&self) -> &str;
238     fn metadata_section_name(&self, target: &Target) -> &str;
239     fn encode_type(&self,
240                    tcx: &TyCtxt<'tcx>,
241                    ty: Ty<'tcx>,
242                    def_id_to_string: fn(&TyCtxt<'tcx>, DefId) -> String)
243                    -> Vec<u8>;
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,
248                        tcx: &TyCtxt<'tcx>,
249                        reexports: &def::ExportMap,
250                        item_symbols: &RefCell<NodeMap<String>>,
251                        link_meta: &LinkMeta,
252                        reachable: &NodeSet,
253                        mir_map: &MirMap<'tcx>,
254                        krate: &hir::Crate) -> Vec<u8>;
255     fn metadata_encoding_version(&self) -> &[u8];
256 }
257
258 impl InlinedItem {
259     pub fn visit<'ast,V>(&'ast self, visitor: &mut V)
260         where V: Visitor<'ast>
261     {
262         match *self {
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),
267         }
268     }
269
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);
273     }
274 }
275
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;
279     {
280         let mut say = |s: &str| {
281             match (sp, sess) {
282                 (_, None) => panic!("{}", s),
283                 (Some(sp), Some(sess)) => sess.span_err(sp, s),
284                 (None, Some(sess)) => sess.err(s),
285             }
286             err_count += 1;
287         };
288         if s.is_empty() {
289             say("crate name must not be empty");
290         }
291         for c in s.chars() {
292             if c.is_alphanumeric() { continue }
293             if c == '_'  { continue }
294             say(&format!("invalid character `{}` in crate name: `{}`", c, s));
295         }
296     }
297
298     if err_count > 0 {
299         sess.unwrap().abort_if_errors();
300     }
301 }
302
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 {
308     // item info
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>
330         { unimplemented!() }
331     fn adt_def(&self, tcx: &TyCtxt<'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>
332         { unimplemented!() }
333     fn method_arg_names(&self, did: DefId) -> Vec<String> { unimplemented!() }
334     fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId> { vec![] }
335
336     // trait info
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!() }
342
343     // impl info
344     fn impl_items(&self, impl_def_id: DefId) -> Vec<ty::ImplOrTraitItemId>
345         { unimplemented!() }
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>
351         { unimplemented!() }
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!() }
355
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!() }
361
362     // flags
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!() }
371
372     // crate metadata
373     fn dylib_dependency_formats(&self, cnum: ast::CrateNum)
374                                     -> Vec<(ast::CrateNum, LinkagePreference)>
375         { unimplemented!() }
376     fn lang_items(&self, cnum: ast::CrateNum) -> Vec<(DefIndex, usize)>
377         { unimplemented!() }
378     fn missing_lang_items(&self, cnum: ast::CrateNum) -> Vec<lang_items::LangItem>
379         { unimplemented!() }
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>
384         { unimplemented!() }
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>>
389         { unimplemented!() }
390     fn plugin_registrar_fn(&self, cnum: ast::CrateNum) -> Option<DefId>
391         { unimplemented!() }
392     fn native_libraries(&self, cnum: ast::CrateNum) -> Vec<(NativeLibraryKind, String)>
393         { unimplemented!() }
394     fn reachable_ids(&self, cnum: ast::CrateNum) -> Vec<DefId> { unimplemented!() }
395
396     // resolve
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>
400         { unimplemented!() }
401     fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>
402         { unimplemented!() }
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>
406         { unimplemented!() }
407
408     // misc. metadata
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 {
414         unimplemented!()
415     }
416
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![] }
422
423     // utility functions
424     fn metadata_filename(&self) -> &str { unimplemented!() }
425     fn metadata_section_name(&self, target: &Target) -> &str { unimplemented!() }
426     fn encode_type(&self,
427                    tcx: &TyCtxt<'tcx>,
428                    ty: Ty<'tcx>,
429                    def_id_to_string: fn(&TyCtxt<'tcx>, DefId) -> String)
430                    -> Vec<u8> {
431         unimplemented!()
432     }
433     fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option<PathBuf>)>
434         { vec![] }
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,
438                        tcx: &TyCtxt<'tcx>,
439                        reexports: &def::ExportMap,
440                        item_symbols: &RefCell<NodeMap<String>>,
441                        link_meta: &LinkMeta,
442                        reachable: &NodeSet,
443                        mir_map: &MirMap<'tcx>,
444                        krate: &hir::Crate) -> Vec<u8> { vec![] }
445     fn metadata_encoding_version(&self) -> &[u8] { unimplemented!() }
446 }
447
448
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.
457 ///
458 /// Note, however, that this only works for RBML-based encoding and decoding at
459 /// the moment.
460 pub mod tls {
461     use rbml::opaque::Encoder as OpaqueEncoder;
462     use rbml::opaque::Decoder as OpaqueDecoder;
463     use serialize;
464     use std::cell::Cell;
465     use std::mem;
466     use middle::ty::{self, Ty, TyCtxt};
467     use middle::subst::Substs;
468     use middle::def_id::DefId;
469
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>);
474     }
475
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.
479     struct TlsPayload;
480
481     thread_local! {
482         static TLS_ENCODING: Cell<Option<*const TlsPayload>> = Cell::new(None)
483     }
484
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,
488                                               f: F) -> R
489         where F: FnOnce(&EncodingContext<'tcx>, &mut OpaqueEncoder) -> R
490     {
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);
497             tls.set(prev);
498             return ret
499         })
500     }
501
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.
505     ///
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
513     {
514         unsafe {
515             unsafe_with_encoding_context(|ecx, tls_encoder| {
516                 assert!(encoder as *mut _ as usize == tls_encoder as *mut _ as usize);
517
518                 let ecx: &EncodingContext<'tcx> = mem::transmute(ecx);
519
520                 f(ecx, tls_encoder)
521             })
522         }
523     }
524
525     /// Execute f with access to the thread-local encoding context and
526     /// rbml encoder.
527     pub unsafe fn unsafe_with_encoding_context<F, R>(f: F) -> R
528         where F: FnOnce(&EncodingContext, &mut OpaqueEncoder) -> R
529     {
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)
534         })
535     }
536
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;
542     }
543
544     thread_local! {
545         static TLS_DECODING: Cell<Option<*const TlsPayload>> = Cell::new(None)
546     }
547
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,
551                                               f: F) -> R
552         where F: FnOnce(&DecodingContext<'tcx>, &mut OpaqueDecoder) -> R
553     {
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);
560             tls.set(prev);
561             return ret
562         })
563     }
564
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.
568     ///
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,
577               'tcx: 'decoder
578     {
579         unsafe {
580             unsafe_with_decoding_context(|dcx, decoder| {
581                 assert!((d as *mut _ as usize) == (decoder as *mut _ as usize));
582
583                 let dcx: &DecodingContext<'tcx> = mem::transmute(dcx);
584
585                 f(dcx, decoder)
586             })
587         }
588     }
589
590     /// Execute f with access to the thread-local decoding context and
591     /// rbml decoder.
592     pub unsafe fn unsafe_with_decoding_context<F, R>(f: F) -> R
593         where F: FnOnce(&DecodingContext, &mut OpaqueDecoder) -> R
594     {
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)
599         })
600     }
601 }