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

Queryify crate metadata #41611

Merged
merged 3 commits into from
May 1, 2017
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
15 changes: 0 additions & 15 deletions src/librustc/middle/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use session::search_paths::PathKind;
use util::nodemap::{NodeSet, DefIdMap};

use std::any::Any;
use std::collections::BTreeMap;
use std::path::PathBuf;
use std::rc::Rc;
use syntax::ast;
Expand Down Expand Up @@ -250,10 +249,6 @@ pub trait CrateStore {
// misc. metadata
fn item_body<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
-> &'tcx hir::Body;
fn item_body_nested_bodies(&self, def: DefId) -> BTreeMap<hir::BodyId, hir::Body>;
fn const_is_rvalue_promotable_to_static(&self, def: DefId) -> bool;

fn is_item_mir_available(&self, def: DefId) -> bool;

// This is basically a 1-based range of ints, which is a little
// silly - I may fix that.
Expand Down Expand Up @@ -401,16 +396,6 @@ impl CrateStore for DummyCrateStore {
-> &'tcx hir::Body {
bug!("item_body")
}
fn item_body_nested_bodies(&self, def: DefId) -> BTreeMap<hir::BodyId, hir::Body> {
bug!("item_body_nested_bodies")
}
fn const_is_rvalue_promotable_to_static(&self, def: DefId) -> bool {
bug!("const_is_rvalue_promotable_to_static")
}

fn is_item_mir_available(&self, def: DefId) -> bool {
bug!("is_item_mir_available")
}

// This is basically a 1-based range of ints, which is a little
// silly - I may fix that.
Expand Down
35 changes: 32 additions & 3 deletions src/librustc/ty/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use util::nodemap::NodeSet;
use rustc_data_structures::indexed_vec::IndexVec;
use std::cell::{RefCell, RefMut};
use std::mem;
use std::collections::BTreeMap;
use std::ops::Deref;
use std::rc::Rc;
use syntax_pos::{Span, DUMMY_SP};
Expand Down Expand Up @@ -291,10 +292,30 @@ impl<'tcx> QueryDescription for queries::def_span<'tcx> {
}
}

impl<'tcx> QueryDescription for queries::item_body_nested_bodies<'tcx> {
fn describe(tcx: TyCtxt, def_id: DefId) -> String {
format!("nested item bodies of `{}`", tcx.item_path_str(def_id))
}
}

impl<'tcx> QueryDescription for queries::const_is_rvalue_promotable_to_static<'tcx> {
fn describe(tcx: TyCtxt, def_id: DefId) -> String {
format!("const checking if rvalue is promotable to static `{}`",
tcx.item_path_str(def_id))
}
}

impl<'tcx> QueryDescription for queries::is_item_mir_available<'tcx> {
fn describe(tcx: TyCtxt, def_id: DefId) -> String {
format!("checking if item is mir available: `{}`",
tcx.item_path_str(def_id))
}
}

macro_rules! define_maps {
(<$tcx:tt>
$($(#[$attr:meta])*
[$($pub:tt)*] $name:ident: $node:ident($K:ty) -> $V:ty),*) => {
[$($pub:tt)*] $name:ident: $node:ident($K:ty) -> $V:ty,)*) => {
pub struct Maps<$tcx> {
providers: IndexVec<CrateNum, Providers<$tcx>>,
query_stack: RefCell<Vec<(Span, Query<$tcx>)>>,
Expand Down Expand Up @@ -577,7 +598,11 @@ define_maps! { <'tcx>
[] symbol_name: symbol_name_dep_node(ty::Instance<'tcx>) -> ty::SymbolName,

[] describe_def: DescribeDef(DefId) -> Option<Def>,
[] def_span: DefSpan(DefId) -> Span
[] def_span: DefSpan(DefId) -> Span,

[] item_body_nested_bodies: metadata_dep_node(DefId) -> Rc<BTreeMap<hir::BodyId, hir::Body>>,
[] const_is_rvalue_promotable_to_static: metadata_dep_node(DefId) -> bool,
[] is_item_mir_available: metadata_dep_node(DefId) -> bool,
}

fn coherent_trait_dep_node((_, def_id): (CrateNum, DefId)) -> DepNode<DefId> {
Expand All @@ -592,6 +617,10 @@ fn reachability_dep_node(_: CrateNum) -> DepNode<DefId> {
DepNode::Reachability
}

fn metadata_dep_node(def_id: DefId) -> DepNode<DefId> {
DepNode::MetaData(def_id)
}

fn mir_shim_dep_node(instance: ty::InstanceDef) -> DepNode<DefId> {
instance.dep_node()
}
Expand All @@ -608,4 +637,4 @@ fn typeck_item_bodies_dep_node(_: CrateNum) -> DepNode<DefId> {

fn const_eval_dep_node((def_id, _): (DefId, &Substs)) -> DepNode<DefId> {
DepNode::ConstEval(def_id)
}
}
2 changes: 1 addition & 1 deletion src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2332,7 +2332,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
return None;
}

if !did.is_local() && !self.sess.cstore.is_item_mir_available(did) {
if !did.is_local() && !self.is_item_mir_available(did) {
return None;
}

Expand Down
30 changes: 15 additions & 15 deletions src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ provide! { <'tcx> tcx, def_id, cdata
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
describe_def => { cdata.get_def(def_id.index) }
def_span => { cdata.get_span(def_id.index, &tcx.sess) }
item_body_nested_bodies => {
let map: BTreeMap<_, _> = cdata.entry(def_id.index).ast.into_iter().flat_map(|ast| {
ast.decode(cdata).nested_bodies.decode(cdata).map(|body| (body.id(), body))
}).collect();

Rc::new(map)
}
const_is_rvalue_promotable_to_static => {
cdata.entry(def_id.index).ast.expect("const item missing `ast`")
.decode(cdata).rvalue_promotable_to_static
}
is_item_mir_available => {
!cdata.is_proc_macro(def_id.index) &&
cdata.maybe_entry(def_id.index).and_then(|item| item.decode(cdata).mir).is_some()
}
}

impl CrateStore for cstore::CStore {
Expand Down Expand Up @@ -432,21 +447,6 @@ impl CrateStore for cstore::CStore {
self.get_crate_data(def_id.krate).item_body(tcx, def_id.index)
}

fn item_body_nested_bodies(&self, def: DefId) -> BTreeMap<hir::BodyId, hir::Body> {
self.dep_graph.read(DepNode::MetaData(def));
self.get_crate_data(def.krate).item_body_nested_bodies(def.index)
}

fn const_is_rvalue_promotable_to_static(&self, def: DefId) -> bool {
self.dep_graph.read(DepNode::MetaData(def));
self.get_crate_data(def.krate).const_is_rvalue_promotable_to_static(def.index)
}

fn is_item_mir_available(&self, def: DefId) -> bool {
self.dep_graph.read(DepNode::MetaData(def));
self.get_crate_data(def.krate).is_item_mir_available(def.index)
}

fn crates(&self) -> Vec<CrateNum>
{
let mut result = vec![];
Expand Down
23 changes: 3 additions & 20 deletions src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use rustc::mir::Mir;

use std::borrow::Cow;
use std::cell::Ref;
use std::collections::BTreeMap;
use std::io;
use std::mem;
use std::rc::Rc;
Expand Down Expand Up @@ -442,16 +441,16 @@ impl<'tcx> EntryKind<'tcx> {
}

impl<'a, 'tcx> CrateMetadata {
fn is_proc_macro(&self, id: DefIndex) -> bool {
pub fn is_proc_macro(&self, id: DefIndex) -> bool {
self.proc_macros.is_some() && id != CRATE_DEF_INDEX
}

fn maybe_entry(&self, item_id: DefIndex) -> Option<Lazy<Entry<'tcx>>> {
pub fn maybe_entry(&self, item_id: DefIndex) -> Option<Lazy<Entry<'tcx>>> {
assert!(!self.is_proc_macro(item_id));
self.root.index.lookup(self.blob.raw_bytes(), item_id)
}

fn entry(&self, item_id: DefIndex) -> Entry<'tcx> {
pub fn entry(&self, item_id: DefIndex) -> Entry<'tcx> {
match self.maybe_entry(item_id) {
None => {
bug!("entry: id not found: {:?} in crate {:?} with number {}",
Expand Down Expand Up @@ -773,22 +772,6 @@ impl<'a, 'tcx> CrateMetadata {
tcx.alloc_tables(ast.tables.decode((self, tcx)))
}

pub fn item_body_nested_bodies(&self, id: DefIndex) -> BTreeMap<hir::BodyId, hir::Body> {
self.entry(id).ast.into_iter().flat_map(|ast| {
ast.decode(self).nested_bodies.decode(self).map(|body| (body.id(), body))
}).collect()
}

pub fn const_is_rvalue_promotable_to_static(&self, id: DefIndex) -> bool {
self.entry(id).ast.expect("const item missing `ast`")
.decode(self).rvalue_promotable_to_static
}

pub fn is_item_mir_available(&self, id: DefIndex) -> bool {
!self.is_proc_macro(id) &&
self.maybe_entry(id).and_then(|item| item.decode(self).mir).is_some()
}

pub fn maybe_get_item_mir(&self,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
id: DefIndex)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_passes/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Expr, node
_ => false
}
} else {
v.tcx.sess.cstore.const_is_rvalue_promotable_to_static(did)
v.tcx.const_is_rvalue_promotable_to_static(did)
};
}
_ => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ fn should_trans_locally<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: &Instan
// in this crate
false
} else {
if !tcx.sess.cstore.is_item_mir_available(def_id) {
if !tcx.is_item_mir_available(def_id) {
bug!("Cannot create local trans-item for {:?}", def_id)
}
true
Expand Down
5 changes: 3 additions & 2 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use std::collections::BTreeMap;
use std::io;
use std::iter::once;
use std::rc::Rc;

use syntax::ast;
use rustc::hir;
Expand Down Expand Up @@ -471,7 +472,7 @@ fn build_module(cx: &DocContext, did: DefId) -> clean::Module {
}

struct InlinedConst {
nested_bodies: BTreeMap<hir::BodyId, hir::Body>
nested_bodies: Rc<BTreeMap<hir::BodyId, hir::Body>>
}

impl hir::print::PpAnn for InlinedConst {
Expand All @@ -488,7 +489,7 @@ impl hir::print::PpAnn for InlinedConst {
fn print_inlined_const(cx: &DocContext, did: DefId) -> String {
let body = cx.tcx.sess.cstore.item_body(cx.tcx, did);
let inlined = InlinedConst {
nested_bodies: cx.tcx.sess.cstore.item_body_nested_bodies(did)
nested_bodies: cx.tcx.item_body_nested_bodies(did)
};
hir::print::to_string(&inlined, |s| s.print_expr(&body.value))
}
Expand Down