authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2018-01-23 23:38:20+13:00
committergravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2018-01-23 23:38:20+13:00
log470ec911640880872491d4f40b182be91fad5075
treebb9c55a072ccb7d15b86880b0fc2603418fc93ea
parentfa7072f3f2d7bdb82e2590f93aa702c395e1934d

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...@@ -5977,6 +5977,16 @@ static void get_c_type(CodeGen *g, GenH *gen_h, TypeTableEntry *type_entry, Buf
5977 return;5977 return;
5978 }5978 }
5979 case TypeTableEntryIdArray:5979 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 }
5980 case TypeTableEntryIdErrorUnion:5990 case TypeTableEntryIdErrorUnion:
5981 case TypeTableEntryIdPureError:5991 case TypeTableEntryIdPureError:
5982 case TypeTableEntryIdFn:5992 case TypeTableEntryIdFn:
...@@ -6081,8 +6091,15 @@ static void gen_h_file(CodeGen *g) {...@@ -6081,8 +6091,15 @@ static void gen_h_file(CodeGen *g) {
6081 const char *comma_str = (param_i == 0) ? "" : ", ";6091 const char *comma_str = (param_i == 0) ? "" : ", ";
6082 const char *restrict_str = param_info->is_noalias ? "restrict" : "";6092 const char *restrict_str = param_info->is_noalias ? "restrict" : "";
6083 get_c_type(g, gen_h, param_info->type, &param_type_c);6093 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),6094
6085 restrict_str, buf_ptr(param_name));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 }
6086 }6103 }
6087 buf_appendf(&h_buf, ")");6104 buf_appendf(&h_buf, ")");
6088 } else {6105 } else {
...@@ -6169,7 +6186,15 @@ static void gen_h_file(CodeGen *g) {...@@ -6169,7 +6186,15 @@ static void gen_h_file(CodeGen *g) {
61696186
6170 Buf *type_name_buf = buf_alloc();6187 Buf *type_name_buf = buf_alloc();
6171 get_c_type(g, gen_h, struct_field->type_entry, type_name_buf);6188 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
6173 }6198 }
6174 fprintf(out_h, "};\n\n");6199 fprintf(out_h, "};\n\n");
6175 break;6200 break;
test/gen_h.zig+16
...@@ -50,4 +50,20 @@ pub fn addCases(cases: &tests.GenHContext) {...@@ -50,4 +50,20 @@ pub fn addCases(cases: &tests.GenHContext) {
50 \\TEST_EXPORT void entry(union Foo foo);50 \\TEST_EXPORT void entry(union Foo foo);
51 \\51 \\
52 );52 );
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 );
53}69}