Skip to content

Commit a96abca

Browse files
committed
rustc_metadata: replace RBML with a simple and type-safe scheme.
1 parent 24aef24 commit a96abca

File tree

21 files changed

+1827
-2471
lines changed

21 files changed

+1827
-2471
lines changed

src/librustc/middle/cstore.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ pub trait CrateStore<'tcx> {
138138
fn item_super_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
139139
-> ty::GenericPredicates<'tcx>;
140140
fn item_generics<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
141-
-> &'tcx ty::Generics<'tcx>;
141+
-> ty::Generics<'tcx>;
142142
fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute>;
143143
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef<'tcx>;
144144
fn adt_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>;
145-
fn fn_arg_names(&self, did: DefId) -> Vec<String>;
145+
fn fn_arg_names(&self, did: DefId) -> Vec<ast::Name>;
146146
fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>;
147147

148148
// trait info
@@ -299,13 +299,13 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
299299
fn item_super_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
300300
-> ty::GenericPredicates<'tcx> { bug!("item_super_predicates") }
301301
fn item_generics<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
302-
-> &'tcx ty::Generics<'tcx> { bug!("item_generics") }
302+
-> ty::Generics<'tcx> { bug!("item_generics") }
303303
fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute> { bug!("item_attrs") }
304304
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef<'tcx>
305305
{ bug!("trait_def") }
306306
fn adt_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>
307307
{ bug!("adt_def") }
308-
fn fn_arg_names(&self, did: DefId) -> Vec<String> { bug!("fn_arg_names") }
308+
fn fn_arg_names(&self, did: DefId) -> Vec<ast::Name> { bug!("fn_arg_names") }
309309
fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId> { vec![] }
310310

311311
// trait info

src/librustc/ty/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,9 @@ pub struct GenericPredicates<'tcx> {
737737
pub predicates: Vec<Predicate<'tcx>>,
738738
}
739739

740+
impl<'tcx> serialize::UseSpecializedEncodable for GenericPredicates<'tcx> {}
741+
impl<'tcx> serialize::UseSpecializedDecodable for GenericPredicates<'tcx> {}
742+
740743
impl<'a, 'gcx, 'tcx> GenericPredicates<'tcx> {
741744
pub fn instantiate(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>, substs: &Substs<'tcx>)
742745
-> InstantiatedPredicates<'tcx> {
@@ -2457,7 +2460,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
24572460
pub fn lookup_generics(self, did: DefId) -> &'gcx Generics<'gcx> {
24582461
lookup_locally_or_in_crate_store(
24592462
"generics", did, &self.generics,
2460-
|| self.sess.cstore.item_generics(self.global_tcx(), did))
2463+
|| self.alloc_generics(self.sess.cstore.item_generics(self.global_tcx(), did)))
24612464
}
24622465

24632466
/// Given the did of an item, returns its full set of predicates.

src/librustc_metadata/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ flate = { path = "../libflate" }
1313
log = { path = "../liblog" }
1414
rustc = { path = "../librustc" }
1515
rustc_back = { path = "../librustc_back" }
16-
rustc_bitflags = { path = "../librustc_bitflags" }
1716
rustc_const_math = { path = "../librustc_const_math" }
1817
rustc_data_structures = { path = "../librustc_data_structures" }
1918
rustc_errors = { path = "../librustc_errors" }

src/librustc_metadata/astencode.rs

Lines changed: 95 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,92 @@ use rustc::hir::map as ast_map;
1313
use rustc::hir::intravisit::{Visitor, IdRangeComputingVisitor, IdRange};
1414

1515
use cstore::CrateMetadata;
16-
use decoder::DecodeContext;
1716
use encoder::EncodeContext;
17+
use schema::*;
1818

1919
use rustc::middle::cstore::{InlinedItem, InlinedItemRef};
20-
use rustc::hir::def;
20+
use rustc::middle::const_qualif::ConstQualif;
21+
use rustc::hir::def::{self, Def};
2122
use rustc::hir::def_id::DefId;
22-
use rustc::ty::TyCtxt;
23+
use rustc::ty::{self, TyCtxt, Ty};
2324

2425
use syntax::ast;
2526

26-
use rbml;
27-
use rustc_serialize::{Decodable, Encodable};
27+
use rustc_serialize::Encodable;
2828

29-
// ______________________________________________________________________
30-
// Top-level methods.
29+
#[derive(RustcEncodable, RustcDecodable)]
30+
pub struct Ast<'tcx> {
31+
id_range: IdRange,
32+
item: Lazy<InlinedItem>,
33+
side_tables: LazySeq<(ast::NodeId, TableEntry<'tcx>)>
34+
}
35+
36+
#[derive(RustcEncodable, RustcDecodable)]
37+
enum TableEntry<'tcx> {
38+
Def(Def),
39+
NodeType(Ty<'tcx>),
40+
ItemSubsts(ty::ItemSubsts<'tcx>),
41+
Adjustment(ty::adjustment::AutoAdjustment<'tcx>),
42+
ConstQualif(ConstQualif)
43+
}
3144

32-
pub fn encode_inlined_item(ecx: &mut EncodeContext, ii: InlinedItemRef) {
33-
ecx.tag(::common::item_tag::ast, |ecx| {
34-
let mut visitor = IdRangeComputingVisitor::new();
45+
impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
46+
pub fn encode_inlined_item(&mut self, ii: InlinedItemRef) -> Lazy<Ast<'tcx>> {
47+
let mut id_visitor = IdRangeComputingVisitor::new();
3548
match ii {
36-
InlinedItemRef::Item(_, i) => visitor.visit_item(i),
37-
InlinedItemRef::TraitItem(_, ti) => visitor.visit_trait_item(ti),
38-
InlinedItemRef::ImplItem(_, ii) => visitor.visit_impl_item(ii)
49+
InlinedItemRef::Item(_, i) => id_visitor.visit_item(i),
50+
InlinedItemRef::TraitItem(_, ti) => id_visitor.visit_trait_item(ti),
51+
InlinedItemRef::ImplItem(_, ii) => id_visitor.visit_impl_item(ii)
3952
}
40-
visitor.result().encode(ecx).unwrap();
4153

42-
ii.encode(ecx).unwrap();
54+
let ii_pos = self.position();
55+
ii.encode(self).unwrap();
56+
57+
let tables_pos = self.position();
58+
let tables_count = {
59+
let mut visitor = SideTableEncodingIdVisitor {
60+
ecx: self,
61+
count: 0
62+
};
63+
match ii {
64+
InlinedItemRef::Item(_, i) => visitor.visit_item(i),
65+
InlinedItemRef::TraitItem(_, ti) => visitor.visit_trait_item(ti),
66+
InlinedItemRef::ImplItem(_, ii) => visitor.visit_impl_item(ii)
67+
}
68+
visitor.count
69+
};
70+
71+
self.lazy(&Ast {
72+
id_range: id_visitor.result(),
73+
item: Lazy::with_position(ii_pos),
74+
side_tables: LazySeq::with_position_and_length(tables_pos, tables_count)
75+
})
76+
}
77+
}
78+
79+
struct SideTableEncodingIdVisitor<'a, 'b:'a, 'tcx:'b> {
80+
ecx: &'a mut EncodeContext<'b, 'tcx>,
81+
count: usize
82+
}
83+
84+
impl<'a, 'b, 'tcx, 'v> Visitor<'v> for SideTableEncodingIdVisitor<'a, 'b, 'tcx> {
85+
fn visit_id(&mut self, id: ast::NodeId) {
86+
debug!("Encoding side tables for id {}", id);
4387

44-
let mut visitor = SideTableEncodingIdVisitor {
45-
ecx: ecx
88+
let tcx = self.ecx.tcx;
89+
let mut encode = |entry: Option<TableEntry>| {
90+
if let Some(entry) = entry {
91+
(id, entry).encode(self.ecx).unwrap();
92+
self.count += 1;
93+
}
4694
};
47-
match ii {
48-
InlinedItemRef::Item(_, i) => visitor.visit_item(i),
49-
InlinedItemRef::TraitItem(_, ti) => visitor.visit_trait_item(ti),
50-
InlinedItemRef::ImplItem(_, ii) => visitor.visit_impl_item(ii)
51-
}
52-
});
95+
96+
encode(tcx.expect_def_or_none(id).map(TableEntry::Def));
97+
encode(tcx.node_types().get(&id).cloned().map(TableEntry::NodeType));
98+
encode(tcx.tables.borrow().item_substs.get(&id).cloned().map(TableEntry::ItemSubsts));
99+
encode(tcx.tables.borrow().adjustments.get(&id).cloned().map(TableEntry::Adjustment));
100+
encode(tcx.const_qualif_map.borrow().get(&id).cloned().map(TableEntry::ConstQualif));
101+
}
53102
}
54103

55104
/// Decodes an item from its AST in the cdata's metadata and adds it to the
@@ -58,17 +107,19 @@ pub fn decode_inlined_item<'a, 'tcx>(cdata: &CrateMetadata,
58107
tcx: TyCtxt<'a, 'tcx, 'tcx>,
59108
parent_def_path: ast_map::DefPath,
60109
parent_did: DefId,
61-
ast_doc: rbml::Doc,
110+
ast: Ast<'tcx>,
62111
orig_did: DefId)
63112
-> &'tcx InlinedItem {
64113
debug!("> Decoding inlined fn: {:?}", tcx.item_path_str(orig_did));
65-
let dcx = &mut DecodeContext::new(ast_doc, Some(cdata)).typed(tcx);
66-
dcx.from_id_range = IdRange::decode(dcx).unwrap();
67-
let cnt = dcx.from_id_range.max.as_usize() - dcx.from_id_range.min.as_usize();
68-
dcx.to_id_range.min = tcx.sess.reserve_node_ids(cnt);
69-
dcx.to_id_range.max = ast::NodeId::new(dcx.to_id_range.min.as_usize() + cnt);
70-
let ii = InlinedItem::decode(dcx).unwrap();
71114

115+
let cnt = ast.id_range.max.as_usize() - ast.id_range.min.as_usize();
116+
let start = tcx.sess.reserve_node_ids(cnt);
117+
let id_ranges = [ast.id_range, IdRange {
118+
min: start,
119+
max: ast::NodeId::new(start.as_usize() + cnt)
120+
}];
121+
122+
let ii = ast.item.decode((cdata, tcx, id_ranges));
72123
let ii = ast_map::map_decoded_item(&tcx.map,
73124
parent_def_path,
74125
parent_did,
@@ -83,107 +134,25 @@ pub fn decode_inlined_item<'a, 'tcx>(cdata: &CrateMetadata,
83134
let inlined_did = tcx.map.local_def_id(item_node_id);
84135
tcx.register_item_type(inlined_did, tcx.lookup_item_type(orig_did));
85136

86-
decode_side_tables(dcx, ast_doc);
87-
88-
ii
89-
}
90-
91-
// ______________________________________________________________________
92-
// Encoding and decoding the side tables
93-
94-
impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
95-
fn tag<F>(&mut self,
96-
tag_id: usize,
97-
f: F) where
98-
F: FnOnce(&mut Self),
99-
{
100-
self.start_tag(tag_id).unwrap();
101-
f(self);
102-
self.end_tag().unwrap();
103-
}
104-
105-
fn entry(&mut self, table: Table, id: ast::NodeId) {
106-
table.encode(self).unwrap();
107-
id.encode(self).unwrap();
108-
}
109-
}
110-
111-
struct SideTableEncodingIdVisitor<'a, 'b:'a, 'tcx:'b> {
112-
ecx: &'a mut EncodeContext<'b, 'tcx>,
113-
}
114-
115-
impl<'a, 'b, 'tcx, 'v> Visitor<'v> for SideTableEncodingIdVisitor<'a, 'b, 'tcx> {
116-
fn visit_id(&mut self, id: ast::NodeId) {
117-
encode_side_tables_for_id(self.ecx, id)
118-
}
119-
}
120-
121-
#[derive(RustcEncodable, RustcDecodable, Debug)]
122-
enum Table {
123-
Def,
124-
NodeType,
125-
ItemSubsts,
126-
Adjustment,
127-
ConstQualif
128-
}
129-
130-
fn encode_side_tables_for_id(ecx: &mut EncodeContext, id: ast::NodeId) {
131-
let tcx = ecx.tcx;
132-
133-
debug!("Encoding side tables for id {}", id);
134-
135-
if let Some(def) = tcx.expect_def_or_none(id) {
136-
ecx.entry(Table::Def, id);
137-
def.encode(ecx).unwrap();
138-
}
139-
140-
if let Some(ty) = tcx.node_types().get(&id) {
141-
ecx.entry(Table::NodeType, id);
142-
ty.encode(ecx).unwrap();
143-
}
144-
145-
if let Some(item_substs) = tcx.tables.borrow().item_substs.get(&id) {
146-
ecx.entry(Table::ItemSubsts, id);
147-
item_substs.substs.encode(ecx).unwrap();
148-
}
149-
150-
if let Some(adjustment) = tcx.tables.borrow().adjustments.get(&id) {
151-
ecx.entry(Table::Adjustment, id);
152-
adjustment.encode(ecx).unwrap();
153-
}
154-
155-
if let Some(qualif) = tcx.const_qualif_map.borrow().get(&id) {
156-
ecx.entry(Table::ConstQualif, id);
157-
qualif.encode(ecx).unwrap();
158-
}
159-
}
160-
161-
fn decode_side_tables(dcx: &mut DecodeContext, ast_doc: rbml::Doc) {
162-
while dcx.opaque.position() < ast_doc.end {
163-
let table = Decodable::decode(dcx).unwrap();
164-
let id = Decodable::decode(dcx).unwrap();
165-
debug!("decode_side_tables: entry for id={}, table={:?}", id, table);
166-
match table {
167-
Table::Def => {
168-
let def = Decodable::decode(dcx).unwrap();
169-
dcx.tcx().def_map.borrow_mut().insert(id, def::PathResolution::new(def));
137+
for (id, entry) in ast.side_tables.decode((cdata, tcx, id_ranges)) {
138+
match entry {
139+
TableEntry::Def(def) => {
140+
tcx.def_map.borrow_mut().insert(id, def::PathResolution::new(def));
170141
}
171-
Table::NodeType => {
172-
let ty = Decodable::decode(dcx).unwrap();
173-
dcx.tcx().node_type_insert(id, ty);
142+
TableEntry::NodeType(ty) => {
143+
tcx.node_type_insert(id, ty);
174144
}
175-
Table::ItemSubsts => {
176-
let item_substs = Decodable::decode(dcx).unwrap();
177-
dcx.tcx().tables.borrow_mut().item_substs.insert(id, item_substs);
145+
TableEntry::ItemSubsts(item_substs) => {
146+
tcx.tables.borrow_mut().item_substs.insert(id, item_substs);
178147
}
179-
Table::Adjustment => {
180-
let adj = Decodable::decode(dcx).unwrap();
181-
dcx.tcx().tables.borrow_mut().adjustments.insert(id, adj);
148+
TableEntry::Adjustment(adj) => {
149+
tcx.tables.borrow_mut().adjustments.insert(id, adj);
182150
}
183-
Table::ConstQualif => {
184-
let qualif = Decodable::decode(dcx).unwrap();
185-
dcx.tcx().const_qualif_map.borrow_mut().insert(id, qualif);
151+
TableEntry::ConstQualif(qualif) => {
152+
tcx.const_qualif_map.borrow_mut().insert(id, qualif);
186153
}
187154
}
188155
}
156+
157+
ii
189158
}

0 commit comments

Comments
 (0)