CodeGen: Remove Incomplete members from Classes

They must not appear in the final generated code as we'd end up with
invalid types with void members, e.g.:
  struct Foo {
    int a;
    void myIncompleteMember;
    int c;
  };

Removing them from the type graph early also ensures that padding is
calculated correctly.
This commit is contained in:
Alastair Robertson 2023-12-12 10:28:50 -08:00 committed by Alastair Robertson
parent 6b780add4a
commit 7cc7aa8882
2 changed files with 25 additions and 1 deletions

View File

@ -61,7 +61,11 @@ void RemoveMembers::visit(Class& c) {
}
std::erase_if(c.members, [this, &c](Member member) {
return ignoreMember(c.name(), member.name);
if (ignoreMember(c.name(), member.name))
return true;
if (dynamic_cast<Incomplete*>(&member.type()))
return true;
return false;
});
}

View File

@ -183,3 +183,23 @@ TEST(RemoveMembersTest, Union) {
[0] Union: MyUnion (size: 4)
)");
}
TEST(RemoveMembersTest, Incomplete) {
test(RemoveMembers::createPass({}),
R"(
[0] Class: ClassA (size: 12)
Member: a (offset: 0)
Primitive: int32_t
Member: b (offset: 4)
Incomplete: [MyIncompleteType]
Member: c (offset: 8)
Primitive: int32_t
)",
R"(
[0] Class: ClassA (size: 12)
Member: a (offset: 0)
Primitive: int32_t
Member: c (offset: 8)
Primitive: int32_t
)");
}