Merge pull request #13877 from ethereum/add-key-name-source-location-for-mappings

Source locations for named mapping keys and values
This commit is contained in:
Daniel 2023-01-17 13:31:32 +01:00 committed by GitHub
commit 0d19b94286
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 177 additions and 31 deletions

View File

@ -1428,28 +1428,36 @@ public:
SourceLocation const& _location, SourceLocation const& _location,
ASTPointer<TypeName> _keyType, ASTPointer<TypeName> _keyType,
ASTPointer<ASTString> _keyName, ASTPointer<ASTString> _keyName,
SourceLocation _keyNameLocation,
ASTPointer<TypeName> _valueType, ASTPointer<TypeName> _valueType,
ASTPointer<ASTString> _valueName ASTPointer<ASTString> _valueName,
SourceLocation _valueNameLocation
): ):
TypeName(_id, _location), TypeName(_id, _location),
m_keyType(std::move(_keyType)), m_keyType(std::move(_keyType)),
m_keyName(std::move(_keyName)), m_keyName(std::move(_keyName)),
m_keyNameLocation(std::move(_keyNameLocation)),
m_valueType(std::move(_valueType)), m_valueType(std::move(_valueType)),
m_valueName(std::move(_valueName)) m_valueName(std::move(_valueName)),
m_valueNameLocation(std::move(_valueNameLocation))
{} {}
void accept(ASTVisitor& _visitor) override; void accept(ASTVisitor& _visitor) override;
void accept(ASTConstVisitor& _visitor) const override; void accept(ASTConstVisitor& _visitor) const override;
TypeName const& keyType() const { return *m_keyType; } TypeName const& keyType() const { return *m_keyType; }
ASTString keyName() const { return *m_keyName; } ASTString keyName() const { return *m_keyName; }
SourceLocation keyNameLocation() const { return m_keyNameLocation; }
TypeName const& valueType() const { return *m_valueType; } TypeName const& valueType() const { return *m_valueType; }
ASTString valueName() const { return *m_valueName; } ASTString valueName() const { return *m_valueName; }
SourceLocation valueNameLocation() const { return m_valueNameLocation; }
private: private:
ASTPointer<TypeName> m_keyType; ASTPointer<TypeName> m_keyType;
ASTPointer<ASTString> m_keyName; ASTPointer<ASTString> m_keyName;
SourceLocation m_keyNameLocation;
ASTPointer<TypeName> m_valueType; ASTPointer<TypeName> m_valueType;
ASTPointer<ASTString> m_valueName; ASTPointer<ASTString> m_valueName;
SourceLocation m_valueNameLocation;
}; };
/** /**

View File

@ -599,8 +599,10 @@ bool ASTJsonExporter::visit(Mapping const& _node)
setJsonNode(_node, "Mapping", { setJsonNode(_node, "Mapping", {
make_pair("keyType", toJson(_node.keyType())), make_pair("keyType", toJson(_node.keyType())),
make_pair("keyName", _node.keyName()), make_pair("keyName", _node.keyName()),
make_pair("keyNameLocation", sourceLocationToString(_node.keyNameLocation())),
make_pair("valueType", toJson(_node.valueType())), make_pair("valueType", toJson(_node.valueType())),
make_pair("valueName", _node.valueName()), make_pair("valueName", _node.valueName()),
make_pair("valueNameLocation", sourceLocationToString(_node.valueNameLocation())),
make_pair("typeDescriptions", typePointerToJson(_node.annotation().type, true)) make_pair("typeDescriptions", typePointerToJson(_node.annotation().type, true))
}); });
return false; return false;

View File

@ -116,6 +116,20 @@ SourceLocation ASTJsonImporter::createNameSourceLocation(Json::Value const& _nod
return solidity::langutil::parseSourceLocation(_node["nameLocation"].asString(), m_sourceNames); return solidity::langutil::parseSourceLocation(_node["nameLocation"].asString(), m_sourceNames);
} }
SourceLocation ASTJsonImporter::createKeyNameSourceLocation(Json::Value const& _node)
{
astAssert(member(_node, "keyNameLocation").isString(), "'keyNameLocation' must be a string");
return solidity::langutil::parseSourceLocation(_node["keyNameLocation"].asString(), m_sourceNames);
}
SourceLocation ASTJsonImporter::createValueNameSourceLocation(Json::Value const& _node)
{
astAssert(member(_node, "valueNameLocation").isString(), "'valueNameLocation' must be a string");
return solidity::langutil::parseSourceLocation(_node["valueNameLocation"].asString(), m_sourceNames);
}
template<class T> template<class T>
ASTPointer<T> ASTJsonImporter::convertJsonToASTNode(Json::Value const& _node) ASTPointer<T> ASTJsonImporter::convertJsonToASTNode(Json::Value const& _node)
{ {
@ -649,8 +663,10 @@ ASTPointer<Mapping> ASTJsonImporter::createMapping(Json::Value const& _node)
_node, _node,
convertJsonToASTNode<TypeName>(member(_node, "keyType")), convertJsonToASTNode<TypeName>(member(_node, "keyType")),
memberAsASTString(_node, "keyName"), memberAsASTString(_node, "keyName"),
createKeyNameSourceLocation(_node),
convertJsonToASTNode<TypeName>(member(_node, "valueType")), convertJsonToASTNode<TypeName>(member(_node, "valueType")),
memberAsASTString(_node, "valueName") memberAsASTString(_node, "valueName"),
createValueNameSourceLocation(_node)
); );
} }

View File

@ -69,6 +69,10 @@ private:
ASTPointer<T> convertJsonToASTNode(Json::Value const& _node); ASTPointer<T> convertJsonToASTNode(Json::Value const& _node);
langutil::SourceLocation createNameSourceLocation(Json::Value const& _node); langutil::SourceLocation createNameSourceLocation(Json::Value const& _node);
/// @returns source location of a mapping key name
langutil::SourceLocation createKeyNameSourceLocation(Json::Value const& _node);
/// @returns source location of a mapping value name
langutil::SourceLocation createValueNameSourceLocation(Json::Value const& _node);
/// \defgroup nodeCreators JSON to AST-Nodes /// \defgroup nodeCreators JSON to AST-Nodes
///@{ ///@{

View File

@ -1187,21 +1187,19 @@ ASTPointer<Mapping> Parser::parseMapping()
} }
else else
fatalParserError(1005_error, "Expected elementary type name or identifier for mapping key type"); fatalParserError(1005_error, "Expected elementary type name or identifier for mapping key type");
ASTPointer<ASTString> keyName; ASTPointer<ASTString> keyName = make_shared<ASTString>("");
SourceLocation keyNameLocation{};
if (m_scanner->currentToken() == Token::Identifier) if (m_scanner->currentToken() == Token::Identifier)
keyName = getLiteralAndAdvance(); tie(keyName, keyNameLocation) = expectIdentifierWithLocation();
else
keyName = make_shared<ASTString>("");
expectToken(Token::DoubleArrow); expectToken(Token::DoubleArrow);
ASTPointer<TypeName> valueType = parseTypeName(); ASTPointer<TypeName> valueType = parseTypeName();
ASTPointer<ASTString> valueName; ASTPointer<ASTString> valueName = make_shared<ASTString>("");
SourceLocation valueNameLocation{};
if (m_scanner->currentToken() == Token::Identifier) if (m_scanner->currentToken() == Token::Identifier)
valueName = getLiteralAndAdvance(); tie(valueName, valueNameLocation) = expectIdentifierWithLocation();
else
valueName = make_shared<ASTString>("");
nodeFactory.markEndPosition(); nodeFactory.markEndPosition();
expectToken(Token::RParen); expectToken(Token::RParen);
return nodeFactory.createNode<Mapping>(keyType, keyName, valueType, valueName); return nodeFactory.createNode<Mapping>(keyType, keyName, keyNameLocation, valueType, valueName, valueNameLocation);
} }
ASTPointer<ParameterList> Parser::parseParameterList( ASTPointer<ParameterList> Parser::parseParameterList(

View File

@ -49,6 +49,7 @@
{ {
"id": 3, "id": 3,
"keyName": "", "keyName": "",
"keyNameLocation": "-1:-1:-1",
"keyType": "keyType":
{ {
"id": 1, "id": 1,
@ -69,6 +70,7 @@
"typeString": "mapping(address => address payable)" "typeString": "mapping(address => address payable)"
}, },
"valueName": "", "valueName": "",
"valueNameLocation": "-1:-1:-1",
"valueType": "valueType":
{ {
"id": 2, "id": 2,

View File

@ -30,6 +30,7 @@
{ {
"id": 3, "id": 3,
"keyName": "", "keyName": "",
"keyNameLocation": "-1:-1:-1",
"keyType": "keyType":
{ {
"id": 1, "id": 1,
@ -42,6 +43,7 @@
"src": "17:35:1", "src": "17:35:1",
"typeDescriptions": {}, "typeDescriptions": {},
"valueName": "", "valueName": "",
"valueNameLocation": "-1:-1:-1",
"valueType": "valueType":
{ {
"id": 2, "id": 2,

View File

@ -4,10 +4,10 @@
{ {
"C": "C":
[ [
19 23
] ]
}, },
"id": 20, "id": 24,
"nodeType": "SourceUnit", "nodeType": "SourceUnit",
"nodes": "nodes":
[ [
@ -18,10 +18,10 @@
"contractDependencies": [], "contractDependencies": [],
"contractKind": "contract", "contractKind": "contract",
"fullyImplemented": true, "fullyImplemented": true,
"id": 19, "id": 23,
"linearizedBaseContracts": "linearizedBaseContracts":
[ [
19 23
], ],
"name": "C", "name": "C",
"nameLocation": "9:1:1", "nameLocation": "9:1:1",
@ -67,19 +67,20 @@
"name": "a", "name": "a",
"nameLocation": "59:1:1", "nameLocation": "59:1:1",
"nodeType": "VariableDeclaration", "nodeType": "VariableDeclaration",
"scope": 19, "scope": 23,
"src": "40:20:1", "src": "40:20:1",
"stateVariable": true, "stateVariable": true,
"storageLocation": "default", "storageLocation": "default",
"typeDescriptions": "typeDescriptions":
{ {
"typeIdentifier": "t_mapping$_t_contract$_C_$19_$_t_bool_$", "typeIdentifier": "t_mapping$_t_contract$_C_$23_$_t_bool_$",
"typeString": "mapping(contract C => bool)" "typeString": "mapping(contract C => bool)"
}, },
"typeName": "typeName":
{ {
"id": 8, "id": 8,
"keyName": "", "keyName": "",
"keyNameLocation": "-1:-1:-1",
"keyType": "keyType":
{ {
"id": 6, "id": 6,
@ -93,14 +94,14 @@
"48:1:1" "48:1:1"
], ],
"nodeType": "IdentifierPath", "nodeType": "IdentifierPath",
"referencedDeclaration": 19, "referencedDeclaration": 23,
"src": "48:1:1" "src": "48:1:1"
}, },
"referencedDeclaration": 19, "referencedDeclaration": 23,
"src": "48:1:1", "src": "48:1:1",
"typeDescriptions": "typeDescriptions":
{ {
"typeIdentifier": "t_contract$_C_$19", "typeIdentifier": "t_contract$_C_$23",
"typeString": "contract C" "typeString": "contract C"
} }
}, },
@ -108,10 +109,11 @@
"src": "40:18:1", "src": "40:18:1",
"typeDescriptions": "typeDescriptions":
{ {
"typeIdentifier": "t_mapping$_t_contract$_C_$19_$_t_bool_$", "typeIdentifier": "t_mapping$_t_contract$_C_$23_$_t_bool_$",
"typeString": "mapping(contract C => bool)" "typeString": "mapping(contract C => bool)"
}, },
"valueName": "", "valueName": "",
"valueNameLocation": "-1:-1:-1",
"valueType": "valueType":
{ {
"id": 7, "id": 7,
@ -134,7 +136,7 @@
"name": "b", "name": "b",
"nameLocation": "91:1:1", "nameLocation": "91:1:1",
"nodeType": "VariableDeclaration", "nodeType": "VariableDeclaration",
"scope": 19, "scope": 23,
"src": "66:26:1", "src": "66:26:1",
"stateVariable": true, "stateVariable": true,
"storageLocation": "default", "storageLocation": "default",
@ -147,6 +149,7 @@
{ {
"id": 12, "id": 12,
"keyName": "", "keyName": "",
"keyNameLocation": "-1:-1:-1",
"keyType": "keyType":
{ {
"id": 10, "id": 10,
@ -167,6 +170,7 @@
"typeString": "mapping(address => bool)" "typeString": "mapping(address => bool)"
}, },
"valueName": "", "valueName": "",
"valueNameLocation": "-1:-1:-1",
"valueType": "valueType":
{ {
"id": 11, "id": 11,
@ -189,7 +193,7 @@
"name": "c", "name": "c",
"nameLocation": "117:1:1", "nameLocation": "117:1:1",
"nodeType": "VariableDeclaration", "nodeType": "VariableDeclaration",
"scope": 19, "scope": 23,
"src": "98:20:1", "src": "98:20:1",
"stateVariable": true, "stateVariable": true,
"storageLocation": "default", "storageLocation": "default",
@ -202,6 +206,7 @@
{ {
"id": 17, "id": 17,
"keyName": "", "keyName": "",
"keyNameLocation": "-1:-1:-1",
"keyType": "keyType":
{ {
"id": 15, "id": 15,
@ -234,6 +239,7 @@
"typeString": "mapping(enum C.E => bool)" "typeString": "mapping(enum C.E => bool)"
}, },
"valueName": "", "valueName": "",
"valueNameLocation": "-1:-1:-1",
"valueType": "valueType":
{ {
"id": 16, "id": 16,
@ -248,12 +254,69 @@
} }
}, },
"visibility": "internal" "visibility": "internal"
},
{
"constant": false,
"id": 22,
"mutability": "mutable",
"name": "d",
"nameLocation": "169:1:1",
"nodeType": "VariableDeclaration",
"scope": 23,
"src": "124:46:1",
"stateVariable": true,
"storageLocation": "default",
"typeDescriptions":
{
"typeIdentifier": "t_mapping$_t_address_$_t_uint256_$",
"typeString": "mapping(address => uint256)"
},
"typeName":
{
"id": 21,
"keyName": "keyAddress",
"keyNameLocation": "140:10:1",
"keyType":
{
"id": 19,
"name": "address",
"nodeType": "ElementaryTypeName",
"src": "132:7:1",
"typeDescriptions":
{
"typeIdentifier": "t_address",
"typeString": "address"
}
},
"nodeType": "Mapping",
"src": "124:44:1",
"typeDescriptions":
{
"typeIdentifier": "t_mapping$_t_address_$_t_uint256_$",
"typeString": "mapping(address => uint256)"
},
"valueName": "value",
"valueNameLocation": "162:5:1",
"valueType":
{
"id": 20,
"name": "uint256",
"nodeType": "ElementaryTypeName",
"src": "154:7:1",
"typeDescriptions":
{
"typeIdentifier": "t_uint256",
"typeString": "uint256"
}
}
},
"visibility": "internal"
} }
], ],
"scope": 20, "scope": 24,
"src": "0:121:1", "src": "0:173:1",
"usedErrors": [] "usedErrors": []
} }
], ],
"src": "0:122:1" "src": "0:174:1"
} }

View File

@ -3,6 +3,7 @@ contract C {
mapping(C => bool) a; mapping(C => bool) a;
mapping(address => bool) b; mapping(address => bool) b;
mapping(E => bool) c; mapping(E => bool) c;
mapping(address keyAddress => uint256 value) d;
} }
// ---- // ----

View File

@ -1,6 +1,6 @@
{ {
"absolutePath": "a", "absolutePath": "a",
"id": 20, "id": 24,
"nodeType": "SourceUnit", "nodeType": "SourceUnit",
"nodes": "nodes":
[ [
@ -9,7 +9,7 @@
"baseContracts": [], "baseContracts": [],
"contractDependencies": [], "contractDependencies": [],
"contractKind": "contract", "contractKind": "contract",
"id": 19, "id": 23,
"name": "C", "name": "C",
"nameLocation": "9:1:1", "nameLocation": "9:1:1",
"nodeType": "ContractDefinition", "nodeType": "ContractDefinition",
@ -61,6 +61,7 @@
{ {
"id": 8, "id": 8,
"keyName": "", "keyName": "",
"keyNameLocation": "-1:-1:-1",
"keyType": "keyType":
{ {
"id": 6, "id": 6,
@ -83,6 +84,7 @@
"src": "40:18:1", "src": "40:18:1",
"typeDescriptions": {}, "typeDescriptions": {},
"valueName": "", "valueName": "",
"valueNameLocation": "-1:-1:-1",
"valueType": "valueType":
{ {
"id": 7, "id": 7,
@ -109,6 +111,7 @@
{ {
"id": 12, "id": 12,
"keyName": "", "keyName": "",
"keyNameLocation": "-1:-1:-1",
"keyType": "keyType":
{ {
"id": 10, "id": 10,
@ -121,6 +124,7 @@
"src": "66:24:1", "src": "66:24:1",
"typeDescriptions": {}, "typeDescriptions": {},
"valueName": "", "valueName": "",
"valueNameLocation": "-1:-1:-1",
"valueType": "valueType":
{ {
"id": 11, "id": 11,
@ -147,6 +151,7 @@
{ {
"id": 17, "id": 17,
"keyName": "", "keyName": "",
"keyNameLocation": "-1:-1:-1",
"keyType": "keyType":
{ {
"id": 15, "id": 15,
@ -169,6 +174,7 @@
"src": "98:18:1", "src": "98:18:1",
"typeDescriptions": {}, "typeDescriptions": {},
"valueName": "", "valueName": "",
"valueNameLocation": "-1:-1:-1",
"valueType": "valueType":
{ {
"id": 16, "id": 16,
@ -179,11 +185,51 @@
} }
}, },
"visibility": "internal" "visibility": "internal"
},
{
"constant": false,
"id": 22,
"mutability": "mutable",
"name": "d",
"nameLocation": "169:1:1",
"nodeType": "VariableDeclaration",
"src": "124:46:1",
"stateVariable": false,
"storageLocation": "default",
"typeDescriptions": {},
"typeName":
{
"id": 21,
"keyName": "keyAddress",
"keyNameLocation": "140:10:1",
"keyType":
{
"id": 19,
"name": "address",
"nodeType": "ElementaryTypeName",
"src": "132:7:1",
"typeDescriptions": {}
},
"nodeType": "Mapping",
"src": "124:44:1",
"typeDescriptions": {},
"valueName": "value",
"valueNameLocation": "162:5:1",
"valueType":
{
"id": 20,
"name": "uint256",
"nodeType": "ElementaryTypeName",
"src": "154:7:1",
"typeDescriptions": {}
}
},
"visibility": "internal"
} }
], ],
"src": "0:121:1", "src": "0:173:1",
"usedErrors": [] "usedErrors": []
} }
], ],
"src": "0:122:1" "src": "0:174:1"
} }

View File

@ -288,6 +288,7 @@
{ {
"id": 25, "id": 25,
"keyName": "", "keyName": "",
"keyNameLocation": "-1:-1:-1",
"keyType": "keyType":
{ {
"id": 22, "id": 22,
@ -320,6 +321,7 @@
"typeString": "mapping(C.MyAddress => C.MyUInt)" "typeString": "mapping(C.MyAddress => C.MyUInt)"
}, },
"valueName": "", "valueName": "",
"valueNameLocation": "-1:-1:-1",
"valueType": "valueType":
{ {
"id": 24, "id": 24,

View File

@ -214,6 +214,7 @@
{ {
"id": 25, "id": 25,
"keyName": "", "keyName": "",
"keyNameLocation": "-1:-1:-1",
"keyType": "keyType":
{ {
"id": 22, "id": 22,
@ -236,6 +237,7 @@
"src": "169:28:1", "src": "169:28:1",
"typeDescriptions": {}, "typeDescriptions": {},
"valueName": "", "valueName": "",
"valueNameLocation": "-1:-1:-1",
"valueType": "valueType":
{ {
"id": 24, "id": 24,