Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/include/pl/patterns/pattern.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ namespace pl::ptrn {

[[nodiscard]] std::endian getEndian() const {
if (this->m_evaluator == nullptr) return std::endian::native;
else if (!this->m_endian.has_value() &&
(this->m_section == HeapSectionId || this->m_section == PatternLocalSectionId || this->m_section == InstantiationSectionId)) return std::endian::native;
else return this->m_endian.value_or(this->m_evaluator->getDefaultEndian());
}
virtual void setEndian(std::endian endian) {
Expand Down
5 changes: 4 additions & 1 deletion lib/source/pl/core/ast/ast_node_type_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace pl::core::ast {
: m_type(std::move(type)) { };

ASTNodeTypeApplication::ASTNodeTypeApplication(const ASTNodeTypeApplication &other)
: ASTNode(other), m_type(other.m_type) {
: ASTNode(other), m_type(other.m_type), m_reference(other.m_reference), m_endian(other.m_endian), m_templateParameterIndex(other.m_templateParameterIndex) {
for (const auto &arg : other.m_templateArguments)
this->m_templateArguments.emplace_back(arg->clone());
};
Expand Down Expand Up @@ -71,6 +71,9 @@ namespace pl::core::ast {
}

evaluatedType->m_templateArguments = std::move(evaluatedTemplateArguments);
evaluatedType->m_reference = this->m_reference;
evaluatedType->m_endian = this->m_endian;
evaluatedType->m_templateParameterIndex = this->m_templateParameterIndex;
evaluatedType->setLocation(this->getLocation());
evaluatedType->setShouldDocument(this->shouldDocument());
evaluatedType->setDocComment(this->getDocComment());
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ set(AVAILABLE_TESTS
Format
RValuesAssignmentInStruct
TemplateParametersScope
TemplateEndian
TypeNameOf
CustomBuiltInType
Using
Expand Down
99 changes: 99 additions & 0 deletions tests/include/test_patterns/test_pattern_template_endian.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#pragma once

#include "test_pattern.hpp"

namespace pl::test {

class TestPatternTemplateEndian : public TestPattern {
public:
TestPatternTemplateEndian(core::Evaluator *evaluator) : TestPattern(evaluator, "TemplateEndian") {
}
~TestPatternTemplateEndian() override = default;

[[nodiscard]] std::string getSourceCode() const override {
return R"(
struct EqBox<V> {
V v;
};
struct EqPayload {
u32 x;
};
struct EqBodyBe<V> {
be V v;
};
struct EqStructPayload {
be u32 x;
};

be u32 eqPlainBe @ 0;
std::assert(eqPlainBe == 0x89504E47, "sanity: plain 'be u32' must read big endian");

u32 eqPlainDefault @ 0;
std::assert(eqPlainDefault == 0x474E5089, "sanity: default 'u32' must read little endian");

EqBox<be u32> eqBoxBe @ 0;
std::assert(eqBoxBe.v == 0x89504E47, "'be' on a type template argument must be preserved");

EqBox<be EqPayload> eqBoxCustomBe @ 0;
std::assert(eqBoxCustomBe.v.x == 0x89504E47, "'be' on a custom type template argument must be preserved");

EqBox<EqBox<be u32>> eqBoxNestedBe @ 0;
std::assert(eqBoxNestedBe.v.v == 0x89504E47, "'be' in a nested template argument must be preserved");

using EqBoxAlias = EqBox<be u32>;
EqBoxAlias eqAliasBe @ 0;
std::assert(eqAliasBe.v == 0x89504E47, "'be' in an alias template argument must be preserved");

using EqBoxAliasTemplate<V> = EqBox<V>;
EqBoxAliasTemplate<be u32> eqAliasTemplateBe @ 0;
std::assert(eqAliasTemplateBe.v == 0x89504E47, "'be' passed through an alias template must be preserved");

using EqIdentity<V> = V;
EqIdentity<be u32> eqIdentityBe @ 0;
std::assert(eqIdentityBe == 0x89504E47, "'be' passed through an identity alias must be preserved");

EqBox<be u32> eqBoxBeArray[2] @ 0;
std::assert(eqBoxBeArray[0].v == 0x89504E47, "'be' in an array of template instantiations must be preserved");
std::assert(eqBoxBeArray[1].v == 0x0D0A1A0A, "'be' in an array of template instantiations must be preserved");

be EqBox<u32> eqOuterBe @ 0;
std::assert(eqOuterBe.v == 0x89504E47, "'be' on the outside of a template instantiation must still apply");

EqBodyBe<u32> eqBodyBe @ 0;
std::assert(eqBodyBe.v == 0x89504E47, "'be' applied to a template parameter inside the body must apply");

le EqBox<be u32> eqInnerBeWins @ 4;
std::assert(eqInnerBeWins.v == 0x0D0A1A0A, "endian of the template argument must win over the outer default");

EqBox<EqStructPayload> eqStructArg @ 0;
std::assert(eqStructArg.v.x == 0x89504E47, "a struct with 'be' members must stay big endian when used as a template argument");

struct AutoBox<auto t> {
u32 f;
u32 a = t;
};

be AutoBox<5> autoBe @ 0;
std::assert(autoBe.f == 0x89504E47, "'be' instantiation must still read file data big endian");
std::assert(autoBe.t == 5, "non-type template parameter must not be endian-swapped by an outer 'be'");
std::assert(autoBe.a == 5, "member initialized from a non-type template parameter must keep its value");

le AutoBox<5> autoLe @ 4;
std::assert(autoLe.f == 0x0A1A0A0D, "'le' instantiation must read file data little endian");
std::assert(autoLe.t == 5, "non-type template parameter must not be endian-swapped by an outer 'le'");

be AutoBox<5> autoBeLocal;
std::assert(autoBeLocal.t == 5, "non-type template parameter must not be endian-swapped by a local 'be' instantiation");
std::assert(autoBeLocal.a == 5, "member initialized from a non-type template parameter must keep its value when local");

auto endianGlobal = 16909060;
struct AutoGlobal<auto t> {
u32 a = endianGlobal;
};
be AutoGlobal<5> autoGlobalBe;
std::assert(autoGlobalBe.a == 16909060, "a heap variable read under an outer 'be' must keep its value");
)";
}
};

}
2 changes: 2 additions & 0 deletions tests/source/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "test_patterns/test_pattern_format.hpp"
#include "test_patterns/test_pattern_rvalues_assignment_in_struct.hpp"
#include "test_patterns/test_pattern_template_parameters_scope.hpp"
#include "test_patterns/test_pattern_template_endian.hpp"
#include "test_patterns/test_pattern_typenameof.hpp"
#include "test_patterns/test_pattern_custom_builtin_type.hpp"
#include "test_patterns/test_pattern_using.hpp"
Expand Down Expand Up @@ -85,6 +86,7 @@ std::array Tests = {
TEST(Format),
TEST(RValuesAssignmentInStruct),
TEST(TemplateParametersScope),
TEST(TemplateEndian),
TEST(TypeNameOf),
TEST(CustomBuiltinType),
TEST(Using),
Expand Down
Loading