Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export immutability. #8628

Merged
merged 1 commit into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Compiler Features:


Bugfixes:
* AST export: Export `immutable` property in the field `mutability`.
* SMTChecker: Fix internal error in the CHC engine when calling inherited functions internally.


Expand Down
23 changes: 17 additions & 6 deletions libsolidity/ast/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,17 @@ class VariableDeclaration: public Declaration
{
public:
enum Location { Unspecified, Storage, Memory, CallData };
enum class Constantness { Mutable, Immutable, Constant };
enum class Mutability { Mutable, Immutable, Constant };
static std::string mutabilityToString(Mutability _mutability)
{
switch (_mutability)
{
case Mutability::Mutable: return "mutable";
case Mutability::Immutable: return "immutable";
case Mutability::Constant: return "constant";
}
return {};
}

VariableDeclaration(
int64_t _id,
Expand All @@ -865,7 +875,7 @@ class VariableDeclaration: public Declaration
Visibility _visibility,
bool _isStateVar = false,
bool _isIndexed = false,
Constantness _constantness = Constantness::Mutable,
Mutability _mutability = Mutability::Mutable,
ASTPointer<OverrideSpecifier> const& _overrides = nullptr,
Location _referenceLocation = Location::Unspecified
):
Expand All @@ -874,7 +884,7 @@ class VariableDeclaration: public Declaration
m_value(_value),
m_isStateVariable(_isStateVar),
m_isIndexed(_isIndexed),
m_constantness(_constantness),
m_mutability(_mutability),
m_overrides(_overrides),
m_location(_referenceLocation) {}

Expand Down Expand Up @@ -918,8 +928,9 @@ class VariableDeclaration: public Declaration
bool hasReferenceOrMappingType() const;
bool isStateVariable() const { return m_isStateVariable; }
bool isIndexed() const { return m_isIndexed; }
bool isConstant() const { return m_constantness == Constantness::Constant; }
bool immutable() const { return m_constantness == Constantness::Immutable; }
Mutability mutability() const { return m_mutability; }
bool isConstant() const { return m_mutability == Mutability::Constant; }
bool immutable() const { return m_mutability == Mutability::Immutable; }
ASTPointer<OverrideSpecifier> const& overrides() const { return m_overrides; }
Location referenceLocation() const { return m_location; }
/// @returns a set of allowed storage locations for the variable.
Expand Down Expand Up @@ -947,7 +958,7 @@ class VariableDeclaration: public Declaration
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).
/// Whether the variable is "constant", "immutable" or non-marked (mutable).
Constantness m_constantness = Constantness::Mutable;
Mutability m_mutability = Mutability::Mutable;
ASTPointer<OverrideSpecifier> m_overrides; ///< Contains the override specifier node
Location m_location = Location::Unspecified; ///< Location of the variable if it is of reference type.
};
Expand Down
1 change: 1 addition & 0 deletions libsolidity/ast/ASTJsonConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ bool ASTJsonConverter::visit(VariableDeclaration const& _node)
make_pair("name", _node.name()),
make_pair("typeName", toJsonOrNull(_node.typeName())),
make_pair("constant", _node.isConstant()),
make_pair("mutability", VariableDeclaration::mutabilityToString(_node.mutability())),
make_pair("stateVariable", _node.isStateVariable()),
make_pair("storageLocation", location(_node.referenceLocation())),
make_pair("overrides", _node.overrides() ? toJson(*_node.overrides()) : Json::nullValue),
Expand Down
23 changes: 18 additions & 5 deletions libsolidity/ast/ASTJsonImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,24 @@ ASTPointer<VariableDeclaration> ASTJsonImporter::createVariableDeclaration(Json:
{
astAssert(_node["name"].isString(), "Expected 'name' to be a string!");

VariableDeclaration::Constantness constantness{};
if (memberAsBool(_node, "constant"))
constantness = VariableDeclaration::Constantness::Constant;
VariableDeclaration::Mutability mutability{};
astAssert(member(_node, "mutability").isString(), "'mutability' expected to be string.");
string const mutabilityStr = member(_node, "mutability").asString();
if (mutabilityStr == "constant")
{
mutability = VariableDeclaration::Mutability::Constant;
astAssert(memberAsBool(_node, "constant"), "");
}
else
constantness = VariableDeclaration::Constantness::Mutable;
{
astAssert(!memberAsBool(_node, "constant"), "");
if (mutabilityStr == "mutable")
mutability = VariableDeclaration::Mutability::Mutable;
else if (mutabilityStr == "immutable")
mutability = VariableDeclaration::Mutability::Immutable;
else
astAssert(false, "");
}

return createASTNode<VariableDeclaration>(
_node,
Expand All @@ -425,7 +438,7 @@ ASTPointer<VariableDeclaration> ASTJsonImporter::createVariableDeclaration(Json:
visibility(_node),
memberAsBool(_node, "stateVariable"),
_node.isMember("indexed") ? memberAsBool(_node, "indexed") : false,
constantness,
mutability,
_node["overrides"].isNull() ? nullptr : createOverrideSpecifier(member(_node, "overrides")),
location(_node)
);
Expand Down
14 changes: 7 additions & 7 deletions libsolidity/parsing/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ ASTPointer<VariableDeclaration> Parser::parseVariableDeclaration(
);

bool isIndexed = false;
VariableDeclaration::Constantness constantness = VariableDeclaration::Constantness::Mutable;
VariableDeclaration::Mutability mutability = VariableDeclaration::Mutability::Mutable;
ASTPointer<OverrideSpecifier> overrides = nullptr;
Visibility visibility(Visibility::Default);
VariableDeclaration::Location location = VariableDeclaration::Location::Unspecified;
Expand Down Expand Up @@ -732,15 +732,15 @@ ASTPointer<VariableDeclaration> Parser::parseVariableDeclaration(
isIndexed = true;
else if (token == Token::Constant || token == Token::Immutable)
{
if (constantness != VariableDeclaration::Constantness::Mutable)
if (mutability != VariableDeclaration::Mutability::Mutable)
parserError(
string("Constantness already set to ") +
(constantness == VariableDeclaration::Constantness::Constant ? "\"constant\"" : "\"immutable\"")
string("Mutability already set to ") +
(mutability == VariableDeclaration::Mutability::Constant ? "\"constant\"" : "\"immutable\"")
);
else if (token == Token::Constant)
constantness = VariableDeclaration::Constantness::Constant;
mutability = VariableDeclaration::Mutability::Constant;
else if (token == Token::Immutable)
constantness = VariableDeclaration::Constantness::Immutable;
mutability = VariableDeclaration::Mutability::Immutable;
}
else if (_options.allowLocationSpecifier && TokenTraits::isLocationSpecifier(token))
{
Expand Down Expand Up @@ -800,7 +800,7 @@ ASTPointer<VariableDeclaration> Parser::parseVariableDeclaration(
visibility,
_options.isStateVariable,
isIndexed,
constantness,
mutability,
overrides,
location
);
Expand Down
1 change: 1 addition & 0 deletions test/cmdlineTests/recovery_ast_constructor/output
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ JSON AST:
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "",
"overrides": null,
"scope": 17,
Expand Down
5 changes: 5 additions & 0 deletions test/libsolidity/ASTJSON/address_payable.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"constant": false,
"functionSelector": "97682884",
"id": 4,
"mutability": "mutable",
"name": "m",
"nodeType": "VariableDeclaration",
"overrides": null,
Expand Down Expand Up @@ -100,6 +101,7 @@
{
"constant": false,
"id": 12,
"mutability": "mutable",
"name": "a",
"nodeType": "VariableDeclaration",
"overrides": null,
Expand Down Expand Up @@ -241,6 +243,7 @@
{
"constant": false,
"id": 22,
"mutability": "mutable",
"name": "c",
"nodeType": "VariableDeclaration",
"overrides": null,
Expand Down Expand Up @@ -507,6 +510,7 @@
{
"constant": false,
"id": 6,
"mutability": "mutable",
"name": "arg",
"nodeType": "VariableDeclaration",
"overrides": null,
Expand Down Expand Up @@ -547,6 +551,7 @@
{
"constant": false,
"id": 9,
"mutability": "mutable",
"name": "r",
"nodeType": "VariableDeclaration",
"overrides": null,
Expand Down
5 changes: 5 additions & 0 deletions test/libsolidity/ASTJSON/address_payable_legacy.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
{
"constant": false,
"functionSelector": "97682884",
"mutability": "mutable",
"name": "m",
"overrides": null,
"scope": 39,
Expand Down Expand Up @@ -118,6 +119,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "arg",
"overrides": null,
"scope": 38,
Expand Down Expand Up @@ -157,6 +159,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "r",
"overrides": null,
"scope": 38,
Expand Down Expand Up @@ -206,6 +209,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "a",
"overrides": null,
"scope": 37,
Expand Down Expand Up @@ -359,6 +363,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "c",
"overrides": null,
"scope": 37,
Expand Down
1 change: 1 addition & 0 deletions test/libsolidity/ASTJSON/array_type_name.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
{
"constant": false,
"id": 3,
"mutability": "mutable",
"name": "i",
"nodeType": "VariableDeclaration",
"overrides": null,
Expand Down
1 change: 1 addition & 0 deletions test/libsolidity/ASTJSON/array_type_name_legacy.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "i",
"overrides": null,
"scope": 4,
Expand Down
1 change: 1 addition & 0 deletions test/libsolidity/ASTJSON/assembly/nested_functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
{
"constant": false,
"id": 3,
"mutability": "mutable",
"name": "x",
"nodeType": "VariableDeclaration",
"overrides": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "x",
"overrides": null,
"scope": 7,
Expand Down
2 changes: 2 additions & 0 deletions test/libsolidity/ASTJSON/assembly/slot_offset.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
{
"constant": false,
"id": 2,
"mutability": "mutable",
"name": "x",
"nodeType": "VariableDeclaration",
"overrides": null,
Expand Down Expand Up @@ -72,6 +73,7 @@
{
"constant": false,
"id": 5,
"mutability": "mutable",
"name": "s",
"nodeType": "VariableDeclaration",
"overrides": null,
Expand Down
2 changes: 2 additions & 0 deletions test/libsolidity/ASTJSON/assembly/slot_offset_legacy.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "x",
"overrides": null,
"scope": 3,
Expand Down Expand Up @@ -85,6 +86,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "s",
"overrides": null,
"scope": 11,
Expand Down
1 change: 1 addition & 0 deletions test/libsolidity/ASTJSON/assembly/var_access.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
{
"constant": false,
"id": 4,
"mutability": "mutable",
"name": "x",
"nodeType": "VariableDeclaration",
"overrides": null,
Expand Down
1 change: 1 addition & 0 deletions test/libsolidity/ASTJSON/assembly/var_access_legacy.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "x",
"overrides": null,
"scope": 7,
Expand Down
4 changes: 4 additions & 0 deletions test/libsolidity/ASTJSON/function_type.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
{
"constant": false,
"id": 6,
"mutability": "mutable",
"name": "x",
"nodeType": "VariableDeclaration",
"overrides": null,
Expand Down Expand Up @@ -85,6 +86,7 @@
{
"constant": false,
"id": 3,
"mutability": "mutable",
"name": "",
"nodeType": "VariableDeclaration",
"overrides": null,
Expand Down Expand Up @@ -139,6 +141,7 @@
{
"constant": false,
"id": 13,
"mutability": "mutable",
"name": "",
"nodeType": "VariableDeclaration",
"overrides": null,
Expand Down Expand Up @@ -171,6 +174,7 @@
{
"constant": false,
"id": 10,
"mutability": "mutable",
"name": "",
"nodeType": "VariableDeclaration",
"overrides": null,
Expand Down
4 changes: 4 additions & 0 deletions test/libsolidity/ASTJSON/function_type_legacy.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "x",
"overrides": null,
"scope": 16,
Expand Down Expand Up @@ -104,6 +105,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "",
"overrides": null,
"scope": 5,
Expand Down Expand Up @@ -157,6 +159,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "",
"overrides": null,
"scope": 16,
Expand Down Expand Up @@ -197,6 +200,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "",
"overrides": null,
"scope": 12,
Expand Down
1 change: 1 addition & 0 deletions test/libsolidity/ASTJSON/global_struct.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
{
"constant": false,
"id": 2,
"mutability": "mutable",
"name": "a",
"nodeType": "VariableDeclaration",
"overrides": null,
Expand Down
1 change: 1 addition & 0 deletions test/libsolidity/ASTJSON/global_struct_legacy.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "a",
"overrides": null,
"scope": 3,
Expand Down
Loading