Skip to content

Commit 8569dad

Browse files
committed
---
yaml --- r: 273241 b: refs/heads/beta c: 3342da4 h: refs/heads/master i: 273239: f4f8dbd
1 parent 2b22347 commit 8569dad

File tree

6 files changed

+66
-101
lines changed

6 files changed

+66
-101
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ refs/tags/0.9: 36870b185fc5f5486636d4515f0e22677493f225
2323
refs/tags/0.10: ac33f2b15782272ae348dbd7b14b8257b2148b5a
2424
refs/tags/0.11.0: e1247cb1d0d681be034adb4b558b5a0c0d5720f9
2525
refs/tags/0.12.0: f0c419429ef30723ceaf6b42f9b5a2aeb5d2e2d1
26-
refs/heads/beta: da66431d06b961efd323f25978964df54d44d3c4
26+
refs/heads/beta: 3342da41135a63ba6d20c3f3e90fb40419164706
2727
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
2828
refs/heads/tmp: e06d2ad9fcd5027bcaac5b08fc9aa39a49d0ecd3
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f

branches/beta/src/librustc_trans/trans/attributes.rs

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ use libc::{c_uint, c_ulonglong};
1313
use llvm::{self, ValueRef, AttrHelper};
1414
use middle::ty;
1515
use middle::infer;
16-
use middle::traits::ProjectionMode;
1716
use session::config::NoDebugInfo;
1817
pub use syntax::attr::InlineAttr;
1918
use syntax::ast;
2019
use rustc_front::hir;
2120
use trans::abi::Abi;
22-
use trans::base;
2321
use trans::common;
2422
use trans::context::CrateContext;
2523
use trans::machine;
@@ -130,21 +128,12 @@ pub fn from_fn_type<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fn_type: ty::Ty<'tcx
130128
-> llvm::AttrBuilder {
131129
use middle::ty::{BrAnon, ReLateBound};
132130

133-
let function_type;
134-
let (fn_sig, abi, env_ty) = match fn_type.sty {
135-
ty::TyFnDef(_, _, ref f) | ty::TyFnPtr(ref f) => (&f.sig, f.abi, None),
136-
ty::TyClosure(closure_did, ref substs) => {
137-
let infcx = infer::normalizing_infer_ctxt(ccx.tcx(),
138-
&ccx.tcx().tables,
139-
ProjectionMode::Any);
140-
function_type = infcx.closure_type(closure_did, substs);
141-
let self_type = base::self_type_for_closure(ccx, closure_did, fn_type);
142-
(&function_type.sig, Abi::RustCall, Some(self_type))
143-
}
144-
_ => ccx.sess().bug("expected closure or function.")
131+
let f = match fn_type.sty {
132+
ty::TyFnDef(_, _, f) | ty::TyFnPtr(f) => f,
133+
_ => unreachable!("expected fn type, found {:?}", fn_type)
145134
};
146135

147-
let fn_sig = ccx.tcx().erase_late_bound_regions(fn_sig);
136+
let fn_sig = ccx.tcx().erase_late_bound_regions(&f.sig);
148137
let fn_sig = infer::normalize_associated_type(ccx.tcx(), &fn_sig);
149138

150139
let mut attrs = llvm::AttrBuilder::new();
@@ -153,30 +142,17 @@ pub fn from_fn_type<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fn_type: ty::Ty<'tcx
153142
// These have an odd calling convention, so we need to manually
154143
// unpack the input ty's
155144
let input_tys = match fn_type.sty {
156-
ty::TyClosure(..) => {
157-
assert!(abi == Abi::RustCall);
158-
159-
match fn_sig.inputs[0].sty {
160-
ty::TyTuple(ref inputs) => {
161-
let mut full_inputs = vec![env_ty.expect("Missing closure environment")];
162-
full_inputs.extend_from_slice(inputs);
163-
full_inputs
164-
}
165-
_ => ccx.sess().bug("expected tuple'd inputs")
166-
}
167-
},
168-
ty::TyFnDef(..) | ty::TyFnPtr(_) if abi == Abi::RustCall => {
169-
let mut inputs = vec![fn_sig.inputs[0]];
145+
ty::TyFnDef(..) | ty::TyFnPtr(_) if f.abi == Abi::RustCall => {
146+
let first = Some(fn_sig.inputs[0]).into_iter();
170147

171148
match fn_sig.inputs[1].sty {
172149
ty::TyTuple(ref t_in) => {
173-
inputs.extend_from_slice(&t_in[..]);
174-
inputs
150+
first.chain(t_in.iter().cloned())
175151
}
176152
_ => ccx.sess().bug("expected tuple'd inputs")
177153
}
178154
}
179-
_ => fn_sig.inputs.clone()
155+
_ => None.into_iter().chain(fn_sig.inputs.iter().cloned())
180156
};
181157

182158
// Index 0 is the return value of the llvm func, so we start at 1
@@ -228,7 +204,7 @@ pub fn from_fn_type<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fn_type: ty::Ty<'tcx
228204
}
229205
}
230206

231-
for &t in input_tys.iter() {
207+
for t in input_tys {
232208
match t.sty {
233209
_ if type_of::arg_is_indirect(ccx, t) => {
234210
let llarg_sz = machine::llsize_of_real(ccx, type_of::type_of(ccx, t));

branches/beta/src/librustc_trans/trans/base.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -192,22 +192,6 @@ impl<'a, 'tcx> Drop for StatRecorder<'a, 'tcx> {
192192
}
193193
}
194194

195-
pub fn self_type_for_closure<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
196-
closure_id: DefId,
197-
fn_ty: Ty<'tcx>)
198-
-> Ty<'tcx> {
199-
let closure_kind = ccx.tcx().closure_kind(closure_id);
200-
match closure_kind {
201-
ty::ClosureKind::Fn => {
202-
ccx.tcx().mk_imm_ref(ccx.tcx().mk_region(ty::ReStatic), fn_ty)
203-
}
204-
ty::ClosureKind::FnMut => {
205-
ccx.tcx().mk_mut_ref(ccx.tcx().mk_region(ty::ReStatic), fn_ty)
206-
}
207-
ty::ClosureKind::FnOnce => fn_ty,
208-
}
209-
}
210-
211195
pub fn kind_for_closure(ccx: &CrateContext, closure_id: DefId) -> ty::ClosureKind {
212196
*ccx.tcx().tables.borrow().closure_kinds.get(&closure_id).unwrap()
213197
}

branches/beta/src/librustc_trans/trans/closure.rs

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use llvm::{ValueRef, get_params};
1414
use middle::def_id::DefId;
1515
use middle::infer;
1616
use middle::traits::ProjectionMode;
17-
use trans::abi::Abi::RustCall;
17+
use trans::abi::Abi;
1818
use trans::adt;
1919
use trans::attributes;
2020
use trans::base::*;
@@ -30,7 +30,7 @@ use trans::monomorphize::{Instance};
3030
use trans::type_of::*;
3131
use trans::value::Value;
3232
use trans::Disr;
33-
use middle::ty;
33+
use middle::ty::{self, Ty, TyCtxt};
3434
use session::config::FullDebugInfo;
3535

3636
use syntax::ast;
@@ -49,7 +49,7 @@ fn load_closure_environment<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
4949

5050
// Special case for small by-value selfs.
5151
let closure_ty = node_id_type(bcx, bcx.fcx.id);
52-
let self_type = self_type_for_closure(bcx.ccx(), closure_def_id, closure_ty);
52+
let self_type = get_self_type(bcx.tcx(), closure_def_id, closure_ty);
5353
let kind = kind_for_closure(bcx.ccx(), closure_def_id);
5454
let llenv = if kind == ty::ClosureKind::FnOnce &&
5555
!arg_is_indirect(bcx.ccx(), self_type) {
@@ -131,6 +131,21 @@ impl<'a> ClosureEnv<'a> {
131131
}
132132
}
133133

134+
pub fn get_self_type<'tcx>(tcx: &TyCtxt<'tcx>,
135+
closure_id: DefId,
136+
fn_ty: Ty<'tcx>)
137+
-> Ty<'tcx> {
138+
match tcx.closure_kind(closure_id) {
139+
ty::ClosureKind::Fn => {
140+
tcx.mk_imm_ref(tcx.mk_region(ty::ReStatic), fn_ty)
141+
}
142+
ty::ClosureKind::FnMut => {
143+
tcx.mk_mut_ref(tcx.mk_region(ty::ReStatic), fn_ty)
144+
}
145+
ty::ClosureKind::FnOnce => fn_ty,
146+
}
147+
}
148+
134149
/// Returns the LLVM function declaration for a closure, creating it if
135150
/// necessary. If the ID does not correspond to a closure ID, returns None.
136151
pub fn get_or_create_closure_declaration<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
@@ -139,31 +154,47 @@ pub fn get_or_create_closure_declaration<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
139154
-> ValueRef {
140155
// Normalize type so differences in regions and typedefs don't cause
141156
// duplicate declarations
142-
let substs = ccx.tcx().erase_regions(substs);
143-
let mono_id = Instance {
157+
let tcx = ccx.tcx();
158+
let substs = tcx.erase_regions(substs);
159+
let instance = Instance {
144160
def: closure_id,
145161
params: &substs.func_substs.types
146162
};
147163

148-
if let Some(&llfn) = ccx.closure_vals().borrow().get(&mono_id) {
164+
if let Some(&llfn) = ccx.instances().borrow().get(&instance) {
149165
debug!("get_or_create_closure_declaration(): found closure {:?}: {:?}",
150-
mono_id, Value(llfn));
166+
instance, Value(llfn));
151167
return llfn;
152168
}
153169

154-
let path = ccx.tcx().def_path(closure_id);
170+
let path = tcx.def_path(closure_id);
155171
let symbol = mangle_internal_name_by_path_and_seq(path, "closure");
156172

157-
let function_type = ccx.tcx().mk_closure_from_closure_substs(closure_id, Box::new(substs));
173+
// Compute the rust-call form of the closure call method.
174+
let infcx = infer::normalizing_infer_ctxt(tcx, &tcx.tables, ProjectionMode::Any);
175+
let sig = &infcx.closure_type(closure_id, &substs).sig;
176+
let sig = tcx.erase_late_bound_regions(sig);
177+
let sig = infer::normalize_associated_type(tcx, &sig);
178+
let closure_type = tcx.mk_closure_from_closure_substs(closure_id, Box::new(substs));
179+
let function_type = tcx.mk_fn_ptr(ty::BareFnTy {
180+
unsafety: hir::Unsafety::Normal,
181+
abi: Abi::RustCall,
182+
sig: ty::Binder(ty::FnSig {
183+
inputs: Some(get_self_type(tcx, closure_id, closure_type))
184+
.into_iter().chain(sig.inputs).collect(),
185+
output: sig.output,
186+
variadic: false
187+
})
188+
});
158189
let llfn = declare::define_internal_fn(ccx, &symbol, function_type);
159190

160191
// set an inline hint for all closures
161192
attributes::inline(llfn, attributes::InlineAttr::Hint);
162193

163194
debug!("get_or_create_declaration_if_closure(): inserting new \
164195
closure {:?}: {:?}",
165-
mono_id, Value(llfn));
166-
ccx.closure_vals().borrow_mut().insert(mono_id, llfn);
196+
instance, Value(llfn));
197+
ccx.instances().borrow_mut().insert(instance, llfn);
167198

168199
llfn
169200
}
@@ -347,7 +378,7 @@ fn trans_fn_once_adapter_shim<'a, 'tcx>(
347378

348379
// Make a version of the closure type with the same arguments, but
349380
// with argument #0 being by value.
350-
assert_eq!(abi, RustCall);
381+
assert_eq!(abi, Abi::RustCall);
351382
sig.0.inputs[0] = closure_ty;
352383
let llonce_fn_ty = tcx.mk_fn_ptr(ty::BareFnTy {
353384
unsafety: unsafety,

branches/beta/src/librustc_trans/trans/declare.rs

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
use llvm::{self, ValueRef};
2323
use middle::ty;
2424
use middle::infer;
25-
use middle::traits::ProjectionMode;
2625
use trans::abi::{Abi, FnType};
2726
use trans::attributes;
28-
use trans::base;
2927
use trans::context::CrateContext;
3028
use trans::type_::Type;
3129
use trans::type_of;
@@ -94,37 +92,21 @@ pub fn declare_cfn(ccx: &CrateContext, name: &str, fn_type: Type) -> ValueRef {
9492
/// update the declaration and return existing ValueRef instead.
9593
pub fn declare_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, name: &str,
9694
fn_type: ty::Ty<'tcx>) -> ValueRef {
97-
debug!("declare_rust_fn(name={:?}, fn_type={:?})", name,
98-
fn_type);
99-
100-
let function_type; // placeholder so that the memory ownership works out ok
101-
let (sig, abi, env) = match fn_type.sty {
102-
ty::TyFnDef(_, _, f) |
103-
ty::TyFnPtr(f) => {
104-
(&f.sig, f.abi, None)
105-
}
106-
ty::TyClosure(closure_did, ref substs) => {
107-
let infcx = infer::normalizing_infer_ctxt(ccx.tcx(),
108-
&ccx.tcx().tables,
109-
ProjectionMode::Any);
110-
function_type = infcx.closure_type(closure_did, substs);
111-
let self_type = base::self_type_for_closure(ccx, closure_did, fn_type);
112-
debug!("declare_rust_fn function_type={:?} self_type={:?}",
113-
function_type, self_type);
114-
(&function_type.sig, Abi::RustCall, Some(self_type))
115-
}
116-
_ => ccx.sess().bug("expected closure or fn")
117-
};
95+
debug!("declare_rust_fn(name={:?}, fn_type={:?})", name, fn_type);
11896

97+
let f = match fn_type.sty {
98+
ty::TyFnDef(_, _, f) | ty::TyFnPtr(f) => f,
99+
_ => unreachable!("expected fn type for {:?}, found {:?}", name, fn_type)
100+
};
119101

120-
let sig = ccx.tcx().erase_late_bound_regions(sig);
102+
let sig = ccx.tcx().erase_late_bound_regions(&f.sig);
121103
let sig = infer::normalize_associated_type(ccx.tcx(), &sig);
122104
debug!("declare_rust_fn (after region erasure) sig={:?}", sig);
123105

124-
let (cconv, llfty) = if abi == Abi::Rust || abi == Abi::RustCall {
125-
(llvm::CCallConv, type_of::type_of_rust_fn(ccx, env, &sig, abi))
106+
let (cconv, llfty) = if f.abi == Abi::Rust || f.abi == Abi::RustCall {
107+
(llvm::CCallConv, type_of::type_of_rust_fn(ccx, &sig, f.abi))
126108
} else {
127-
let fty = FnType::new(ccx, abi, &sig, &[]);
109+
let fty = FnType::new(ccx, f.abi, &sig, &[]);
128110
(fty.cconv, fty.to_llvm(ccx))
129111
};
130112

@@ -137,10 +119,10 @@ pub fn declare_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, name: &str,
137119
llvm::SetFunctionAttribute(llfn, llvm::Attribute::NoReturn);
138120
}
139121

140-
if abi == Abi::Rust || abi == Abi::RustCall {
122+
if f.abi == Abi::Rust || f.abi == Abi::RustCall {
141123
attributes::from_fn_type(ccx, fn_type).apply_llfn(llfn);
142124
} else {
143-
FnType::new(ccx, abi, &sig, &[]).add_attributes(llfn);
125+
FnType::new(ccx, f.abi, &sig, &[]).add_attributes(llfn);
144126
}
145127

146128
llfn

branches/beta/src/librustc_trans/trans/type_of.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,11 @@ pub fn untuple_arguments<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
8888
}
8989

9090
pub fn type_of_rust_fn<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
91-
maybe_env: Option<Ty<'tcx>>,
9291
sig: &ty::FnSig<'tcx>,
9392
abi: Abi)
9493
-> Type
9594
{
96-
debug!("type_of_rust_fn(sig={:?},abi={:?})",
97-
sig,
98-
abi);
95+
debug!("type_of_rust_fn(sig={:?}, abi={:?})", sig, abi);
9996

10097
assert!(!sig.variadic); // rust fns are never variadic
10198

@@ -129,11 +126,6 @@ pub fn type_of_rust_fn<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
129126
ty::FnDiverging => Type::void(cx)
130127
};
131128

132-
// Arg 1: Environment
133-
if let Some(env_ty) = maybe_env {
134-
atys.push(type_of_explicit_arg(cx, env_ty));
135-
}
136-
137129
// ... then explicit args.
138130
for input in inputs {
139131
let arg_ty = type_of_explicit_arg(cx, input);
@@ -384,7 +376,7 @@ pub fn in_memory_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) ->
384376
let sig = cx.tcx().erase_late_bound_regions(&f.sig);
385377
let sig = infer::normalize_associated_type(cx.tcx(), &sig);
386378
if f.abi == Abi::Rust || f.abi == Abi::RustCall {
387-
type_of_rust_fn(cx, None, &sig, f.abi).ptr_to()
379+
type_of_rust_fn(cx, &sig, f.abi).ptr_to()
388380
} else {
389381
FnType::new(cx, f.abi, &sig, &[]).to_llvm(cx).ptr_to()
390382
}

0 commit comments

Comments
 (0)