1 // Copyright 2012-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 // This module provides implementations for the thread-local encoding and
12 // decoding context traits in rustc::middle::cstore::tls.
14 use rbml::opaque::Encoder as OpaqueEncoder;
15 use rbml::opaque::Decoder as OpaqueDecoder;
16 use rustc::middle::cstore::tls;
17 use rustc::middle::def_id::DefId;
18 use rustc::middle::subst::Substs;
19 use rustc::middle::ty;
21 use decoder::{self, Cmd};
23 use tydecode::TyDecoder;
26 impl<'a, 'tcx: 'a> tls::EncodingContext<'tcx> for encoder::EncodeContext<'a, 'tcx> {
28 fn tcx<'s>(&'s self) -> &'s ty::ctxt<'tcx> {
32 fn encode_ty(&self, encoder: &mut OpaqueEncoder, t: ty::Ty<'tcx>) {
33 tyencode::enc_ty(encoder.cursor, &self.ty_str_ctxt(), t);
36 fn encode_substs(&self, encoder: &mut OpaqueEncoder, substs: &Substs<'tcx>) {
37 tyencode::enc_substs(encoder.cursor, &self.ty_str_ctxt(), substs);
41 pub struct DecodingContext<'a, 'tcx: 'a> {
42 pub crate_metadata: Cmd<'a>,
43 pub tcx: &'a ty::ctxt<'tcx>,
46 impl<'a, 'tcx: 'a> tls::DecodingContext<'tcx> for DecodingContext<'a, 'tcx> {
48 fn tcx<'s>(&'s self) -> &'s ty::ctxt<'tcx> {
52 fn decode_ty(&self, decoder: &mut OpaqueDecoder) -> ty::Ty<'tcx> {
53 let def_id_convert = &mut |did| {
54 decoder::translate_def_id(self.crate_metadata, did)
57 let starting_position = decoder.position();
59 let mut ty_decoder = TyDecoder::new(
60 self.crate_metadata.data.as_slice(),
61 self.crate_metadata.cnum,
66 let ty = ty_decoder.parse_ty();
68 let end_position = ty_decoder.position();
70 // We can just reuse the tydecode implementation for parsing types, but
71 // we have to make sure to leave the rbml reader at the position just
73 decoder.advance(end_position - starting_position);
77 fn decode_substs(&self, decoder: &mut OpaqueDecoder) -> Substs<'tcx> {
78 let def_id_convert = &mut |did| {
79 decoder::translate_def_id(self.crate_metadata, did)
82 let starting_position = decoder.position();
84 let mut ty_decoder = TyDecoder::new(
85 self.crate_metadata.data.as_slice(),
86 self.crate_metadata.cnum,
91 let substs = ty_decoder.parse_substs();
93 let end_position = ty_decoder.position();
95 decoder.advance(end_position - starting_position);
99 fn translate_def_id(&self, def_id: DefId) -> DefId {
100 decoder::translate_def_id(self.crate_metadata, def_id)