From 1ea64d6cc05745c5ef5c90d75e71211682aba542 Mon Sep 17 00:00:00 2001 From: Matheus Aguiar Date: Sat, 6 Apr 2024 17:17:51 -0300 Subject: [PATCH] Make parser accept transient as location (but not a keyword) --- .../analysis/DeclarationTypeChecker.cpp | 18 ++++++++++++++++-- libsolidity/ast/AST.h | 2 +- libsolidity/ast/ASTJsonExporter.cpp | 2 ++ libsolidity/parsing/Parser.cpp | 17 +++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/libsolidity/analysis/DeclarationTypeChecker.cpp b/libsolidity/analysis/DeclarationTypeChecker.cpp index 28c051c0a000..8bfd8fdd265f 100644 --- a/libsolidity/analysis/DeclarationTypeChecker.cpp +++ b/libsolidity/analysis/DeclarationTypeChecker.cpp @@ -407,6 +407,7 @@ void DeclarationTypeChecker::endVisit(VariableDeclaration const& _variable) { case Location::Memory: return "\"memory\""; case Location::Storage: return "\"storage\""; + case Location::Transient: return "\"transient\""; case Location::CallData: return "\"calldata\""; case Location::Unspecified: return "none"; } @@ -456,8 +457,18 @@ void DeclarationTypeChecker::endVisit(VariableDeclaration const& _variable) } else if (_variable.isStateVariable()) { - solAssert(varLoc == Location::Unspecified, ""); - typeLoc = (_variable.isConstant() || _variable.immutable()) ? DataLocation::Memory : DataLocation::Storage; + switch (varLoc) + { + case Location::Unspecified: + typeLoc = (_variable.isConstant() || _variable.immutable()) ? DataLocation::Memory : DataLocation::Storage; + break; + case Location::Transient: + solAssert(false, "Transient storage type checking not implemented yet"); + break; + default: + solAssert(false, ""); + break; + } } else if ( dynamic_cast(_variable.scope()) || @@ -477,6 +488,9 @@ void DeclarationTypeChecker::endVisit(VariableDeclaration const& _variable) case Location::CallData: typeLoc = DataLocation::CallData; break; + case Location::Transient: + solAssert(false, "Transient storage type checking not implemented yet"); + break; case Location::Unspecified: solAssert(!_variable.hasReferenceOrMappingType(), "Data location not properly set."); } diff --git a/libsolidity/ast/AST.h b/libsolidity/ast/AST.h index 27926721aa6f..8e6b5f8202a3 100644 --- a/libsolidity/ast/AST.h +++ b/libsolidity/ast/AST.h @@ -1053,7 +1053,7 @@ class FunctionDefinition: public CallableDeclaration, public StructurallyDocumen class VariableDeclaration: public Declaration, public StructurallyDocumented { public: - enum Location { Unspecified, Storage, Memory, CallData }; + enum Location { Unspecified, Storage, Transient, Memory, CallData }; enum class Mutability { Mutable, Immutable, Constant }; static std::string mutabilityToString(Mutability _mutability) { diff --git a/libsolidity/ast/ASTJsonExporter.cpp b/libsolidity/ast/ASTJsonExporter.cpp index c54a2d013ad0..6de4f2b55c1e 100644 --- a/libsolidity/ast/ASTJsonExporter.cpp +++ b/libsolidity/ast/ASTJsonExporter.cpp @@ -1059,6 +1059,8 @@ std::string ASTJsonExporter::location(VariableDeclaration::Location _location) return "memory"; case VariableDeclaration::Location::CallData: return "calldata"; + case VariableDeclaration::Location::Transient: + return "transient"; } // To make the compiler happy return {}; diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index f650e0dbf1dc..05bb4995b6f2 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -899,6 +899,23 @@ ASTPointer Parser::parseVariableDeclaration( { nodeFactory.markEndPosition(); tie(identifier, nameLocation) = expectIdentifierWithLocation(); + if ( + *identifier == "transient" && + m_scanner->currentToken() == Token::Identifier && + ( + _options.allowLocationSpecifier || + _options.kind == VarDeclKind::State + ) + ) + { + if (location != VariableDeclaration::Location::Unspecified) + parserError(ErrorId{3548}, "Location already specified."); + else + location = VariableDeclaration::Location::Transient; + + nodeFactory.markEndPosition(); + tie(identifier, nameLocation) = expectIdentifierWithLocation(); + } } ASTPointer value; if (_options.allowInitialValue)