1
0
mirror of synced 2024-11-12 02:00:52 +01:00

patterns: Fix unions causing crashes on cleanup

This commit is contained in:
WerWolv 2021-09-03 10:30:40 +02:00
parent 4b40546750
commit f29febdc86
3 changed files with 10 additions and 21 deletions

View File

@ -858,6 +858,7 @@ namespace hex::lang {
}
void setMembers(const std::vector<PatternData*> & members) {
this->m_members.clear();
for (auto &member : members) {
if (member == nullptr) continue;

View File

@ -771,8 +771,8 @@ namespace hex::lang {
auto startOffset = this->m_currOffset;
for (auto &member : node->getMembers()) {
this->evaluateMember(member, memberPatterns, true);
structPattern->setMembers(memberPatterns);
}
structPattern->setMembers(memberPatterns);
structPattern->setSize(this->m_currOffset - startOffset);
this->m_currRecursionDepth--;
@ -801,8 +801,8 @@ namespace hex::lang {
for (auto &member : node->getMembers()) {
this->evaluateMember(member, memberPatterns, false);
unionPattern->setMembers(memberPatterns);
}
unionPattern->setMembers(memberPatterns);
this->m_currRecursionDepth--;

View File

@ -667,19 +667,15 @@ namespace hex::lang {
variables.push_back(new ASTNodeVariableDecl(getValue<std::string>(-1), type->clone()));
} while (MATCHES(sequence(SEPARATOR_COMMA, IDENTIFIER)));
delete type;
variableCleanup.release();
return new ASTNodeMultiVariableDecl(variables);
} else
return new ASTNodeVariableDecl(getValue<std::string>(-1), type);
return new ASTNodeVariableDecl(getValue<std::string>(-1), type->clone());
}
// (parseType) Identifier[(parseMathematicalExpression)]
ASTNode* Parser::parseMemberArrayVariable(ASTNodeTypeDecl *type) {
if (type == nullptr) throwParseError("invalid type used in variable declaration", -1);
auto name = getValue<std::string>(-2);
ASTNode *size = nullptr;
@ -697,7 +693,7 @@ namespace hex::lang {
sizeCleanup.release();
return new ASTNodeArrayVariableDecl(name, type, size);
return new ASTNodeArrayVariableDecl(name, type->clone(), size);
}
// (parseType) *Identifier : (parseType)
@ -713,7 +709,7 @@ namespace hex::lang {
throwParseError("invalid type used for pointer size", -1);
}
return new ASTNodePointerVariableDecl(name, type, sizeType);
return new ASTNodePointerVariableDecl(name, type->clone(), sizeType);
}
// [(parsePadding)|(parseMemberVariable)|(parseMemberArrayVariable)|(parseMemberPointerVariable)]
@ -725,6 +721,7 @@ namespace hex::lang {
// Some kind of variable definition
auto type = parseType();
ON_SCOPE_EXIT { delete type; };
if (MATCHES(sequence(IDENTIFIER, SEPARATOR_SQUAREBRACKETOPEN)) && sequence<Not>(SEPARATOR_SQUAREBRACKETOPEN))
member = parseMemberArrayVariable(type);
@ -759,9 +756,6 @@ namespace hex::lang {
const auto &typeName = getValue<std::string>(-2);
auto structGuard = SCOPE_GUARD { delete structNode; };
if (this->m_types.contains(typeName))
throwParseError(hex::format("redefinition of type '{}'", typeName));
while (!MATCHES(sequence(SEPARATOR_CURLYBRACKETCLOSE))) {
structNode->addMember(parseMember());
}
@ -777,9 +771,6 @@ namespace hex::lang {
const auto &typeName = getValue<std::string>(-2);
auto unionGuard = SCOPE_GUARD { delete unionNode; };
if (this->m_types.contains(typeName))
throwParseError(hex::format("redefinition of type '{}'", typeName));
while (!MATCHES(sequence(SEPARATOR_CURLYBRACKETCLOSE))) {
unionNode->addMember(parseMember());
}
@ -799,9 +790,6 @@ namespace hex::lang {
const auto enumNode = new ASTNodeEnum(underlyingType);
auto enumGuard = SCOPE_GUARD { delete enumNode; };
if (this->m_types.contains(typeName))
throwParseError(hex::format("redefinition of type '{}'", typeName));
if (!MATCHES(sequence(SEPARATOR_CURLYBRACKETOPEN)))
throwParseError("expected '{' after enum definition", -1);
@ -849,9 +837,6 @@ namespace hex::lang {
const auto bitfieldNode = new ASTNodeBitfield();
auto enumGuard = SCOPE_GUARD { delete bitfieldNode; };
if (this->m_types.contains(typeName))
throwParseError(hex::format("redefinition of type '{}'", typeName));
while (!MATCHES(sequence(SEPARATOR_CURLYBRACKETCLOSE))) {
if (MATCHES(sequence(IDENTIFIER, OPERATOR_INHERIT))) {
auto name = getValue<std::string>(-2);
@ -1024,6 +1009,9 @@ namespace hex::lang {
if (auto typeDecl = dynamic_cast<ASTNodeTypeDecl*>(statement); typeDecl != nullptr) {
auto typeName = getNamespacePrefixedName(typeDecl->getName().data());
if (this->m_types.contains(typeName))
throwParseError(hex::format("redefinition of type '{}'", typeName));
typeDecl->setName(typeName);
this->m_types.insert({ typeName, typeDecl });
}