Skip to content

Commit cb45703

Browse files
committed
Move get_extern_fn and get_res_dtor out of base
These functions have only a single use and functionally belong to foreign and glue respectively anyway
1 parent c71970e commit cb45703

File tree

3 files changed

+68
-69
lines changed

3 files changed

+68
-69
lines changed

src/librustc_trans/trans/base.rs

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use trans::cleanup;
5454
use trans::closure;
5555
use trans::common::{Block, C_bool, C_bytes_in_context, C_i32, C_int, C_integral};
5656
use trans::common::{C_null, C_struct_in_context, C_u64, C_u8, C_undef};
57-
use trans::common::{CrateContext, ExternMap, FunctionContext};
57+
use trans::common::{CrateContext, FunctionContext};
5858
use trans::common::{Result, NodeIdAndSpan};
5959
use trans::common::{node_id_type, return_type_is_void};
6060
use trans::common::{type_is_immediate, type_is_zero_size, val_ty};
@@ -67,7 +67,6 @@ use trans::debuginfo::{self, DebugLoc, ToDebugLoc};
6767
use trans::expr;
6868
use trans::foreign;
6969
use trans::glue;
70-
use trans::inline;
7170
use trans::intrinsic;
7271
use trans::machine;
7372
use trans::machine::{llsize_of, llsize_of_real};
@@ -84,7 +83,7 @@ use util::sha2::Sha256;
8483
use util::nodemap::NodeMap;
8584

8685
use arena::TypedArena;
87-
use libc::{c_uint, uint64_t};
86+
use libc::c_uint;
8887
use std::ffi::{CStr, CString};
8988
use std::cell::{Cell, RefCell};
9089
use std::collections::HashSet;
@@ -218,23 +217,6 @@ pub fn decl_cdecl_fn(ccx: &CrateContext,
218217
decl_fn(ccx, name, llvm::CCallConv, ty, ty::FnConverging(output))
219218
}
220219

221-
// only use this for foreign function ABIs and glue, use `get_extern_rust_fn` for Rust functions
222-
pub fn get_extern_fn(ccx: &CrateContext,
223-
externs: &mut ExternMap,
224-
name: &str,
225-
cc: llvm::CallConv,
226-
ty: Type,
227-
output: Ty)
228-
-> ValueRef {
229-
match externs.get(name) {
230-
Some(n) => return *n,
231-
None => {}
232-
}
233-
let f = decl_fn(ccx, name, cc, ty, ty::FnConverging(output));
234-
externs.insert(name.to_string(), f);
235-
f
236-
}
237-
238220
fn get_extern_rust_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fn_ty: Ty<'tcx>,
239221
name: &str, did: ast::DefId) -> ValueRef {
240222
match ccx.externs().borrow().get(name) {
@@ -400,45 +382,6 @@ pub fn note_unique_llvm_symbol(ccx: &CrateContext, sym: String) {
400382
ccx.all_llvm_symbols().borrow_mut().insert(sym);
401383
}
402384

403-
404-
pub fn get_res_dtor<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
405-
did: ast::DefId,
406-
t: Ty<'tcx>,
407-
parent_id: ast::DefId,
408-
substs: &Substs<'tcx>)
409-
-> ValueRef {
410-
let _icx = push_ctxt("trans_res_dtor");
411-
let did = inline::maybe_instantiate_inline(ccx, did);
412-
413-
if !substs.types.is_empty() {
414-
assert_eq!(did.krate, ast::LOCAL_CRATE);
415-
416-
// Since we're in trans we don't care for any region parameters
417-
let substs = ccx.tcx().mk_substs(Substs::erased(substs.types.clone()));
418-
419-
let (val, _, _) = monomorphize::monomorphic_fn(ccx, did, substs, None);
420-
421-
val
422-
} else if did.krate == ast::LOCAL_CRATE {
423-
get_item_val(ccx, did.node)
424-
} else {
425-
let tcx = ccx.tcx();
426-
let name = csearch::get_symbol(&ccx.sess().cstore, did);
427-
let class_ty = ty::lookup_item_type(tcx, parent_id).ty.subst(tcx, substs);
428-
let llty = type_of_dtor(ccx, class_ty);
429-
let dtor_ty = ty::mk_ctor_fn(ccx.tcx(),
430-
did,
431-
&[glue::get_drop_glue_type(ccx, t)],
432-
ty::mk_nil(ccx.tcx()));
433-
get_extern_fn(ccx,
434-
&mut *ccx.externs().borrow_mut(),
435-
&name[..],
436-
llvm::CCallConv,
437-
llty,
438-
dtor_ty)
439-
}
440-
}
441-
442385
pub fn bin_op_to_icmp_predicate(ccx: &CrateContext, op: ast::BinOp_, signed: bool)
443386
-> llvm::IntPredicate {
444387
match op {

src/librustc_trans/trans/foreign.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,23 @@ pub fn register_static(ccx: &CrateContext,
165165
}
166166
}
167167

168+
// only use this for foreign function ABIs and glue, use `get_extern_rust_fn` for Rust functions
169+
pub fn get_extern_fn(ccx: &CrateContext,
170+
externs: &mut ExternMap,
171+
name: &str,
172+
cc: llvm::CallConv,
173+
ty: Type,
174+
output: Ty)
175+
-> ValueRef {
176+
match externs.get(name) {
177+
Some(n) => return *n,
178+
None => {}
179+
}
180+
let f = base::decl_fn(ccx, name, cc, ty, ty::FnConverging(output));
181+
externs.insert(name.to_string(), f);
182+
f
183+
}
184+
168185
/// Registers a foreign function found in a library. Just adds a LLVM global.
169186
pub fn register_foreign_item_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
170187
abi: Abi, fty: Ty<'tcx>,
@@ -190,12 +207,7 @@ pub fn register_foreign_item_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
190207
// Create the LLVM value for the C extern fn
191208
let llfn_ty = lltype_for_fn_from_foreign_types(ccx, &tys);
192209

193-
let llfn = base::get_extern_fn(ccx,
194-
&mut *ccx.externs().borrow_mut(),
195-
name,
196-
cc,
197-
llfn_ty,
198-
fty);
210+
let llfn = get_extern_fn(ccx, &mut *ccx.externs().borrow_mut(), name, cc, llfn_ty, fty);
199211
add_argument_attributes(&tys, llfn);
200212

201213
llfn

src/librustc_trans/trans/glue.rs

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515

1616
use back::abi;
1717
use back::link::*;
18-
use llvm::{ValueRef, get_param};
1918
use llvm;
19+
use llvm::{ValueRef, get_param};
20+
use metadata::csearch;
2021
use middle::lang_items::ExchangeFreeFnLangItem;
2122
use middle::subst;
2223
use middle::subst::{Subst, Substs};
24+
use middle::ty::{self, Ty};
2325
use trans::adt;
2426
use trans::adt::GetDtorType; // for tcx.dtor_type()
2527
use trans::base::*;
@@ -28,15 +30,19 @@ use trans::callee;
2830
use trans::cleanup;
2931
use trans::cleanup::CleanupMethods;
3032
use trans::common::*;
33+
use trans::consts;
3134
use trans::datum;
3235
use trans::debuginfo::DebugLoc;
3336
use trans::expr;
37+
use trans::foreign;
38+
use trans::inline;
3439
use trans::machine::*;
35-
use trans::type_::Type;
40+
use trans::monomorphize;
41+
use trans::tvec;
3642
use trans::type_of::{type_of, sizing_type_of, align_of};
37-
use middle::ty::{self, Ty};
38-
use util::ppaux::{ty_to_short_str, Repr};
43+
use trans::type_::Type;
3944
use util::ppaux;
45+
use util::ppaux::{ty_to_short_str, Repr};
4046

4147
use arena::TypedArena;
4248
use libc::c_uint;
@@ -259,6 +265,44 @@ fn trans_struct_drop_flag<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
259265

260266
}
261267

268+
pub fn get_res_dtor<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
269+
did: ast::DefId,
270+
t: Ty<'tcx>,
271+
parent_id: ast::DefId,
272+
substs: &Substs<'tcx>)
273+
-> ValueRef {
274+
let _icx = push_ctxt("trans_res_dtor");
275+
let did = inline::maybe_instantiate_inline(ccx, did);
276+
277+
if !substs.types.is_empty() {
278+
assert_eq!(did.krate, ast::LOCAL_CRATE);
279+
280+
// Since we're in trans we don't care for any region parameters
281+
let substs = ccx.tcx().mk_substs(Substs::erased(substs.types.clone()));
282+
283+
let (val, _, _) = monomorphize::monomorphic_fn(ccx, did, substs, None);
284+
285+
val
286+
} else if did.krate == ast::LOCAL_CRATE {
287+
get_item_val(ccx, did.node)
288+
} else {
289+
let tcx = ccx.tcx();
290+
let name = csearch::get_symbol(&ccx.sess().cstore, did);
291+
let class_ty = ty::lookup_item_type(tcx, parent_id).ty.subst(tcx, substs);
292+
let llty = type_of_dtor(ccx, class_ty);
293+
let dtor_ty = ty::mk_ctor_fn(ccx.tcx(),
294+
did,
295+
&[get_drop_glue_type(ccx, t)],
296+
ty::mk_nil(ccx.tcx()));
297+
foreign::get_extern_fn(ccx,
298+
&mut *ccx.externs().borrow_mut(),
299+
&name[..],
300+
llvm::CCallConv,
301+
llty,
302+
dtor_ty)
303+
}
304+
}
305+
262306
fn trans_struct_drop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
263307
t: Ty<'tcx>,
264308
v0: ValueRef,

0 commit comments

Comments
 (0)