Implemented ASTJson import export for UserDefinedValueType

Also added a test.
This commit is contained in:
hrkrshnn 2021-08-17 17:34:23 +02:00
parent 5f393d1694
commit d67391531e
8 changed files with 677 additions and 0 deletions

View File

@ -354,6 +354,20 @@ bool ASTJsonConverter::visit(EnumValue const& _node)
return false;
}
bool ASTJsonConverter::visit(UserDefinedValueTypeDefinition const& _node)
{
solAssert(_node.underlyingType(), "");
std::vector<pair<string, Json::Value>> 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", {

View File

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

View File

@ -133,6 +133,8 @@ ASTPointer<ASTNode> 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<EnumValue> ASTJsonImporter::createEnumValue(Json::Value const& _node)
);
}
ASTPointer<UserDefinedValueTypeDefinition> ASTJsonImporter::createUserDefinedValueTypeDefinition(Json::Value const& _node)
{
return createASTNode<UserDefinedValueTypeDefinition>(
_node,
memberAsASTString(_node, "name"),
createNameSourceLocation(_node),
convertJsonToASTNode<TypeName>(member(_node, "underlyingType"))
);
}
ASTPointer<ParameterList> ASTJsonImporter::createParameterList(Json::Value const& _node)
{
std::vector<ASTPointer<VariableDeclaration>> parameters;

View File

@ -81,6 +81,7 @@ private:
ASTPointer<ASTNode> createStructDefinition(Json::Value const& _node);
ASTPointer<EnumDefinition> createEnumDefinition(Json::Value const& _node);
ASTPointer<EnumValue> createEnumValue(Json::Value const& _node);
ASTPointer<UserDefinedValueTypeDefinition> createUserDefinedValueTypeDefinition(Json::Value const& _node);
ASTPointer<ParameterList> createParameterList(Json::Value const& _node);
ASTPointer<OverrideSpecifier> createOverrideSpecifier(Json::Value const& _node);
ASTPointer<FunctionDefinition> createFunctionDefinition(Json::Value const& _node);

View File

@ -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"
// }
// ]

View File

@ -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"
}

View File

@ -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;
}
// ----

View File

@ -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"
}