Skip to content

Commit

Permalink
Merge pull request #716 from zig-lang/export-c-additions
Browse files Browse the repository at this point in the history
Add array type handling for gen_h
  • Loading branch information
andrewrk committed Jan 23, 2018
2 parents fa7072f + 470ec91 commit b8dcdc7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/codegen.cpp
Expand Up @@ -5977,6 +5977,16 @@ static void get_c_type(CodeGen *g, GenH *gen_h, TypeTableEntry *type_entry, Buf
return;
}
case TypeTableEntryIdArray:
{
TypeTableEntryArray *array_data = &type_entry->data.array;

Buf *child_buf = buf_alloc();
get_c_type(g, gen_h, array_data->child_type, child_buf);

buf_resize(out_buf, 0);
buf_appendf(out_buf, "%s", buf_ptr(child_buf));
return;
}
case TypeTableEntryIdErrorUnion:
case TypeTableEntryIdPureError:
case TypeTableEntryIdFn:
Expand Down Expand Up @@ -6081,8 +6091,15 @@ static void gen_h_file(CodeGen *g) {
const char *comma_str = (param_i == 0) ? "" : ", ";
const char *restrict_str = param_info->is_noalias ? "restrict" : "";
get_c_type(g, gen_h, param_info->type, &param_type_c);
buf_appendf(&h_buf, "%s%s%s %s", comma_str, buf_ptr(&param_type_c),
restrict_str, buf_ptr(param_name));

if (param_info->type->id == TypeTableEntryIdArray) {
// Arrays decay to pointers
buf_appendf(&h_buf, "%s%s%s %s[]", comma_str, buf_ptr(&param_type_c),
restrict_str, buf_ptr(param_name));
} else {
buf_appendf(&h_buf, "%s%s%s %s", comma_str, buf_ptr(&param_type_c),
restrict_str, buf_ptr(param_name));
}
}
buf_appendf(&h_buf, ")");
} else {
Expand Down Expand Up @@ -6169,7 +6186,15 @@ static void gen_h_file(CodeGen *g) {

Buf *type_name_buf = buf_alloc();
get_c_type(g, gen_h, struct_field->type_entry, type_name_buf);
fprintf(out_h, " %s %s;\n", buf_ptr(type_name_buf), buf_ptr(struct_field->name));

if (struct_field->type_entry->id == TypeTableEntryIdArray) {
fprintf(out_h, " %s %s[%d];\n", buf_ptr(type_name_buf),
buf_ptr(struct_field->name),
struct_field->type_entry->data.array.len);
} else {
fprintf(out_h, " %s %s;\n", buf_ptr(type_name_buf), buf_ptr(struct_field->name));
}

}
fprintf(out_h, "};\n\n");
break;
Expand Down
16 changes: 16 additions & 0 deletions test/gen_h.zig
Expand Up @@ -50,4 +50,20 @@ pub fn addCases(cases: &tests.GenHContext) {
\\TEST_EXPORT void entry(union Foo foo);
\\
);

cases.add("array field-type",
\\const Foo = extern struct {
\\ A: [2]i32,
\\ B: [4]&u32,
\\};
\\export fn entry(foo: Foo, bar: [3]u8) { }
,
\\struct Foo {
\\ int32_t A[2];
\\ uint32_t * B[4];
\\};
\\
\\TEST_EXPORT void entry(struct Foo foo, uint8_t bar[]);
\\
);
}

0 comments on commit b8dcdc7

Please sign in to comment.