Skip to content

Commit

Permalink
Add support for fixed-size array arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Jul 9, 2017
1 parent 466d365 commit f574bfb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,9 @@ impl<'a> Generator<'a> {
format!("{} {}*", self.ty2name(&t.ty, rust),
modifier)
}
ast::TyKind::FixedLengthVec(ref t, _) => {
format!("{}{}*", modifier, self.ty2name(t, rust))
}
_ => {
format!("{}{}*", modifier, self.ty2name(&t.ty, rust))
}
Expand Down Expand Up @@ -1174,10 +1177,19 @@ impl<'a> Generator<'a> {
}
ast::TyKind::Rptr(_, ast::MutTy {
ref ty,
mutbl: ast::Mutability::Immutable,
mutbl,
}) => {
let path = match ty.node {
ast::TyKind::Path(_, ref p) => p,
ast::TyKind::FixedLengthVec(ref t, _) => {
assert!(!rust);
return format!("{}{}*",
match mutbl {
ast::Mutability::Immutable => "const ",
ast::Mutability::Mutable => "",
},
self.ty2name(t, rust))
}
_ => panic!("unknown ty {:?}", ty),
};
if path.segments.len() != 1 {
Expand All @@ -1186,6 +1198,9 @@ impl<'a> Generator<'a> {
if &*path.segments[0].identifier.name.as_str() != "str" {
panic!("unknown ty {:?}", ty)
}
if mutbl != ast::Mutability::Immutable {
panic!("unknown ty {:?}", ty)
}
if rust {
format!("&str")
} else {
Expand Down Expand Up @@ -1251,6 +1266,14 @@ impl<'a> Generator<'a> {
path.segments.last().unwrap().identifier.to_string()
}
ast::ExprKind::Cast(ref e, _) => self.expr2str(e),
ast::ExprKind::Binary(ref op, ref e1, ref e2) => {
let e1 = self.expr2str(e1);
let e2 = self.expr2str(e2);
match op.node {
ast::BinOpKind::Add => format!("{} + {}", e1, e2),
_ => panic!("unknown op: {:?}", op),
}
}
_ => panic!("unknown expr: {:?}", e),
}
}
Expand Down
4 changes: 4 additions & 0 deletions testcrate/src/t1.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ void* T1c(void* a) { return NULL; }
int32_t T1d(unsigned a ) { return 0; }
void T1e(unsigned a, const struct T1Bar* b) { }
void T1f(void) {}
void T1g(const int32_t a[4]) {}
void T1h(const int32_t a[4]) {}
void T1i(int32_t a[4]) {}
void T1j(int32_t a[4]) {}
4 changes: 4 additions & 0 deletions testcrate/src/t1.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ void* T1c(void*);
int32_t T1d(unsigned);
void T1e(unsigned, const struct T1Bar*);
void T1f(void);
void T1g(const int32_t a[4]);
void T1h(const int32_t a[4]);
void T1i(int32_t a[4]);
void T1j(int32_t a[4]);

#define T1C 4

5 changes: 5 additions & 0 deletions testcrate/src/t1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ extern {

#[link_name = "T1f"]
pub fn f() -> ();

pub fn T1g(a: *const [i32; 4]);
pub fn T1h(a: &[i32; 4]);
pub fn T1i(a: *mut [i32; 4]);
pub fn T1j(a: &mut [i32; 4]);
}

0 comments on commit f574bfb

Please sign in to comment.