AlignmentCalc: Recurse into params,parents,members,children of Classes

This commit is contained in:
Alastair Robertson 2023-07-19 07:43:49 -07:00 committed by Alastair Robertson
parent 623f896e9e
commit 14d193df64
2 changed files with 93 additions and 4 deletions

View File

@ -47,11 +47,16 @@ void AlignmentCalc::accept(Type& type) {
type.accept(*this);
}
// TODO we will need to calculate alignment for c.templateParams too??
// TODO same for children. test this
void AlignmentCalc::visit(Class& c) {
// AlignmentCalc should be run after Flattener
assert(c.parents.empty());
for (const auto& param : c.templateParams) {
accept(param.type());
}
for (const auto& parent : c.parents) {
accept(parent.type());
}
for (const auto& child : c.children) {
accept(child);
}
uint64_t alignment = 1;
for (auto& member : c.members) {

View File

@ -81,3 +81,87 @@ TEST(AlignmentCalcTest, Packed) {
Primitive: int64_t
)");
}
TEST(AlignmentCalcTest, RecurseClassParam) {
test(AlignmentCalc::createPass(), R"(
[0] Class: MyClass (size: 0)
Param
[1] Class: ClassA (size: 16)
Member: a (offset: 0)
Primitive: int8_t
Member: b (offset: 4)
Primitive: int64_t
)",
R"(
[0] Class: MyClass (size: 0, align: 1)
Param
[1] Class: ClassA (size: 16, align: 8)
Member: a (offset: 0, align: 1)
Primitive: int8_t
Member: b (offset: 4, align: 8)
Primitive: int64_t
)");
}
TEST(AlignmentCalcTest, RecurseClassParent) {
test(AlignmentCalc::createPass(), R"(
[0] Class: MyClass (size: 0)
Parent (offset: 0)
[1] Class: ClassA (size: 16)
Member: a (offset: 0)
Primitive: int8_t
Member: b (offset: 4)
Primitive: int64_t
)",
R"(
[0] Class: MyClass (size: 0, align: 1)
Parent (offset: 0)
[1] Class: ClassA (size: 16, align: 8)
Member: a (offset: 0, align: 1)
Primitive: int8_t
Member: b (offset: 4, align: 8)
Primitive: int64_t
)");
}
TEST(AlignmentCalcTest, RecurseClassMember) {
test(AlignmentCalc::createPass(), R"(
[0] Class: MyClass (size: 0)
Member: xxx (offset: 0)
[1] Class: ClassA (size: 16)
Member: a (offset: 0)
Primitive: int8_t
Member: b (offset: 4)
Primitive: int64_t
)",
R"(
[0] Class: MyClass (size: 0, align: 8)
Member: xxx (offset: 0, align: 8)
[1] Class: ClassA (size: 16, align: 8)
Member: a (offset: 0, align: 1)
Primitive: int8_t
Member: b (offset: 4, align: 8)
Primitive: int64_t
)");
}
TEST(AlignmentCalcTest, RecurseClassChild) {
test(AlignmentCalc::createPass(), R"(
[0] Class: MyClass (size: 0)
Child
[1] Class: ClassA (size: 16)
Member: a (offset: 0)
Primitive: int8_t
Member: b (offset: 4)
Primitive: int64_t
)",
R"(
[0] Class: MyClass (size: 0, align: 1)
Child
[1] Class: ClassA (size: 16, align: 8)
Member: a (offset: 0, align: 1)
Primitive: int8_t
Member: b (offset: 4, align: 8)
Primitive: int64_t
)");
}