authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-01-23 09:20:57-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2018-01-23 09:20:57-05:00
logb8dcdc75c10866362c9526885d3c0c99ff4bfdf9
treebb9c55a072ccb7d15b86880b0fc2603418fc93ea
parentfa7072f3f2d7bdb82e2590f93aa702c395e1934d
parent470ec911640880872491d4f40b182be91fad5075
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #716 from zig-lang/export-c-additions

Add array type handling for gen_h

2 files changed, 44 insertions(+), 3 deletions(-)

src/codegen.cpp+28-3
......@@ -5977,6 +5977,16 @@ static void get_c_type(CodeGen *g, GenH *gen_h, TypeTableEntry *type_entry, Buf
59775977 return;
59785978 }
59795979 case TypeTableEntryIdArray:
5980 {
5981 TypeTableEntryArray *array_data = &type_entry->data.array;
5982
5983 Buf *child_buf = buf_alloc();
5984 get_c_type(g, gen_h, array_data->child_type, child_buf);
5985
5986 buf_resize(out_buf, 0);
5987 buf_appendf(out_buf, "%s", buf_ptr(child_buf));
5988 return;
5989 }
59805990 case TypeTableEntryIdErrorUnion:
59815991 case TypeTableEntryIdPureError:
59825992 case TypeTableEntryIdFn:
......@@ -6081,8 +6091,15 @@ static void gen_h_file(CodeGen *g) {
60816091 const char *comma_str = (param_i == 0) ? "" : ", ";
60826092 const char *restrict_str = param_info->is_noalias ? "restrict" : "";
60836093 get_c_type(g, gen_h, param_info->type, &param_type_c);
6084 buf_appendf(&h_buf, "%s%s%s %s", comma_str, buf_ptr(&param_type_c),
6085 restrict_str, buf_ptr(param_name));
6094
6095 if (param_info->type->id == TypeTableEntryIdArray) {
6096 // Arrays decay to pointers
6097 buf_appendf(&h_buf, "%s%s%s %s[]", comma_str, buf_ptr(&param_type_c),
6098 restrict_str, buf_ptr(param_name));
6099 } else {
6100 buf_appendf(&h_buf, "%s%s%s %s", comma_str, buf_ptr(&param_type_c),
6101 restrict_str, buf_ptr(param_name));
6102 }
60866103 }
60876104 buf_appendf(&h_buf, ")");
60886105 } else {
......@@ -6169,7 +6186,15 @@ static void gen_h_file(CodeGen *g) {
61696186
61706187 Buf *type_name_buf = buf_alloc();
61716188 get_c_type(g, gen_h, struct_field->type_entry, type_name_buf);
6172 fprintf(out_h, " %s %s;\n", buf_ptr(type_name_buf), buf_ptr(struct_field->name));
6189
6190 if (struct_field->type_entry->id == TypeTableEntryIdArray) {
6191 fprintf(out_h, " %s %s[%d];\n", buf_ptr(type_name_buf),
6192 buf_ptr(struct_field->name),
6193 struct_field->type_entry->data.array.len);
6194 } else {
6195 fprintf(out_h, " %s %s;\n", buf_ptr(type_name_buf), buf_ptr(struct_field->name));
6196 }
6197
61736198 }
61746199 fprintf(out_h, "};\n\n");
61756200 break;
test/gen_h.zig+16
......@@ -50,4 +50,20 @@ pub fn addCases(cases: &tests.GenHContext) {
5050 \\TEST_EXPORT void entry(union Foo foo);
5151 \\
5252 );
53
54 cases.add("array field-type",
55 \\const Foo = extern struct {
56 \\ A: [2]i32,
57 \\ B: [4]&u32,
58 \\};
59 \\export fn entry(foo: Foo, bar: [3]u8) { }
60 ,
61 \\struct Foo {
62 \\ int32_t A[2];
63 \\ uint32_t * B[4];
64 \\};
65 \\
66 \\TEST_EXPORT void entry(struct Foo foo, uint8_t bar[]);
67 \\
68 );
5369}