diff --git a/src/parseh.cpp b/src/parseh.cpp index 8efef636a5e1c64e550907d95fc61f537aa00e9b..5a829edc4b980823e4d8cd34347aea7fc24bc250 100644 --- a/src/parseh.cpp +++ b/src/parseh.cpp @@ -537,6 +537,10 @@ static void visit_enum_decl(Context *c, const EnumDecl *enum_decl) { return; } + // eagerly put the name in the table, but we need to remember to remove it if it fails + // boy it would be nice to have defer here wouldn't it + c->enum_type_table.put(bare_name, true); + const EnumDecl *enum_def = enum_decl->getDefinition(); if (!enum_def) { @@ -553,10 +557,6 @@ static void visit_enum_decl(Context *c, const EnumDecl *enum_decl) { node->data.struct_decl.visib_mod = VisibModExport; node->data.struct_decl.directives = create_empty_directives(c); - // eagerly put the name in the table, but we need to remember to remove it if it fails - // boy it would be nice to have defer here wouldn't it - c->enum_type_table.put(bare_name, true); - ZigList var_decls = {0}; int i = 0; @@ -624,6 +624,11 @@ static void visit_record_decl(Context *c, const RecordDecl *record_decl) { return; } + // eagerly put the name in the table, but we need to remember to remove it if it fails + // boy it would be nice to have defer here wouldn't it + c->struct_type_table.put(bare_name, true); + + RecordDecl *record_def = record_decl->getDefinition(); if (!record_def) { // this is a type that we can point to but that's it, such as `struct Foo;`. @@ -639,10 +644,6 @@ static void visit_record_decl(Context *c, const RecordDecl *record_decl) { node->data.struct_decl.visib_mod = VisibModExport; node->data.struct_decl.directives = create_empty_directives(c); - // eagerly put the name in the table, but we need to remember to remove it if it fails - // boy it would be nice to have defer here wouldn't it - c->struct_type_table.put(bare_name, true); - for (auto it = record_def->field_begin(), it_end = record_def->field_end(); it != it_end; ++it) diff --git a/test/run_tests.cpp b/test/run_tests.cpp index a4cd3bbe5734d458374bac216077bb72627e861e..bb205031cba02ce5ac3df261414ee5f74685ae5a 100644 --- a/test/run_tests.cpp +++ b/test/run_tests.cpp @@ -1962,6 +1962,7 @@ pub const Bar = enum_Bar;)OUTPUT"); void func(int array[20]); )SOURCE", R"OUTPUT(pub extern fn func(array: [20]c_int);)OUTPUT"); + add_parseh_case("self referential struct with function pointer", R"SOURCE( struct Foo { void (*derp)(struct Foo *foo); @@ -1969,6 +1970,14 @@ struct Foo { )SOURCE", R"OUTPUT(export struct struct_Foo { derp: ?extern fn (?&struct_Foo), } +pub const Foo = struct_Foo;)OUTPUT"); + + + add_parseh_case("struct prototype used in func", R"SOURCE( +struct Foo; +struct Foo *some_func(struct Foo *foo, int x); + )SOURCE", R"OUTPUT(pub const struct_Foo = u8; +pub extern fn some_func(foo: ?&struct_Foo, x: c_int) -> ?&struct_Foo; pub const Foo = struct_Foo;)OUTPUT"); }