]>
Commit | Line | Data |
---|---|---|
9cc50fc6 SL |
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::opaque::Encoder as OpaqueEncoder; | |
15 | use rbml::opaque::Decoder as OpaqueDecoder; | |
16 | use rustc::middle::cstore::tls; | |
54a0048b SL |
17 | use rustc::hir::def_id::DefId; |
18 | use rustc::ty::subst::Substs; | |
19 | use rustc::ty::{self, TyCtxt}; | |
9cc50fc6 SL |
20 | |
21 | use decoder::{self, Cmd}; | |
22 | use encoder; | |
23 | use tydecode::TyDecoder; | |
24 | use tyencode; | |
25 | ||
26 | impl<'a, 'tcx: 'a> tls::EncodingContext<'tcx> for encoder::EncodeContext<'a, 'tcx> { | |
27 | ||
54a0048b | 28 | fn tcx<'s>(&'s self) -> &'s TyCtxt<'tcx> { |
9cc50fc6 SL |
29 | &self.tcx |
30 | } | |
31 | ||
32 | fn encode_ty(&self, encoder: &mut OpaqueEncoder, t: ty::Ty<'tcx>) { | |
33 | tyencode::enc_ty(encoder.cursor, &self.ty_str_ctxt(), t); | |
34 | } | |
35 | ||
36 | fn encode_substs(&self, encoder: &mut OpaqueEncoder, substs: &Substs<'tcx>) { | |
37 | tyencode::enc_substs(encoder.cursor, &self.ty_str_ctxt(), substs); | |
38 | } | |
39 | } | |
40 | ||
41 | pub struct DecodingContext<'a, 'tcx: 'a> { | |
42 | pub crate_metadata: Cmd<'a>, | |
54a0048b | 43 | pub tcx: &'a TyCtxt<'tcx>, |
9cc50fc6 SL |
44 | } |
45 | ||
46 | impl<'a, 'tcx: 'a> tls::DecodingContext<'tcx> for DecodingContext<'a, 'tcx> { | |
47 | ||
54a0048b | 48 | fn tcx<'s>(&'s self) -> &'s TyCtxt<'tcx> { |
9cc50fc6 SL |
49 | &self.tcx |
50 | } | |
51 | ||
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) | |
55 | }; | |
56 | ||
57 | let starting_position = decoder.position(); | |
58 | ||
59 | let mut ty_decoder = TyDecoder::new( | |
60 | self.crate_metadata.data.as_slice(), | |
61 | self.crate_metadata.cnum, | |
62 | starting_position, | |
63 | self.tcx, | |
64 | def_id_convert); | |
65 | ||
66 | let ty = ty_decoder.parse_ty(); | |
67 | ||
68 | let end_position = ty_decoder.position(); | |
69 | ||
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 | |
72 | // after the type. | |
73 | decoder.advance(end_position - starting_position); | |
74 | ty | |
75 | } | |
76 | ||
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) | |
80 | }; | |
81 | ||
82 | let starting_position = decoder.position(); | |
83 | ||
84 | let mut ty_decoder = TyDecoder::new( | |
85 | self.crate_metadata.data.as_slice(), | |
86 | self.crate_metadata.cnum, | |
87 | starting_position, | |
88 | self.tcx, | |
89 | def_id_convert); | |
90 | ||
91 | let substs = ty_decoder.parse_substs(); | |
92 | ||
93 | let end_position = ty_decoder.position(); | |
94 | ||
95 | decoder.advance(end_position - starting_position); | |
96 | substs | |
97 | } | |
98 | ||
99 | fn translate_def_id(&self, def_id: DefId) -> DefId { | |
100 | decoder::translate_def_id(self.crate_metadata, def_id) | |
101 | } | |
102 | } |