diff --git a/libyul/AsmParser.cpp b/libyul/AsmParser.cpp index e1bcabaf2..c43066ee0 100644 --- a/libyul/AsmParser.cpp +++ b/libyul/AsmParser.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include @@ -142,7 +143,7 @@ Statement Parser::parseStatement() // Options left: // Expression/FunctionCall // Assignment - ElementaryOperation elementary(parseLiteralOrIdentifier()); + variant elementary(parseLiteralOrIdentifier()); switch (currentToken()) { @@ -212,7 +213,7 @@ Case Parser::parseCase() else if (currentToken() == Token::Case) { advance(); - ElementaryOperation literal = parseLiteralOrIdentifier(); + variant literal = parseLiteralOrIdentifier(); if (!holds_alternative(literal)) fatalParserError(4805_error, "Literal expected."); _case.value = make_unique(std::get(std::move(literal))); @@ -251,38 +252,37 @@ Expression Parser::parseExpression() { RecursionGuard recursionGuard(*this); - ElementaryOperation operation = parseLiteralOrIdentifier(); - if (holds_alternative(operation)) - { - if (currentToken() == Token::LParen) - return parseCall(std::move(operation)); - auto identifier = std::get(operation); - if (m_dialect.builtin(identifier.name)) - fatalParserError( - 7104_error, - identifier.location, - "Builtin function \"" + identifier.name.str() + "\" must be called." - ); - return identifier; - } - else - { - yulAssert(holds_alternative(operation), ""); - return std::get(operation); - } + variant operation = parseLiteralOrIdentifier(); + return visit(GenericVisitor{ + [&](Identifier& _identifier) -> Expression + { + if (currentToken() == Token::LParen) + return parseCall(std::move(operation)); + if (m_dialect.builtin(_identifier.name)) + fatalParserError( + 7104_error, + _identifier.location, + "Builtin function \"" + _identifier.name.str() + "\" must be called." + ); + return move(_identifier); + }, + [&](Literal& _literal) -> Expression + { + return move(_literal); + } + }, operation); } -Parser::ElementaryOperation Parser::parseLiteralOrIdentifier() +variant Parser::parseLiteralOrIdentifier() { RecursionGuard recursionGuard(*this); - ElementaryOperation ret; switch (currentToken()) { case Token::Identifier: { - ret = Identifier{currentLocation(), YulString{currentLiteral()}}; + Identifier identifier{currentLocation(), YulString{currentLiteral()}}; advance(); - break; + return identifier; } case Token::StringLiteral: case Token::Number: @@ -322,8 +322,7 @@ Parser::ElementaryOperation Parser::parseLiteralOrIdentifier() literal.type = expectAsmIdentifier(); } - ret = std::move(literal); - break; + return literal; } case Token::HexStringLiteral: fatalParserError(3772_error, "Hex literals are not valid in this context."); @@ -331,7 +330,7 @@ Parser::ElementaryOperation Parser::parseLiteralOrIdentifier() default: fatalParserError(1856_error, "Literal or identifier expected."); } - return ret; + return {}; } VariableDeclaration Parser::parseVariableDeclaration() @@ -405,7 +404,7 @@ FunctionDefinition Parser::parseFunctionDefinition() return funDef; } -FunctionCall Parser::parseCall(Parser::ElementaryOperation&& _initialOp) +FunctionCall Parser::parseCall(variant&& _initialOp) { RecursionGuard recursionGuard(*this); diff --git a/libyul/AsmParser.h b/libyul/AsmParser.h index 2dcbcd191..d1294b111 100644 --- a/libyul/AsmParser.h +++ b/libyul/AsmParser.h @@ -61,8 +61,6 @@ public: std::unique_ptr parse(std::shared_ptr const& _scanner, bool _reuseScanner); protected: - using ElementaryOperation = std::variant; - langutil::SourceLocation currentLocation() const override { return m_locationOverride ? *m_locationOverride : ParserBase::currentLocation(); @@ -84,10 +82,10 @@ protected: Expression parseExpression(); /// Parses an elementary operation, i.e. a literal, identifier, instruction or /// builtin functian call (only the name). - ElementaryOperation parseLiteralOrIdentifier(); + std::variant parseLiteralOrIdentifier(); VariableDeclaration parseVariableDeclaration(); FunctionDefinition parseFunctionDefinition(); - FunctionCall parseCall(ElementaryOperation&& _initialOp); + FunctionCall parseCall(std::variant&& _initialOp); TypedName parseTypedName(); YulString expectAsmIdentifier();