Skip to content

Commit

Permalink
Add support for arrays in extern statics
Browse files Browse the repository at this point in the history
  • Loading branch information
gnzlbg committed Oct 17, 2018
1 parent f8bd332 commit d389610
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
46 changes: 43 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,37 @@ impl<'a> Generator<'a> {
}}
}}
"#, name = name, ty = rust_ty));
} else if rust_ty.starts_with("[") && rust_ty.ends_with("]") {
let c_ptr_ty = c_ty.split(" ").nth(0).unwrap();
let mut lens = Vec::new();
for i in c_ty.split(" ").skip(1) {
lens.push(i);
}
lens.reverse();
let array_test_name = format!("{mutbl} {elem} (*__test_static_{name}(void)){lens}",
mutbl = if mutbl { "" } else { "const" },
elem = c_ptr_ty,
name = name,
lens = lens.join("")
);
t!(writeln!(self.c, r#"
{array_test_name} {{
return &{cname};
}}
"#, array_test_name = array_test_name, cname = cname));
t!(writeln!(self.rust, r#"
#[inline(never)]
fn static_{name}() {{
extern {{
fn __test_static_{name}() -> *{mutbl} {ty};
}}
unsafe {{
same(&{name} as *const _ as usize,
__test_static_{name}() as usize,
"{name} static");
}}
}}
"#, name = name, mutbl = if mutbl { "mut" } else { "const" }, ty = rust_ty));
} else {
let c_ty = self.rust_ty_to_c_ty(rust_ty);
t!(writeln!(self.c, r#"
Expand Down Expand Up @@ -1327,8 +1358,13 @@ impl<'a> Generator<'a> {
}
}
ast::TyKind::Array(ref t, ref e) => {
assert!(rust);
format!("[{}; {}]", self.ty2name(t, rust), self.expr2str(e))
if rust {
format!("[{}; {}]", self.ty2name(t, rust), self.expr2str(e))
} else {
let len = self.expr2str(e);
let ty = self.ty2name(t, rust);
format!("{} [{}]", ty, len)
}
}
ast::TyKind::Rptr(_, ast::MutTy {
ref ty,
Expand Down Expand Up @@ -1362,7 +1398,11 @@ impl<'a> Generator<'a> {
format!("char*")
}
}
ast::TyKind::Tup(ref v) if v.is_empty() => if rust { "()".to_string() } else { "void".to_string() },
ast::TyKind::Tup(ref v) if v.is_empty() => if rust {
"()".to_string()
} else {
"void".to_string()
},
_ => panic!("unknown ty {:?}", ty),
}
}
Expand Down
6 changes: 6 additions & 0 deletions testcrate/src/t1.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ uint8_t (*T1_static_right2)(uint8_t, uint8_t) = foo;

uint32_t (*(*T1_fn_ptr_s)(uint8_t))(uint16_t) = nested;
uint32_t (*(*T1_fn_ptr_s2)(uint8_t(*arg0)(uint8_t), uint16_t(*arg1)(uint16_t)))(uint16_t) = nested2;

int32_t T1_arr3[2] = {0};
int32_t T1_arr4[2][3] = {0};
int32_t T1_arr5[1][2][3] = {0};

int32_t T1_arr42[1][2][3] = {0};
10 changes: 10 additions & 0 deletions testcrate/src/t1.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,13 @@ uint32_t (*(*T1_fn_ptr_s)(uint8_t))(uint16_t);
// uint8_t -> uint8_t, and returning a function pointer to a function taking a
// uint16_t and returning a uint32_t
uint32_t (*(*T1_fn_ptr_s2)(uint8_t(*)(uint8_t), uint16_t(*)(uint16_t)))(uint16_t);

const int32_t T1_arr0[2] = {0};
const int32_t T1_arr1[2][3] = {0};
const int32_t T1_arr2[1][2][3] = {0};

int32_t T1_arr3[2];
int32_t T1_arr4[2][3];
int32_t T1_arr5[1][2][3];

int32_t T1_arr42[1][2][3];
14 changes: 13 additions & 1 deletion testcrate/src/t1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,17 @@ extern "C" {

pub static T1_fn_ptr_s: unsafe extern "C" fn(u8) -> extern fn(u16)->u32;
pub static T1_fn_ptr_s2: unsafe extern "C" fn(extern fn(u8)->u8,
extern fn(u16)->u16) -> extern fn(u16)->u32;
extern fn(u16)->u16)
-> extern fn(u16)->u32;

pub static T1_arr0: [i32; 2];
pub static T1_arr1: [[i32; 3]; 2];
pub static T1_arr2: [[[i32; 3]; 2]; 1];

pub static mut T1_arr3: [i32; 2];
pub static mut T1_arr4: [[i32; 3]; 2];
pub static mut T1_arr5: [[[i32; 3]; 2]; 1];

#[link_name = "T1_arr42"]
pub static mut T1_arr6: [[[i32; 3]; 2]; 1];
}

0 comments on commit d389610

Please sign in to comment.