From e579b067db3eb0ad44826f1767197de34378f89d Mon Sep 17 00:00:00 2001 From: Maik Klein Date: Wed, 25 Oct 2017 16:14:51 +0200 Subject: [PATCH] Move trans_item and monomorphize to rustc_mir --- src/librustc_mir/monomorphize/collector.rs | 2 +- src/librustc_mir/monomorphize/mod.rs | 128 ++++++++++++++++++ .../monomorphize/mono_item.rs} | 2 +- src/librustc_trans/base.rs | 2 +- src/librustc_trans/lib.rs | 2 +- src/librustc_trans/trans_item.rs | 4 +- src/librustc_trans_utils/lib.rs | 2 - src/librustc_trans_utils/monomorphize.rs | 127 ----------------- 8 files changed, 134 insertions(+), 135 deletions(-) rename src/{librustc_trans_utils/trans_item.rs => librustc_mir/monomorphize/mono_item.rs} (99%) delete mode 100644 src/librustc_trans_utils/monomorphize.rs diff --git a/src/librustc_mir/monomorphize/collector.rs b/src/librustc_mir/monomorphize/collector.rs index 498ddf9c85c6..a38ee45743c9 100644 --- a/src/librustc_mir/monomorphize/collector.rs +++ b/src/librustc_mir/monomorphize/collector.rs @@ -207,7 +207,7 @@ use common::{def_ty, instance_ty, type_has_metadata}; use monomorphize::{self, Instance}; use rustc::util::nodemap::{FxHashSet, FxHashMap, DefIdMap}; -use rustc_trans_utils::trans_item::{TransItemExt, DefPathBasedNames, InstantiationMode}; +use monomorphize::mono_item::{TransItemExt, DefPathBasedNames, InstantiationMode}; use rustc_data_structures::bitvec::BitVector; diff --git a/src/librustc_mir/monomorphize/mod.rs b/src/librustc_mir/monomorphize/mod.rs index e0247862ec29..f56de9c0fd80 100644 --- a/src/librustc_mir/monomorphize/mod.rs +++ b/src/librustc_mir/monomorphize/mod.rs @@ -1 +1,129 @@ +// Copyright 2012-2014 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use rustc::hir::def_id::DefId; +use rustc::middle::lang_items::DropInPlaceFnLangItem; +use rustc::traits; +use rustc::ty::adjustment::CustomCoerceUnsized; +use rustc::ty::subst::Kind; +use rustc::ty::{self, Ty, TyCtxt}; + +pub use rustc::ty::Instance; + pub mod collector; +pub mod mono_item; + +fn fn_once_adapter_instance<'a, 'tcx>( + tcx: TyCtxt<'a, 'tcx, 'tcx>, + closure_did: DefId, + substs: ty::ClosureSubsts<'tcx>, + ) -> Instance<'tcx> { + debug!("fn_once_adapter_shim({:?}, {:?})", + closure_did, + substs); + let fn_once = tcx.lang_items().fn_once_trait().unwrap(); + let call_once = tcx.associated_items(fn_once) + .find(|it| it.kind == ty::AssociatedKind::Method) + .unwrap().def_id; + let def = ty::InstanceDef::ClosureOnceShim { call_once }; + + let self_ty = tcx.mk_closure_from_closure_substs( + closure_did, substs); + + let sig = substs.closure_sig(closure_did, tcx); + let sig = tcx.erase_late_bound_regions_and_normalize(&sig); + assert_eq!(sig.inputs().len(), 1); + let substs = tcx.mk_substs([ + Kind::from(self_ty), + Kind::from(sig.inputs()[0]), + ].iter().cloned()); + + debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig); + Instance { def, substs } +} + +fn needs_fn_once_adapter_shim(actual_closure_kind: ty::ClosureKind, + trait_closure_kind: ty::ClosureKind) + -> Result +{ + match (actual_closure_kind, trait_closure_kind) { + (ty::ClosureKind::Fn, ty::ClosureKind::Fn) | + (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut) | + (ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => { + // No adapter needed. + Ok(false) + } + (ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => { + // The closure fn `llfn` is a `fn(&self, ...)`. We want a + // `fn(&mut self, ...)`. In fact, at trans time, these are + // basically the same thing, so we can just return llfn. + Ok(false) + } + (ty::ClosureKind::Fn, ty::ClosureKind::FnOnce) | + (ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => { + // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut + // self, ...)`. We want a `fn(self, ...)`. We can produce + // this by doing something like: + // + // fn call_once(self, ...) { call_mut(&self, ...) } + // fn call_once(mut self, ...) { call_mut(&mut self, ...) } + // + // These are both the same at trans time. + Ok(true) + } + _ => Err(()), + } +} + +pub fn resolve_closure<'a, 'tcx> ( + tcx: TyCtxt<'a, 'tcx, 'tcx>, + def_id: DefId, + substs: ty::ClosureSubsts<'tcx>, + requested_kind: ty::ClosureKind) + -> Instance<'tcx> +{ + let actual_kind = substs.closure_kind(def_id, tcx); + + match needs_fn_once_adapter_shim(actual_kind, requested_kind) { + Ok(true) => fn_once_adapter_instance(tcx, def_id, substs), + _ => Instance::new(def_id, substs.substs) + } +} + +pub fn resolve_drop_in_place<'a, 'tcx>( + tcx: TyCtxt<'a, 'tcx, 'tcx>, + ty: Ty<'tcx>) + -> ty::Instance<'tcx> +{ + let def_id = tcx.require_lang_item(DropInPlaceFnLangItem); + let substs = tcx.intern_substs(&[Kind::from(ty)]); + Instance::resolve(tcx, ty::ParamEnv::empty(traits::Reveal::All), def_id, substs).unwrap() +} + +pub fn custom_coerce_unsize_info<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, + source_ty: Ty<'tcx>, + target_ty: Ty<'tcx>) + -> CustomCoerceUnsized { + let def_id = tcx.lang_items().coerce_unsized_trait().unwrap(); + + let trait_ref = ty::Binder(ty::TraitRef { + def_id: def_id, + substs: tcx.mk_substs_trait(source_ty, &[target_ty]) + }); + + match tcx.trans_fulfill_obligation( (ty::ParamEnv::empty(traits::Reveal::All), trait_ref)) { + traits::VtableImpl(traits::VtableImplData { impl_def_id, .. }) => { + tcx.coerce_unsized_info(impl_def_id).custom_kind.unwrap() + } + vtable => { + bug!("invalid CoerceUnsized vtable: {:?}", vtable); + } + } +} diff --git a/src/librustc_trans_utils/trans_item.rs b/src/librustc_mir/monomorphize/mono_item.rs similarity index 99% rename from src/librustc_trans_utils/trans_item.rs rename to src/librustc_mir/monomorphize/mono_item.rs index 817ceefeb7fe..cf852a3f782d 100644 --- a/src/librustc_trans_utils/trans_item.rs +++ b/src/librustc_mir/monomorphize/mono_item.rs @@ -14,7 +14,7 @@ //! item-path. This is used for unit testing the code that generates //! paths etc in all kinds of annoying scenarios. -use common; +use rustc_trans_utils::common; use monomorphize::Instance; use rustc::hir; use rustc::hir::def_id::DefId; diff --git a/src/librustc_trans/base.rs b/src/librustc_trans/base.rs index d28f0c5bd841..6bd8034fe59d 100644 --- a/src/librustc_trans/base.rs +++ b/src/librustc_trans/base.rs @@ -89,7 +89,7 @@ use syntax::ast; use mir::operand::OperandValue; pub use rustc_trans_utils::{find_exported_symbols, check_for_rustc_errors_attr}; -pub use rustc_trans_utils::trans_item::linkage_by_name; +pub use rustc_mir::monomorphize::mono_item::linkage_by_name; pub struct StatRecorder<'a, 'tcx: 'a> { ccx: &'a CrateContext<'a, 'tcx>, diff --git a/src/librustc_trans/lib.rs b/src/librustc_trans/lib.rs index 7f2b875394cb..bf25e74de8b1 100644 --- a/src/librustc_trans/lib.rs +++ b/src/librustc_trans/lib.rs @@ -85,7 +85,7 @@ use rustc::session::config::{OutputFilenames, OutputType}; use rustc::ty::{self, TyCtxt}; use rustc::util::nodemap::{FxHashSet, FxHashMap}; -use rustc_trans_utils::monomorphize; +use rustc_mir::monomorphize; mod diagnostics; diff --git a/src/librustc_trans/trans_item.rs b/src/librustc_trans/trans_item.rs index 991f99e0f6c9..7930643a4ac8 100644 --- a/src/librustc_trans/trans_item.rs +++ b/src/librustc_trans/trans_item.rs @@ -36,8 +36,8 @@ use std::fmt; pub use rustc::middle::trans::TransItem; -pub use rustc_trans_utils::trans_item::*; -pub use rustc_trans_utils::trans_item::TransItemExt as BaseTransItemExt; +pub use rustc_mir::monomorphize::mono_item::*; +pub use rustc_mir::monomorphize::mono_item::TransItemExt as BaseTransItemExt; pub trait TransItemExt<'a, 'tcx>: fmt::Debug + BaseTransItemExt<'a, 'tcx> { fn define(&self, ccx: &CrateContext<'a, 'tcx>) { diff --git a/src/librustc_trans_utils/lib.rs b/src/librustc_trans_utils/lib.rs index 4ee6602dfe63..6de8aa765777 100644 --- a/src/librustc_trans_utils/lib.rs +++ b/src/librustc_trans_utils/lib.rs @@ -46,8 +46,6 @@ use rustc::util::nodemap::NodeSet; pub mod common; pub mod link; -pub mod trans_item; -pub mod monomorphize; pub mod trans_crate; /// check for the #[rustc_error] annotation, which forces an diff --git a/src/librustc_trans_utils/monomorphize.rs b/src/librustc_trans_utils/monomorphize.rs deleted file mode 100644 index d586d1ac3150..000000000000 --- a/src/librustc_trans_utils/monomorphize.rs +++ /dev/null @@ -1,127 +0,0 @@ -// Copyright 2012-2014 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use rustc::hir::def_id::DefId; -use rustc::middle::lang_items::DropInPlaceFnLangItem; -use rustc::traits; -use rustc::ty::adjustment::CustomCoerceUnsized; -use rustc::ty::subst::Kind; -use rustc::ty::{self, Ty, TyCtxt}; - -pub use rustc::ty::Instance; - -fn fn_once_adapter_instance<'a, 'tcx>( - tcx: TyCtxt<'a, 'tcx, 'tcx>, - closure_did: DefId, - substs: ty::ClosureSubsts<'tcx>, - ) -> Instance<'tcx> { - debug!("fn_once_adapter_shim({:?}, {:?})", - closure_did, - substs); - let fn_once = tcx.lang_items().fn_once_trait().unwrap(); - let call_once = tcx.associated_items(fn_once) - .find(|it| it.kind == ty::AssociatedKind::Method) - .unwrap().def_id; - let def = ty::InstanceDef::ClosureOnceShim { call_once }; - - let self_ty = tcx.mk_closure_from_closure_substs( - closure_did, substs); - - let sig = substs.closure_sig(closure_did, tcx); - let sig = tcx.erase_late_bound_regions_and_normalize(&sig); - assert_eq!(sig.inputs().len(), 1); - let substs = tcx.mk_substs([ - Kind::from(self_ty), - Kind::from(sig.inputs()[0]), - ].iter().cloned()); - - debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig); - Instance { def, substs } -} - -fn needs_fn_once_adapter_shim(actual_closure_kind: ty::ClosureKind, - trait_closure_kind: ty::ClosureKind) - -> Result -{ - match (actual_closure_kind, trait_closure_kind) { - (ty::ClosureKind::Fn, ty::ClosureKind::Fn) | - (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut) | - (ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => { - // No adapter needed. - Ok(false) - } - (ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => { - // The closure fn `llfn` is a `fn(&self, ...)`. We want a - // `fn(&mut self, ...)`. In fact, at trans time, these are - // basically the same thing, so we can just return llfn. - Ok(false) - } - (ty::ClosureKind::Fn, ty::ClosureKind::FnOnce) | - (ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => { - // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut - // self, ...)`. We want a `fn(self, ...)`. We can produce - // this by doing something like: - // - // fn call_once(self, ...) { call_mut(&self, ...) } - // fn call_once(mut self, ...) { call_mut(&mut self, ...) } - // - // These are both the same at trans time. - Ok(true) - } - _ => Err(()), - } -} - -pub fn resolve_closure<'a, 'tcx> ( - tcx: TyCtxt<'a, 'tcx, 'tcx>, - def_id: DefId, - substs: ty::ClosureSubsts<'tcx>, - requested_kind: ty::ClosureKind) - -> Instance<'tcx> -{ - let actual_kind = substs.closure_kind(def_id, tcx); - - match needs_fn_once_adapter_shim(actual_kind, requested_kind) { - Ok(true) => fn_once_adapter_instance(tcx, def_id, substs), - _ => Instance::new(def_id, substs.substs) - } -} - -pub fn resolve_drop_in_place<'a, 'tcx>( - tcx: TyCtxt<'a, 'tcx, 'tcx>, - ty: Ty<'tcx>) - -> ty::Instance<'tcx> -{ - let def_id = tcx.require_lang_item(DropInPlaceFnLangItem); - let substs = tcx.intern_substs(&[Kind::from(ty)]); - Instance::resolve(tcx, ty::ParamEnv::empty(traits::Reveal::All), def_id, substs).unwrap() -} - -pub fn custom_coerce_unsize_info<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, - source_ty: Ty<'tcx>, - target_ty: Ty<'tcx>) - -> CustomCoerceUnsized { - let def_id = tcx.lang_items().coerce_unsized_trait().unwrap(); - - let trait_ref = ty::Binder(ty::TraitRef { - def_id: def_id, - substs: tcx.mk_substs_trait(source_ty, &[target_ty]) - }); - - match tcx.trans_fulfill_obligation( (ty::ParamEnv::empty(traits::Reveal::All), trait_ref)) { - traits::VtableImpl(traits::VtableImplData { impl_def_id, .. }) => { - tcx.coerce_unsized_info(impl_def_id).custom_kind.unwrap() - } - vtable => { - bug!("invalid CoerceUnsized vtable: {:?}", vtable); - } - } -} -