From 1488a1ceb807633beef3349ad785ee98be45742d Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 25 Feb 2020 18:42:16 +0100 Subject: [PATCH] Refactor isConstant to add "immutable". --- libsolidity/ast/AST.cpp | 2 +- libsolidity/ast/AST.h | 10 ++++++---- libsolidity/ast/ASTJsonImporter.cpp | 8 +++++++- libsolidity/parsing/Parser.cpp | 6 +++--- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/libsolidity/ast/AST.cpp b/libsolidity/ast/AST.cpp index 90084f9c4..6b03c7b58 100644 --- a/libsolidity/ast/AST.cpp +++ b/libsolidity/ast/AST.cpp @@ -390,7 +390,7 @@ DeclarationAnnotation& Declaration::annotation() const bool VariableDeclaration::isLValue() const { // Constant declared variables are Read-Only - if (m_isConstant) + if (isConstant()) return false; // External function arguments of reference type are Read-Only if (isExternalCallableParameter() && dynamic_cast(type())) diff --git a/libsolidity/ast/AST.h b/libsolidity/ast/AST.h index 336c221c6..adb957cfe 100644 --- a/libsolidity/ast/AST.h +++ b/libsolidity/ast/AST.h @@ -814,6 +814,7 @@ class VariableDeclaration: public Declaration { public: enum Location { Unspecified, Storage, Memory, CallData }; + enum class Constantness { Mutable, Immutable, Constant }; VariableDeclaration( int64_t _id, @@ -824,7 +825,7 @@ public: Visibility _visibility, bool _isStateVar = false, bool _isIndexed = false, - bool _isConstant = false, + Constantness _constantness = Constantness::Mutable, ASTPointer const& _overrides = nullptr, Location _referenceLocation = Location::Unspecified ): @@ -833,7 +834,7 @@ public: m_value(_value), m_isStateVariable(_isStateVar), m_isIndexed(_isIndexed), - m_isConstant(_isConstant), + m_constantness(_constantness), m_overrides(_overrides), m_location(_referenceLocation) {} @@ -877,7 +878,7 @@ public: bool hasReferenceOrMappingType() const; bool isStateVariable() const { return m_isStateVariable; } bool isIndexed() const { return m_isIndexed; } - bool isConstant() const { return m_isConstant; } + bool isConstant() const { return m_constantness == Constantness::Constant; } ASTPointer const& overrides() const { return m_overrides; } Location referenceLocation() const { return m_location; } /// @returns a set of allowed storage locations for the variable. @@ -904,7 +905,8 @@ private: ASTPointer m_value; bool m_isStateVariable = false; ///< Whether or not this is a contract state variable bool m_isIndexed = false; ///< Whether this is an indexed variable (used by events). - bool m_isConstant = false; ///< Whether the variable is a compile-time constant. + /// Whether the variable is "constant", "immutable" or non-marked (mutable). + Constantness m_constantness = Constantness::Mutable; ASTPointer m_overrides; ///< Contains the override specifier node Location m_location = Location::Unspecified; ///< Location of the variable if it is of reference type. }; diff --git a/libsolidity/ast/ASTJsonImporter.cpp b/libsolidity/ast/ASTJsonImporter.cpp index aa28e3f17..26a7c7b7e 100644 --- a/libsolidity/ast/ASTJsonImporter.cpp +++ b/libsolidity/ast/ASTJsonImporter.cpp @@ -411,6 +411,12 @@ ASTPointer ASTJsonImporter::createVariableDeclaration(Json: { astAssert(_node["name"].isString(), "Expected 'name' to be a string!"); + VariableDeclaration::Constantness constantness{}; + if (memberAsBool(_node, "constant")) + constantness = VariableDeclaration::Constantness::Constant; + else + constantness = VariableDeclaration::Constantness::Mutable; + return createASTNode( _node, nullOrCast(member(_node, "typeName")), @@ -419,7 +425,7 @@ ASTPointer ASTJsonImporter::createVariableDeclaration(Json: visibility(_node), memberAsBool(_node, "stateVariable"), _node.isMember("indexed") ? memberAsBool(_node, "indexed") : false, - memberAsBool(_node, "constant"), + constantness, _node["overrides"].isNull() ? nullptr : createOverrideSpecifier(member(_node, "overrides")), location(_node) ); diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 99aa6e2a1..edf43377f 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -695,7 +695,7 @@ ASTPointer Parser::parseVariableDeclaration( ); bool isIndexed = false; - bool isDeclaredConst = false; + VariableDeclaration::Constantness constantness = VariableDeclaration::Constantness::Mutable; ASTPointer overrides = nullptr; Visibility visibility(Visibility::Default); VariableDeclaration::Location location = VariableDeclaration::Location::Unspecified; @@ -731,7 +731,7 @@ ASTPointer Parser::parseVariableDeclaration( if (_options.allowIndexed && token == Token::Indexed) isIndexed = true; else if (token == Token::Constant) - isDeclaredConst = true; + constantness = VariableDeclaration::Constantness::Constant; else if (_options.allowLocationSpecifier && TokenTraits::isLocationSpecifier(token)) { if (location != VariableDeclaration::Location::Unspecified) @@ -790,7 +790,7 @@ ASTPointer Parser::parseVariableDeclaration( visibility, _options.isStateVariable, isIndexed, - isDeclaredConst, + constantness, overrides, location );