Skip to content
Merged
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: 1 addition & 1 deletion lib/include/pl/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ namespace pl::api {
/**
* @brief A function callback called when a custom built-in type is being instantiated
*/
using TypeCallback = std::function<std::unique_ptr<ptrn::Pattern>(core::Evaluator *, const std::vector<core::Token::Literal> &)>;
using TypeCallback = std::function<std::shared_ptr<ptrn::Pattern>(core::Evaluator *, const std::vector<core::Token::Literal> &)>;

/**
* @brief A type representing a function.
Expand Down
16 changes: 11 additions & 5 deletions lib/include/pl/patterns/pattern.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ namespace pl::ptrn {
}

virtual std::shared_ptr<Pattern> clone() const = 0;
std::shared_ptr<Pattern> reference() { return shared_from_this(); }
std::shared_ptr<Pattern> reference() {
auto weakPtr = weak_from_this();
if (weakPtr.expired()) {
core::err::E0001.throwError("Cannot call shared_from_this if this is not shared.");
}
return shared_from_this();
}

[[nodiscard]] u64 getOffset() const { return this->m_offset; }
[[nodiscard]] virtual u128 getOffsetForSorting() const { return this->getOffset() << 3; }
Expand Down Expand Up @@ -538,14 +544,14 @@ namespace pl::ptrn {
}

[[nodiscard]] const Pattern* getParent() const {
return m_parent;
return m_parent.lock().get();
}

[[nodiscard]] Pattern* getParent() {
return m_parent;
return m_parent.lock().get();
}

void setParent(Pattern *parent) {
void setParent(std::shared_ptr<Pattern> parent) {
m_parent = parent;
}

Expand Down Expand Up @@ -623,7 +629,7 @@ namespace pl::ptrn {
core::Evaluator *m_evaluator;

std::unique_ptr<std::map<std::string, std::vector<core::Token::Literal>>> m_attributes;
Pattern *m_parent = nullptr;
std::weak_ptr<Pattern> m_parent;
u32 m_line = 0;

std::set<std::string>::const_iterator m_variableName;
Expand Down
7 changes: 5 additions & 2 deletions lib/include/pl/patterns/pattern_array_dynamic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ namespace pl::ptrn {
}

[[nodiscard]] std::shared_ptr<Pattern> clone() const override {
return std::unique_ptr<Pattern>(new PatternArrayDynamic(*this));
auto other = std::make_shared<PatternArrayDynamic>(*this);
for (const auto &entry : other->m_entries)
entry->setParent(other->reference());

return other;
}

void setColor(u32 color) override {
Expand Down Expand Up @@ -133,7 +137,6 @@ namespace pl::ptrn {

if (!entry->hasOverriddenColor())
entry->setBaseColor(this->getColor());
entry->setParent(this);

this->m_entries.emplace_back(entry);
}
Expand Down
5 changes: 3 additions & 2 deletions lib/include/pl/patterns/pattern_array_static.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ namespace pl::ptrn {
}

[[nodiscard]] std::shared_ptr<Pattern> clone() const override {
return std::unique_ptr<Pattern>(new PatternArrayStatic(*this));
auto other = std::make_shared<PatternArrayStatic>(*this);
other->m_template->setParent(other->reference());
return other;
}

[[nodiscard]] std::shared_ptr<Pattern> getEntry(size_t index) const override {
Expand Down Expand Up @@ -153,7 +155,6 @@ namespace pl::ptrn {

void setEntries(std::shared_ptr<Pattern> &&templatePattern, size_t count) {
this->m_template = std::move(templatePattern);
this->m_template->setParent(this);
this->m_highlightTemplates.push_back(this->m_template->clone());
this->m_entryCount = count;

Expand Down
12 changes: 7 additions & 5 deletions lib/include/pl/patterns/pattern_bitfield.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ namespace pl::ptrn {
public:
PatternBitfieldField(core::Evaluator *evaluator, u64 offset, u8 bitOffset, u8 bitSize, u32 line, PatternBitfieldMember *parentBitfield = nullptr)
: PatternBitfieldMember(evaluator, offset, (bitOffset + bitSize + 7) / 8, line), m_bitOffset(bitOffset % 8), m_bitSize(bitSize) {
this->setParent(parentBitfield);
if (parentBitfield != nullptr)
this->setParent(parentBitfield->reference());
}

PatternBitfieldField(const PatternBitfieldField &other) : PatternBitfieldMember(other) {
Expand Down Expand Up @@ -282,7 +283,10 @@ namespace pl::ptrn {
}

[[nodiscard]] std::shared_ptr<Pattern> clone() const override {
return std::unique_ptr<Pattern>(new PatternBitfieldArray(*this));
auto other = std::make_shared<PatternBitfieldArray>(*this);
for (const auto &entry : other->m_entries)
entry->setParent(other->reference());
return other;
}

[[nodiscard]] u8 getBitOffset() const override {
Expand Down Expand Up @@ -422,8 +426,6 @@ namespace pl::ptrn {
if (!entry->hasOverriddenColor())
entry->setBaseColor(this->getColor());

entry->setParent(this);

this->m_sortedEntries.push_back(entry.get());
}

Expand Down Expand Up @@ -643,7 +645,7 @@ namespace pl::ptrn {
this->setBaseColor(this->m_fields.front()->getColor());

for (const auto &field : this->m_fields) {
field->setParent(this);
field->setParent(this->reference());
this->m_sortedFields.push_back(field.get());
}
}
Expand Down
9 changes: 6 additions & 3 deletions lib/include/pl/patterns/pattern_struct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ namespace pl::ptrn {
for (const auto &member : other.m_members) {
auto copy = member->clone();

copy->setParent(this);
this->m_sortedMembers.push_back(copy.get());
this->m_members.push_back(std::move(copy));
}
}

[[nodiscard]] std::shared_ptr<Pattern> clone() const override {
return std::unique_ptr<Pattern>(new PatternStruct(*this));
auto other = std::make_shared<PatternStruct>(*this);
for (const auto &member : other->m_members)
member->setParent(other->reference());

return other;
}

[[nodiscard]] std::shared_ptr<Pattern> getEntry(size_t index) const override {
Expand All @@ -36,7 +39,7 @@ namespace pl::ptrn {
void addEntry(const std::shared_ptr<Pattern> &entry) override {
if (entry == nullptr) return;

entry->setParent(this);
entry->setParent(this->reference());
this->m_sortedMembers.push_back(entry.get());
this->m_members.push_back(entry);
}
Expand Down
7 changes: 5 additions & 2 deletions lib/include/pl/patterns/pattern_union.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ namespace pl::ptrn {
}

[[nodiscard]] std::shared_ptr<Pattern> clone() const override {
return std::unique_ptr<Pattern>(new PatternUnion(*this));
auto other = std::make_shared<PatternUnion>(*this);
for (const auto &member : other->m_members)
member->setParent(other->reference());

return other;
}

[[nodiscard]] std::shared_ptr<Pattern> getEntry(size_t index) const override {
Expand All @@ -35,7 +39,6 @@ namespace pl::ptrn {
void addEntry(const std::shared_ptr<Pattern> &entry) override {
if (entry == nullptr) return;

entry->setParent(this);
this->m_sortedMembers.push_back(entry.get());
this->m_members.push_back(entry);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/source/pl/core/ast/ast_node_bitfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ namespace pl::core::ast {

for (auto &pattern : potentialPatterns) {
if (auto bitfieldMember = dynamic_cast<ptrn::PatternBitfieldMember*>(pattern.get()); bitfieldMember != nullptr) {
bitfieldMember->setParent(bitfieldPattern.get());
bitfieldMember->setParent(bitfieldPattern->reference());
if (!bitfieldMember->isPadding())
fields.push_back(pattern);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace pl::core::ast {
};

auto position = evaluator->getBitwiseReadOffset();
auto arrayPattern = std::make_unique<ptrn::PatternBitfieldArray>(evaluator, position.byteOffset, position.bitOffset, 0, getLocation().line);
auto arrayPattern = std::make_shared<ptrn::PatternBitfieldArray>(evaluator, position.byteOffset, position.bitOffset, 0, getLocation().line);
arrayPattern->setVariableName(this->m_name);
arrayPattern->setSection(evaluator->getSectionId());
arrayPattern->setReversed(evaluator->isReadOrderReversed());
Expand Down Expand Up @@ -133,7 +133,7 @@ namespace pl::core::ast {

for (auto &pattern : entries) {
if (auto bitfieldMember = dynamic_cast<ptrn::PatternBitfieldMember*>(pattern.get()); bitfieldMember != nullptr)
bitfieldMember->setParent(arrayPattern.get());
bitfieldMember->setParent(arrayPattern->reference());
}

arrayPattern->setEntries(entries);
Expand Down
18 changes: 9 additions & 9 deletions lib/source/pl/core/ast/ast_node_builtin_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ namespace pl::core::ast {
auto size = Token::getTypeSize(this->m_type);
auto offset = evaluator->getReadOffsetAndIncrement(size);

std::unique_ptr<ptrn::Pattern> pattern;
std::shared_ptr<ptrn::Pattern> pattern;
if (Token::isUnsigned(this->m_type))
pattern = std::make_unique<ptrn::PatternUnsigned>(evaluator, offset, size, getLocation().line);
pattern = std::make_shared<ptrn::PatternUnsigned>(evaluator, offset, size, getLocation().line);
else if (Token::isSigned(this->m_type))
pattern = std::make_unique<ptrn::PatternSigned>(evaluator, offset, size, getLocation().line);
pattern = std::make_shared<ptrn::PatternSigned>(evaluator, offset, size, getLocation().line);
else if (Token::isFloatingPoint(this->m_type))
pattern = std::make_unique<ptrn::PatternFloat>(evaluator, offset, size, getLocation().line);
pattern = std::make_shared<ptrn::PatternFloat>(evaluator, offset, size, getLocation().line);
else if (this->m_type == Token::ValueType::Boolean)
pattern = std::make_unique<ptrn::PatternBoolean>(evaluator, offset, getLocation().line);
pattern = std::make_shared<ptrn::PatternBoolean>(evaluator, offset, getLocation().line);
else if (this->m_type == Token::ValueType::Character)
pattern = std::make_unique<ptrn::PatternCharacter>(evaluator, offset, getLocation().line);
pattern = std::make_shared<ptrn::PatternCharacter>(evaluator, offset, getLocation().line);
else if (this->m_type == Token::ValueType::Character16)
pattern = std::make_unique<ptrn::PatternWideCharacter>(evaluator, offset, getLocation().line);
pattern = std::make_shared<ptrn::PatternWideCharacter>(evaluator, offset, getLocation().line);
else if (this->m_type == Token::ValueType::Padding)
pattern = std::make_unique<ptrn::PatternPadding>(evaluator, offset, 1, getLocation().line);
pattern = std::make_shared<ptrn::PatternPadding>(evaluator, offset, 1, getLocation().line);
else if (this->m_type == Token::ValueType::String)
pattern = std::make_unique<ptrn::PatternString>(evaluator, offset, 0, getLocation().line);
pattern = std::make_shared<ptrn::PatternString>(evaluator, offset, 0, getLocation().line);
else if (this->m_type == Token::ValueType::CustomType) {
std::vector<Token::Literal> params;

Expand Down
12 changes: 6 additions & 6 deletions tests/include/test_patterns/test_pattern.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ namespace pl::test {
virtual ~TestPattern() = default;

template<typename T>
std::unique_ptr<T> create(const std::string &typeName, const std::string &varName, auto... args) {
auto pattern = std::make_unique<T>(m_evaluator, args...);
std::shared_ptr<T> create(const std::string &typeName, const std::string &varName, auto... args) {
auto pattern = std::make_shared<T>(m_evaluator, args...);
pattern->setTypeName(typeName);
pattern->setVariableName(varName);

return std::move(pattern);
return pattern;
}

[[nodiscard]] virtual std::string getSourceCode() const = 0;

[[nodiscard]] virtual const std::vector<std::unique_ptr<ptrn::Pattern>> &getPatterns() const final { return this->m_patterns; }
virtual void addPattern(std::unique_ptr<ptrn::Pattern> &&pattern) final {
[[nodiscard]] virtual const std::vector<std::shared_ptr<ptrn::Pattern>> &getPatterns() const final { return this->m_patterns; }
virtual void addPattern(std::shared_ptr<ptrn::Pattern> &&pattern) final {
this->m_patterns.push_back(std::move(pattern));
}

Expand All @@ -60,7 +60,7 @@ namespace pl::test {
}

private:
std::vector<std::unique_ptr<ptrn::Pattern>> m_patterns;
std::vector<std::shared_ptr<ptrn::Pattern>> m_patterns;
core::Evaluator *m_evaluator;
Mode m_mode;

Expand Down
8 changes: 4 additions & 4 deletions tests/include/test_patterns/test_pattern_bitfields.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace pl::test {
nestedFields.push_back(create<PatternBitfieldField>("", "nestedA", 0x25, 5, 4, 0, testBitfield.get()));
nestedFields.push_back(create<PatternBitfieldField>("", "nestedB", 0x26, 1, 4, 0, testBitfield.get()));
}
nestedBitfield->setParent(testBitfield.get());
nestedBitfield->setParent(testBitfield->reference());
nestedBitfield->setFields(std::move(nestedFields));
nestedBitfield->setEndian(std::endian::big);
bitfieldFields.push_back(std::move(nestedBitfield));
Expand All @@ -39,7 +39,7 @@ namespace pl::test {
array0Fields.push_back(create<PatternBitfieldField>("", "nestedA", 0x27, 5, 4, 0, array0Bitfield.get()));
array0Fields.push_back(create<PatternBitfieldField>("", "nestedB", 0x28, 1, 4, 0, array0Bitfield.get()));
}
array0Bitfield->setParent(array.get());
array0Bitfield->setParent(array->reference());
array0Bitfield->setFields(std::move(array0Fields));
array0Bitfield->setEndian(std::endian::big);
arrayEntries.push_back(std::move(array0Bitfield));
Expand All @@ -50,12 +50,12 @@ namespace pl::test {
array1Fields.push_back(create<PatternBitfieldField>("", "nestedA", 0x28, 5, 4, 0, array1Bitfield.get()));
array1Fields.push_back(create<PatternBitfieldField>("", "nestedB", 0x29, 1, 4, 0, array1Bitfield.get()));
}
array1Bitfield->setParent(array.get());
array1Bitfield->setParent(array->reference());
array1Bitfield->setFields(std::move(array1Fields));
array1Bitfield->setEndian(std::endian::big);
arrayEntries.push_back(std::move(array1Bitfield));
}
array->setParent(testBitfield.get());
array->setParent(testBitfield->reference());
array->setEntries(arrayEntries);
array->setEndian(std::endian::big);
bitfieldFields.push_back(std::move(array));
Expand Down
Loading