From d67391531e7a67ad6b51771d20c8b977fc35daee Mon Sep 17 00:00:00 2001 From: hrkrshnn Date: Tue, 17 Aug 2021 17:34:23 +0200 Subject: [PATCH] Implemented ASTJson import export for UserDefinedValueType Also added a test. --- libsolidity/ast/ASTJsonConverter.cpp | 14 + libsolidity/ast/ASTJsonConverter.h | 1 + libsolidity/ast/ASTJsonImporter.cpp | 12 + libsolidity/ast/ASTJsonImporter.h | 1 + .../ABIJson/user_defined_value_type.sol | 173 ++++++++++++ .../ASTJSON/userDefinedValueType.json | 264 ++++++++++++++++++ .../ASTJSON/userDefinedValueType.sol | 12 + .../userDefinedValueType_parseOnly.json | 200 +++++++++++++ 8 files changed, 677 insertions(+) create mode 100644 test/libsolidity/ABIJson/user_defined_value_type.sol create mode 100644 test/libsolidity/ASTJSON/userDefinedValueType.json create mode 100644 test/libsolidity/ASTJSON/userDefinedValueType.sol create mode 100644 test/libsolidity/ASTJSON/userDefinedValueType_parseOnly.json diff --git a/libsolidity/ast/ASTJsonConverter.cpp b/libsolidity/ast/ASTJsonConverter.cpp index e009bc8cb..f8c284483 100644 --- a/libsolidity/ast/ASTJsonConverter.cpp +++ b/libsolidity/ast/ASTJsonConverter.cpp @@ -354,6 +354,20 @@ bool ASTJsonConverter::visit(EnumValue const& _node) return false; } +bool ASTJsonConverter::visit(UserDefinedValueTypeDefinition const& _node) +{ + solAssert(_node.underlyingType(), ""); + std::vector> attributes = { + make_pair("name", _node.name()), + make_pair("nameLocation", sourceLocationToString(_node.nameLocation())), + make_pair("underlyingType", toJson(*_node.underlyingType())) + }; + + setJsonNode(_node, "UserDefinedValueTypeDefinition", std::move(attributes)); + + return false; +} + bool ASTJsonConverter::visit(ParameterList const& _node) { setJsonNode(_node, "ParameterList", { diff --git a/libsolidity/ast/ASTJsonConverter.h b/libsolidity/ast/ASTJsonConverter.h index 34903c7a1..56fdb1304 100644 --- a/libsolidity/ast/ASTJsonConverter.h +++ b/libsolidity/ast/ASTJsonConverter.h @@ -81,6 +81,7 @@ public: bool visit(StructDefinition const& _node) override; bool visit(EnumDefinition const& _node) override; bool visit(EnumValue const& _node) override; + bool visit(UserDefinedValueTypeDefinition const& _node) override; bool visit(ParameterList const& _node) override; bool visit(OverrideSpecifier const& _node) override; bool visit(FunctionDefinition const& _node) override; diff --git a/libsolidity/ast/ASTJsonImporter.cpp b/libsolidity/ast/ASTJsonImporter.cpp index 5d7ba1211..dd928cc03 100644 --- a/libsolidity/ast/ASTJsonImporter.cpp +++ b/libsolidity/ast/ASTJsonImporter.cpp @@ -133,6 +133,8 @@ ASTPointer ASTJsonImporter::convertJsonToASTNode(Json::Value const& _js return createEnumDefinition(_json); if (nodeType == "EnumValue") return createEnumValue(_json); + if (nodeType == "UserDefinedValueTypeDefinition") + return createUserDefinedValueTypeDefinition(_json); if (nodeType == "ParameterList") return createParameterList(_json); if (nodeType == "OverrideSpecifier") @@ -387,6 +389,16 @@ ASTPointer ASTJsonImporter::createEnumValue(Json::Value const& _node) ); } +ASTPointer ASTJsonImporter::createUserDefinedValueTypeDefinition(Json::Value const& _node) +{ + return createASTNode( + _node, + memberAsASTString(_node, "name"), + createNameSourceLocation(_node), + convertJsonToASTNode(member(_node, "underlyingType")) + ); +} + ASTPointer ASTJsonImporter::createParameterList(Json::Value const& _node) { std::vector> parameters; diff --git a/libsolidity/ast/ASTJsonImporter.h b/libsolidity/ast/ASTJsonImporter.h index 3f1f5265c..4566a58a4 100644 --- a/libsolidity/ast/ASTJsonImporter.h +++ b/libsolidity/ast/ASTJsonImporter.h @@ -81,6 +81,7 @@ private: ASTPointer createStructDefinition(Json::Value const& _node); ASTPointer createEnumDefinition(Json::Value const& _node); ASTPointer createEnumValue(Json::Value const& _node); + ASTPointer createUserDefinedValueTypeDefinition(Json::Value const& _node); ASTPointer createParameterList(Json::Value const& _node); ASTPointer createOverrideSpecifier(Json::Value const& _node); ASTPointer createFunctionDefinition(Json::Value const& _node); diff --git a/test/libsolidity/ABIJson/user_defined_value_type.sol b/test/libsolidity/ABIJson/user_defined_value_type.sol new file mode 100644 index 000000000..da10f7c44 --- /dev/null +++ b/test/libsolidity/ABIJson/user_defined_value_type.sol @@ -0,0 +1,173 @@ +type MyInt is int; +type MyByte1 is bytes1; +contract C { + type MyAddress is address; + type MyUInt8 is uint8; + type MyBytes32 is bytes32; + + MyInt public myInt; + MyByte1 public myByte1; + MyAddress public myAddress; + MyUInt8 public myUInt8; + MyBytes32 public myBytes32; + + function setMyInt(MyInt a) external { + myInt = a; + } + function setMyByte1(MyByte1 a) external { + myByte1 = a; + } + function setMyAddress(MyAddress a) external { + myAddress = a; + } + function setMyUInt8(MyUInt8 a) external { + myUInt8 = a; + } + function setMyBytes32(MyBytes32 a) external { + myBytes32 = a; + } +} +// ---- +// :C +// [ +// { +// "inputs": [], +// "name": "myAddress", +// "outputs": +// [ +// { +// "internalType": "user defined type MyAddress", +// "name": "", +// "type": "address" +// } +// ], +// "stateMutability": "view", +// "type": "function" +// }, +// { +// "inputs": [], +// "name": "myByte1", +// "outputs": +// [ +// { +// "internalType": "user defined type MyByte1", +// "name": "", +// "type": "bytes1" +// } +// ], +// "stateMutability": "view", +// "type": "function" +// }, +// { +// "inputs": [], +// "name": "myBytes32", +// "outputs": +// [ +// { +// "internalType": "user defined type MyBytes32", +// "name": "", +// "type": "bytes32" +// } +// ], +// "stateMutability": "view", +// "type": "function" +// }, +// { +// "inputs": [], +// "name": "myInt", +// "outputs": +// [ +// { +// "internalType": "user defined type MyInt", +// "name": "", +// "type": "int256" +// } +// ], +// "stateMutability": "view", +// "type": "function" +// }, +// { +// "inputs": [], +// "name": "myUInt8", +// "outputs": +// [ +// { +// "internalType": "user defined type MyUInt8", +// "name": "", +// "type": "uint8" +// } +// ], +// "stateMutability": "view", +// "type": "function" +// }, +// { +// "inputs": +// [ +// { +// "internalType": "user defined type MyAddress", +// "name": "a", +// "type": "address" +// } +// ], +// "name": "setMyAddress", +// "outputs": [], +// "stateMutability": "nonpayable", +// "type": "function" +// }, +// { +// "inputs": +// [ +// { +// "internalType": "user defined type MyByte1", +// "name": "a", +// "type": "bytes1" +// } +// ], +// "name": "setMyByte1", +// "outputs": [], +// "stateMutability": "nonpayable", +// "type": "function" +// }, +// { +// "inputs": +// [ +// { +// "internalType": "user defined type MyBytes32", +// "name": "a", +// "type": "bytes32" +// } +// ], +// "name": "setMyBytes32", +// "outputs": [], +// "stateMutability": "nonpayable", +// "type": "function" +// }, +// { +// "inputs": +// [ +// { +// "internalType": "user defined type MyInt", +// "name": "a", +// "type": "int256" +// } +// ], +// "name": "setMyInt", +// "outputs": [], +// "stateMutability": "nonpayable", +// "type": "function" +// }, +// { +// "inputs": +// [ +// { +// "internalType": "user defined type MyUInt8", +// "name": "a", +// "type": "uint8" +// } +// ], +// "name": "setMyUInt8", +// "outputs": [], +// "stateMutability": "nonpayable", +// "type": "function" +// } +// ] diff --git a/test/libsolidity/ASTJSON/userDefinedValueType.json b/test/libsolidity/ASTJSON/userDefinedValueType.json new file mode 100644 index 000000000..09b81a4d4 --- /dev/null +++ b/test/libsolidity/ASTJSON/userDefinedValueType.json @@ -0,0 +1,264 @@ +{ + "absolutePath": "a", + "exportedSymbols": + { + "C": + [ + 21 + ], + "MyAddress": + [ + 2 + ], + "MyUInt": + [ + 4 + ], + "f": + [ + 16 + ] + }, + "id": 22, + "nodeType": "SourceUnit", + "nodes": + [ + { + "id": 2, + "name": "MyAddress", + "nameLocation": "5:9:1", + "nodeType": "UserDefinedValueTypeDefinition", + "src": "0:26:1", + "underlyingType": + { + "id": 1, + "name": "address", + "nodeType": "ElementaryTypeName", + "src": "18:7:1", + "stateMutability": "nonpayable", + "typeDescriptions": + { + "typeIdentifier": "t_address", + "typeString": "address" + } + } + }, + { + "id": 4, + "name": "MyUInt", + "nameLocation": "32:6:1", + "nodeType": "UserDefinedValueTypeDefinition", + "src": "27:20:1", + "underlyingType": + { + "id": 3, + "name": "uint", + "nodeType": "ElementaryTypeName", + "src": "42:4:1", + "typeDescriptions": + { + "typeIdentifier": "t_uint256", + "typeString": "uint256" + } + } + }, + { + "body": + { + "id": 15, + "nodeType": "Block", + "src": "61:34:1", + "statements": + [ + { + "assignments": + [ + 9 + ], + "declarations": + [ + { + "constant": false, + "id": 9, + "mutability": "mutable", + "name": "a", + "nameLocation": "77:1:1", + "nodeType": "VariableDeclaration", + "scope": 15, + "src": "67:11:1", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": + { + "typeIdentifier": "t_userDefinedValueType$_MyAddress_$2", + "typeString": "user defined type MyAddress" + }, + "typeName": + { + "id": 8, + "nodeType": "UserDefinedTypeName", + "pathNode": + { + "id": 7, + "name": "MyAddress", + "nodeType": "IdentifierPath", + "referencedDeclaration": 2, + "src": "67:9:1" + }, + "referencedDeclaration": 2, + "src": "67:9:1", + "typeDescriptions": + { + "typeIdentifier": "t_userDefinedValueType$_MyAddress_$2", + "typeString": "user defined type MyAddress" + } + }, + "visibility": "internal" + } + ], + "id": 10, + "nodeType": "VariableDeclarationStatement", + "src": "67:11:1" + }, + { + "assignments": + [ + 13 + ], + "declarations": + [ + { + "constant": false, + "id": 13, + "mutability": "mutable", + "name": "b", + "nameLocation": "91:1:1", + "nodeType": "VariableDeclaration", + "scope": 15, + "src": "84:8:1", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": + { + "typeIdentifier": "t_userDefinedValueType$_MyUInt_$4", + "typeString": "user defined type MyUInt" + }, + "typeName": + { + "id": 12, + "nodeType": "UserDefinedTypeName", + "pathNode": + { + "id": 11, + "name": "MyUInt", + "nodeType": "IdentifierPath", + "referencedDeclaration": 4, + "src": "84:6:1" + }, + "referencedDeclaration": 4, + "src": "84:6:1", + "typeDescriptions": + { + "typeIdentifier": "t_userDefinedValueType$_MyUInt_$4", + "typeString": "user defined type MyUInt" + } + }, + "visibility": "internal" + } + ], + "id": 14, + "nodeType": "VariableDeclarationStatement", + "src": "84:8:1" + } + ] + }, + "id": 16, + "implemented": true, + "kind": "freeFunction", + "modifiers": [], + "name": "f", + "nameLocation": "57:1:1", + "nodeType": "FunctionDefinition", + "parameters": + { + "id": 5, + "nodeType": "ParameterList", + "parameters": [], + "src": "58:2:1" + }, + "returnParameters": + { + "id": 6, + "nodeType": "ParameterList", + "parameters": [], + "src": "61:0:1" + }, + "scope": 22, + "src": "48:47:1", + "stateMutability": "nonpayable", + "virtual": false, + "visibility": "internal" + }, + { + "abstract": false, + "baseContracts": [], + "contractDependencies": [], + "contractKind": "contract", + "fullyImplemented": true, + "id": 21, + "linearizedBaseContracts": + [ + 21 + ], + "name": "C", + "nameLocation": "105:1:1", + "nodeType": "ContractDefinition", + "nodes": + [ + { + "id": 18, + "name": "MyAddress", + "nameLocation": "118:9:1", + "nodeType": "UserDefinedValueTypeDefinition", + "src": "113:26:1", + "underlyingType": + { + "id": 17, + "name": "address", + "nodeType": "ElementaryTypeName", + "src": "131:7:1", + "stateMutability": "nonpayable", + "typeDescriptions": + { + "typeIdentifier": "t_address", + "typeString": "address" + } + } + }, + { + "id": 20, + "name": "MyUInt", + "nameLocation": "149:6:1", + "nodeType": "UserDefinedValueTypeDefinition", + "src": "144:20:1", + "underlyingType": + { + "id": 19, + "name": "uint", + "nodeType": "ElementaryTypeName", + "src": "159:4:1", + "typeDescriptions": + { + "typeIdentifier": "t_uint256", + "typeString": "uint256" + } + } + } + ], + "scope": 22, + "src": "96:70:1", + "usedErrors": [] + } + ], + "src": "0:167:1" +} diff --git a/test/libsolidity/ASTJSON/userDefinedValueType.sol b/test/libsolidity/ASTJSON/userDefinedValueType.sol new file mode 100644 index 000000000..cc179b118 --- /dev/null +++ b/test/libsolidity/ASTJSON/userDefinedValueType.sol @@ -0,0 +1,12 @@ +type MyAddress is address; +type MyUInt is uint; +function f() { + MyAddress a; + MyUInt b; +} +contract C { + type MyAddress is address; + type MyUInt is uint; +} + +// ---- diff --git a/test/libsolidity/ASTJSON/userDefinedValueType_parseOnly.json b/test/libsolidity/ASTJSON/userDefinedValueType_parseOnly.json new file mode 100644 index 000000000..1dd4b0edd --- /dev/null +++ b/test/libsolidity/ASTJSON/userDefinedValueType_parseOnly.json @@ -0,0 +1,200 @@ +{ + "absolutePath": "a", + "id": 22, + "nodeType": "SourceUnit", + "nodes": + [ + { + "id": 2, + "name": "MyAddress", + "nameLocation": "5:9:1", + "nodeType": "UserDefinedValueTypeDefinition", + "src": "0:26:1", + "underlyingType": + { + "id": 1, + "name": "address", + "nodeType": "ElementaryTypeName", + "src": "18:7:1", + "stateMutability": "nonpayable", + "typeDescriptions": {} + } + }, + { + "id": 4, + "name": "MyUInt", + "nameLocation": "32:6:1", + "nodeType": "UserDefinedValueTypeDefinition", + "src": "27:20:1", + "underlyingType": + { + "id": 3, + "name": "uint", + "nodeType": "ElementaryTypeName", + "src": "42:4:1", + "typeDescriptions": {} + } + }, + { + "body": + { + "id": 15, + "nodeType": "Block", + "src": "61:34:1", + "statements": + [ + { + "assignments": + [ + 9 + ], + "declarations": + [ + { + "constant": false, + "id": 9, + "mutability": "mutable", + "name": "a", + "nameLocation": "77:1:1", + "nodeType": "VariableDeclaration", + "src": "67:11:1", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": {}, + "typeName": + { + "id": 8, + "nodeType": "UserDefinedTypeName", + "pathNode": + { + "id": 7, + "name": "MyAddress", + "nodeType": "IdentifierPath", + "src": "67:9:1" + }, + "src": "67:9:1", + "typeDescriptions": {} + }, + "visibility": "internal" + } + ], + "id": 10, + "nodeType": "VariableDeclarationStatement", + "src": "67:11:1" + }, + { + "assignments": + [ + 13 + ], + "declarations": + [ + { + "constant": false, + "id": 13, + "mutability": "mutable", + "name": "b", + "nameLocation": "91:1:1", + "nodeType": "VariableDeclaration", + "src": "84:8:1", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": {}, + "typeName": + { + "id": 12, + "nodeType": "UserDefinedTypeName", + "pathNode": + { + "id": 11, + "name": "MyUInt", + "nodeType": "IdentifierPath", + "src": "84:6:1" + }, + "src": "84:6:1", + "typeDescriptions": {} + }, + "visibility": "internal" + } + ], + "id": 14, + "nodeType": "VariableDeclarationStatement", + "src": "84:8:1" + } + ] + }, + "id": 16, + "implemented": true, + "kind": "freeFunction", + "modifiers": [], + "name": "f", + "nameLocation": "57:1:1", + "nodeType": "FunctionDefinition", + "parameters": + { + "id": 5, + "nodeType": "ParameterList", + "parameters": [], + "src": "58:2:1" + }, + "returnParameters": + { + "id": 6, + "nodeType": "ParameterList", + "parameters": [], + "src": "61:0:1" + }, + "src": "48:47:1", + "stateMutability": "nonpayable", + "virtual": false, + "visibility": "internal" + }, + { + "abstract": false, + "baseContracts": [], + "contractDependencies": [], + "contractKind": "contract", + "id": 21, + "name": "C", + "nameLocation": "105:1:1", + "nodeType": "ContractDefinition", + "nodes": + [ + { + "id": 18, + "name": "MyAddress", + "nameLocation": "118:9:1", + "nodeType": "UserDefinedValueTypeDefinition", + "src": "113:26:1", + "underlyingType": + { + "id": 17, + "name": "address", + "nodeType": "ElementaryTypeName", + "src": "131:7:1", + "stateMutability": "nonpayable", + "typeDescriptions": {} + } + }, + { + "id": 20, + "name": "MyUInt", + "nameLocation": "149:6:1", + "nodeType": "UserDefinedValueTypeDefinition", + "src": "144:20:1", + "underlyingType": + { + "id": 19, + "name": "uint", + "nodeType": "ElementaryTypeName", + "src": "159:4:1", + "typeDescriptions": {} + } + } + ], + "src": "96:70:1", + "usedErrors": [] + } + ], + "src": "0:167:1" +}