diff --git a/source/lang/evaluator.cpp b/source/lang/evaluator.cpp index a7c1c09e1..fa1887b00 100644 --- a/source/lang/evaluator.cpp +++ b/source/lang/evaluator.cpp @@ -461,6 +461,8 @@ namespace hex::lang { if (!node->getName().empty()) pattern->setTypeName(node->getName().data()); + pattern->setEndian(this->getCurrentEndian()); + this->m_endianStack.pop_back(); return pattern; @@ -490,7 +492,6 @@ namespace hex::lang { throwEvaluateError("ASTNodeVariableDecl had an invalid type. This is a bug!", 1); pattern->setVariableName(node->getName().data()); - pattern->setEndian(this->getCurrentEndian()); return pattern; } @@ -605,18 +606,24 @@ namespace hex::lang { } PatternData *sizeType; - if (auto builtinTypeNode = dynamic_cast(node->getSizeType()); builtinTypeNode != nullptr) { + + auto underlyingType = dynamic_cast(node->getSizeType()); + if (underlyingType == nullptr) + throwEvaluateError("underlying type is not ASTNodeTypeDecl. This is a bug", node->getLineNumber()); + + if (auto builtinTypeNode = dynamic_cast(underlyingType->getType()); builtinTypeNode != nullptr) { sizeType = evaluateBuiltinType(builtinTypeNode); } else - throwEvaluateError("Pointer size is not a builtin type", node->getLineNumber()); + throwEvaluateError("pointer size is not a builtin type", node->getLineNumber()); size_t pointerSize = sizeType->getSize(); - delete sizeType; u128 pointedAtOffset = 0; this->m_provider->read(pointerOffset, &pointedAtOffset, pointerSize); + this->m_currOffset = hex::changeEndianess(pointedAtOffset, pointerSize, underlyingType->getEndian().value_or(this->m_defaultDataEndian)); + + delete sizeType; - this->m_currOffset = hex::changeEndianess(pointedAtOffset, 1, this->getCurrentEndian()); if (this->m_currOffset > this->m_provider->getActualSize()) throwEvaluateError("pointer points past the end of the data", 1); diff --git a/source/lang/parser.cpp b/source/lang/parser.cpp index 8043c21d6..ee5045c7f 100644 --- a/source/lang/parser.cpp +++ b/source/lang/parser.cpp @@ -315,14 +315,13 @@ namespace hex::lang { // using Identifier = (parseType) ASTNode* Parser::parseUsingDeclaration() { - auto *temporaryType = dynamic_cast(parseType(-1)); - if (temporaryType == nullptr) throwParseError("invalid type used in variable declaration", -1); - SCOPE_EXIT( delete temporaryType; ); + auto *type = dynamic_cast(parseType(-1)); + if (type == nullptr) throwParseError("invalid type used in variable declaration", -1); if (peekOptional(KEYWORD_BE) || peekOptional(KEYWORD_LE)) - return new ASTNodeTypeDecl(getValue(-4), temporaryType->getType()->clone(), temporaryType->getEndian()); + return new ASTNodeTypeDecl(getValue(-4), type, type->getEndian()); else - return new ASTNodeTypeDecl(getValue(-3), temporaryType->getType()->clone(), temporaryType->getEndian()); + return new ASTNodeTypeDecl(getValue(-3), type, type->getEndian()); } // padding[(parseMathematicalExpression)] @@ -339,18 +338,16 @@ namespace hex::lang { // (parseType) Identifier ASTNode* Parser::parseMemberVariable() { - auto temporaryType = dynamic_cast(parseType(-2)); - if (temporaryType == nullptr) throwParseError("invalid type used in variable declaration", -1); - SCOPE_EXIT( delete temporaryType; ); + auto type = dynamic_cast(parseType(-2)); + if (type == nullptr) throwParseError("invalid type used in variable declaration", -1); - return new ASTNodeVariableDecl(getValue(-1), temporaryType->getType()->clone()); + return new ASTNodeVariableDecl(getValue(-1), type); } // (parseType) Identifier[(parseMathematicalExpression)] ASTNode* Parser::parseMemberArrayVariable() { - auto temporaryType = dynamic_cast(parseType(-3)); - if (temporaryType == nullptr) throwParseError("invalid type used in variable declaration", -1); - SCOPE_EXIT( delete temporaryType; ); + auto type = dynamic_cast(parseType(-3)); + if (type == nullptr) throwParseError("invalid type used in variable declaration", -1); auto name = getValue(-2); @@ -366,25 +363,23 @@ namespace hex::lang { sizeCleanup.release(); - return new ASTNodeArrayVariableDecl(name, temporaryType->getType()->clone(), size); + return new ASTNodeArrayVariableDecl(name, type, size); } // (parseType) *Identifier : (parseType) ASTNode* Parser::parseMemberPointerVariable() { auto name = getValue(-2); - auto temporaryPointerType = dynamic_cast(parseType(-4)); - if (temporaryPointerType == nullptr) throwParseError("invalid type used in variable declaration", -1); - SCOPE_EXIT( delete temporaryPointerType; ); + auto pointerType = dynamic_cast(parseType(-4)); + if (pointerType == nullptr) throwParseError("invalid type used in variable declaration", -1); if (!MATCHES((optional(KEYWORD_BE), optional(KEYWORD_LE)) && sequence(VALUETYPE_UNSIGNED))) throwParseError("expected unsigned builtin type as size", -1); - auto temporarySizeType = dynamic_cast(parseType(-1)); - if (temporarySizeType == nullptr) throwParseError("invalid type used for pointer size", -1); - SCOPE_EXIT( delete temporarySizeType; ); + auto sizeType = dynamic_cast(parseType(-1)); + if (sizeType == nullptr) throwParseError("invalid type used for pointer size", -1); - return new ASTNodePointerVariableDecl(name, temporaryPointerType->getType()->clone(), temporarySizeType->getType()->clone()); + return new ASTNodePointerVariableDecl(name, pointerType, sizeType); } // [(parsePadding)|(parseMemberVariable)|(parseMemberArrayVariable)|(parseMemberPointerVariable)] @@ -450,10 +445,8 @@ namespace hex::lang { else typeName = getValue(-4); - auto temporaryTypeDecl = dynamic_cast(parseType(-2)); - if (temporaryTypeDecl == nullptr) throwParseError("failed to parse type", -2); - auto underlyingType = dynamic_cast(temporaryTypeDecl->getType()); - if (underlyingType == nullptr) throwParseError("underlying type is not a built-in type", -2); + auto underlyingType = dynamic_cast(parseType(-2)); + if (underlyingType == nullptr) throwParseError("failed to parse type", -2); const auto enumNode = new ASTNodeEnum(underlyingType); ScopeExit enumGuard([&]{ delete enumNode; }); @@ -524,18 +517,16 @@ namespace hex::lang { // (parseType) Identifier @ Integer ASTNode* Parser::parseVariablePlacement() { - auto temporaryType = dynamic_cast(parseType(-3)); - if (temporaryType == nullptr) throwParseError("invalid type used in variable declaration", -1); - SCOPE_EXIT( delete temporaryType; ); + auto type = dynamic_cast(parseType(-3)); + if (type == nullptr) throwParseError("invalid type used in variable declaration", -1); - return new ASTNodeVariableDecl(getValue(-2), temporaryType->getType()->clone(), parseMathematicalExpression()); + return new ASTNodeVariableDecl(getValue(-2), type, parseMathematicalExpression()); } // (parseType) Identifier[[(parseMathematicalExpression)]] @ Integer ASTNode* Parser::parseArrayVariablePlacement() { - auto temporaryType = dynamic_cast(parseType(-3)); - if (temporaryType == nullptr) throwParseError("invalid type used in variable declaration", -1); - SCOPE_EXIT( delete temporaryType; ); + auto type = dynamic_cast(parseType(-3)); + if (type == nullptr) throwParseError("invalid type used in variable declaration", -1); auto name = getValue(-2); @@ -554,7 +545,7 @@ namespace hex::lang { sizeCleanup.release(); - return new ASTNodeArrayVariableDecl(name, temporaryType->getType()->clone(), size, parseMathematicalExpression()); + return new ASTNodeArrayVariableDecl(name, type, size, parseMathematicalExpression()); } // (parseType) *Identifier : (parseType) @ Integer @@ -563,19 +554,17 @@ namespace hex::lang { auto temporaryPointerType = dynamic_cast(parseType(-4)); if (temporaryPointerType == nullptr) throwParseError("invalid type used in variable declaration", -1); - SCOPE_EXIT( delete temporaryPointerType; ); if (!MATCHES((optional(KEYWORD_BE), optional(KEYWORD_LE)) && sequence(VALUETYPE_UNSIGNED))) throwParseError("expected unsigned builtin type as size", -1); auto temporaryPointerSizeType = dynamic_cast(parseType(-1)); if (temporaryPointerSizeType == nullptr) throwParseError("invalid size type used in pointer declaration", -1); - SCOPE_EXIT( delete temporaryPointerSizeType; ); if (!MATCHES(sequence(OPERATOR_AT))) throwParseError("expected placement instruction", -1); - return new ASTNodePointerVariableDecl(name, temporaryPointerType->getType()->clone(), temporaryPointerSizeType->getType()->clone(), parseMathematicalExpression()); + return new ASTNodePointerVariableDecl(name, temporaryPointerType, temporaryPointerSizeType, parseMathematicalExpression()); }