1
0
mirror of synced 2025-02-06 22:24:24 +01:00

patterns: Added [[inline]] attribute

This commit is contained in:
WerWolv 2021-09-27 13:31:10 +02:00
parent 5db608c3fc
commit 888976873a
3 changed files with 131 additions and 140 deletions

View File

@ -1,47 +0,0 @@
namespace std::math {
fn min_u(u128 a, u128 b) {
return (a < b) ? a : b;
};
fn min_i(s128 a, s128 b) {
return (a < b) ? a : b;
};
fn min_f(double a, double b) {
return (a < b) ? a : b;
};
fn max_u(u128 a, u128 b) {
return (a > b) ? a : b;
};
fn max_i(s128 a, s128 b) {
return (a > b) ? a : b;
};
fn max_f(double a, double b) {
return (a > b) ? a : b;
};
fn abs_i(s128 value) {
return value < 0 ? -value : value;
};
fn abs_d(double value) {
return value < 0 ? -value : value;
};
fn ceil(double value) {
s128 cast;
cast = value;
return cast + 1;
};
}
std::print("{}", std::math::ceil(123.6));

View File

@ -572,6 +572,14 @@ namespace hex::pl {
pattern->setComment(*value); pattern->setComment(*value);
} else if (name == "hidden" && noValue()) { } else if (name == "hidden" && noValue()) {
pattern->setHidden(true); pattern->setHidden(true);
} else if (name == "inline" && noValue()) {
auto inlinable = dynamic_cast<Inlinable*>(pattern);
if (inlinable == nullptr)
LogConsole::abortEvaluation("inline attribute can only be applied to nested types", node);
else
inlinable->setInlined(true);
} else if (name == "format" && requiresValue()) { } else if (name == "format" && requiresValue()) {
auto functions = evaluator->getCustomFunctions(); auto functions = evaluator->getCustomFunctions();
if (!functions.contains(*value)) if (!functions.contains(*value))

View File

@ -43,6 +43,14 @@ namespace hex::pl {
} }
class Inlinable {
public:
[[nodiscard]] bool isInlined() const { return this->m_inlined; }
void setInlined(bool inlined) { this->m_inlined = inlined; }
private:
bool m_inlined = false;
};
class PatternData { class PatternData {
public: public:
PatternData(u64 offset, size_t size, u32 color = 0) PatternData(u64 offset, size_t size, u32 color = 0)
@ -619,7 +627,7 @@ namespace hex::pl {
[[nodiscard]] bool operator==(const PatternData &other) const override { return areCommonPropertiesEqual<decltype(*this)>(other); } [[nodiscard]] bool operator==(const PatternData &other) const override { return areCommonPropertiesEqual<decltype(*this)>(other); }
}; };
class PatternDataDynamicArray : public PatternData { class PatternDataDynamicArray : public PatternData, public Inlinable {
public: public:
PatternDataDynamicArray(u64 offset, size_t size, u32 color = 0) PatternDataDynamicArray(u64 offset, size_t size, u32 color = 0)
: PatternData(offset, size, color) { : PatternData(offset, size, color) {
@ -654,34 +662,38 @@ namespace hex::pl {
if (this->m_entries.empty()) if (this->m_entries.empty())
return; return;
ImGui::TableNextRow(); bool open = true;
ImGui::TableNextColumn(); if (this->isInlined()) {
bool open = ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_AllowItemOverlap); ImGui::TableNextRow();
this->drawCommentTooltip(); ImGui::TableNextColumn();
ImGui::TableNextColumn(); open = ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth);
ImGui::ColorButton("color", ImColor(this->getColor()), ImGuiColorEditFlags_NoTooltip, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight())); this->drawCommentTooltip();
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::Text("0x%08llX : 0x%08llX", this->getOffset(), this->getOffset() + this->getSize() - 1); ImGui::ColorButton("color", ImColor(this->getColor()), ImGuiColorEditFlags_NoTooltip, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight()));
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::Text("0x%04llX", this->getSize()); ImGui::Text("0x%08llX : 0x%08llX", this->getOffset(), this->getOffset() + this->getSize() - 1);
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::TextColored(ImColor(0xFF9BC64D), "%s", this->m_entries[0]->getTypeName().c_str()); ImGui::Text("0x%04llX", this->getSize());
ImGui::SameLine(0, 0); ImGui::TableNextColumn();
ImGui::TextColored(ImColor(0xFF9BC64D), "%s", this->m_entries[0]->getTypeName().c_str());
ImGui::SameLine(0, 0);
ImGui::TextUnformatted("["); ImGui::TextUnformatted("[");
ImGui::SameLine(0, 0); ImGui::SameLine(0, 0);
ImGui::TextColored(ImColor(0xFF00FF00), "%llu", this->m_entries.size()); ImGui::TextColored(ImColor(0xFF00FF00), "%llu", this->m_entries.size());
ImGui::SameLine(0, 0); ImGui::SameLine(0, 0);
ImGui::TextUnformatted("]"); ImGui::TextUnformatted("]");
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::Text("%s", "{ ... }"); ImGui::Text("%s", "{ ... }");
}
if (open) { if (open) {
for (auto &member : this->m_entries) for (auto &member : this->m_entries)
member->draw(provider); member->draw(provider);
ImGui::TreePop(); if (!this->isInlined())
ImGui::TreePop();
} }
} }
@ -747,7 +759,7 @@ namespace hex::pl {
std::vector<PatternData*> m_entries; std::vector<PatternData*> m_entries;
}; };
class PatternDataStaticArray : public PatternData { class PatternDataStaticArray : public PatternData, public Inlinable {
public: public:
PatternDataStaticArray(u64 offset, size_t size, u32 color = 0) PatternDataStaticArray(u64 offset, size_t size, u32 color = 0)
: PatternData(offset, size, color) { : PatternData(offset, size, color) {
@ -769,28 +781,32 @@ namespace hex::pl {
if (this->getEntryCount() == 0) if (this->getEntryCount() == 0)
return; return;
ImGui::TableNextRow(); bool open = true;
ImGui::TableNextColumn();
bool open = ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_AllowItemOverlap);
this->drawCommentTooltip();
ImGui::TableNextColumn();
ImGui::ColorButton("color", ImColor(this->getColor()), ImGuiColorEditFlags_NoTooltip, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight()));
ImGui::TableNextColumn();
ImGui::Text("0x%08llX : 0x%08llX", this->getOffset(), this->getOffset() + this->getSize() - 1);
ImGui::TableNextColumn();
ImGui::Text("0x%04llX", this->getSize());
ImGui::TableNextColumn();
ImGui::TextColored(ImColor(0xFF9BC64D), "%s", this->m_template->getTypeName().c_str());
ImGui::SameLine(0, 0);
ImGui::TextUnformatted("["); if (this->isInlined()) {
ImGui::SameLine(0, 0); ImGui::TableNextRow();
ImGui::TextColored(ImColor(0xFF00FF00), "%llu", this->m_entryCount); ImGui::TableNextColumn();
ImGui::SameLine(0, 0); open = ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth);
ImGui::TextUnformatted("]"); this->drawCommentTooltip();
ImGui::TableNextColumn();
ImGui::ColorButton("color", ImColor(this->getColor()), ImGuiColorEditFlags_NoTooltip, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight()));
ImGui::TableNextColumn();
ImGui::Text("0x%08llX : 0x%08llX", this->getOffset(), this->getOffset() + this->getSize() - 1);
ImGui::TableNextColumn();
ImGui::Text("0x%04llX", this->getSize());
ImGui::TableNextColumn();
ImGui::TextColored(ImColor(0xFF9BC64D), "%s", this->m_template->getTypeName().c_str());
ImGui::SameLine(0, 0);
ImGui::TableNextColumn(); ImGui::TextUnformatted("[");
ImGui::Text("%s", "{ ... }"); ImGui::SameLine(0, 0);
ImGui::TextColored(ImColor(0xFF00FF00), "%llu", this->m_entryCount);
ImGui::SameLine(0, 0);
ImGui::TextUnformatted("]");
ImGui::TableNextColumn();
ImGui::Text("%s", "{ ... }");
}
if (open) { if (open) {
auto entry = this->m_template->clone(); auto entry = this->m_template->clone();
@ -801,7 +817,8 @@ namespace hex::pl {
} }
delete entry; delete entry;
ImGui::TreePop(); if (!this->isInlined())
ImGui::TreePop();
} }
} }
@ -879,7 +896,7 @@ namespace hex::pl {
size_t m_entryCount; size_t m_entryCount;
}; };
class PatternDataStruct : public PatternData { class PatternDataStruct : public PatternData, public Inlinable {
public: public:
PatternDataStruct(u64 offset, size_t size, u32 color = 0) : PatternData(offset, size, color){ PatternDataStruct(u64 offset, size_t size, u32 color = 0) : PatternData(offset, size, color){
} }
@ -908,25 +925,30 @@ namespace hex::pl {
} }
void createEntry(prv::Provider* &provider) override { void createEntry(prv::Provider* &provider) override {
ImGui::TableNextRow(); bool open = true;
ImGui::TableNextColumn();
bool open = ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth); if (!this->isInlined()) {
this->drawCommentTooltip(); ImGui::TableNextRow();
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::TableNextColumn(); open = ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth);
ImGui::Text("0x%08llX : 0x%08llX", this->getOffset(), this->getOffset() + this->getSize() - (this->getSize() == 0 ? 0 : 1)); this->drawCommentTooltip();
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::Text("0x%04llX", this->getSize()); ImGui::TableNextColumn();
ImGui::TableNextColumn(); ImGui::Text("0x%08llX : 0x%08llX", this->getOffset(), this->getOffset() + this->getSize() - (this->getSize() == 0 ? 0 : 1));
ImGui::TextColored(ImColor(0xFFD69C56), "struct"); ImGui::SameLine(); ImGui::Text("%s", this->getTypeName().c_str()); ImGui::TableNextColumn();
ImGui::TableNextColumn(); ImGui::Text("0x%04llX", this->getSize());
ImGui::Text("%s", "{ ... }"); ImGui::TableNextColumn();
ImGui::TextColored(ImColor(0xFFD69C56), "struct"); ImGui::SameLine(); ImGui::Text("%s", this->getTypeName().c_str());
ImGui::TableNextColumn();
ImGui::Text("%s", "{ ... }");
}
if (open) { if (open) {
for (auto &member : this->m_sortedMembers) for (auto &member : this->m_sortedMembers)
member->draw(provider); member->draw(provider);
ImGui::TreePop(); if (!this->isInlined())
ImGui::TreePop();
} }
} }
@ -1009,7 +1031,7 @@ namespace hex::pl {
std::vector<PatternData*> m_sortedMembers; std::vector<PatternData*> m_sortedMembers;
}; };
class PatternDataUnion : public PatternData { class PatternDataUnion : public PatternData, public Inlinable {
public: public:
PatternDataUnion(u64 offset, size_t size, u32 color = 0) : PatternData(offset, size, color) { PatternDataUnion(u64 offset, size_t size, u32 color = 0) : PatternData(offset, size, color) {
@ -1039,25 +1061,30 @@ namespace hex::pl {
} }
void createEntry(prv::Provider* &provider) override { void createEntry(prv::Provider* &provider) override {
ImGui::TableNextRow(); bool open = true;
ImGui::TableNextColumn();
bool open = ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_AllowItemOverlap);
this->drawCommentTooltip();
ImGui::TableNextColumn();
ImGui::TableNextColumn();
ImGui::Text("0x%08llX : 0x%08llX", this->getOffset(), std::max(this->getOffset() + this->getSize() - (this->getSize() == 0 ? 0 : 1), u64(0)));
ImGui::TableNextColumn();
ImGui::Text("0x%04llX", this->getSize());
ImGui::TableNextColumn();
ImGui::TextColored(ImColor(0xFFD69C56), "union"); ImGui::SameLine(); ImGui::Text("%s", PatternData::getTypeName().c_str());
ImGui::TableNextColumn(); if (this->isInlined()) {
ImGui::Text("%s", "{ ... }"); ImGui::TableNextRow();
ImGui::TableNextColumn();
open = ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth);
this->drawCommentTooltip();
ImGui::TableNextColumn();
ImGui::TableNextColumn();
ImGui::Text("0x%08llX : 0x%08llX", this->getOffset(), std::max(this->getOffset() + this->getSize() - (this->getSize() == 0 ? 0 : 1), u64(0)));
ImGui::TableNextColumn();
ImGui::Text("0x%04llX", this->getSize());
ImGui::TableNextColumn();
ImGui::TextColored(ImColor(0xFFD69C56), "union"); ImGui::SameLine(); ImGui::Text("%s", PatternData::getTypeName().c_str());
ImGui::TableNextColumn();
ImGui::Text("%s", "{ ... }");
}
if (open) { if (open) {
for (auto &member : this->m_sortedMembers) for (auto &member : this->m_sortedMembers)
member->draw(provider); member->draw(provider);
if (!this->isInlined())
ImGui::TreePop(); ImGui::TreePop();
} }
@ -1252,7 +1279,6 @@ namespace hex::pl {
std::reverse(value.begin(), value.end()); std::reverse(value.begin(), value.end());
ImGui::TableNextRow(); ImGui::TableNextRow();
ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_AllowItemOverlap);
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::Text("%s", this->getDisplayName().c_str()); ImGui::Text("%s", this->getDisplayName().c_str());
ImGui::TableNextColumn(); ImGui::TableNextColumn();
@ -1303,7 +1329,7 @@ namespace hex::pl {
u8 m_bitOffset, m_bitSize; u8 m_bitOffset, m_bitSize;
}; };
class PatternDataBitfield : public PatternData { class PatternDataBitfield : public PatternData, public Inlinable {
public: public:
PatternDataBitfield(u64 offset, size_t size, u32 color = 0) : PatternData(offset, size, color) { PatternDataBitfield(u64 offset, size_t size, u32 color = 0) : PatternData(offset, size, color) {
@ -1330,32 +1356,36 @@ namespace hex::pl {
if (this->m_endian == std::endian::little) if (this->m_endian == std::endian::little)
std::reverse(value.begin(), value.end()); std::reverse(value.begin(), value.end());
ImGui::TableNextRow(); bool open = true;
ImGui::TableNextColumn(); if (this->isInlined()) {
bool open = ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_AllowItemOverlap); ImGui::TableNextRow();
this->drawCommentTooltip(); ImGui::TableNextColumn();
ImGui::TableNextColumn(); open = ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth);
ImGui::TableNextColumn(); this->drawCommentTooltip();
ImGui::Text("0x%08llX : 0x%08llX", this->getOffset(), this->getOffset() + this->getSize() - 1); ImGui::TableNextColumn();
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::Text("0x%04llX", this->getSize()); ImGui::Text("0x%08llX : 0x%08llX", this->getOffset(), this->getOffset() + this->getSize() - 1);
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::TextColored(ImColor(0xFFD69C56), "bitfield"); ImGui::SameLine(); ImGui::Text("%s", PatternData::getTypeName().c_str()); ImGui::Text("0x%04llX", this->getSize());
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::TextColored(ImColor(0xFFD69C56), "bitfield"); ImGui::SameLine(); ImGui::Text("%s", PatternData::getTypeName().c_str());
ImGui::TableNextColumn();
std::string valueString = "{ "; std::string valueString = "{ ";
for (auto i : value) for (auto i : value)
valueString += hex::format("{0:02X} ", i); valueString += hex::format("{0:02X} ", i);
valueString += "}"; valueString += "}";
ImGui::TextUnformatted(valueString.c_str()); ImGui::TextUnformatted(valueString.c_str());
}
if (open) { if (open) {
for (auto &field : this->m_fields) for (auto &field : this->m_fields)
field->draw(provider); field->draw(provider);
ImGui::TreePop(); if (!this->isInlined())
ImGui::TreePop();
} }
} }