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::writer::Encoder as RbmlEncoder;
15 use rbml::reader::Decoder as RbmlDecoder;
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;
27 impl<'a, 'tcx: 'a> tls::EncodingContext<'tcx> for encoder::EncodeContext<'a, 'tcx> {
29 fn tcx<'s>(&'s self) -> &'s ty::ctxt<'tcx> {
33 fn encode_ty(&self, rbml_w: &mut RbmlEncoder, t: ty::Ty<'tcx>) {
34 encoder::write_type(self, rbml_w, t);
37 fn encode_substs(&self, rbml_w: &mut RbmlEncoder, substs: &Substs<'tcx>) {
38 let ty_str_ctxt = &tyencode::ctxt {
40 ds: encoder::def_to_string,
42 abbrevs: &self.type_abbrevs
44 tyencode::enc_substs(rbml_w, ty_str_ctxt, substs);
48 pub struct DecodingContext<'a, 'tcx: 'a> {
49 pub crate_metadata: Cmd<'a>,
50 pub tcx: &'a ty::ctxt<'tcx>,
53 impl<'a, 'tcx: 'a> tls::DecodingContext<'tcx> for DecodingContext<'a, 'tcx> {
55 fn tcx<'s>(&'s self) -> &'s ty::ctxt<'tcx> {
59 fn decode_ty(&self, rbml_r: &mut RbmlDecoder) -> ty::Ty<'tcx> {
60 let def_id_convert = &mut |did| {
61 decoder::translate_def_id(self.crate_metadata, did)
64 let starting_position = rbml_r.position();
66 let mut ty_decoder = TyDecoder::new(
67 self.crate_metadata.data.as_slice(),
68 self.crate_metadata.cnum,
73 let ty = ty_decoder.parse_ty();
75 let end_position = ty_decoder.position();
77 // We can just reuse the tydecode implementation for parsing types, but
78 // we have to make sure to leave the rbml reader at the position just
80 rbml_r.advance(end_position - starting_position);
84 fn decode_substs(&self, rbml_r: &mut RbmlDecoder) -> Substs<'tcx> {
85 let def_id_convert = &mut |did| {
86 decoder::translate_def_id(self.crate_metadata, did)
89 let starting_position = rbml_r.position();
91 let mut ty_decoder = TyDecoder::new(
92 self.crate_metadata.data.as_slice(),
93 self.crate_metadata.cnum,
98 let substs = ty_decoder.parse_substs();
100 let end_position = ty_decoder.position();
102 rbml_r.advance(end_position - starting_position);
106 fn translate_def_id(&self, def_id: DefId) -> DefId {
107 decoder::translate_def_id(self.crate_metadata, def_id)