Refactor isConstant to add "immutable".

This commit is contained in:
chriseth 2020-02-25 18:42:16 +01:00 committed by Daniel Kirchner
parent f10c6500b2
commit 1488a1ceb8
4 changed files with 17 additions and 9 deletions

View File

@ -390,7 +390,7 @@ DeclarationAnnotation& Declaration::annotation() const
bool VariableDeclaration::isLValue() const bool VariableDeclaration::isLValue() const
{ {
// Constant declared variables are Read-Only // Constant declared variables are Read-Only
if (m_isConstant) if (isConstant())
return false; return false;
// External function arguments of reference type are Read-Only // External function arguments of reference type are Read-Only
if (isExternalCallableParameter() && dynamic_cast<ReferenceType const*>(type())) if (isExternalCallableParameter() && dynamic_cast<ReferenceType const*>(type()))

View File

@ -814,6 +814,7 @@ class VariableDeclaration: public Declaration
{ {
public: public:
enum Location { Unspecified, Storage, Memory, CallData }; enum Location { Unspecified, Storage, Memory, CallData };
enum class Constantness { Mutable, Immutable, Constant };
VariableDeclaration( VariableDeclaration(
int64_t _id, int64_t _id,
@ -824,7 +825,7 @@ public:
Visibility _visibility, Visibility _visibility,
bool _isStateVar = false, bool _isStateVar = false,
bool _isIndexed = false, bool _isIndexed = false,
bool _isConstant = false, Constantness _constantness = Constantness::Mutable,
ASTPointer<OverrideSpecifier> const& _overrides = nullptr, ASTPointer<OverrideSpecifier> const& _overrides = nullptr,
Location _referenceLocation = Location::Unspecified Location _referenceLocation = Location::Unspecified
): ):
@ -833,7 +834,7 @@ public:
m_value(_value), m_value(_value),
m_isStateVariable(_isStateVar), m_isStateVariable(_isStateVar),
m_isIndexed(_isIndexed), m_isIndexed(_isIndexed),
m_isConstant(_isConstant), m_constantness(_constantness),
m_overrides(_overrides), m_overrides(_overrides),
m_location(_referenceLocation) {} m_location(_referenceLocation) {}
@ -877,7 +878,7 @@ public:
bool hasReferenceOrMappingType() const; bool hasReferenceOrMappingType() const;
bool isStateVariable() const { return m_isStateVariable; } bool isStateVariable() const { return m_isStateVariable; }
bool isIndexed() const { return m_isIndexed; } bool isIndexed() const { return m_isIndexed; }
bool isConstant() const { return m_isConstant; } bool isConstant() const { return m_constantness == Constantness::Constant; }
ASTPointer<OverrideSpecifier> const& overrides() const { return m_overrides; } ASTPointer<OverrideSpecifier> const& overrides() const { return m_overrides; }
Location referenceLocation() const { return m_location; } Location referenceLocation() const { return m_location; }
/// @returns a set of allowed storage locations for the variable. /// @returns a set of allowed storage locations for the variable.
@ -904,7 +905,8 @@ private:
ASTPointer<Expression> m_value; ASTPointer<Expression> m_value;
bool m_isStateVariable = false; ///< Whether or not this is a contract state variable 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_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<OverrideSpecifier> m_overrides; ///< Contains the override specifier node ASTPointer<OverrideSpecifier> m_overrides; ///< Contains the override specifier node
Location m_location = Location::Unspecified; ///< Location of the variable if it is of reference type. Location m_location = Location::Unspecified; ///< Location of the variable if it is of reference type.
}; };

View File

@ -411,6 +411,12 @@ ASTPointer<VariableDeclaration> ASTJsonImporter::createVariableDeclaration(Json:
{ {
astAssert(_node["name"].isString(), "Expected 'name' to be a string!"); 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<VariableDeclaration>( return createASTNode<VariableDeclaration>(
_node, _node,
nullOrCast<TypeName>(member(_node, "typeName")), nullOrCast<TypeName>(member(_node, "typeName")),
@ -419,7 +425,7 @@ ASTPointer<VariableDeclaration> ASTJsonImporter::createVariableDeclaration(Json:
visibility(_node), visibility(_node),
memberAsBool(_node, "stateVariable"), memberAsBool(_node, "stateVariable"),
_node.isMember("indexed") ? memberAsBool(_node, "indexed") : false, _node.isMember("indexed") ? memberAsBool(_node, "indexed") : false,
memberAsBool(_node, "constant"), constantness,
_node["overrides"].isNull() ? nullptr : createOverrideSpecifier(member(_node, "overrides")), _node["overrides"].isNull() ? nullptr : createOverrideSpecifier(member(_node, "overrides")),
location(_node) location(_node)
); );

View File

@ -695,7 +695,7 @@ ASTPointer<VariableDeclaration> Parser::parseVariableDeclaration(
); );
bool isIndexed = false; bool isIndexed = false;
bool isDeclaredConst = false; VariableDeclaration::Constantness constantness = VariableDeclaration::Constantness::Mutable;
ASTPointer<OverrideSpecifier> overrides = nullptr; ASTPointer<OverrideSpecifier> overrides = nullptr;
Visibility visibility(Visibility::Default); Visibility visibility(Visibility::Default);
VariableDeclaration::Location location = VariableDeclaration::Location::Unspecified; VariableDeclaration::Location location = VariableDeclaration::Location::Unspecified;
@ -731,7 +731,7 @@ ASTPointer<VariableDeclaration> Parser::parseVariableDeclaration(
if (_options.allowIndexed && token == Token::Indexed) if (_options.allowIndexed && token == Token::Indexed)
isIndexed = true; isIndexed = true;
else if (token == Token::Constant) else if (token == Token::Constant)
isDeclaredConst = true; constantness = VariableDeclaration::Constantness::Constant;
else if (_options.allowLocationSpecifier && TokenTraits::isLocationSpecifier(token)) else if (_options.allowLocationSpecifier && TokenTraits::isLocationSpecifier(token))
{ {
if (location != VariableDeclaration::Location::Unspecified) if (location != VariableDeclaration::Location::Unspecified)
@ -790,7 +790,7 @@ ASTPointer<VariableDeclaration> Parser::parseVariableDeclaration(
visibility, visibility,
_options.isStateVariable, _options.isStateVariable,
isIndexed, isIndexed,
isDeclaredConst, constantness,
overrides, overrides,
location location
); );