Skip to content

Commit 91e7239

Browse files
committed
rustc_metadata: combine DecodeContext and rbml::reader::Decoder.
1 parent 97864d4 commit 91e7239

File tree

12 files changed

+309
-716
lines changed

12 files changed

+309
-716
lines changed

src/librustc/middle/cstore.rs

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -495,68 +495,4 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
495495

496496
pub trait MacroLoader {
497497
fn load_crate(&mut self, extern_crate: &ast::Item, allows_macros: bool) -> Vec<LoadedMacro>;
498-
}
499-
500-
/// Metadata encoding and decoding can make use of thread-local encoding and
501-
/// decoding contexts. These allow implementers of serialize::Encodable and
502-
/// Decodable to access information and datastructures that would otherwise not
503-
/// be available to them. For example, we can automatically translate def-id and
504-
/// span information during decoding because the decoding context knows which
505-
/// crate the data is decoded from. Or it allows to make ty::Ty decodable
506-
/// because the context has access to the TyCtxt that is needed for creating
507-
/// ty::Ty instances.
508-
///
509-
/// Note, however, that this only works for RBML-based encoding and decoding at
510-
/// the moment.
511-
pub mod tls {
512-
use rbml::opaque::Decoder as OpaqueDecoder;
513-
use std::cell::Cell;
514-
use ty::{Ty, TyCtxt};
515-
use ty::subst::Substs;
516-
use hir::def_id::DefId;
517-
518-
/// Marker type used for the TLS slot.
519-
/// The type context cannot be used directly because the TLS
520-
/// in libstd doesn't allow types generic over lifetimes.
521-
struct TlsPayload;
522-
523-
pub trait DecodingContext<'tcx> {
524-
fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx>;
525-
fn decode_ty(&self, decoder: &mut OpaqueDecoder) -> Ty<'tcx>;
526-
fn decode_substs(&self, decoder: &mut OpaqueDecoder) -> &'tcx Substs<'tcx>;
527-
fn translate_def_id(&self, def_id: DefId) -> DefId;
528-
}
529-
530-
thread_local! {
531-
static TLS_DECODING: Cell<Option<*const TlsPayload>> = Cell::new(None)
532-
}
533-
534-
/// Execute f after pushing the given DecodingContext onto the TLS stack.
535-
pub fn enter_decoding_context<'tcx, F, R>(dcx: &DecodingContext<'tcx>, f: F) -> R
536-
where F: FnOnce(&DecodingContext<'tcx>) -> R
537-
{
538-
let tls_payload = dcx as *const _;
539-
let tls_ptr = &tls_payload as *const _ as *const TlsPayload;
540-
TLS_DECODING.with(|tls| {
541-
let prev = tls.get();
542-
tls.set(Some(tls_ptr));
543-
let ret = f(dcx);
544-
tls.set(prev);
545-
ret
546-
})
547-
}
548-
549-
/// Execute f with access to the thread-local decoding context.
550-
/// FIXME(eddyb) This is horribly unsound as it allows the
551-
/// caler to pick any lifetime for 'tcx, including 'static.
552-
pub fn with_decoding_context<'tcx, F, R>(f: F) -> R
553-
where F: FnOnce(&DecodingContext<'tcx>) -> R,
554-
{
555-
unsafe {
556-
TLS_DECODING.with(|tls| {
557-
let tls = tls.get().unwrap();
558-
f(*(tls as *mut &DecodingContext))
559-
})
560-
}
561-
}
562-
}
498+
}

src/librustc/middle/region.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use dep_graph::DepNode;
2020
use hir::map as ast_map;
2121
use session::Session;
2222
use util::nodemap::{FnvHashMap, NodeMap, NodeSet};
23-
use middle::cstore::InlinedItem;
2423
use ty;
2524

2625
use std::cell::RefCell;
@@ -1256,19 +1255,3 @@ pub fn resolve_crate(sess: &Session, map: &ast_map::Map) -> RegionMaps {
12561255
}
12571256
return maps;
12581257
}
1259-
1260-
pub fn resolve_inlined_item(sess: &Session,
1261-
region_maps: &RegionMaps,
1262-
item: &InlinedItem) {
1263-
let mut visitor = RegionResolutionVisitor {
1264-
sess: sess,
1265-
region_maps: region_maps,
1266-
cx: Context {
1267-
root_id: None,
1268-
parent: ROOT_CODE_EXTENT,
1269-
var_parent: ROOT_CODE_EXTENT
1270-
},
1271-
terminating_scopes: NodeSet()
1272-
};
1273-
item.visit(&mut visitor);
1274-
}

src/librustc/ty/mod.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub use self::fold::TypeFoldable;
2121
use dep_graph::{self, DepNode};
2222
use hir::map as ast_map;
2323
use middle;
24-
use middle::cstore::{self, LOCAL_CRATE};
24+
use middle::cstore::LOCAL_CRATE;
2525
use hir::def::{Def, PathResolution, ExportMap};
2626
use hir::def_id::DefId;
2727
use middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem};
@@ -34,7 +34,7 @@ use util::common::MemoizationMap;
3434
use util::nodemap::NodeSet;
3535
use util::nodemap::FnvHashMap;
3636

37-
use serialize::{self, Encodable, Encoder, Decodable, Decoder};
37+
use serialize::{self, Encodable, Encoder};
3838
use std::borrow::Cow;
3939
use std::cell::Cell;
4040
use std::hash::{Hash, Hasher};
@@ -1487,17 +1487,7 @@ impl<'tcx> Encodable for AdtDef<'tcx> {
14871487
}
14881488
}
14891489

1490-
impl<'tcx> Decodable for AdtDef<'tcx> {
1491-
fn decode<D: Decoder>(d: &mut D) -> Result<AdtDef<'tcx>, D::Error> {
1492-
let def_id: DefId = Decodable::decode(d)?;
1493-
1494-
cstore::tls::with_decoding_context(|dcx| {
1495-
let def_id = dcx.translate_def_id(def_id);
1496-
Ok(dcx.tcx().lookup_adt_def(def_id))
1497-
})
1498-
}
1499-
}
1500-
1490+
impl<'tcx> serialize::UseSpecializedDecodable for AdtDef<'tcx> {}
15011491

15021492
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
15031493
pub enum AdtKind { Struct, Union, Enum }

src/librustc/ty/sty.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
//! This module contains TypeVariants and its major components
1212
13-
use middle::cstore;
1413
use hir::def_id::DefId;
1514
use middle::region;
1615
use ty::subst::Substs;
@@ -25,7 +24,7 @@ use syntax::abi;
2524
use syntax::ast::{self, Name};
2625
use syntax::parse::token::{keywords, InternedString};
2726

28-
use serialize::{Decodable, Decoder, Encodable, Encoder};
27+
use serialize;
2928

3029
use hir;
3130

@@ -253,7 +252,7 @@ pub enum TypeVariants<'tcx> {
253252
/// closure C wind up influencing the decisions we ought to make for
254253
/// closure C (which would then require fixed point iteration to
255254
/// handle). Plus it fixes an ICE. :P
256-
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
255+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable)]
257256
pub struct ClosureSubsts<'tcx> {
258257
/// Lifetime and type parameters from the enclosing function.
259258
/// These are separated out because trans wants to pass them around
@@ -266,23 +265,7 @@ pub struct ClosureSubsts<'tcx> {
266265
pub upvar_tys: &'tcx [Ty<'tcx>]
267266
}
268267

269-
impl<'tcx> Encodable for ClosureSubsts<'tcx> {
270-
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
271-
(self.func_substs, self.upvar_tys).encode(s)
272-
}
273-
}
274-
275-
impl<'tcx> Decodable for ClosureSubsts<'tcx> {
276-
fn decode<D: Decoder>(d: &mut D) -> Result<ClosureSubsts<'tcx>, D::Error> {
277-
let (func_substs, upvar_tys) = Decodable::decode(d)?;
278-
cstore::tls::with_decoding_context(|dcx| {
279-
Ok(ClosureSubsts {
280-
func_substs: func_substs,
281-
upvar_tys: dcx.tcx().mk_type_list(upvar_tys)
282-
})
283-
})
284-
}
285-
}
268+
impl<'tcx> serialize::UseSpecializedDecodable for ClosureSubsts<'tcx> {}
286269

287270
#[derive(Clone, PartialEq, Eq, Hash)]
288271
pub struct TraitObject<'tcx> {
@@ -663,14 +646,7 @@ pub enum Region {
663646
ReErased,
664647
}
665648

666-
impl<'tcx> Decodable for &'tcx Region {
667-
fn decode<D: Decoder>(d: &mut D) -> Result<&'tcx Region, D::Error> {
668-
let r = Decodable::decode(d)?;
669-
cstore::tls::with_decoding_context(|dcx| {
670-
Ok(dcx.tcx().mk_region(r))
671-
})
672-
}
673-
}
649+
impl<'tcx> serialize::UseSpecializedDecodable for &'tcx Region {}
674650

675651
#[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug)]
676652
pub struct EarlyBoundRegion {

0 commit comments

Comments
 (0)