Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup ty decoder #27851

Merged
merged 7 commits into from
Aug 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions src/librustc/ast_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub use self::Node::*;
pub use self::PathElem::*;
use self::MapEntry::*;

use metadata::inline::InlinedItem;
use metadata::inline::InlinedItem as II;
use syntax::abi;
use syntax::ast::*;
use syntax::ast_util;
Expand Down Expand Up @@ -374,8 +376,8 @@ impl<'ast> Map<'ast> {
pub fn get_parent_did(&self, id: NodeId) -> DefId {
let parent = self.get_parent(id);
match self.find_entry(parent) {
Some(RootInlinedParent(&InlinedParent {ii: IITraitItem(did, _), ..})) => did,
Some(RootInlinedParent(&InlinedParent {ii: IIImplItem(did, _), ..})) => did,
Some(RootInlinedParent(&InlinedParent {ii: II::TraitItem(did, _), ..})) => did,
Some(RootInlinedParent(&InlinedParent {ii: II::ImplItem(did, _), ..})) => did,
_ => ast_util::local_def(parent)
}
}
Expand Down Expand Up @@ -967,16 +969,16 @@ pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
-> &'ast InlinedItem {
let mut fld = IdAndSpanUpdater { fold_ops: fold_ops };
let ii = match ii {
IIItem(i) => IIItem(fld.fold_item(i).expect_one("expected one item")),
IITraitItem(d, ti) => {
IITraitItem(fld.fold_ops.new_def_id(d),
fld.fold_trait_item(ti).expect_one("expected one trait item"))
II::Item(i) => II::Item(fld.fold_item(i).expect_one("expected one item")),
II::TraitItem(d, ti) => {
II::TraitItem(fld.fold_ops.new_def_id(d),
fld.fold_trait_item(ti).expect_one("expected one trait item"))
}
IIImplItem(d, ii) => {
IIImplItem(fld.fold_ops.new_def_id(d),
fld.fold_impl_item(ii).expect_one("expected one impl item"))
II::ImplItem(d, ii) => {
II::ImplItem(fld.fold_ops.new_def_id(d),
fld.fold_impl_item(ii).expect_one("expected one impl item"))
}
IIForeign(i) => IIForeign(fld.fold_foreign_item(i))
II::Foreign(i) => II::Foreign(fld.fold_foreign_item(i))
};

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

// Methods get added to the AST map when their impl is visited. Since we
// don't decode and instantiate the impl, but just the method, we have to
// add it to the table now. Likewise with foreign items.
match ii_parent.ii {
IIItem(_) => {}
IITraitItem(_, ref ti) => {
II::Item(_) => {}
II::TraitItem(_, ref ti) => {
collector.insert(ti.id, NodeTraitItem(ti));
}
IIImplItem(_, ref ii) => {
II::ImplItem(_, ref ii) => {
collector.insert(ii.id, NodeImplItem(ii));
}
IIForeign(ref i) => {
II::Foreign(ref i) => {
collector.insert(i.id, NodeForeignItem(i));
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/librustc/metadata/csearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use ast_map;
use metadata::common::*;
use metadata::cstore;
use metadata::decoder;
use metadata::inline::InlinedItem;
use middle::lang_items;
use middle::ty;

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

pub enum FoundAst<'ast> {
Found(&'ast ast::InlinedItem),
FoundParent(ast::DefId, &'ast ast::InlinedItem),
Found(&'ast InlinedItem),
FoundParent(ast::DefId, &'ast InlinedItem),
NotFound,
}

Expand Down
45 changes: 26 additions & 19 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ use metadata::csearch::MethodInfo;
use metadata::csearch;
use metadata::cstore;
use metadata::encoder::def_to_u64;
use metadata::tydecode::{parse_ty_data, parse_region_data,
parse_type_param_def_data, parse_bare_fn_ty_data,
parse_trait_ref_data, parse_predicate_data};
use metadata::inline::InlinedItem;
use metadata::tydecode::TyDecoder;
use middle::def;
use middle::lang_items;
use middle::subst;
Expand Down Expand Up @@ -234,22 +233,25 @@ fn variant_disr_val(d: rbml::Doc) -> Option<ty::Disr> {

fn doc_type<'tcx>(doc: rbml::Doc, tcx: &ty::ctxt<'tcx>, cdata: Cmd) -> Ty<'tcx> {
let tp = reader::get_doc(doc, tag_items_data_item_type);
parse_ty_data(tp.data, cdata.cnum, tp.start, tcx,
|_, did| translate_def_id(cdata, did))
TyDecoder::with_doc(tcx, cdata.cnum, tp,
&mut |_, did| translate_def_id(cdata, did))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this be created by a method on cdata, providing the translation function to TyDecoder?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eddyb hmm perhaps -- it's a bit tricky to get lifetime of the closure to work out I think, but I guess now that the closure is stored in a struct, we could give ownership of it instead of passing an &mut. Would be kind of nicer anyway. Maybe I should do that.

.parse_ty()
}

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

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

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

fn doc_trait_ref<'tcx>(doc: rbml::Doc, tcx: &ty::ctxt<'tcx>, cdata: Cmd)
-> ty::TraitRef<'tcx> {
parse_trait_ref_data(doc.data, cdata.cnum, doc.start, tcx,
|_, did| translate_def_id(cdata, did))
TyDecoder::with_doc(tcx, cdata.cnum, doc,
&mut |_, did| translate_def_id(cdata, did))
.parse_trait_ref()
}

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

pub fn maybe_get_item_ast<'tcx>(cdata: Cmd, tcx: &ty::ctxt<'tcx>, id: ast::NodeId,
mut decode_inlined_item: DecodeInlinedItem)
Expand Down Expand Up @@ -1464,9 +1467,10 @@ fn doc_generics<'tcx>(base_doc: rbml::Doc,

let mut types = subst::VecPerParamSpace::empty();
for p in reader::tagged_docs(doc, tag_type_param_def) {
let bd = parse_type_param_def_data(
p.data, p.start, cdata.cnum, tcx,
|_, did| translate_def_id(cdata, did));
let bd =
TyDecoder::with_doc(tcx, cdata.cnum, p,
&mut |_, did| translate_def_id(cdata, did))
.parse_type_param_def();
types.push(bd.space, bd);
}

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

let bounds = reader::tagged_docs(rp_doc, tag_items_data_region).map(|p| {
parse_region_data(p.data, cdata.cnum, p.start, tcx,
|_, did| translate_def_id(cdata, did))
TyDecoder::with_doc(tcx, cdata.cnum, p,
&mut |_, did| translate_def_id(cdata, did))
.parse_region()
}).collect();

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

let data_doc = reader::get_doc(predicate_doc, tag_predicate_data);
let data = parse_predicate_data(data_doc.data, data_doc.start, cdata.cnum, tcx,
|_, did| translate_def_id(cdata, did));
let data =
TyDecoder::with_doc(tcx, cdata.cnum, data_doc,
&mut |_, did| translate_def_id(cdata, did))
.parse_predicate();

predicates.push(space, data);
}
Expand Down
30 changes: 11 additions & 19 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
#![allow(unused_must_use)] // everything is just a MemWriter, can't fail
#![allow(non_camel_case_types)]

pub use self::InlinedItemRef::*;

use ast_map::{self, LinkedPath, PathElem, PathElems};
use back::svh::Svh;
use session::config;
use metadata::common::*;
use metadata::cstore;
use metadata::decoder;
use metadata::tyencode;
use metadata::inline::InlinedItemRef;
use middle::def;
use middle::ty::{self, Ty};
use middle::stability;
Expand All @@ -46,14 +45,6 @@ use syntax::visit;
use syntax;
use rbml::writer::Encoder;

/// A borrowed version of `ast::InlinedItem`.
pub enum InlinedItemRef<'a> {
IIItemRef(&'a ast::Item),
IITraitItemRef(DefId, &'a ast::TraitItem),
IIImplItemRef(DefId, &'a ast::ImplItem),
IIForeignRef(&'a ast::ForeignItem)
}

pub type EncodeInlinedItem<'a> =
Box<FnMut(&EncodeContext, &mut Encoder, InlinedItemRef) + 'a>;

Expand Down Expand Up @@ -830,7 +821,7 @@ fn encode_info_for_associated_const(ecx: &EncodeContext,

if let Some(ii) = impl_item_opt {
encode_attributes(rbml_w, &ii.attrs);
encode_inlined_item(ecx, rbml_w, IIImplItemRef(local_def(parent_id), ii));
encode_inlined_item(ecx, rbml_w, InlinedItemRef::ImplItem(local_def(parent_id), ii));
}

rbml_w.end_tag();
Expand Down Expand Up @@ -868,7 +859,7 @@ fn encode_info_for_method<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
let needs_inline = any_types || is_default_impl ||
attr::requests_inline(&impl_item.attrs);
if needs_inline || sig.constness == ast::Constness::Const {
encode_inlined_item(ecx, rbml_w, IIImplItemRef(local_def(parent_id),
encode_inlined_item(ecx, rbml_w, InlinedItemRef::ImplItem(local_def(parent_id),
impl_item));
}
encode_constness(rbml_w, sig.constness);
Expand Down Expand Up @@ -1050,7 +1041,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
encode_name(rbml_w, item.ident.name);
encode_path(rbml_w, path);
encode_attributes(rbml_w, &item.attrs);
encode_inlined_item(ecx, rbml_w, IIItemRef(item));
encode_inlined_item(ecx, rbml_w, InlinedItemRef::Item(item));
encode_visibility(rbml_w, vis);
encode_stability(rbml_w, stab);
rbml_w.end_tag();
Expand All @@ -1067,7 +1058,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
encode_attributes(rbml_w, &item.attrs);
let needs_inline = tps_len > 0 || attr::requests_inline(&item.attrs);
if needs_inline || constness == ast::Constness::Const {
encode_inlined_item(ecx, rbml_w, IIItemRef(item));
encode_inlined_item(ecx, rbml_w, InlinedItemRef::Item(item));
}
if tps_len == 0 {
encode_symbol(ecx, rbml_w, item.id);
Expand Down Expand Up @@ -1132,7 +1123,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
for v in &enum_definition.variants {
encode_variant_id(rbml_w, local_def(v.node.id));
}
encode_inlined_item(ecx, rbml_w, IIItemRef(item));
encode_inlined_item(ecx, rbml_w, InlinedItemRef::Item(item));
encode_path(rbml_w, path);

// Encode inherent implementations for this enumeration.
Expand Down Expand Up @@ -1180,7 +1171,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
needs to know*/
encode_struct_fields(rbml_w, variant, def_id);

encode_inlined_item(ecx, rbml_w, IIItemRef(item));
encode_inlined_item(ecx, rbml_w, InlinedItemRef::Item(item));

// Encode inherent implementations for this structure.
encode_inherent_implementations(ecx, rbml_w, def_id);
Expand Down Expand Up @@ -1455,7 +1446,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
match trait_item.node {
ast::ConstTraitItem(_, _) => {
encode_inlined_item(ecx, rbml_w,
IITraitItemRef(def_id, trait_item));
InlinedItemRef::TraitItem(def_id, trait_item));
}
ast::MethodTraitItem(ref sig, ref body) => {
// If this is a static method, we've already
Expand All @@ -1469,7 +1460,8 @@ fn encode_info_for_item(ecx: &EncodeContext,

if body.is_some() {
encode_item_sort(rbml_w, 'p');
encode_inlined_item(ecx, rbml_w, IITraitItemRef(def_id, trait_item));
encode_inlined_item(ecx, rbml_w,
InlinedItemRef::TraitItem(def_id, trait_item));
} else {
encode_item_sort(rbml_w, 'r');
}
Expand Down Expand Up @@ -1508,7 +1500,7 @@ fn encode_info_for_foreign_item(ecx: &EncodeContext,
encode_bounds_and_type_for_item(rbml_w, ecx, nitem.id);
encode_name(rbml_w, nitem.ident.name);
if abi == abi::RustIntrinsic {
encode_inlined_item(ecx, rbml_w, IIForeignRef(nitem));
encode_inlined_item(ecx, rbml_w, InlinedItemRef::Foreign(nitem));
}
encode_attributes(rbml_w, &*nitem.attrs);
let stab = stability::lookup(ecx.tcx, ast_util::local_def(nitem.id));
Expand Down
64 changes: 64 additions & 0 deletions src/librustc/metadata/inline.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use syntax::ast;
use syntax::ast_util::{IdRange, IdRangeComputingVisitor,
IdVisitor, IdVisitingOperation};
use syntax::ptr::P;
use syntax::visit::Visitor;
use self::InlinedItem::*;

/// The data we save and restore about an inlined item or method. This is not
/// part of the AST that we parse from a file, but it becomes part of the tree
/// that we trans.
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum InlinedItem {
Item(P<ast::Item>),
TraitItem(ast::DefId /* impl id */, P<ast::TraitItem>),
ImplItem(ast::DefId /* impl id */, P<ast::ImplItem>),
Foreign(P<ast::ForeignItem>),
}

/// A borrowed version of `ast::InlinedItem`.
pub enum InlinedItemRef<'a> {
Item(&'a ast::Item),
TraitItem(ast::DefId, &'a ast::TraitItem),
ImplItem(ast::DefId, &'a ast::ImplItem),
Foreign(&'a ast::ForeignItem)
}

impl InlinedItem {
pub fn visit<'ast,V>(&'ast self, visitor: &mut V)
where V: Visitor<'ast>
{
match *self {
Item(ref i) => visitor.visit_item(&**i),
Foreign(ref i) => visitor.visit_foreign_item(&**i),
TraitItem(_, ref ti) => visitor.visit_trait_item(ti),
ImplItem(_, ref ii) => visitor.visit_impl_item(ii),
}
}

pub fn visit_ids<O: IdVisitingOperation>(&self, operation: &mut O) {
let mut id_visitor = IdVisitor {
operation: operation,
pass_through_items: true,
visited_outermost: false,
};
self.visit(&mut id_visitor);
}

pub fn compute_id_range(&self) -> IdRange {
let mut visitor = IdRangeComputingVisitor::new();
self.visit_ids(&mut visitor);
visitor.result()
}
}

1 change: 1 addition & 0 deletions src/librustc/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ pub mod csearch;
pub mod loader;
pub mod filesearch;
pub mod macro_import;
pub mod inline;
Loading