]> git.lizzy.rs Git - rust.git/blob - src/librustc_metadata/tls_context.rs
Remove unused imports
[rust.git] / src / librustc_metadata / tls_context.rs
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.
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 // This module provides implementations for the thread-local encoding and
12 // decoding context traits in rustc::middle::cstore::tls.
13
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;
20
21 use decoder::{self, Cmd};
22 use encoder;
23 use tydecode::TyDecoder;
24 use tyencode;
25
26
27 impl<'a, 'tcx: 'a> tls::EncodingContext<'tcx> for encoder::EncodeContext<'a, 'tcx> {
28
29     fn tcx<'s>(&'s self) -> &'s ty::ctxt<'tcx> {
30         &self.tcx
31     }
32
33     fn encode_ty(&self, rbml_w: &mut RbmlEncoder, t: ty::Ty<'tcx>) {
34         encoder::write_type(self, rbml_w, t);
35     }
36
37     fn encode_substs(&self, rbml_w: &mut RbmlEncoder, substs: &Substs<'tcx>) {
38         let ty_str_ctxt = &tyencode::ctxt {
39             diag: self.diag,
40             ds: encoder::def_to_string,
41             tcx: self.tcx,
42             abbrevs: &self.type_abbrevs
43         };
44         tyencode::enc_substs(rbml_w, ty_str_ctxt, substs);
45     }
46 }
47
48 pub struct DecodingContext<'a, 'tcx: 'a> {
49     pub crate_metadata: Cmd<'a>,
50     pub tcx: &'a ty::ctxt<'tcx>,
51 }
52
53 impl<'a, 'tcx: 'a> tls::DecodingContext<'tcx> for DecodingContext<'a, 'tcx> {
54
55     fn tcx<'s>(&'s self) -> &'s ty::ctxt<'tcx> {
56         &self.tcx
57     }
58
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)
62         };
63
64         let starting_position = rbml_r.position();
65
66         let mut ty_decoder = TyDecoder::new(
67             self.crate_metadata.data.as_slice(),
68             self.crate_metadata.cnum,
69             starting_position,
70             self.tcx,
71             def_id_convert);
72
73         let ty = ty_decoder.parse_ty();
74
75         let end_position = ty_decoder.position();
76
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
79         // after the type.
80         rbml_r.advance(end_position - starting_position);
81         ty
82     }
83
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)
87         };
88
89         let starting_position = rbml_r.position();
90
91         let mut ty_decoder = TyDecoder::new(
92             self.crate_metadata.data.as_slice(),
93             self.crate_metadata.cnum,
94             starting_position,
95             self.tcx,
96             def_id_convert);
97
98         let substs = ty_decoder.parse_substs();
99
100         let end_position = ty_decoder.position();
101
102         rbml_r.advance(end_position - starting_position);
103         substs
104     }
105
106     fn translate_def_id(&self, def_id: DefId) -> DefId {
107         decoder::translate_def_id(self.crate_metadata, def_id)
108     }
109 }