Skip to content

Commit 0b850bf

Browse files
authored
evaluator: Support fixed_size in variable position and add tests (#156)
1 parent 7729025 commit 0b850bf

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

lib/source/pl/core/ast/ast_node_attribute.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ namespace pl::core::ast {
335335
auto actualSize = pattern->getSize();
336336
if (actualSize < requestedSize) {
337337
pattern->setSize(requestedSize);
338-
evaluator->setReadOffset(u64(evaluator->getReadOffset() + (requestedSize - actualSize)));
338+
evaluator->setReadOffset(pattern->getOffset() + requestedSize);
339339
}
340340
else if (actualSize > requestedSize)
341341
err::E0004.throwError("Type size larger than expected", fmt::format("Pattern of type {} is larger than expected. Expected size {}, got {}", pattern->getTypeName(), requestedSize, actualSize), node->getLocation());
@@ -384,6 +384,8 @@ namespace pl::core::ast {
384384
.bitOffset = bitfieldPattern->getBitOffset()
385385
});
386386
}
387+
} else if (attributable->hasAttribute("fixed_size", true)) {
388+
// read offset might be modified by fixed_size's padding. keep it as is.
387389
} else {
388390
evaluator->setBitwiseReadOffset(endOffset);
389391
}

tests/include/test_patterns/test_pattern_attributes.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,31 @@ namespace pl::test {
4141
return 1337;
4242
};
4343
44+
struct FixedSizeTest1 {
45+
u8 x;
46+
} [[fixed_size(4)]];
47+
48+
struct FixedSizeTest2 {
49+
u32 pos = $;
50+
u32 elem [[fixed_size(7)]];
51+
std::assert($ == pos + 7, "Fixed size variable attribute padding not working");
52+
53+
pos = $;
54+
FixedSizeTest1 fixedSizeTest1;
55+
std::assert($ == pos + 4, "Fixed size pattern attribute padding not working");
56+
};
57+
4458
FormatTransformTest formatTransformTest @ 0x00;
4559
SealedTest sealedTest @ 0x10;
4660
HiddenTest hiddenTest @ 0x20;
4761
ColorTest colorTest @ 0x30;
4862
NoUniqueAddressTest noUniqueAddressTest @ 0x40;
63+
FixedSizeTest1 fixedSizeTest1 @ 0x50;
64+
FixedSizeTest2 fixedSizeTest2 @ 0x60;
4965
5066
std::assert(formatTransformTest == 1337, "Transform attribute not working");
5167
std::assert(sizeof(noUniqueAddressTest) == sizeof(u32), "No Unique Address attribute not working");
68+
std::assert(sizeof(fixedSizeTest1) == 4, "Fixed size attribute not working");
5269
)test";
5370
}
5471

0 commit comments

Comments
 (0)