Export immutability.

This commit is contained in:
chriseth 2020-04-07 11:10:10 +02:00
parent 582c754598
commit 236eff8416
42 changed files with 493 additions and 20 deletions

View File

@ -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.

View File

@ -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,
@ -865,7 +875,7 @@ public:
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
):
@ -874,7 +884,7 @@ public:
m_value(_value),
m_isStateVariable(_isStateVar),
m_isIndexed(_isIndexed),
m_constantness(_constantness),
m_mutability(_mutability),
m_overrides(_overrides),
m_location(_referenceLocation) {}
@ -918,8 +928,9 @@ public:
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.
@ -947,7 +958,7 @@ private:
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.
};

View File

@ -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),

View File

@ -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,
@ -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)
);

View File

@ -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;
@ -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))
{
@ -800,7 +800,7 @@ ASTPointer<VariableDeclaration> Parser::parseVariableDeclaration(
visibility,
_options.isStateVariable,
isIndexed,
constantness,
mutability,
overrides,
location
);

View File

@ -160,6 +160,7 @@ JSON AST:
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "",
"overrides": null,
"scope": 17,

View File

@ -31,6 +31,7 @@
"constant": false,
"functionSelector": "97682884",
"id": 4,
"mutability": "mutable",
"name": "m",
"nodeType": "VariableDeclaration",
"overrides": null,
@ -100,6 +101,7 @@
{
"constant": false,
"id": 12,
"mutability": "mutable",
"name": "a",
"nodeType": "VariableDeclaration",
"overrides": null,
@ -241,6 +243,7 @@
{
"constant": false,
"id": 22,
"mutability": "mutable",
"name": "c",
"nodeType": "VariableDeclaration",
"overrides": null,
@ -507,6 +510,7 @@
{
"constant": false,
"id": 6,
"mutability": "mutable",
"name": "arg",
"nodeType": "VariableDeclaration",
"overrides": null,
@ -547,6 +551,7 @@
{
"constant": false,
"id": 9,
"mutability": "mutable",
"name": "r",
"nodeType": "VariableDeclaration",
"overrides": null,

View File

@ -41,6 +41,7 @@
{
"constant": false,
"functionSelector": "97682884",
"mutability": "mutable",
"name": "m",
"overrides": null,
"scope": 39,
@ -118,6 +119,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "arg",
"overrides": null,
"scope": 38,
@ -157,6 +159,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "r",
"overrides": null,
"scope": 38,
@ -206,6 +209,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "a",
"overrides": null,
"scope": 37,
@ -359,6 +363,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "c",
"overrides": null,
"scope": 37,

View File

@ -30,6 +30,7 @@
{
"constant": false,
"id": 3,
"mutability": "mutable",
"name": "i",
"nodeType": "VariableDeclaration",
"overrides": null,

View File

@ -40,6 +40,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "i",
"overrides": null,
"scope": 4,

View File

@ -121,6 +121,7 @@
{
"constant": false,
"id": 3,
"mutability": "mutable",
"name": "x",
"nodeType": "VariableDeclaration",
"overrides": null,

View File

@ -77,6 +77,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "x",
"overrides": null,
"scope": 7,

View File

@ -35,6 +35,7 @@
{
"constant": false,
"id": 2,
"mutability": "mutable",
"name": "x",
"nodeType": "VariableDeclaration",
"overrides": null,
@ -72,6 +73,7 @@
{
"constant": false,
"id": 5,
"mutability": "mutable",
"name": "s",
"nodeType": "VariableDeclaration",
"overrides": null,

View File

@ -50,6 +50,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "x",
"overrides": null,
"scope": 3,
@ -85,6 +86,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "s",
"overrides": null,
"scope": 11,

View File

@ -45,6 +45,7 @@
{
"constant": false,
"id": 4,
"mutability": "mutable",
"name": "x",
"nodeType": "VariableDeclaration",
"overrides": null,

View File

@ -101,6 +101,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "x",
"overrides": null,
"scope": 7,

View File

@ -53,6 +53,7 @@
{
"constant": false,
"id": 6,
"mutability": "mutable",
"name": "x",
"nodeType": "VariableDeclaration",
"overrides": null,
@ -85,6 +86,7 @@
{
"constant": false,
"id": 3,
"mutability": "mutable",
"name": "",
"nodeType": "VariableDeclaration",
"overrides": null,
@ -139,6 +141,7 @@
{
"constant": false,
"id": 13,
"mutability": "mutable",
"name": "",
"nodeType": "VariableDeclaration",
"overrides": null,
@ -171,6 +174,7 @@
{
"constant": false,
"id": 10,
"mutability": "mutable",
"name": "",
"nodeType": "VariableDeclaration",
"overrides": null,

View File

@ -64,6 +64,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "x",
"overrides": null,
"scope": 16,
@ -104,6 +105,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "",
"overrides": null,
"scope": 5,
@ -157,6 +159,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "",
"overrides": null,
"scope": 16,
@ -197,6 +200,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "",
"overrides": null,
"scope": 12,

View File

@ -19,6 +19,7 @@
{
"constant": false,
"id": 2,
"mutability": "mutable",
"name": "a",
"nodeType": "VariableDeclaration",
"overrides": null,

View File

@ -26,6 +26,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "a",
"overrides": null,
"scope": 3,

View File

@ -45,6 +45,7 @@
{
"constant": false,
"id": 4,
"mutability": "mutable",
"name": "a",
"nodeType": "VariableDeclaration",
"overrides": null,

View File

@ -100,6 +100,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "a",
"overrides": null,
"scope": 9,

View File

@ -30,6 +30,7 @@
{
"constant": false,
"id": 3,
"mutability": "mutable",
"name": "a",
"nodeType": "VariableDeclaration",
"overrides": null,
@ -87,6 +88,7 @@
{
"constant": false,
"id": 10,
"mutability": "mutable",
"name": "b",
"nodeType": "VariableDeclaration",
"overrides": null,

View File

@ -40,6 +40,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "a",
"overrides": null,
"scope": 15,
@ -143,6 +144,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "b",
"overrides": null,
"scope": 13,

View File

@ -58,6 +58,7 @@
{
"constant": false,
"id": 8,
"mutability": "mutable",
"name": "a",
"nodeType": "VariableDeclaration",
"overrides": null,
@ -113,6 +114,7 @@
{
"constant": false,
"id": 12,
"mutability": "mutable",
"name": "b",
"nodeType": "VariableDeclaration",
"overrides": null,
@ -166,6 +168,7 @@
{
"constant": false,
"id": 16,
"mutability": "mutable",
"name": "c",
"nodeType": "VariableDeclaration",
"overrides": null,

View File

@ -80,6 +80,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "a",
"overrides": null,
"scope": 17,
@ -134,6 +135,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "b",
"overrides": null,
"scope": 17,
@ -186,6 +188,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "c",
"overrides": null,
"scope": 17,

View File

@ -56,6 +56,7 @@
{
"constant": false,
"id": 2,
"mutability": "mutable",
"name": "i",
"nodeType": "VariableDeclaration",
"overrides": null,

View File

@ -54,6 +54,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "i",
"overrides": null,
"scope": 6,

View File

@ -56,6 +56,7 @@
{
"constant": false,
"id": 2,
"mutability": "mutable",
"name": "i",
"nodeType": "VariableDeclaration",
"overrides": null,

View File

@ -54,6 +54,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "i",
"overrides": null,
"scope": 6,

View File

@ -0,0 +1,189 @@
{
"absolutePath": "a",
"exportedSymbols":
{
"C":
[
10
]
},
"id": 11,
"nodeType": "SourceUnit",
"nodes":
[
{
"abstract": false,
"baseContracts": [],
"contractDependencies": [],
"contractKind": "contract",
"documentation": null,
"fullyImplemented": true,
"id": 10,
"linearizedBaseContracts":
[
10
],
"name": "C",
"nodeType": "ContractDefinition",
"nodes":
[
{
"constant": false,
"functionSelector": "0dbe671f",
"id": 3,
"mutability": "immutable",
"name": "a",
"nodeType": "VariableDeclaration",
"overrides": null,
"scope": 10,
"src": "17:27:1",
"stateVariable": true,
"storageLocation": "default",
"typeDescriptions":
{
"typeIdentifier": "t_uint256",
"typeString": "uint256"
},
"typeName":
{
"id": 1,
"name": "uint",
"nodeType": "ElementaryTypeName",
"src": "17:4:1",
"typeDescriptions":
{
"typeIdentifier": "t_uint256",
"typeString": "uint256"
}
},
"value":
{
"argumentTypes": null,
"hexValue": "34",
"id": 2,
"isConstant": false,
"isLValue": false,
"isPure": true,
"kind": "number",
"lValueRequested": false,
"nodeType": "Literal",
"src": "43:1:1",
"subdenomination": null,
"typeDescriptions":
{
"typeIdentifier": "t_rational_4_by_1",
"typeString": "int_const 4"
},
"value": "4"
},
"visibility": "public"
},
{
"constant": true,
"functionSelector": "4df7e3d0",
"id": 6,
"mutability": "constant",
"name": "b",
"nodeType": "VariableDeclaration",
"overrides": null,
"scope": 10,
"src": "50:26:1",
"stateVariable": true,
"storageLocation": "default",
"typeDescriptions":
{
"typeIdentifier": "t_uint256",
"typeString": "uint256"
},
"typeName":
{
"id": 4,
"name": "uint",
"nodeType": "ElementaryTypeName",
"src": "50:4:1",
"typeDescriptions":
{
"typeIdentifier": "t_uint256",
"typeString": "uint256"
}
},
"value":
{
"argumentTypes": null,
"hexValue": "32",
"id": 5,
"isConstant": false,
"isLValue": false,
"isPure": true,
"kind": "number",
"lValueRequested": false,
"nodeType": "Literal",
"src": "75:1:1",
"subdenomination": null,
"typeDescriptions":
{
"typeIdentifier": "t_rational_2_by_1",
"typeString": "int_const 2"
},
"value": "2"
},
"visibility": "public"
},
{
"constant": false,
"functionSelector": "c3da42b8",
"id": 9,
"mutability": "mutable",
"name": "c",
"nodeType": "VariableDeclaration",
"overrides": null,
"scope": 10,
"src": "82:17:1",
"stateVariable": true,
"storageLocation": "default",
"typeDescriptions":
{
"typeIdentifier": "t_uint256",
"typeString": "uint256"
},
"typeName":
{
"id": 7,
"name": "uint",
"nodeType": "ElementaryTypeName",
"src": "82:4:1",
"typeDescriptions":
{
"typeIdentifier": "t_uint256",
"typeString": "uint256"
}
},
"value":
{
"argumentTypes": null,
"hexValue": "33",
"id": 8,
"isConstant": false,
"isLValue": false,
"isPure": true,
"kind": "number",
"lValueRequested": false,
"nodeType": "Literal",
"src": "98:1:1",
"subdenomination": null,
"typeDescriptions":
{
"typeIdentifier": "t_rational_3_by_1",
"typeString": "int_const 3"
},
"value": "3"
},
"visibility": "public"
}
],
"scope": 11,
"src": "0:102:1"
}
],
"src": "0:103:1"
}

View File

@ -0,0 +1,8 @@
contract C
{
uint public immutable a = 4;
uint public constant b = 2;
uint public c = 3;
}
// ----

View File

@ -0,0 +1,195 @@
{
"attributes":
{
"absolutePath": "a",
"exportedSymbols":
{
"C":
[
10
]
}
},
"children":
[
{
"attributes":
{
"abstract": false,
"baseContracts":
[
null
],
"contractDependencies":
[
null
],
"contractKind": "contract",
"documentation": null,
"fullyImplemented": true,
"linearizedBaseContracts":
[
10
],
"name": "C",
"scope": 11
},
"children":
[
{
"attributes":
{
"constant": false,
"functionSelector": "0dbe671f",
"mutability": "immutable",
"name": "a",
"overrides": null,
"scope": 10,
"stateVariable": true,
"storageLocation": "default",
"type": "uint256",
"visibility": "public"
},
"children":
[
{
"attributes":
{
"name": "uint",
"type": "uint256"
},
"id": 1,
"name": "ElementaryTypeName",
"src": "17:4:1"
},
{
"attributes":
{
"argumentTypes": null,
"hexvalue": "34",
"isConstant": false,
"isLValue": false,
"isPure": true,
"lValueRequested": false,
"subdenomination": null,
"token": "number",
"type": "int_const 4",
"value": "4"
},
"id": 2,
"name": "Literal",
"src": "43:1:1"
}
],
"id": 3,
"name": "VariableDeclaration",
"src": "17:27:1"
},
{
"attributes":
{
"constant": true,
"functionSelector": "4df7e3d0",
"mutability": "constant",
"name": "b",
"overrides": null,
"scope": 10,
"stateVariable": true,
"storageLocation": "default",
"type": "uint256",
"visibility": "public"
},
"children":
[
{
"attributes":
{
"name": "uint",
"type": "uint256"
},
"id": 4,
"name": "ElementaryTypeName",
"src": "50:4:1"
},
{
"attributes":
{
"argumentTypes": null,
"hexvalue": "32",
"isConstant": false,
"isLValue": false,
"isPure": true,
"lValueRequested": false,
"subdenomination": null,
"token": "number",
"type": "int_const 2",
"value": "2"
},
"id": 5,
"name": "Literal",
"src": "75:1:1"
}
],
"id": 6,
"name": "VariableDeclaration",
"src": "50:26:1"
},
{
"attributes":
{
"constant": false,
"functionSelector": "c3da42b8",
"mutability": "mutable",
"name": "c",
"overrides": null,
"scope": 10,
"stateVariable": true,
"storageLocation": "default",
"type": "uint256",
"visibility": "public"
},
"children":
[
{
"attributes":
{
"name": "uint",
"type": "uint256"
},
"id": 7,
"name": "ElementaryTypeName",
"src": "82:4:1"
},
{
"attributes":
{
"argumentTypes": null,
"hexvalue": "33",
"isConstant": false,
"isLValue": false,
"isPure": true,
"lValueRequested": false,
"subdenomination": null,
"token": "number",
"type": "int_const 3",
"value": "3"
},
"id": 8,
"name": "Literal",
"src": "98:1:1"
}
],
"id": 9,
"name": "VariableDeclaration",
"src": "82:17:1"
}
],
"id": 10,
"name": "ContractDefinition",
"src": "0:102:1"
}
],
"id": 11,
"name": "SourceUnit",
"src": "0:103:1"
}

View File

@ -45,6 +45,7 @@
{
"constant": false,
"id": 3,
"mutability": "mutable",
"name": "x",
"nodeType": "VariableDeclaration",
"overrides": null,

View File

@ -100,6 +100,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "x",
"overrides": null,
"scope": 6,

View File

@ -45,6 +45,7 @@
{
"constant": false,
"id": 7,
"mutability": "mutable",
"name": "x",
"nodeType": "VariableDeclaration",
"overrides": null,

View File

@ -101,6 +101,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "x",
"overrides": null,
"scope": 9,

View File

@ -45,6 +45,7 @@
{
"constant": false,
"id": 8,
"mutability": "mutable",
"name": "rows",
"nodeType": "VariableDeclaration",
"overrides": null,

View File

@ -101,6 +101,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "rows",
"overrides": null,
"scope": 10,

View File

@ -45,6 +45,7 @@
{
"constant": false,
"id": 3,
"mutability": "mutable",
"name": "x",
"nodeType": "VariableDeclaration",
"overrides": null,

View File

@ -100,6 +100,7 @@
"attributes":
{
"constant": false,
"mutability": "mutable",
"name": "x",
"overrides": null,
"scope": 9,

View File

@ -3,5 +3,5 @@ contract C {
uint immutable constant x;
}
// ----
// ParserError: (32-41): Constantness already set to "immutable"
// ParserError: (64-72): Constantness already set to "immutable"
// ParserError: (32-41): Mutability already set to "immutable"
// ParserError: (64-72): Mutability already set to "immutable"