Skip to content

Commit

Permalink
rustc_trans: Refactor collection to use tcx
Browse files Browse the repository at this point in the history
This commit refactors the `collect_crate_translation_items` function to only
require the `TyCtxt` instead of a `SharedCrateContext` in preparation for
query-ifying this portion of trans.
  • Loading branch information
alexcrichton committed Sep 17, 2017
1 parent 1cdd689 commit c72240a
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 155 deletions.
2 changes: 1 addition & 1 deletion src/librustc_trans/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ pub struct FnType<'tcx> {
impl<'a, 'tcx> FnType<'tcx> {
pub fn of_instance(ccx: &CrateContext<'a, 'tcx>, instance: &ty::Instance<'tcx>)
-> Self {
let fn_ty = instance_ty(ccx.shared(), &instance);
let fn_ty = instance_ty(ccx.tcx(), &instance);
let sig = ty_fn_sig(ccx, fn_ty);
let sig = ccx.tcx().erase_late_bound_regions_and_normalize(&sig);
Self::new(ccx, sig, &[])
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ pub fn trans_instance<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, instance: Instance
// release builds.
info!("trans_instance({})", instance);

let fn_ty = common::instance_ty(ccx.shared(), &instance);
let fn_ty = common::instance_ty(ccx.tcx(), &instance);
let sig = common::ty_fn_sig(ccx, fn_ty);
let sig = ccx.tcx().erase_late_bound_regions_and_normalize(&sig);

Expand Down Expand Up @@ -1424,7 +1424,7 @@ fn collect_and_partition_translation_items<'a, 'tcx>(scx: &SharedCrateContext<'a

let (items, inlining_map) =
time(time_passes, "translation item collection", || {
collector::collect_crate_translation_items(&scx,
collector::collect_crate_translation_items(scx.tcx(),
exported_symbols,
collection_mode)
});
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_trans/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
assert!(!instance.substs.has_escaping_regions());
assert!(!instance.substs.has_param_types());

let fn_ty = common::instance_ty(ccx.shared(), &instance);
let fn_ty = common::instance_ty(ccx.tcx(), &instance);
if let Some(&llfn) = ccx.instances().borrow().get(&instance) {
return llfn;
}
Expand Down Expand Up @@ -148,5 +148,5 @@ pub fn resolve_and_get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
substs: &'tcx Substs<'tcx>)
-> ValueRef
{
get_fn(ccx, monomorphize::resolve(ccx.shared(), def_id, substs))
get_fn(ccx, monomorphize::resolve(ccx.tcx(), def_id, substs))
}
188 changes: 93 additions & 95 deletions src/librustc_trans/collector.rs

Large diffs are not rendered by default.

27 changes: 20 additions & 7 deletions src/librustc_trans/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use machine;
use monomorphize;
use type_::Type;
use value::Value;
use rustc::traits;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::layout::{Layout, LayoutTyper};
use rustc::ty::subst::{Kind, Subst, Substs};
Expand All @@ -37,7 +38,7 @@ use std::iter;
use syntax::abi::Abi;
use syntax::attr;
use syntax::symbol::InternedString;
use syntax_pos::Span;
use syntax_pos::{Span, DUMMY_SP};

pub use context::{CrateContext, SharedCrateContext};

Expand Down Expand Up @@ -140,6 +141,18 @@ pub fn type_is_zero_size<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -
!layout.is_unsized() && layout.size(ccx).bytes() == 0
}

pub fn type_needs_drop<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
ty.needs_drop(tcx, ty::ParamEnv::empty(traits::Reveal::All))
}

pub fn type_is_sized<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
ty.is_sized(tcx, ty::ParamEnv::empty(traits::Reveal::All), DUMMY_SP)
}

pub fn type_is_freeze<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
ty.is_freeze(tcx, ty::ParamEnv::empty(traits::Reveal::All), DUMMY_SP)
}

/*
* A note on nomenclature of linking: "extern", "foreign", and "upcall".
*
Expand Down Expand Up @@ -573,20 +586,20 @@ pub fn is_inline_instance<'a, 'tcx>(
}

/// Given a DefId and some Substs, produces the monomorphic item type.
pub fn def_ty<'a, 'tcx>(shared: &SharedCrateContext<'a, 'tcx>,
pub fn def_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId,
substs: &'tcx Substs<'tcx>)
-> Ty<'tcx>
{
let ty = shared.tcx().type_of(def_id);
shared.tcx().trans_apply_param_substs(substs, &ty)
let ty = tcx.type_of(def_id);
tcx.trans_apply_param_substs(substs, &ty)
}

/// Return the substituted type of an instance.
pub fn instance_ty<'a, 'tcx>(shared: &SharedCrateContext<'a, 'tcx>,
pub fn instance_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
instance: &ty::Instance<'tcx>)
-> Ty<'tcx>
{
let ty = instance.def.def_ty(shared.tcx());
shared.tcx().trans_apply_param_substs(instance.substs, &ty)
let ty = instance.def.def_ty(tcx);
tcx.trans_apply_param_substs(instance.substs, &ty)
}
4 changes: 2 additions & 2 deletions src/librustc_trans/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub fn get_static(ccx: &CrateContext, def_id: DefId) -> ValueRef {
return g;
}

let ty = common::instance_ty(ccx.shared(), &instance);
let ty = common::instance_ty(ccx.tcx(), &instance);
let g = if let Some(id) = ccx.tcx().hir.as_local_node_id(def_id) {

let llty = type_of::type_of(ccx, ty);
Expand Down Expand Up @@ -269,7 +269,7 @@ pub fn trans_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
};

let instance = Instance::mono(ccx.tcx(), def_id);
let ty = common::instance_ty(ccx.shared(), &instance);
let ty = common::instance_ty(ccx.tcx(), &instance);
let llty = type_of::type_of(ccx, ty);
let g = if val_llty == llty {
g
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_trans/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use common;
use llvm;
use llvm::{ContextRef, ModuleRef, ValueRef};
use rustc::dep_graph::{DepGraph, DepGraphSafe};
Expand Down Expand Up @@ -39,7 +40,6 @@ use std::str;
use std::sync::Arc;
use std::marker::PhantomData;
use syntax::symbol::InternedString;
use syntax_pos::DUMMY_SP;
use abi::Abi;

#[derive(Clone, Default)]
Expand Down Expand Up @@ -319,15 +319,15 @@ impl<'b, 'tcx> SharedCrateContext<'b, 'tcx> {
}

pub fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool {
ty.needs_drop(self.tcx, ty::ParamEnv::empty(traits::Reveal::All))
common::type_needs_drop(self.tcx, ty)
}

pub fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
ty.is_sized(self.tcx, ty::ParamEnv::empty(traits::Reveal::All), DUMMY_SP)
common::type_is_sized(self.tcx, ty)
}

pub fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
ty.is_freeze(self.tcx, ty::ParamEnv::empty(traits::Reveal::All), DUMMY_SP)
common::type_is_freeze(self.tcx, ty)
}

pub fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1803,7 +1803,7 @@ pub fn create_global_var_metadata(cx: &CrateContext,
};

let is_local_to_unit = is_node_local_to_unit(cx, node_id);
let variable_type = common::def_ty(cx.shared(), node_def_id, Substs::empty());
let variable_type = common::def_ty(cx.tcx(), node_def_id, Substs::empty());
let type_metadata = type_metadata(cx, variable_type, span);
let var_name = tcx.item_name(node_def_id).to_string();
let linkage_name = mangled_name_of_item(cx, node_def_id, "");
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/debuginfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
// If the method does *not* belong to a trait, proceed
if cx.tcx().trait_id_of_impl(impl_def_id).is_none() {
let impl_self_ty =
common::def_ty(cx.shared(), impl_def_id, instance.substs);
common::def_ty(cx.tcx(), impl_def_id, instance.substs);

// Only "class" methods are generally understood by LLVM,
// so avoid methods on other types (e.g. `<*mut T>::null`).
Expand Down
11 changes: 6 additions & 5 deletions src/librustc_trans/glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@

use std;

use llvm;
use llvm::{ValueRef};
use rustc::ty::{self, Ty};
use rustc::ty::layout::LayoutTyper;
use builder::Builder;
use common::*;
use llvm::{ValueRef};
use llvm;
use meth;
use monomorphize;
use rustc::traits;
use rustc::ty::layout::LayoutTyper;
use rustc::ty::{self, Ty, TypeFoldable, TyCtxt};
use value::Value;
use builder::Builder;

pub fn size_and_align_of_dst<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, t: Ty<'tcx>, info: ValueRef)
-> (ValueRef, ValueRef) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/meth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn get_vtable<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
let nullptr = C_null(Type::nil(ccx).ptr_to());

let mut components: Vec<_> = [
callee::get_fn(ccx, monomorphize::resolve_drop_in_place(ccx.shared(), ty)),
callee::get_fn(ccx, monomorphize::resolve_drop_in_place(ccx.tcx(), ty)),
C_usize(ccx, ccx.size_of(ty)),
C_usize(ccx, ccx.align_of(ty) as u64)
].iter().cloned().collect();
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_trans/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
mir::TerminatorKind::Drop { ref location, target, unwind } => {
let ty = location.ty(self.mir, bcx.tcx()).to_ty(bcx.tcx());
let ty = self.monomorphize(&ty);
let drop_fn = monomorphize::resolve_drop_in_place(bcx.ccx.shared(), ty);
let drop_fn = monomorphize::resolve_drop_in_place(bcx.ccx.tcx(), ty);

if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def {
// we don't actually need to drop anything.
Expand Down Expand Up @@ -429,7 +429,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {

let (instance, mut llfn) = match callee.ty.sty {
ty::TyFnDef(def_id, substs) => {
(Some(monomorphize::resolve(bcx.ccx.shared(), def_id, substs)),
(Some(monomorphize::resolve(bcx.ccx.tcx(), def_id, substs)),
None)
}
ty::TyFnPtr(_) => {
Expand Down Expand Up @@ -546,7 +546,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
};

let callee_ty = common::instance_ty(
bcx.ccx.shared(), instance.as_ref().unwrap());
bcx.ccx.tcx(), instance.as_ref().unwrap());
trans_intrinsic_call(&bcx, callee_ty, &fn_ty, &llargs, dest,
terminator.source_info.span);

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/mir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
substs: &'tcx Substs<'tcx>,
args: IndexVec<mir::Local, Result<Const<'tcx>, ConstEvalErr<'tcx>>>)
-> Result<Const<'tcx>, ConstEvalErr<'tcx>> {
let instance = monomorphize::resolve(ccx.shared(), def_id, substs);
let instance = monomorphize::resolve(ccx.tcx(), def_id, substs);
let mir = ccx.tcx().instance_mir(instance.def);
MirConstContext::new(ccx, &mir, instance.substs, args).trans()
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/mir/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
match operand.ty.sty {
ty::TyClosure(def_id, substs) => {
let instance = monomorphize::resolve_closure(
bcx.ccx.shared(), def_id, substs, ty::ClosureKind::FnOnce);
bcx.ccx.tcx(), def_id, substs, ty::ClosureKind::FnOnce);
OperandValue::Immediate(callee::get_fn(bcx.ccx, instance))
}
_ => {
Expand Down
51 changes: 25 additions & 26 deletions src/librustc_trans/monomorphize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,26 @@ fn needs_fn_once_adapter_shim(actual_closure_kind: ty::ClosureKind,
}

pub fn resolve_closure<'a, 'tcx> (
scx: &SharedCrateContext<'a, 'tcx>,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId,
substs: ty::ClosureSubsts<'tcx>,
requested_kind: ty::ClosureKind)
-> Instance<'tcx>
{
let actual_kind = scx.tcx().closure_kind(def_id);
let actual_kind = tcx.closure_kind(def_id);

match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
Ok(true) => fn_once_adapter_instance(scx.tcx(), def_id, substs),
Ok(true) => fn_once_adapter_instance(tcx, def_id, substs),
_ => Instance::new(def_id, substs.substs)
}
}

fn resolve_associated_item<'a, 'tcx>(
scx: &SharedCrateContext<'a, 'tcx>,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
trait_item: &ty::AssociatedItem,
trait_id: DefId,
rcvr_substs: &'tcx Substs<'tcx>
) -> Instance<'tcx> {
let tcx = scx.tcx();
let def_id = trait_item.def_id;
debug!("resolve_associated_item(trait_item={:?}, \
trait_id={:?}, \
Expand All @@ -132,7 +131,7 @@ fn resolve_associated_item<'a, 'tcx>(
}
traits::VtableClosure(closure_data) => {
let trait_closure_kind = tcx.lang_items().fn_trait_kind(trait_id).unwrap();
resolve_closure(scx, closure_data.closure_def_id, closure_data.substs,
resolve_closure(tcx, closure_data.closure_def_id, closure_data.substs,
trait_closure_kind)
}
traits::VtableFnPointer(ref data) => {
Expand Down Expand Up @@ -163,21 +162,21 @@ fn resolve_associated_item<'a, 'tcx>(
/// The point where linking happens. Resolve a (def_id, substs)
/// pair to an instance.
pub fn resolve<'a, 'tcx>(
scx: &SharedCrateContext<'a, 'tcx>,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId,
substs: &'tcx Substs<'tcx>
) -> Instance<'tcx> {
debug!("resolve(def_id={:?}, substs={:?})",
def_id, substs);
let result = if let Some(trait_def_id) = scx.tcx().trait_of_item(def_id) {
let result = if let Some(trait_def_id) = tcx.trait_of_item(def_id) {
debug!(" => associated item, attempting to find impl");
let item = scx.tcx().associated_item(def_id);
resolve_associated_item(scx, &item, trait_def_id, substs)
let item = tcx.associated_item(def_id);
resolve_associated_item(tcx, &item, trait_def_id, substs)
} else {
let item_type = def_ty(scx, def_id, substs);
let item_type = def_ty(tcx, def_id, substs);
let def = match item_type.sty {
ty::TyFnDef(..) if {
let f = item_type.fn_sig(scx.tcx());
let f = item_type.fn_sig(tcx);
f.abi() == Abi::RustIntrinsic ||
f.abi() == Abi::PlatformIntrinsic
} =>
Expand All @@ -186,9 +185,9 @@ pub fn resolve<'a, 'tcx>(
ty::InstanceDef::Intrinsic(def_id)
}
_ => {
if Some(def_id) == scx.tcx().lang_items().drop_in_place_fn() {
if Some(def_id) == tcx.lang_items().drop_in_place_fn() {
let ty = substs.type_at(0);
if scx.type_needs_drop(ty) {
if common::type_needs_drop(tcx, ty) {
debug!(" => nontrivial drop glue");
ty::InstanceDef::DropGlue(def_id, Some(ty))
} else {
Expand All @@ -209,27 +208,27 @@ pub fn resolve<'a, 'tcx>(
}

pub fn resolve_drop_in_place<'a, 'tcx>(
scx: &SharedCrateContext<'a, 'tcx>,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
ty: Ty<'tcx>)
-> ty::Instance<'tcx>
{
let def_id = scx.tcx().require_lang_item(DropInPlaceFnLangItem);
let substs = scx.tcx().intern_substs(&[Kind::from(ty)]);
resolve(scx, def_id, substs)
let def_id = tcx.require_lang_item(DropInPlaceFnLangItem);
let substs = tcx.intern_substs(&[Kind::from(ty)]);
resolve(tcx, def_id, substs)
}

pub fn custom_coerce_unsize_info<'scx, 'tcx>(scx: &SharedCrateContext<'scx, 'tcx>,
source_ty: Ty<'tcx>,
target_ty: Ty<'tcx>)
-> CustomCoerceUnsized {
pub fn custom_coerce_unsize_info<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
source_ty: Ty<'tcx>,
target_ty: Ty<'tcx>)
-> CustomCoerceUnsized {
let trait_ref = ty::Binder(ty::TraitRef {
def_id: scx.tcx().lang_items().coerce_unsized_trait().unwrap(),
substs: scx.tcx().mk_substs_trait(source_ty, &[target_ty])
def_id: tcx.lang_items().coerce_unsized_trait().unwrap(),
substs: tcx.mk_substs_trait(source_ty, &[target_ty])
});

match scx.tcx().trans_fulfill_obligation(DUMMY_SP, trait_ref) {
match tcx.trans_fulfill_obligation(DUMMY_SP, trait_ref) {
traits::VtableImpl(traits::VtableImplData { impl_def_id, .. }) => {
scx.tcx().coerce_unsized_info(impl_def_id).custom_kind.unwrap()
tcx.coerce_unsized_info(impl_def_id).custom_kind.unwrap()
}
vtable => {
bug!("invalid CoerceUnsized vtable: {:?}", vtable);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/partitioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ fn characteristic_def_id_of_trans_item<'a, 'tcx>(scx: &SharedCrateContext<'a, 't
if let Some(impl_def_id) = tcx.impl_of_method(def_id) {
// This is a method within an inherent impl, find out what the
// self-type is:
let impl_self_ty = common::def_ty(scx, impl_def_id, instance.substs);
let impl_self_ty = common::def_ty(scx.tcx(), impl_def_id, instance.substs);
if let Some(def_id) = characteristic_def_id_of_type(impl_self_ty) {
return Some(def_id);
}
Expand Down
Loading

0 comments on commit c72240a

Please sign in to comment.