Skip to content

Commit

Permalink
Correctly set return type attributes on foreign function declarations
Browse files Browse the repository at this point in the history
The ArgType type gives us a generic way to specify an attribute for a
type to ensure ABI conformance for foreign functions. But the code that
actually sets the argument attributes in the function declaration
only sets the attribute for the return type when the type is indirect.

Since LLVMAddAttribute() doesn't allow to set attributes on the return
type, we have to use LLVMAddFunctionAttribute() instead.

This didn't cause problems yet, because currently only some indirect
types require attributes to be set.
  • Loading branch information
dotdash committed Jun 21, 2014
1 parent f556c8c commit abdbaa2
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions src/librustc/middle/trans/foreign.rs
Expand Up @@ -934,22 +934,17 @@ pub fn lltype_for_foreign_fn(ccx: &CrateContext, ty: ty::t) -> Type {

fn add_argument_attributes(tys: &ForeignTypes,
llfn: ValueRef) {
let mut i = 0;

if tys.fn_ty.ret_ty.is_indirect() {
match tys.fn_ty.ret_ty.attr {
Some(attr) => {
let llarg = get_param(llfn, i);
unsafe {
llvm::LLVMAddAttribute(llarg, attr as c_uint);
}
}
None => {}
}
let mut i = if tys.fn_ty.ret_ty.is_indirect() { 1 } else { 0 };

i += 1;
match tys.fn_ty.ret_ty.attr {
Some(attr) => unsafe {
llvm::LLVMAddFunctionAttribute(llfn, i as c_uint, attr as u64);
},
None => {}
}

i += 1;

for &arg_ty in tys.fn_ty.arg_tys.iter() {
if arg_ty.is_ignore() {
continue;
Expand All @@ -958,12 +953,9 @@ fn add_argument_attributes(tys: &ForeignTypes,
if arg_ty.pad.is_some() { i += 1; }

match arg_ty.attr {
Some(attr) => {
let llarg = get_param(llfn, i);
unsafe {
llvm::LLVMAddAttribute(llarg, attr as c_uint);
}
}
Some(attr) => unsafe {
llvm::LLVMAddFunctionAttribute(llfn, i as c_uint, attr as u64);
},
None => ()
}

Expand Down

0 comments on commit abdbaa2

Please sign in to comment.