Skip to content

Commit fc7efab

Browse files
committed
Auto merge of #27851 - nikomatsakis:cleanup-ty-decoder, r=eddyb
Just a little code cleanup I was doing as part of another refactoring (which may turn out not to be needed). The main thrust of this is to cleanup the interface to `tydecode.rs` to be less ridiculously repetitive. I also purged the generic "def-id conversion" parameter in favor of a trait object, just to reduce code duplication a bit and make the signatures a bit less messy. I measured the bootstrapping time to build stage2 with these changes, it was identical. (But it'd be easy enough to restore the unboxed closure if we wanted it.)
2 parents f05b22e + 7a3a1be commit fc7efab

File tree

14 files changed

+786
-1011
lines changed

14 files changed

+786
-1011
lines changed

src/librustc/ast_map/mod.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub use self::Node::*;
1212
pub use self::PathElem::*;
1313
use self::MapEntry::*;
1414

15+
use metadata::inline::InlinedItem;
16+
use metadata::inline::InlinedItem as II;
1517
use syntax::abi;
1618
use syntax::ast::*;
1719
use syntax::ast_util;
@@ -374,8 +376,8 @@ impl<'ast> Map<'ast> {
374376
pub fn get_parent_did(&self, id: NodeId) -> DefId {
375377
let parent = self.get_parent(id);
376378
match self.find_entry(parent) {
377-
Some(RootInlinedParent(&InlinedParent {ii: IITraitItem(did, _), ..})) => did,
378-
Some(RootInlinedParent(&InlinedParent {ii: IIImplItem(did, _), ..})) => did,
379+
Some(RootInlinedParent(&InlinedParent {ii: II::TraitItem(did, _), ..})) => did,
380+
Some(RootInlinedParent(&InlinedParent {ii: II::ImplItem(did, _), ..})) => did,
379381
_ => ast_util::local_def(parent)
380382
}
381383
}
@@ -967,16 +969,16 @@ pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
967969
-> &'ast InlinedItem {
968970
let mut fld = IdAndSpanUpdater { fold_ops: fold_ops };
969971
let ii = match ii {
970-
IIItem(i) => IIItem(fld.fold_item(i).expect_one("expected one item")),
971-
IITraitItem(d, ti) => {
972-
IITraitItem(fld.fold_ops.new_def_id(d),
973-
fld.fold_trait_item(ti).expect_one("expected one trait item"))
972+
II::Item(i) => II::Item(fld.fold_item(i).expect_one("expected one item")),
973+
II::TraitItem(d, ti) => {
974+
II::TraitItem(fld.fold_ops.new_def_id(d),
975+
fld.fold_trait_item(ti).expect_one("expected one trait item"))
974976
}
975-
IIImplItem(d, ii) => {
976-
IIImplItem(fld.fold_ops.new_def_id(d),
977-
fld.fold_impl_item(ii).expect_one("expected one impl item"))
977+
II::ImplItem(d, ii) => {
978+
II::ImplItem(fld.fold_ops.new_def_id(d),
979+
fld.fold_impl_item(ii).expect_one("expected one impl item"))
978980
}
979-
IIForeign(i) => IIForeign(fld.fold_foreign_item(i))
981+
II::Foreign(i) => II::Foreign(fld.fold_foreign_item(i))
980982
};
981983

982984
let ii_parent = map.forest.inlined_items.alloc(InlinedParent {
@@ -990,20 +992,20 @@ pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
990992
parent_node: ii_parent_id,
991993
};
992994
collector.insert_entry(ii_parent_id, RootInlinedParent(ii_parent));
993-
visit::walk_inlined_item(&mut collector, &ii_parent.ii);
995+
ii_parent.ii.visit(&mut collector);
994996

995997
// Methods get added to the AST map when their impl is visited. Since we
996998
// don't decode and instantiate the impl, but just the method, we have to
997999
// add it to the table now. Likewise with foreign items.
9981000
match ii_parent.ii {
999-
IIItem(_) => {}
1000-
IITraitItem(_, ref ti) => {
1001+
II::Item(_) => {}
1002+
II::TraitItem(_, ref ti) => {
10011003
collector.insert(ti.id, NodeTraitItem(ti));
10021004
}
1003-
IIImplItem(_, ref ii) => {
1005+
II::ImplItem(_, ref ii) => {
10041006
collector.insert(ii.id, NodeImplItem(ii));
10051007
}
1006-
IIForeign(ref i) => {
1008+
II::Foreign(ref i) => {
10071009
collector.insert(i.id, NodeForeignItem(i));
10081010
}
10091011
}

src/librustc/metadata/csearch.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use ast_map;
1414
use metadata::common::*;
1515
use metadata::cstore;
1616
use metadata::decoder;
17+
use metadata::inline::InlinedItem;
1718
use middle::lang_items;
1819
use middle::ty;
1920

@@ -96,8 +97,8 @@ pub fn get_item_path(tcx: &ty::ctxt, def: ast::DefId) -> Vec<ast_map::PathElem>
9697
}
9798

9899
pub enum FoundAst<'ast> {
99-
Found(&'ast ast::InlinedItem),
100-
FoundParent(ast::DefId, &'ast ast::InlinedItem),
100+
Found(&'ast InlinedItem),
101+
FoundParent(ast::DefId, &'ast InlinedItem),
101102
NotFound,
102103
}
103104

src/librustc/metadata/decoder.rs

+26-19
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ use metadata::csearch::MethodInfo;
2323
use metadata::csearch;
2424
use metadata::cstore;
2525
use metadata::encoder::def_to_u64;
26-
use metadata::tydecode::{parse_ty_data, parse_region_data,
27-
parse_type_param_def_data, parse_bare_fn_ty_data,
28-
parse_trait_ref_data, parse_predicate_data};
26+
use metadata::inline::InlinedItem;
27+
use metadata::tydecode::TyDecoder;
2928
use middle::def;
3029
use middle::lang_items;
3130
use middle::subst;
@@ -234,22 +233,25 @@ fn variant_disr_val(d: rbml::Doc) -> Option<ty::Disr> {
234233

235234
fn doc_type<'tcx>(doc: rbml::Doc, tcx: &ty::ctxt<'tcx>, cdata: Cmd) -> Ty<'tcx> {
236235
let tp = reader::get_doc(doc, tag_items_data_item_type);
237-
parse_ty_data(tp.data, cdata.cnum, tp.start, tcx,
238-
|_, did| translate_def_id(cdata, did))
236+
TyDecoder::with_doc(tcx, cdata.cnum, tp,
237+
&mut |_, did| translate_def_id(cdata, did))
238+
.parse_ty()
239239
}
240240

241241
fn maybe_doc_type<'tcx>(doc: rbml::Doc, tcx: &ty::ctxt<'tcx>, cdata: Cmd) -> Option<Ty<'tcx>> {
242242
reader::maybe_get_doc(doc, tag_items_data_item_type).map(|tp| {
243-
parse_ty_data(tp.data, cdata.cnum, tp.start, tcx,
244-
|_, did| translate_def_id(cdata, did))
243+
TyDecoder::with_doc(tcx, cdata.cnum, tp,
244+
&mut |_, did| translate_def_id(cdata, did))
245+
.parse_ty()
245246
})
246247
}
247248

248249
fn doc_method_fty<'tcx>(doc: rbml::Doc, tcx: &ty::ctxt<'tcx>,
249250
cdata: Cmd) -> ty::BareFnTy<'tcx> {
250251
let tp = reader::get_doc(doc, tag_item_method_fty);
251-
parse_bare_fn_ty_data(tp.data, cdata.cnum, tp.start, tcx,
252-
|_, did| translate_def_id(cdata, did))
252+
TyDecoder::with_doc(tcx, cdata.cnum, tp,
253+
&mut |_, did| translate_def_id(cdata, did))
254+
.parse_bare_fn_ty()
253255
}
254256

255257
pub fn item_type<'tcx>(_item_id: ast::DefId, item: rbml::Doc,
@@ -259,8 +261,9 @@ pub fn item_type<'tcx>(_item_id: ast::DefId, item: rbml::Doc,
259261

260262
fn doc_trait_ref<'tcx>(doc: rbml::Doc, tcx: &ty::ctxt<'tcx>, cdata: Cmd)
261263
-> ty::TraitRef<'tcx> {
262-
parse_trait_ref_data(doc.data, cdata.cnum, doc.start, tcx,
263-
|_, did| translate_def_id(cdata, did))
264+
TyDecoder::with_doc(tcx, cdata.cnum, doc,
265+
&mut |_, did| translate_def_id(cdata, did))
266+
.parse_trait_ref()
264267
}
265268

266269
fn item_trait_ref<'tcx>(doc: rbml::Doc, tcx: &ty::ctxt<'tcx>, cdata: Cmd)
@@ -776,7 +779,7 @@ pub type DecodeInlinedItem<'a> =
776779
&ty::ctxt<'tcx>,
777780
Vec<ast_map::PathElem>,
778781
rbml::Doc)
779-
-> Result<&'tcx ast::InlinedItem, Vec<ast_map::PathElem>> + 'a>;
782+
-> Result<&'tcx InlinedItem, Vec<ast_map::PathElem>> + 'a>;
780783

781784
pub fn maybe_get_item_ast<'tcx>(cdata: Cmd, tcx: &ty::ctxt<'tcx>, id: ast::NodeId,
782785
mut decode_inlined_item: DecodeInlinedItem)
@@ -1468,9 +1471,10 @@ fn doc_generics<'tcx>(base_doc: rbml::Doc,
14681471

14691472
let mut types = subst::VecPerParamSpace::empty();
14701473
for p in reader::tagged_docs(doc, tag_type_param_def) {
1471-
let bd = parse_type_param_def_data(
1472-
p.data, p.start, cdata.cnum, tcx,
1473-
|_, did| translate_def_id(cdata, did));
1474+
let bd =
1475+
TyDecoder::with_doc(tcx, cdata.cnum, p,
1476+
&mut |_, did| translate_def_id(cdata, did))
1477+
.parse_type_param_def();
14741478
types.push(bd.space, bd);
14751479
}
14761480

@@ -1490,8 +1494,9 @@ fn doc_generics<'tcx>(base_doc: rbml::Doc,
14901494
let index = reader::doc_as_u64(doc) as u32;
14911495

14921496
let bounds = reader::tagged_docs(rp_doc, tag_items_data_region).map(|p| {
1493-
parse_region_data(p.data, cdata.cnum, p.start, tcx,
1494-
|_, did| translate_def_id(cdata, did))
1497+
TyDecoder::with_doc(tcx, cdata.cnum, p,
1498+
&mut |_, did| translate_def_id(cdata, did))
1499+
.parse_region()
14951500
}).collect();
14961501

14971502
regions.push(space, ty::RegionParameterDef { name: name,
@@ -1518,8 +1523,10 @@ fn doc_predicates<'tcx>(base_doc: rbml::Doc,
15181523
let space = subst::ParamSpace::from_uint(reader::doc_as_u8(space_doc) as usize);
15191524

15201525
let data_doc = reader::get_doc(predicate_doc, tag_predicate_data);
1521-
let data = parse_predicate_data(data_doc.data, data_doc.start, cdata.cnum, tcx,
1522-
|_, did| translate_def_id(cdata, did));
1526+
let data =
1527+
TyDecoder::with_doc(tcx, cdata.cnum, data_doc,
1528+
&mut |_, did| translate_def_id(cdata, did))
1529+
.parse_predicate();
15231530

15241531
predicates.push(space, data);
15251532
}

src/librustc/metadata/encoder.rs

+11-19
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
#![allow(unused_must_use)] // everything is just a MemWriter, can't fail
1414
#![allow(non_camel_case_types)]
1515

16-
pub use self::InlinedItemRef::*;
17-
1816
use ast_map::{self, LinkedPath, PathElem, PathElems};
1917
use back::svh::Svh;
2018
use session::config;
2119
use metadata::common::*;
2220
use metadata::cstore;
2321
use metadata::decoder;
2422
use metadata::tyencode;
23+
use metadata::inline::InlinedItemRef;
2524
use middle::def;
2625
use middle::dependency_format::Linkage;
2726
use middle::stability;
@@ -48,14 +47,6 @@ use syntax::visit;
4847
use syntax;
4948
use rbml::writer::Encoder;
5049

51-
/// A borrowed version of `ast::InlinedItem`.
52-
pub enum InlinedItemRef<'a> {
53-
IIItemRef(&'a ast::Item),
54-
IITraitItemRef(DefId, &'a ast::TraitItem),
55-
IIImplItemRef(DefId, &'a ast::ImplItem),
56-
IIForeignRef(&'a ast::ForeignItem)
57-
}
58-
5950
pub type EncodeInlinedItem<'a> =
6051
Box<FnMut(&EncodeContext, &mut Encoder, InlinedItemRef) + 'a>;
6152

@@ -832,7 +823,7 @@ fn encode_info_for_associated_const(ecx: &EncodeContext,
832823

833824
if let Some(ii) = impl_item_opt {
834825
encode_attributes(rbml_w, &ii.attrs);
835-
encode_inlined_item(ecx, rbml_w, IIImplItemRef(local_def(parent_id), ii));
826+
encode_inlined_item(ecx, rbml_w, InlinedItemRef::ImplItem(local_def(parent_id), ii));
836827
}
837828

838829
rbml_w.end_tag();
@@ -870,7 +861,7 @@ fn encode_info_for_method<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
870861
let needs_inline = any_types || is_default_impl ||
871862
attr::requests_inline(&impl_item.attrs);
872863
if needs_inline || sig.constness == ast::Constness::Const {
873-
encode_inlined_item(ecx, rbml_w, IIImplItemRef(local_def(parent_id),
864+
encode_inlined_item(ecx, rbml_w, InlinedItemRef::ImplItem(local_def(parent_id),
874865
impl_item));
875866
}
876867
encode_constness(rbml_w, sig.constness);
@@ -1052,7 +1043,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
10521043
encode_name(rbml_w, item.ident.name);
10531044
encode_path(rbml_w, path);
10541045
encode_attributes(rbml_w, &item.attrs);
1055-
encode_inlined_item(ecx, rbml_w, IIItemRef(item));
1046+
encode_inlined_item(ecx, rbml_w, InlinedItemRef::Item(item));
10561047
encode_visibility(rbml_w, vis);
10571048
encode_stability(rbml_w, stab);
10581049
rbml_w.end_tag();
@@ -1069,7 +1060,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
10691060
encode_attributes(rbml_w, &item.attrs);
10701061
let needs_inline = tps_len > 0 || attr::requests_inline(&item.attrs);
10711062
if needs_inline || constness == ast::Constness::Const {
1072-
encode_inlined_item(ecx, rbml_w, IIItemRef(item));
1063+
encode_inlined_item(ecx, rbml_w, InlinedItemRef::Item(item));
10731064
}
10741065
if tps_len == 0 {
10751066
encode_symbol(ecx, rbml_w, item.id);
@@ -1134,7 +1125,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
11341125
for v in &enum_definition.variants {
11351126
encode_variant_id(rbml_w, local_def(v.node.id));
11361127
}
1137-
encode_inlined_item(ecx, rbml_w, IIItemRef(item));
1128+
encode_inlined_item(ecx, rbml_w, InlinedItemRef::Item(item));
11381129
encode_path(rbml_w, path);
11391130

11401131
// Encode inherent implementations for this enumeration.
@@ -1182,7 +1173,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
11821173
needs to know*/
11831174
encode_struct_fields(rbml_w, variant, def_id);
11841175

1185-
encode_inlined_item(ecx, rbml_w, IIItemRef(item));
1176+
encode_inlined_item(ecx, rbml_w, InlinedItemRef::Item(item));
11861177

11871178
// Encode inherent implementations for this structure.
11881179
encode_inherent_implementations(ecx, rbml_w, def_id);
@@ -1457,7 +1448,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
14571448
match trait_item.node {
14581449
ast::ConstTraitItem(_, _) => {
14591450
encode_inlined_item(ecx, rbml_w,
1460-
IITraitItemRef(def_id, trait_item));
1451+
InlinedItemRef::TraitItem(def_id, trait_item));
14611452
}
14621453
ast::MethodTraitItem(ref sig, ref body) => {
14631454
// If this is a static method, we've already
@@ -1471,7 +1462,8 @@ fn encode_info_for_item(ecx: &EncodeContext,
14711462

14721463
if body.is_some() {
14731464
encode_item_sort(rbml_w, 'p');
1474-
encode_inlined_item(ecx, rbml_w, IITraitItemRef(def_id, trait_item));
1465+
encode_inlined_item(ecx, rbml_w,
1466+
InlinedItemRef::TraitItem(def_id, trait_item));
14751467
} else {
14761468
encode_item_sort(rbml_w, 'r');
14771469
}
@@ -1510,7 +1502,7 @@ fn encode_info_for_foreign_item(ecx: &EncodeContext,
15101502
encode_bounds_and_type_for_item(rbml_w, ecx, nitem.id);
15111503
encode_name(rbml_w, nitem.ident.name);
15121504
if abi == abi::RustIntrinsic {
1513-
encode_inlined_item(ecx, rbml_w, IIForeignRef(nitem));
1505+
encode_inlined_item(ecx, rbml_w, InlinedItemRef::Foreign(nitem));
15141506
}
15151507
encode_attributes(rbml_w, &*nitem.attrs);
15161508
let stab = stability::lookup(ecx.tcx, ast_util::local_def(nitem.id));

src/librustc/metadata/inline.rs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
use syntax::ast;
12+
use syntax::ast_util::{IdRange, IdRangeComputingVisitor,
13+
IdVisitor, IdVisitingOperation};
14+
use syntax::ptr::P;
15+
use syntax::visit::Visitor;
16+
use self::InlinedItem::*;
17+
18+
/// The data we save and restore about an inlined item or method. This is not
19+
/// part of the AST that we parse from a file, but it becomes part of the tree
20+
/// that we trans.
21+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
22+
pub enum InlinedItem {
23+
Item(P<ast::Item>),
24+
TraitItem(ast::DefId /* impl id */, P<ast::TraitItem>),
25+
ImplItem(ast::DefId /* impl id */, P<ast::ImplItem>),
26+
Foreign(P<ast::ForeignItem>),
27+
}
28+
29+
/// A borrowed version of `ast::InlinedItem`.
30+
pub enum InlinedItemRef<'a> {
31+
Item(&'a ast::Item),
32+
TraitItem(ast::DefId, &'a ast::TraitItem),
33+
ImplItem(ast::DefId, &'a ast::ImplItem),
34+
Foreign(&'a ast::ForeignItem)
35+
}
36+
37+
impl InlinedItem {
38+
pub fn visit<'ast,V>(&'ast self, visitor: &mut V)
39+
where V: Visitor<'ast>
40+
{
41+
match *self {
42+
Item(ref i) => visitor.visit_item(&**i),
43+
Foreign(ref i) => visitor.visit_foreign_item(&**i),
44+
TraitItem(_, ref ti) => visitor.visit_trait_item(ti),
45+
ImplItem(_, ref ii) => visitor.visit_impl_item(ii),
46+
}
47+
}
48+
49+
pub fn visit_ids<O: IdVisitingOperation>(&self, operation: &mut O) {
50+
let mut id_visitor = IdVisitor {
51+
operation: operation,
52+
pass_through_items: true,
53+
visited_outermost: false,
54+
};
55+
self.visit(&mut id_visitor);
56+
}
57+
58+
pub fn compute_id_range(&self) -> IdRange {
59+
let mut visitor = IdRangeComputingVisitor::new();
60+
self.visit_ids(&mut visitor);
61+
visitor.result()
62+
}
63+
}
64+

src/librustc/metadata/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ pub mod csearch;
1919
pub mod loader;
2020
pub mod filesearch;
2121
pub mod macro_import;
22+
pub mod inline;

0 commit comments

Comments
 (0)