Skip to content

Commit b122d16

Browse files
committed
trans: simplify the declare interface.
1 parent c6d214b commit b122d16

File tree

7 files changed

+65
-114
lines changed

7 files changed

+65
-114
lines changed

src/librustc_trans/trans/callee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ pub fn trans_fn_pointer_shim<'a, 'tcx>(
337337
//
338338
let function_name = link::mangle_internal_name_by_type_and_seq(ccx, bare_fn_ty,
339339
"fn_pointer_shim");
340-
let llfn = declare::declare_internal_rust_fn(ccx, &function_name[..], tuple_fn_ty);
340+
let llfn = declare::define_internal_fn(ccx, &function_name, tuple_fn_ty);
341341

342342
//
343343
let empty_substs = tcx.mk_substs(Substs::trans_empty());

src/librustc_trans/trans/closure.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ pub fn get_or_create_closure_declaration<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
155155
let symbol = mangle_internal_name_by_path_and_seq(path, "closure");
156156

157157
let function_type = ccx.tcx().mk_closure_from_closure_substs(closure_id, Box::new(substs));
158-
let llfn = declare::define_internal_rust_fn(ccx, &symbol[..], function_type);
158+
let llfn = declare::define_internal_fn(ccx, &symbol, function_type);
159159

160160
// set an inline hint for all closures
161161
attributes::inline(llfn, attributes::InlineAttr::Hint);
@@ -357,8 +357,7 @@ fn trans_fn_once_adapter_shim<'a, 'tcx>(
357357

358358
// Create the by-value helper.
359359
let function_name = link::mangle_internal_name_by_type_and_seq(ccx, llonce_fn_ty, "once_shim");
360-
let lloncefn = declare::define_internal_rust_fn(ccx, &function_name,
361-
llonce_fn_ty);
360+
let lloncefn = declare::define_internal_fn(ccx, &function_name, llonce_fn_ty);
362361

363362
let (block_arena, fcx): (TypedArena<_>, FunctionContext);
364363
block_arena = TypedArena::new();

src/librustc_trans/trans/common.rs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
//! Code that is useful in various trans modules.
1414
15-
pub use self::ExprOrMethodCall::*;
16-
1715
use session::Session;
1816
use llvm;
1917
use llvm::{ValueRef, BasicBlockRef, BuilderRef, ContextRef, TypeKind};
@@ -23,11 +21,11 @@ use middle::def::Def;
2321
use middle::def_id::DefId;
2422
use middle::infer;
2523
use middle::lang_items::LangItem;
26-
use middle::subst::{self, Substs};
24+
use middle::subst::Substs;
2725
use trans::base;
2826
use trans::build;
2927
use trans::builder::Builder;
30-
use trans::callee;
28+
use trans::callee::Callee;
3129
use trans::cleanup;
3230
use trans::consts;
3331
use trans::datum;
@@ -43,14 +41,15 @@ use middle::traits::{self, SelectionContext, ProjectionMode};
4341
use middle::ty::fold::{TypeFolder, TypeFoldable};
4442
use rustc_front::hir;
4543
use rustc::mir::repr::Mir;
46-
use util::nodemap::{FnvHashMap, NodeMap};
44+
use util::nodemap::NodeMap;
4745

4846
use arena::TypedArena;
4947
use libc::{c_uint, c_char};
5048
use std::ops::Deref;
5149
use std::ffi::CString;
5250
use std::cell::{Cell, RefCell};
53-
use std::vec::Vec;
51+
52+
use syntax::abi::Abi;
5453
use syntax::ast;
5554
use syntax::codemap::{DUMMY_SP, Span};
5655
use syntax::parse::token::InternedString;
@@ -253,8 +252,6 @@ pub fn BuilderRef_res(b: BuilderRef) -> BuilderRef_res {
253252
}
254253
}
255254

256-
pub type ExternMap = FnvHashMap<String, ValueRef>;
257-
258255
pub fn validate_substs(substs: &Substs) {
259256
assert!(!substs.types.needs_infer());
260257
}
@@ -539,31 +536,33 @@ impl<'a, 'tcx> FunctionContext<'a, 'tcx> {
539536

540537
// Returns a ValueRef of the "eh_unwind_resume" lang item if one is defined,
541538
// otherwise declares it as an external function.
542-
pub fn eh_unwind_resume(&self) -> ValueRef {
539+
pub fn eh_unwind_resume(&self) -> Callee<'tcx> {
543540
use trans::attributes;
544-
assert!(self.ccx.sess().target.target.options.custom_unwind_resume);
545-
match self.ccx.tcx().lang_items.eh_unwind_resume() {
546-
Some(def_id) => {
547-
callee::trans_fn_ref(self.ccx, def_id, ExprId(0),
548-
self.param_substs).val
549-
}
550-
None => {
551-
let mut unwresume = self.ccx.eh_unwind_resume().borrow_mut();
552-
match *unwresume {
553-
Some(llfn) => llfn,
554-
None => {
555-
let fty = Type::func(&[Type::i8p(self.ccx)], &Type::void(self.ccx));
556-
let llfn = declare::declare_fn(self.ccx,
557-
"rust_eh_unwind_resume",
558-
llvm::CCallConv,
559-
fty, ty::FnDiverging);
560-
attributes::unwind(llfn, true);
561-
*unwresume = Some(llfn);
562-
llfn
563-
}
564-
}
565-
}
541+
let ccx = self.ccx;
542+
let tcx = ccx.tcx();
543+
assert!(ccx.sess().target.target.options.custom_unwind_resume);
544+
if let Some(def_id) = tcx.lang_items.eh_unwind_resume() {
545+
return Callee::def(ccx, def_id, tcx.mk_substs(Substs::empty()));
546+
}
547+
548+
let ty = tcx.mk_fn_ptr(ty::BareFnTy {
549+
unsafety: hir::Unsafety::Unsafe,
550+
abi: Abi::C,
551+
sig: ty::Binder(ty::FnSig {
552+
inputs: vec![tcx.mk_mut_ptr(tcx.types.u8)],
553+
output: ty::FnDiverging,
554+
variadic: false
555+
}),
556+
});
557+
558+
let unwresume = ccx.eh_unwind_resume();
559+
if let Some(llfn) = unwresume.get() {
560+
return Callee::ptr(datum::immediate_rvalue(llfn, ty));
566561
}
562+
let llfn = declare::declare_fn(ccx, "rust_eh_unwind_resume", ty);
563+
attributes::unwind(llfn, true);
564+
unwresume.set(Some(llfn));
565+
Callee::ptr(datum::immediate_rvalue(llfn, ty))
567566
}
568567
}
569568

src/librustc_trans/trans/declare.rs

Lines changed: 29 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use middle::traits::ProjectionMode;
2626
use syntax::abi::Abi;
2727
use trans::attributes;
2828
use trans::base;
29+
use trans::cabi::FnType;
2930
use trans::context::CrateContext;
3031
use trans::type_::Type;
3132
use trans::type_of;
@@ -51,8 +52,6 @@ pub fn declare_global(ccx: &CrateContext, name: &str, ty: Type) -> llvm::ValueRe
5152

5253
/// Declare a function.
5354
///
54-
/// For rust functions use `declare_rust_fn` instead.
55-
///
5655
/// If there’s a value with the same name already declared, the function will
5756
/// update the declaration and return existing ValueRef instead.
5857
fn declare_raw_fn(ccx: &CrateContext, name: &str, callconv: llvm::CallConv, ty: Type) -> ValueRef {
@@ -81,7 +80,7 @@ fn declare_raw_fn(ccx: &CrateContext, name: &str, callconv: llvm::CallConv, ty:
8180
/// Declare a C ABI function.
8281
///
8382
/// Only use this for foreign function ABIs and glue. For Rust functions use
84-
/// `declare_rust_fn` instead.
83+
/// `declare_fn` instead.
8584
///
8685
/// If there’s a value with the same name already declared, the function will
8786
/// update the declaration and return existing ValueRef instead.
@@ -94,8 +93,8 @@ pub fn declare_cfn(ccx: &CrateContext, name: &str, fn_type: Type) -> ValueRef {
9493
///
9594
/// If there’s a value with the same name already declared, the function will
9695
/// update the declaration and return existing ValueRef instead.
97-
pub fn declare_rust_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, name: &str,
98-
fn_type: ty::Ty<'tcx>) -> ValueRef {
96+
pub fn declare_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, name: &str,
97+
fn_type: ty::Ty<'tcx>) -> ValueRef {
9998
debug!("declare_rust_fn(name={:?}, fn_type={:?})", name,
10099
fn_type);
101100

@@ -118,28 +117,33 @@ pub fn declare_rust_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, name: &str,
118117
_ => ccx.sess().bug("expected closure or fn")
119118
};
120119

120+
121121
let sig = ccx.tcx().erase_late_bound_regions(sig);
122122
let sig = infer::normalize_associated_type(ccx.tcx(), &sig);
123123
debug!("declare_rust_fn (after region erasure) sig={:?}", sig);
124-
let llfty = type_of::type_of_rust_fn(ccx, env, &sig, abi);
125-
debug!("declare_rust_fn llfty={:?}", llfty);
124+
125+
let (cconv, llfty) = if abi == Abi::Rust || abi == Abi::RustCall {
126+
(llvm::CCallConv, type_of::type_of_rust_fn(ccx, env, &sig, abi))
127+
} else {
128+
let fty = FnType::new(ccx, abi, &sig, &[]);
129+
(fty.cconv, fty.to_llvm(ccx))
130+
};
126131

127132
// it is ok to directly access sig.0.output because we erased all
128133
// late-bound-regions above
129-
let llfn = declare_fn(ccx, name, llvm::CCallConv, llfty, sig.output);
130-
attributes::from_fn_type(ccx, fn_type).apply_llfn(llfn);
131-
llfn
132-
}
134+
debug!("declare_rust_fn llfty={:?}", llfty);
135+
let llfn = declare_raw_fn(ccx, name, cconv, llfty);
133136

137+
if sig.output == ty::FnDiverging {
138+
llvm::SetFunctionAttribute(llfn, llvm::Attribute::NoReturn);
139+
}
140+
141+
if abi == Abi::Rust || abi == Abi::RustCall {
142+
attributes::from_fn_type(ccx, fn_type).apply_llfn(llfn);
143+
} else {
144+
FnType::new(ccx, abi, &sig, &[]).add_attributes(llfn);
145+
}
134146

135-
/// Declare a Rust function with internal linkage.
136-
///
137-
/// If there’s a value with the same name already declared, the function will
138-
/// update the declaration and return existing ValueRef instead.
139-
pub fn declare_internal_rust_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, name: &str,
140-
fn_type: ty::Ty<'tcx>) -> ValueRef {
141-
let llfn = declare_rust_fn(ccx, name, fn_type);
142-
llvm::SetLinkage(llfn, llvm::InternalLinkage);
143147
llfn
144148
}
145149

@@ -159,71 +163,20 @@ pub fn define_global(ccx: &CrateContext, name: &str, ty: Type) -> Option<ValueRe
159163
}
160164

161165

162-
/// Declare a function with an intention to define it.
163-
///
164-
/// For rust functions use `define_rust_fn` instead.
165-
///
166-
/// Use this function when you intend to define a function. This function will
167-
/// return None if the name already has a definition associated with it. In that
168-
/// case an error should be reported to the user, because it usually happens due
169-
/// to user’s fault (e.g. misuse of #[no_mangle] or #[export_name] attributes).
170-
pub fn define_fn(ccx: &CrateContext, name: &str, callconv: llvm::CallConv,
171-
fn_type: Type, output: ty::FnOutput) -> Option<ValueRef> {
172-
if get_defined_value(ccx, name).is_some() {
173-
None
174-
} else {
175-
Some(declare_fn(ccx, name, callconv, fn_type, output))
176-
}
177-
}
178-
179-
180-
/// Declare a C ABI function with an intention to define it.
181-
///
182-
/// Use this function when you intend to define a function. This function will
183-
/// return None if the name already has a definition associated with it. In that
184-
/// case an error should be reported to the user, because it usually happens due
185-
/// to user’s fault (e.g. misuse of #[no_mangle] or #[export_name] attributes).
186-
///
187-
/// Only use this for foreign function ABIs and glue. For Rust functions use
188-
/// `declare_rust_fn` instead.
189-
pub fn define_cfn(ccx: &CrateContext, name: &str, fn_type: Type,
190-
output: ty::Ty) -> Option<ValueRef> {
191-
if get_defined_value(ccx, name).is_some() {
192-
None
193-
} else {
194-
Some(declare_cfn(ccx, name, fn_type, output))
195-
}
196-
}
197-
198-
199-
/// Declare a Rust function with an intention to define it.
200-
///
201-
/// Use this function when you intend to define a function. This function will
202-
/// return None if the name already has a definition associated with it. In that
203-
/// case an error should be reported to the user, because it usually happens due
204-
/// to user’s fault (e.g. misuse of #[no_mangle] or #[export_name] attributes).
205-
pub fn define_rust_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, name: &str,
206-
fn_type: ty::Ty<'tcx>) -> Option<ValueRef> {
207-
if get_defined_value(ccx, name).is_some() {
208-
None
209-
} else {
210-
Some(declare_rust_fn(ccx, name, fn_type))
211-
}
212-
}
213-
214-
215166
/// Declare a Rust function with an intention to define it.
216167
///
217168
/// Use this function when you intend to define a function. This function will
218169
/// return panic if the name already has a definition associated with it. This
219170
/// can happen with #[no_mangle] or #[export_name], for example.
220-
pub fn define_internal_rust_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
221-
name: &str,
222-
fn_type: ty::Ty<'tcx>) -> ValueRef {
171+
pub fn define_internal_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
172+
name: &str,
173+
fn_type: ty::Ty<'tcx>) -> ValueRef {
223174
if get_defined_value(ccx, name).is_some() {
224175
ccx.sess().fatal(&format!("symbol `{}` already defined", name))
225176
} else {
226-
declare_internal_rust_fn(ccx, name, fn_type)
177+
let llfn = declare_fn(ccx, name, fn_type);
178+
llvm::SetLinkage(llfn, llvm::InternalLinkage);
179+
llfn
227180
}
228181
}
229182

src/librustc_trans/trans/foreign.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ pub fn trans_rust_fn_with_foreign_abi<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
530530
ccx.tcx().map.path_to_string(id),
531531
id, rust_fn_ty);
532532

533-
let llfn = declare::define_internal_rust_fn(ccx, &ps, t);
533+
let llfn = declare::define_internal_fn(ccx, &ps, rust_fn_ty);
534534
attributes::from_fn_attrs(ccx, attrs, llfn);
535535
base::trans_fn(ccx, decl, body, llfn, param_substs, id, attrs);
536536
llfn

src/librustc_trans/trans/intrinsic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ fn gen_fn<'a, 'tcx>(fcx: &FunctionContext<'a, 'tcx>,
12751275
trans: &mut for<'b> FnMut(Block<'b, 'tcx>))
12761276
-> ValueRef {
12771277
let ccx = fcx.ccx;
1278-
let llfn = declare::define_internal_rust_fn(ccx, name, ty);
1278+
let llfn = declare::define_internal_fn(ccx, name, ty);
12791279
let (fcx, block_arena);
12801280
block_arena = TypedArena::new();
12811281
fcx = new_fn_ctxt(ccx, llfn, ast::DUMMY_NODE_ID, false,

src/librustc_trans/trans/meth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub fn trans_object_shim<'a, 'tcx>(ccx: &'a CrateContext<'a, 'tcx>,
108108

109109
//
110110
let function_name = link::mangle_internal_name_by_type_and_seq(ccx, shim_fn_ty, "object_shim");
111-
let llfn = declare::define_internal_rust_fn(ccx, &function_name, shim_fn_ty);
111+
let llfn = declare::define_internal_fn(ccx, &function_name, shim_fn_ty);
112112

113113
let empty_substs = tcx.mk_substs(Substs::trans_empty());
114114
let (block_arena, fcx): (TypedArena<_>, FunctionContext);

0 commit comments

Comments
 (0)