Skip to content

Commit e568e98

Browse files
committed
Move codegen_llvm::common::ty_fn_sig into rustc::ty::Instance.
1 parent 06b6b1c commit e568e98

File tree

7 files changed

+73
-87
lines changed

7 files changed

+73
-87
lines changed

src/librustc/ty/instance.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use hir::Unsafety;
1112
use hir::def_id::DefId;
12-
use ty::{self, Ty, TypeFoldable, Substs, TyCtxt};
13+
use ty::{self, Ty, PolyFnSig, TypeFoldable, Substs, TyCtxt};
1314
use traits;
1415
use rustc_target::spec::abi::Abi;
1516
use util::ppaux;
1617

1718
use std::fmt;
19+
use std::iter;
1820

1921
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
2022
pub struct Instance<'tcx> {
@@ -59,6 +61,65 @@ impl<'a, 'tcx> Instance<'tcx> {
5961
&ty,
6062
)
6163
}
64+
65+
fn fn_sig_noadjust(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> PolyFnSig<'tcx> {
66+
let ty = self.ty(tcx);
67+
match ty.sty {
68+
ty::FnDef(..) |
69+
// Shims currently have type FnPtr. Not sure this should remain.
70+
ty::FnPtr(_) => ty.fn_sig(tcx),
71+
ty::Closure(def_id, substs) => {
72+
let sig = substs.closure_sig(def_id, tcx);
73+
74+
let env_ty = tcx.closure_env_ty(def_id, substs).unwrap();
75+
sig.map_bound(|sig| tcx.mk_fn_sig(
76+
iter::once(*env_ty.skip_binder()).chain(sig.inputs().iter().cloned()),
77+
sig.output(),
78+
sig.variadic,
79+
sig.unsafety,
80+
sig.abi
81+
))
82+
}
83+
ty::Generator(def_id, substs, _) => {
84+
let sig = substs.poly_sig(def_id, tcx);
85+
86+
let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
87+
let env_ty = tcx.mk_mut_ref(tcx.mk_region(env_region), ty);
88+
89+
sig.map_bound(|sig| {
90+
let state_did = tcx.lang_items().gen_state().unwrap();
91+
let state_adt_ref = tcx.adt_def(state_did);
92+
let state_substs = tcx.intern_substs(&[
93+
sig.yield_ty.into(),
94+
sig.return_ty.into(),
95+
]);
96+
let ret_ty = tcx.mk_adt(state_adt_ref, state_substs);
97+
98+
tcx.mk_fn_sig(iter::once(env_ty),
99+
ret_ty,
100+
false,
101+
Unsafety::Normal,
102+
Abi::Rust
103+
)
104+
})
105+
}
106+
_ => bug!("unexpected type {:?} in Instance::fn_sig_noadjust", ty)
107+
}
108+
}
109+
110+
pub fn fn_sig(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> ty::PolyFnSig<'tcx> {
111+
let mut fn_sig = self.fn_sig_noadjust(tcx);
112+
if let InstanceDef::VtableShim(..) = self.def {
113+
// Modify fn(self, ...) to fn(self: *mut Self, ...)
114+
fn_sig = fn_sig.map_bound(|mut fn_sig| {
115+
let mut inputs_and_output = fn_sig.inputs_and_output.to_vec();
116+
inputs_and_output[0] = tcx.mk_mut_ptr(inputs_and_output[0]);
117+
fn_sig.inputs_and_output = tcx.intern_type_list(&inputs_and_output);
118+
fn_sig
119+
});
120+
}
121+
fn_sig
122+
}
62123
}
63124

64125
impl<'tcx> InstanceDef<'tcx> {

src/librustc_codegen_llvm/abi.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use llvm::{self, AttributePlace};
1212
use base;
1313
use builder::{Builder, MemFlags};
14-
use common::{ty_fn_sig, C_usize};
14+
use common::C_usize;
1515
use context::CodegenCx;
1616
use mir::place::PlaceRef;
1717
use mir::operand::OperandValue;
@@ -283,8 +283,7 @@ pub trait FnTypeExt<'tcx> {
283283

284284
impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> {
285285
fn of_instance(cx: &CodegenCx<'ll, 'tcx>, instance: &ty::Instance<'tcx>) -> Self {
286-
let fn_ty = instance.ty(cx.tcx);
287-
let sig = ty_fn_sig(cx, fn_ty);
286+
let sig = instance.fn_sig(cx.tcx);
288287
let sig = cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
289288
FnType::new(cx, sig, &[])
290289
}

src/librustc_codegen_llvm/base.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use callee;
5656
use common::{C_bool, C_bytes_in_context, C_i32, C_usize};
5757
use rustc_mir::monomorphize::collector::{self, MonoItemCollectionMode};
5858
use rustc_mir::monomorphize::item::DefPathBasedNames;
59-
use common::{self, C_struct_in_context, C_array, val_ty};
59+
use common::{C_struct_in_context, C_array, val_ty};
6060
use consts;
6161
use context::CodegenCx;
6262
use debuginfo;
@@ -491,8 +491,7 @@ pub fn codegen_instance<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, instance: Instance<'
491491
// release builds.
492492
info!("codegen_instance({})", instance);
493493

494-
let fn_ty = instance.ty(cx.tcx);
495-
let sig = common::ty_fn_sig_vtable(cx, fn_ty, instance.is_vtable_shim());
494+
let sig = instance.fn_sig(cx.tcx);
496495
let sig = cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
497496

498497
let lldecl = cx.instances.borrow().get(&instance).cloned().unwrap_or_else(||

src/librustc_codegen_llvm/callee.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ pub fn get_fn(
4747
assert!(!instance.substs.has_escaping_regions());
4848
assert!(!instance.substs.has_param_types());
4949

50-
let fn_ty = instance.ty(cx.tcx);
50+
let sig = instance.fn_sig(cx.tcx);
5151
if let Some(&llfn) = cx.instances.borrow().get(&instance) {
5252
return llfn;
5353
}
5454

5555
let sym = tcx.symbol_name(instance).as_str();
56-
debug!("get_fn({:?}: {:?}) => {}", instance, fn_ty, sym);
56+
debug!("get_fn({:?}: {:?}) => {}", instance, sig, sym);
5757

5858
// Create a fn pointer with the substituted signature.
59-
let fn_ptr_ty = tcx.mk_fn_ptr(common::ty_fn_sig_vtable(cx, fn_ty, instance.is_vtable_shim()));
59+
let fn_ptr_ty = tcx.mk_fn_ptr(sig);
6060
let llptrty = cx.layout_of(fn_ptr_ty).llvm_type(cx);
6161

6262
let llfn = if let Some(llfn) = declare::get_declared_value(cx, &sym) {
@@ -91,7 +91,7 @@ pub fn get_fn(
9191
llfn
9292
}
9393
} else {
94-
let llfn = declare::declare_fn(cx, &sym, common::ty_fn_sig_vtable(cx, fn_ty, instance.is_vtable_shim()));
94+
let llfn = declare::declare_fn(cx, &sym, sig);
9595
assert_eq!(common::val_ty(llfn), llptrty);
9696
debug!("get_fn: not casting pointer!");
9797

src/librustc_codegen_llvm/common.rs

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ use rustc::ty::layout::{HasDataLayout, LayoutOf};
3030
use rustc::hir;
3131

3232
use libc::{c_uint, c_char};
33-
use std::iter;
3433

35-
use rustc_target::spec::abi::Abi;
3634
use syntax::symbol::LocalInternedString;
3735
use syntax_pos::{Span, DUMMY_SP};
3836

@@ -404,71 +402,3 @@ pub fn shift_mask_val(
404402
_ => bug!("shift_mask_val: expected Integer or Vector, found {:?}", kind),
405403
}
406404
}
407-
408-
pub fn ty_fn_sig<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
409-
ty: Ty<'tcx>)
410-
-> ty::PolyFnSig<'tcx>
411-
{
412-
match ty.sty {
413-
ty::FnDef(..) |
414-
// Shims currently have type FnPtr. Not sure this should remain.
415-
ty::FnPtr(_) => ty.fn_sig(cx.tcx),
416-
ty::Closure(def_id, substs) => {
417-
let tcx = cx.tcx;
418-
let sig = substs.closure_sig(def_id, tcx);
419-
420-
let env_ty = tcx.closure_env_ty(def_id, substs).unwrap();
421-
sig.map_bound(|sig| tcx.mk_fn_sig(
422-
iter::once(*env_ty.skip_binder()).chain(sig.inputs().iter().cloned()),
423-
sig.output(),
424-
sig.variadic,
425-
sig.unsafety,
426-
sig.abi
427-
))
428-
}
429-
ty::Generator(def_id, substs, _) => {
430-
let tcx = cx.tcx;
431-
let sig = substs.poly_sig(def_id, cx.tcx);
432-
433-
let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
434-
let env_ty = tcx.mk_mut_ref(tcx.mk_region(env_region), ty);
435-
436-
sig.map_bound(|sig| {
437-
let state_did = tcx.lang_items().gen_state().unwrap();
438-
let state_adt_ref = tcx.adt_def(state_did);
439-
let state_substs = tcx.intern_substs(&[
440-
sig.yield_ty.into(),
441-
sig.return_ty.into(),
442-
]);
443-
let ret_ty = tcx.mk_adt(state_adt_ref, state_substs);
444-
445-
tcx.mk_fn_sig(iter::once(env_ty),
446-
ret_ty,
447-
false,
448-
hir::Unsafety::Normal,
449-
Abi::Rust
450-
)
451-
})
452-
}
453-
_ => bug!("unexpected type {:?} to ty_fn_sig", ty)
454-
}
455-
}
456-
457-
pub fn ty_fn_sig_vtable<'a, 'tcx>(
458-
cx: &CodegenCx<'a, 'tcx>,
459-
ty: Ty<'tcx>,
460-
is_vtable_shim: bool
461-
) -> ty::PolyFnSig<'tcx>
462-
{
463-
let mut fn_sig = ty_fn_sig(cx, ty);
464-
if is_vtable_shim {
465-
// Modify fn(self, ...) to fn(self: *mut Self, ...)
466-
fn_sig = fn_sig.map_bound(|mut fn_sig| {
467-
let mut inputs_and_output = fn_sig.inputs_and_output.to_vec();
468-
inputs_and_output[0] = cx.tcx.mk_mut_ptr(inputs_and_output[0]);
469-
fn_sig.inputs_and_output = cx.tcx.intern_type_list(&inputs_and_output);
470-
fn_sig
471-
});
472-
}
473-
fn_sig
474-
}

src/librustc_codegen_llvm/mir/block.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,7 @@ impl FunctionCx<'a, 'll, 'tcx> {
298298
};
299299
let (drop_fn, fn_ty) = match ty.sty {
300300
ty::Dynamic(..) => {
301-
let fn_ty = drop_fn.ty(bx.cx.tcx);
302-
let sig = common::ty_fn_sig(bx.cx, fn_ty);
301+
let sig = drop_fn.fn_sig(bx.cx.tcx);
303302
let sig = bx.tcx().normalize_erasing_late_bound_regions(
304303
ty::ParamEnv::reveal_all(),
305304
&sig,

src/librustc_codegen_llvm/mono_item.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use asm;
1818
use attributes;
1919
use base;
20-
use common;
2120
use consts;
2221
use context::CodegenCx;
2322
use declare;
@@ -154,8 +153,7 @@ fn predefine_fn<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
154153
assert!(!instance.substs.needs_infer() &&
155154
!instance.substs.has_param_types());
156155

157-
let mono_ty = instance.ty(cx.tcx);
158-
let mono_sig = common::ty_fn_sig_vtable(cx, mono_ty, instance.is_vtable_shim());
156+
let mono_sig = instance.fn_sig(cx.tcx);
159157
let attrs = cx.tcx.codegen_fn_attrs(instance.def_id());
160158
let lldecl = declare::declare_fn(cx, symbol_name, mono_sig);
161159
unsafe { llvm::LLVMRustSetLinkage(lldecl, base::linkage_to_llvm(linkage)) };
@@ -180,7 +178,7 @@ fn predefine_fn<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
180178
}
181179
}
182180

183-
debug!("predefine_fn: mono_ty = {:?} instance = {:?}", mono_ty, instance);
181+
debug!("predefine_fn: mono_sig = {:?} instance = {:?}", mono_sig, instance);
184182
if instance.def.is_inline(cx.tcx) {
185183
attributes::inline(cx, lldecl, attributes::InlineAttr::Hint);
186184
}

0 commit comments

Comments
 (0)