Skip to content

Commit e579b06

Browse files
MaikKleinarielb1
authored andcommitted
Move trans_item and monomorphize to rustc_mir
1 parent 094c021 commit e579b06

File tree

8 files changed

+134
-135
lines changed

8 files changed

+134
-135
lines changed

src/librustc_mir/monomorphize/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ use common::{def_ty, instance_ty, type_has_metadata};
207207
use monomorphize::{self, Instance};
208208
use rustc::util::nodemap::{FxHashSet, FxHashMap, DefIdMap};
209209

210-
use rustc_trans_utils::trans_item::{TransItemExt, DefPathBasedNames, InstantiationMode};
210+
use monomorphize::mono_item::{TransItemExt, DefPathBasedNames, InstantiationMode};
211211

212212
use rustc_data_structures::bitvec::BitVector;
213213

src/librustc_mir/monomorphize/mod.rs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,129 @@
1+
// Copyright 2012-2014 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 rustc::hir::def_id::DefId;
12+
use rustc::middle::lang_items::DropInPlaceFnLangItem;
13+
use rustc::traits;
14+
use rustc::ty::adjustment::CustomCoerceUnsized;
15+
use rustc::ty::subst::Kind;
16+
use rustc::ty::{self, Ty, TyCtxt};
17+
18+
pub use rustc::ty::Instance;
19+
120
pub mod collector;
21+
pub mod mono_item;
22+
23+
fn fn_once_adapter_instance<'a, 'tcx>(
24+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
25+
closure_did: DefId,
26+
substs: ty::ClosureSubsts<'tcx>,
27+
) -> Instance<'tcx> {
28+
debug!("fn_once_adapter_shim({:?}, {:?})",
29+
closure_did,
30+
substs);
31+
let fn_once = tcx.lang_items().fn_once_trait().unwrap();
32+
let call_once = tcx.associated_items(fn_once)
33+
.find(|it| it.kind == ty::AssociatedKind::Method)
34+
.unwrap().def_id;
35+
let def = ty::InstanceDef::ClosureOnceShim { call_once };
36+
37+
let self_ty = tcx.mk_closure_from_closure_substs(
38+
closure_did, substs);
39+
40+
let sig = substs.closure_sig(closure_did, tcx);
41+
let sig = tcx.erase_late_bound_regions_and_normalize(&sig);
42+
assert_eq!(sig.inputs().len(), 1);
43+
let substs = tcx.mk_substs([
44+
Kind::from(self_ty),
45+
Kind::from(sig.inputs()[0]),
46+
].iter().cloned());
47+
48+
debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig);
49+
Instance { def, substs }
50+
}
51+
52+
fn needs_fn_once_adapter_shim(actual_closure_kind: ty::ClosureKind,
53+
trait_closure_kind: ty::ClosureKind)
54+
-> Result<bool, ()>
55+
{
56+
match (actual_closure_kind, trait_closure_kind) {
57+
(ty::ClosureKind::Fn, ty::ClosureKind::Fn) |
58+
(ty::ClosureKind::FnMut, ty::ClosureKind::FnMut) |
59+
(ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => {
60+
// No adapter needed.
61+
Ok(false)
62+
}
63+
(ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => {
64+
// The closure fn `llfn` is a `fn(&self, ...)`. We want a
65+
// `fn(&mut self, ...)`. In fact, at trans time, these are
66+
// basically the same thing, so we can just return llfn.
67+
Ok(false)
68+
}
69+
(ty::ClosureKind::Fn, ty::ClosureKind::FnOnce) |
70+
(ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
71+
// The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
72+
// self, ...)`. We want a `fn(self, ...)`. We can produce
73+
// this by doing something like:
74+
//
75+
// fn call_once(self, ...) { call_mut(&self, ...) }
76+
// fn call_once(mut self, ...) { call_mut(&mut self, ...) }
77+
//
78+
// These are both the same at trans time.
79+
Ok(true)
80+
}
81+
_ => Err(()),
82+
}
83+
}
84+
85+
pub fn resolve_closure<'a, 'tcx> (
86+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
87+
def_id: DefId,
88+
substs: ty::ClosureSubsts<'tcx>,
89+
requested_kind: ty::ClosureKind)
90+
-> Instance<'tcx>
91+
{
92+
let actual_kind = substs.closure_kind(def_id, tcx);
93+
94+
match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
95+
Ok(true) => fn_once_adapter_instance(tcx, def_id, substs),
96+
_ => Instance::new(def_id, substs.substs)
97+
}
98+
}
99+
100+
pub fn resolve_drop_in_place<'a, 'tcx>(
101+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
102+
ty: Ty<'tcx>)
103+
-> ty::Instance<'tcx>
104+
{
105+
let def_id = tcx.require_lang_item(DropInPlaceFnLangItem);
106+
let substs = tcx.intern_substs(&[Kind::from(ty)]);
107+
Instance::resolve(tcx, ty::ParamEnv::empty(traits::Reveal::All), def_id, substs).unwrap()
108+
}
109+
110+
pub fn custom_coerce_unsize_info<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
111+
source_ty: Ty<'tcx>,
112+
target_ty: Ty<'tcx>)
113+
-> CustomCoerceUnsized {
114+
let def_id = tcx.lang_items().coerce_unsized_trait().unwrap();
115+
116+
let trait_ref = ty::Binder(ty::TraitRef {
117+
def_id: def_id,
118+
substs: tcx.mk_substs_trait(source_ty, &[target_ty])
119+
});
120+
121+
match tcx.trans_fulfill_obligation( (ty::ParamEnv::empty(traits::Reveal::All), trait_ref)) {
122+
traits::VtableImpl(traits::VtableImplData { impl_def_id, .. }) => {
123+
tcx.coerce_unsized_info(impl_def_id).custom_kind.unwrap()
124+
}
125+
vtable => {
126+
bug!("invalid CoerceUnsized vtable: {:?}", vtable);
127+
}
128+
}
129+
}

src/librustc_trans_utils/trans_item.rs renamed to src/librustc_mir/monomorphize/mono_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! item-path. This is used for unit testing the code that generates
1515
//! paths etc in all kinds of annoying scenarios.
1616
17-
use common;
17+
use rustc_trans_utils::common;
1818
use monomorphize::Instance;
1919
use rustc::hir;
2020
use rustc::hir::def_id::DefId;

src/librustc_trans/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ use syntax::ast;
8989
use mir::operand::OperandValue;
9090

9191
pub use rustc_trans_utils::{find_exported_symbols, check_for_rustc_errors_attr};
92-
pub use rustc_trans_utils::trans_item::linkage_by_name;
92+
pub use rustc_mir::monomorphize::mono_item::linkage_by_name;
9393

9494
pub struct StatRecorder<'a, 'tcx: 'a> {
9595
ccx: &'a CrateContext<'a, 'tcx>,

src/librustc_trans/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ use rustc::session::config::{OutputFilenames, OutputType};
8585
use rustc::ty::{self, TyCtxt};
8686
use rustc::util::nodemap::{FxHashSet, FxHashMap};
8787

88-
use rustc_trans_utils::monomorphize;
88+
use rustc_mir::monomorphize;
8989

9090
mod diagnostics;
9191

src/librustc_trans/trans_item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ use std::fmt;
3636

3737
pub use rustc::middle::trans::TransItem;
3838

39-
pub use rustc_trans_utils::trans_item::*;
40-
pub use rustc_trans_utils::trans_item::TransItemExt as BaseTransItemExt;
39+
pub use rustc_mir::monomorphize::mono_item::*;
40+
pub use rustc_mir::monomorphize::mono_item::TransItemExt as BaseTransItemExt;
4141

4242
pub trait TransItemExt<'a, 'tcx>: fmt::Debug + BaseTransItemExt<'a, 'tcx> {
4343
fn define(&self, ccx: &CrateContext<'a, 'tcx>) {

src/librustc_trans_utils/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ use rustc::util::nodemap::NodeSet;
4646

4747
pub mod common;
4848
pub mod link;
49-
pub mod trans_item;
50-
pub mod monomorphize;
5149
pub mod trans_crate;
5250

5351
/// check for the #[rustc_error] annotation, which forces an

src/librustc_trans_utils/monomorphize.rs

Lines changed: 0 additions & 127 deletions
This file was deleted.

0 commit comments

Comments
 (0)