Commit be7805e0 authored by aniket-engg's avatar aniket-engg Committed by Aniket

common unit tests updated

parent 0569590c
import { getStateVariableDeclarationsFormContractNode,
import { getStateVariableDeclarationsFromContractNode,
getInheritsFromName, getContractName,
getFunctionOrModifierDefinitionParameterPart, getType, getDeclaredVariableName,
getFunctionDefinitionReturnParameterPart } from './staticAnalysisCommon'
......@@ -57,7 +57,7 @@ export default class abstractAstView {
relevantNodes: [],
modifiers: [],
inheritsFrom: [],
stateVariables: getStateVariableDeclarationsFormContractNode(node)
stateVariables: getStateVariableDeclarationsFromContractNode(node)
})
} else if (node.nodeType === "InheritanceSpecifier") {
const currentContract = that.getCurrentContract(that)
......
......@@ -47,7 +47,7 @@ const basicRegex = {
FUNCTIONTYPE: '^function \\(',
EXTERNALFUNCTIONTYPE: '^function \\(.*\\).* external',
CONSTANTFUNCTIONTYPE: '^function \\(.*\\).* (view|pure)',
REFTYPE: '( storage )|(mapping\\()|(\\[\\])',
REFTYPE: '(storage)|(mapping\\()|(\\[\\])',
FUNCTIONSIGNATURE: '^function \\(([^\\(]*)\\)',
LIBRARYTYPE: '^type\\(library (.*)\\)'
}
......@@ -169,9 +169,9 @@ function getLocalCallName (localCallNode: FunctionCallAstNode): string {
* @localCallNode {ASTNode} Function call node
* @return {string} name of the function called
*/
function getThisLocalCallName (localCallNode: FunctionCallAstNode): string {
if (!isThisLocalCall(localCallNode.expression)) throw new Error('staticAnalysisCommon.js: not an this local call Node')
return localCallNode.expression.memberName
function getThisLocalCallName (thisLocalCallNode: FunctionCallAstNode): string {
if (!isThisLocalCall(thisLocalCallNode.expression)) throw new Error('staticAnalysisCommon.js: not an this local call Node')
return thisLocalCallNode.expression.memberName
}
/**
......@@ -180,9 +180,9 @@ function getThisLocalCallName (localCallNode: FunctionCallAstNode): string {
* @localCallNode {ASTNode} Function call node
* @return {string} name of the function called
*/
function getSuperLocalCallName (localCallNode: FunctionCallAstNode): string {
if (!isSuperLocalCall(localCallNode.expression)) throw new Error('staticAnalysisCommon.js: not an super local call Node')
return localCallNode.expression.memberName
function getSuperLocalCallName (superLocalCallNode: FunctionCallAstNode): string {
if (!isSuperLocalCall(superLocalCallNode.expression)) throw new Error('staticAnalysisCommon.js: not an super local call Node')
return superLocalCallNode.expression.memberName
}
/**
......@@ -256,8 +256,8 @@ function getFunctionDefinitionName (funcDef: FunctionDefinitionAstNode): string
* @func {ASTNode} Inheritance specifier
* @return {string} name of contract inherited from
*/
function getInheritsFromName (inheritsNode: InheritanceSpecifierAstNode): UserDefinedTypeNameAstNode {
return inheritsNode.baseName
function getInheritsFromName (inheritsNode: InheritanceSpecifierAstNode): string {
return inheritsNode.baseName.name
}
/**
......@@ -292,7 +292,7 @@ function getDeclaredVariableType (varDeclNode: VariableDeclarationAstNode): stri
* @contractNode {ASTNode} Contract Definition node
* @return {list variable declaration} state variable node list
*/
function getStateVariableDeclarationsFormContractNode (contractNode: ContractDefinitionAstNode): VariableDeclarationAstNode[] {
function getStateVariableDeclarationsFromContractNode (contractNode: ContractDefinitionAstNode): VariableDeclarationAstNode[] {
return contractNode.nodes.filter(el => el.nodeType === "VariableDeclaration")
}
......@@ -355,9 +355,9 @@ function getFunctionCallTypeParameterType (func: FunctionCallAstNode): string |
* @funcCall {ASTNode} function call node
* @return {string} name of the lib defined
*/
function getLibraryCallContractName (node: MemberAccessAstNode): string | undefined {
if (!isLibraryCall(node)) throw new Error('staticAnalysisCommon.js: not an this library call Node')
const types: RegExpExecArray | null = new RegExp(basicRegex.LIBRARYTYPE).exec(node.expression.typeDescriptions.typeString)
function getLibraryCallContractName (node: FunctionCallAstNode): string | undefined {
if (!isLibraryCall(node.expression)) throw new Error('staticAnalysisCommon.js: not an this library call Node')
const types: RegExpExecArray | null = new RegExp(basicRegex.LIBRARYTYPE).exec(node.expression.expression.typeDescriptions.typeString)
if(types)
return types[1]
}
......@@ -522,6 +522,9 @@ function isLocalCallGraphRelevantNode (node: FunctionCallAstNode): boolean {
* @return {bool}
*/
function isBuiltinFunctionCall (node: FunctionCallAstNode): boolean {
// console.log('isBuiltinFunctionCall isLocalCall', isLocalCall(node))
// console.log('isBuiltinFunctionCall getLocalCallName', getLocalCallName(node))
// console.log('isBuiltinFunctionCall getFunctionCallTypeParameterType', getFunctionCallTypeParameterType(node))
return (isLocalCall(node) && builtinFunctions[getLocalCallName(node) + '(' + getFunctionCallTypeParameterType(node) + ')'] === true) || isAbiNamespaceCall(node)
}
......@@ -567,7 +570,7 @@ function isRequireCall (node: FunctionCallAstNode): boolean {
* @return {bool}
*/
function isStorageVariableDeclaration (node: VariableDeclarationAstNode): boolean {
return expressionType(node, basicRegex.REFTYPE)
return node.storageLocation === 'storage' && new RegExp(basicRegex.REFTYPE).test(node.typeDescriptions.typeIdentifier)
}
/**
......@@ -731,7 +734,7 @@ function isLibrary (node: ContractDefinitionAstNode): boolean {
* @return {bool}
*/
function isCallToNonConstLocalFunction (node: FunctionCallAstNode): boolean {
return isLocalCall(node) && !expressionType(node, basicRegex.CONSTANTFUNCTIONTYPE)
return isLocalCall(node) && !expressionTypeDescription(node, basicRegex.CONSTANTFUNCTIONTYPE)
}
/**
......@@ -862,8 +865,8 @@ function isLLSend (node: MemberAccessAstNode): boolean {
*/
function isLLCall (node: MemberAccessAstNode): boolean {
return isMemberAccess(node,
exactMatch(util.escapeRegExp(lowLevelCallTypes.CALL.type)),
undefined, exactMatch(basicTypes.ADDRESS), exactMatch(lowLevelCallTypes.CALL.ident))
exactMatch(util.escapeRegExp(lowLevelCallTypes['CALL-v0.5'].type)),
undefined, exactMatch(basicTypes.ADDRESS), exactMatch(lowLevelCallTypes['CALL-v0.5'].ident))
}
/**
......@@ -895,8 +898,8 @@ function isLLCallcode (node: MemberAccessAstNode): boolean {
*/
function isLLDelegatecall (node: MemberAccessAstNode): boolean {
return isMemberAccess(node,
exactMatch(util.escapeRegExp(lowLevelCallTypes.DELEGATECALL.type)),
undefined, exactMatch(basicTypes.ADDRESS), exactMatch(lowLevelCallTypes.DELEGATECALL.ident))
exactMatch(util.escapeRegExp(lowLevelCallTypes['DELEGATECALL-v0.5'].type)),
undefined, exactMatch(basicTypes.ADDRESS), exactMatch(lowLevelCallTypes['DELEGATECALL-v0.5'].ident))
}
/**
......@@ -959,10 +962,15 @@ function isBytesLengthCheck (node: MemberAccessAstNode): boolean {
// #################### Complex Node Identification - Private
function isMemberAccess (node: MemberAccessAstNode, retType: string, accessor: string| undefined, accessorType: string, memberName: string | undefined): boolean {
return typeDescription(node, retType) &&
memName(node, memberName) &&
memName(node.expression, accessor) &&
expressionTypeDescription(node.expression, accessorType)
const nodeTypeDef: boolean = typeDescription(node, retType)
console.log('MemberAccess typeDef ->',nodeTypeDef)
const nodeMemName: boolean = memName(node, memberName)
console.log('MemberAccess nodeMemName ->',nodeMemName)
const nodeExpMemName: boolean = memName(node.expression, accessor)
console.log('MemberAccess nodeExpMemName ->',nodeExpMemName)
const nodeExpTypeDef: boolean = expressionTypeDescription(node, accessorType)
console.log('MemberAccess nodeExpTypeDef ->',nodeExpTypeDef)
return nodeTypeDef && nodeMemName && nodeExpTypeDef && nodeExpMemName
}
function isSpecialVariableAccess (node: MemberAccessAstNode, varType: any): boolean {
......@@ -979,9 +987,9 @@ function isSpecialVariableAccess (node: MemberAccessAstNode, varType: any): bool
// return (node && (nr === undefined || nr === null)) || (node && nr === 0 && !node.children) || (node && node.children && node.children.length >= nr)
// }
function expressionType (node, typeRegex) {
return new RegExp(typeRegex).test(node.expression.typeDescriptions.typeString)
}
// function expressionType (node, typeRegex) {
// return new RegExp(typeRegex).test(node.expression.typeDescriptions.typeString)
// }
function expressionTypeDescription (node, typeRegex) {
return new RegExp(typeRegex).test(node.expression.typeDescriptions.typeString)
......@@ -996,8 +1004,8 @@ function nodeType (node, typeRegex) {
}
function memName (node, memNameRegex) {
const regex = new RegExp(memNameRegex)
return regex.test(node.name) || regex.test(node.memberName)
// const regex = new RegExp(memNameRegex)
return (node && !memNameRegex) || new RegExp(memNameRegex).test(node.name) || new RegExp(memNameRegex).test(node.memberName)
}
function operator (node, opRegex) {
......@@ -1056,7 +1064,7 @@ function buildAbiSignature (funName: string, paramTypes: any[]): string {
const helpers = {
// nrOfChildren,
// minNrOfChildren,
expressionType,
expressionTypeDescription,
nodeType,
memName,
operator,
......@@ -1086,7 +1094,7 @@ export {
getLibraryCallMemberName,
getFullQualifiedFunctionCallIdent,
getFullQuallyfiedFuncDefinitionIdent,
getStateVariableDeclarationsFormContractNode,
getStateVariableDeclarationsFromContractNode,
getFunctionOrModifierDefinitionParameterPart,
getFunctionDefinitionReturnParameterPart,
getUnAssignedTopLevelBinOps,
......
{
"abstract": false,
"baseContracts": [],
"contractDependencies": [],
"contractKind": "contract",
"documentation": null,
"fullyImplemented": true,
"id": 8,
"linearizedBaseContracts":
[
8
],
"name": "C",
"nodeType": "ContractDefinition",
"nodes":
[
{
"body":
{
"id": 6,
"nodeType": "Block",
"src": "42:23:0",
"statements":
[
{
"assignments":
[
4
],
"declarations":
[
{
"constant": false,
"id": 4,
"name": "a",
"nodeType": "VariableDeclaration",
"overrides": null,
"scope": 6,
"src": "52:6:0",
"stateVariable": false,
"storageLocation": "default",
"typeDescriptions":
{
"typeIdentifier": "t_uint256",
"typeString": "uint256"
},
"typeName":
{
"id": 3,
"name": "uint",
"nodeType": "ElementaryTypeName",
"src": "52:4:0",
"typeDescriptions":
{
"typeIdentifier": "t_uint256",
"typeString": "uint256"
}
},
"value": null,
"visibility": "internal"
}
],
"id": 5,
"initialValue": null,
"nodeType": "VariableDeclarationStatement",
"src": "52:6:0"
}
]
},
"documentation": null,
"functionSelector": "26121ff0",
"id": 7,
"implemented": true,
"kind": "function",
"modifiers": [],
"name": "f",
"nodeType": "FunctionDefinition",
"overrides": null,
"parameters":
{
"id": 1,
"nodeType": "ParameterList",
"parameters": [],
"src": "27:2:0"
},
"returnParameters":
{
"id": 2,
"nodeType": "ParameterList",
"parameters": [],
"src": "42:0:0"
},
"scope": 8,
"src": "17:48:0",
"stateMutability": "pure",
"virtual": false,
"visibility": "public"
}
],
"scope": 9,
"src": "0:67:0"
}
\ No newline at end of file
{
"attributes": {
"member_name": "info",
"type": "function () payable external returns (uint256)"
},
"children": [
"argumentTypes": null,
"arguments": [],
"expression":
{
"argumentTypes": [],
"expression":
{
"argumentTypes": null,
"arguments":
[
{
"attributes": {
"type": "contract InfoFeed",
"value": "f"
"argumentTypes": null,
"hexValue": "30",
"id": 9,
"isConstant": false,
"isLValue": false,
"isPure": true,
"kind": "number",
"lValueRequested": false,
"nodeType": "Literal",
"src": "83:1:0",
"subdenomination": null,
"typeDescriptions":
{
"typeIdentifier": "t_rational_0_by_1",
"typeString": "int_const 0"
},
"id": 30,
"name": "Identifier",
"src": "405:1:0"
"value": "0"
}
],
"expression":
{
"argumentTypes":
[
{
"typeIdentifier": "t_rational_0_by_1",
"typeString": "int_const 0"
}
],
"id": 32,
"name": "MemberAccess",
"src": "405:6:0"
"id": 8,
"name": "c",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 5,
"src": "81:1:0",
"typeDescriptions":
{
"typeIdentifier": "t_type$_t_contract$_c_$5_$",
"typeString": "type(contract c)"
}
},
"id": 10,
"isConstant": false,
"isLValue": false,
"isPure": true,
"kind": "typeConversion",
"lValueRequested": false,
"names": [],
"nodeType": "FunctionCall",
"src": "81:4:0",
"tryCall": false,
"typeDescriptions":
{
"typeIdentifier": "t_contract$_c_$5",
"typeString": "contract c"
}
},
"id": 11,
"isConstant": false,
"isLValue": false,
"isPure": false,
"lValueRequested": false,
"memberName": "f",
"nodeType": "MemberAccess",
"referencedDeclaration": 4,
"src": "81:6:0",
"typeDescriptions":
{
"typeIdentifier": "t_function_external_nonpayable$__$returns$__$",
"typeString": "function () external"
}
},
"id": 12,
"isConstant": false,
"isLValue": false,
"isPure": false,
"kind": "functionCall",
"lValueRequested": false,
"names": [],
"nodeType": "FunctionCall",
"src": "81:8:0",
"tryCall": false,
"typeDescriptions":
{
"typeIdentifier": "t_tuple$__$",
"typeString": "tuple()"
}
}
\ No newline at end of file
module.exports = {
localCall: require('./localCall.json'),
contractDefinition: require('./contractDefinition.json'),
unaryOperation: require('./unaryOperation.json'),
nowAst: require('./nowAst.json'),
thisLocalCall: require('./thisLocalCall.json'),
libCall: require('./libCall.json'),
externalDirect: require('./externalDirect.json'),
......
{
"argumentTypes": null,
{ "argumentTypes": null,
"arguments":
[ { "argumentTypes": null,
"id": 95,
"name": "knownValues",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 79,
"src": "1392:11:0",
"typeDescriptions": { "typeIdentifier": "t_struct$_Data_$6_storage",
"typeString": "struct Set.Data storage ref" } },
{ "argumentTypes": null,
"id": 96,
"name": "value",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 81,
"src": "1405:5:0",
"typeDescriptions": { "typeIdentifier": "t_uint256", "typeString": "uint256" } } ],
"expression":
{ "argumentTypes": [ { "typeIdentifier": "t_struct$_Data_$6_storage",
"typeString": "struct Set.Data storage ref" },
{ "typeIdentifier": "t_uint256', typeString: 'uint256" } ],
"expression":
{
"argumentTypes": null,
"id": 33,
"name": "L",
{ "argumentTypes": null,
"id": 93,
"name": "Set",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 21,
"src": "244:1:0",
"typeDescriptions":
{
"typeIdentifier": "t_type$_t_contract$_L_$21_$",
"typeString": "type(library L)"
}
},
"id": 34,
"referencedDeclaration": 77,
"src": "1381:3:0",
"typeDescriptions": { "typeIdentifier": "t_type$_t_contract$_Set_$77_$",
"typeString": "type(library Set)" } },
"id": 94,
"isConstant": false,
"isLValue": false,
"isPure": false,
"lValueRequested": false,
"memberName": "f",
"memberName": "insert",
"nodeType": "MemberAccess",
"referencedDeclaration": 6,
"src": "244:3:0",
"referencedDeclaration": 33,
"src": "1381:10:0",
"typeDescriptions":
{
"typeIdentifier": "t_function_delegatecall_nonpayable$_t_uint256_$returns$__$",
"typeString": "function (uint256)"
}
{ "typeIdentifier":
"t_function_delegatecall_nonpayable$_t_struct$_Data_$6_storage_ptr_$_t_uint256_$returns$_t_bool_$",
"typeString":
"function (struct Set.Data storage pointer,uint256) returns (bool)" } },
"id": 97,
"isConstant": false,
"isLValue": false,
"isPure": false,
"kind": "functionCall",
"lValueRequested": false,
"names": [],
"nodeType": "FunctionCall",
"src": "1381:30:0",
"tryCall": false,
"typeDescriptions": { "typeIdentifier": "t_bool", "typeString": "bool" }
}
\ No newline at end of file
{
"argumentTypes": null,
"arguments": [],
"arguments":
[
{
"argumentTypes": null,
"hexValue": "616263",
"id": 15,
"isConstant": false,
"isLValue": false,
"isPure": true,
"kind": "string",
"lValueRequested": false,
"nodeType": "Literal",
"src": "115:5:0",
"subdenomination": null,
"typeDescriptions":
{
"typeIdentifier": "t_stringliteral_4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45",
"typeString": "literal_string \"abc\""
},
"value": "abc"
},
{
"argumentTypes": null,
"hexValue": "38",
"id": 16,
"isConstant": false,
"isLValue": false,
"isPure": true,
"kind": "number",
"lValueRequested": false,
"nodeType": "Literal",
"src": "125:1:0",
"subdenomination": null,
"typeDescriptions":
{
"typeIdentifier": "t_rational_8_by_1",
"typeString": "int_const 8"
},
"value": "8"
}
],
"expression":
{
"argumentTypes": [],
"id": 13,
"name": "sha3",
"argumentTypes":
[
{
"typeIdentifier": "t_stringliteral_4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45",
"typeString": "literal_string \"abc\""
},
{
"typeIdentifier": "t_rational_8_by_1",
"typeString": "int_const 8"
}
],
"id": 14,
"name": "e",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 8,
"src": "129:4:0",
"referencedDeclaration": 6,
"src": "109:1:0",
"typeDescriptions":
{
"typeIdentifier": "t_function_internal_pure$__$returns$_t_bool_$",
"typeString": "function () pure returns (bool)"
"typeIdentifier": "t_function_event_nonpayable$_t_uint256_$_t_string_memory_ptr_$returns$__$",
"typeString": "function (uint256,string memory)"
}
},
"id": 14,
"id": 17,
"isConstant": false,
"isLValue": false,
"isPure": false,
"kind": "functionCall",
"lValueRequested": false,
"names": [],
"names":
[
"b",
"a"
],
"nodeType": "FunctionCall",
"src": "129:6:0",
"src": "109:19:0",
"tryCall": false,
"typeDescriptions":
{
"typeIdentifier": "t_bool",
"typeString": "bool"
"typeIdentifier": "t_tuple$__$",
"typeString": "tuple()"
}
}
\ No newline at end of file
{
"argumentTypes": null,
"id": 11,
"name": "now",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": -17,
"src": "110:3:0",
"typeDescriptions":
{
"typeIdentifier": "t_uint256",
"typeString": "uint256"
}
}
\ No newline at end of file
{
"node1": {
"attributes": {
"name": "x",
"type": "struct Ballot.Voter storage pointer"
},
"children": [
"constant": false,
"id": 20,
"name": "c",
"nodeType": "VariableDeclaration",
"overrides": null,
"scope": 33,
"src": "174:11:0",
"stateVariable": false,
"storageLocation": "storage",
"typeDescriptions":
{
"attributes": {
"name": "Voter"
"typeIdentifier": "t_struct$_S_$3_storage_ptr",
"typeString": "struct C.S"
},
"id": 43,
"name": "UserDefinedTypeName",
"src": "604:5:0"
"typeName":
{
"contractScope": null,
"id": 19,
"name": "S",
"nodeType": "UserDefinedTypeName",
"referencedDeclaration": 3,
"src": "174:1:0",
"typeDescriptions":
{
"typeIdentifier": "t_struct$_S_$3_storage_ptr",
"typeString": "struct C.S"
}
],
"id": 44,
"name": "VariableDeclaration",
"src": "604:15:0"
},
"value": null,
"visibility": "internal"
},
"node2": {
"attributes": {
"name": "voters",
"type": "mapping(address => struct Ballot.Voter storage ref)"
"constant": false,
"id": 11,
"name": "",
"nodeType": "VariableDeclaration",
"overrides": null,
"scope": 16,
"src": "82:29:0",
"stateVariable": false,
"storageLocation": "storage",
"typeDescriptions":
{
"typeIdentifier": "t_array$_t_mapping$_t_uint256_$_t_uint256_$_$dyn_storage_ptr",
"typeString": "mapping(uint256 => uint256)[]"
},
"children": [
"typeName":
{
"baseType":
{
"id": 9,
"keyType":
{
"children": [
"id": 7,
"name": "uint",
"nodeType": "ElementaryTypeName",
"src": "90:4:0",
"typeDescriptions":
{
"attributes": {
"name": "address"
"typeIdentifier": "t_uint256",
"typeString": "uint256"
}
},
"id": 16,
"name": "ElementaryTypeName",
"src": "235:7:0"
"nodeType": "Mapping",
"src": "82:19:0",
"typeDescriptions":
{
"typeIdentifier": "t_mapping$_t_uint256_$_t_uint256_$",
"typeString": "mapping(uint256 => uint256)"
},
"valueType":
{
"attributes": {
"name": "Voter"
"id": 8,
"name": "uint",
"nodeType": "ElementaryTypeName",
"src": "96:4:0",
"typeDescriptions":
{
"typeIdentifier": "t_uint256",
"typeString": "uint256"
}
}
},
"id": 17,
"name": "UserDefinedTypeName",
"src": "246:5:0"
"id": 10,
"length": null,
"nodeType": "ArrayTypeName",
"src": "82:21:0",
"typeDescriptions":
{
"typeIdentifier": "t_array$_t_mapping$_t_uint256_$_t_uint256_$_$dyn_storage_ptr",
"typeString": "mapping(uint256 => uint256)[]"
}
],
"id": 18,
"name": "Mapping",
"src": "227:25:0"
}
],
"id": 19,
"name": "VariableDeclaration",
"src": "227:32:0"
},
"node3": {
"attributes": {
"name": "voters",
"type": "bytes32"
},
"children": [
"constant": false,
"id": 125,
"name": "f",
"nodeType": "VariableDeclaration",
"overrides": null,
"scope": 160,
"src": "1005:14:0",
"stateVariable": false,
"storageLocation": "memory",
"typeDescriptions":
{
"attributes": {
"name": "bytes"
"typeIdentifier": "t_bytes_memory_ptr",
"typeString": "bytes"
},
"id": 16,
"name": "ElementaryTypeName",
"src": "235:7:0"
"typeName":
{
"id": 124,
"name": "bytes",
"nodeType": "ElementaryTypeName",
"src": "1005:5:0",
"typeDescriptions":
{
"typeIdentifier": "t_bytes_storage_ptr",
"typeString": "bytes"
}
],
"id": 19,
"name": "VariableDeclaration",
"src": "227:32:0"
},
"value": null,
"visibility": "internal"
}
}
\ No newline at end of file
{
"argumentTypes": null,
"arguments": [],
"expression":
{
"argumentTypes": [],
"expression":
{
......@@ -29,4 +33,20 @@
"typeIdentifier": "t_function_internal_pure$__$returns$__$",
"typeString": "function () pure"
}
},
"id": 13,
"isConstant": false,
"isLValue": false,
"isPure": false,
"kind": "functionCall",
"lValueRequested": false,
"names": [],
"nodeType": "FunctionCall",
"src": "162:9:0",
"tryCall": false,
"typeDescriptions":
{
"typeIdentifier": "t_tuple$__$",
"typeString": "tuple()"
}
}
\ No newline at end of file
{
"argumentTypes": null,
"arguments": [],
"expression":
{
"argumentTypes": [],
"expression":
{
"argumentTypes": null,
"id": 13,
"id": 10,
"name": "this",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": -28,
"src": "138:4:0",
"src": "99:4:0",
"typeDescriptions":
{
"typeIdentifier": "t_contract$_C_$21",
"typeIdentifier": "t_contract$_C_$26",
"typeString": "contract C"
}
},
"id": 14,
"id": 11,
"isConstant": false,
"isLValue": false,
"isPure": false,
"lValueRequested": false,
"memberName": "h",
"memberName": "f",
"nodeType": "MemberAccess",
"referencedDeclaration": 4,
"src": "138:6:0",
"referencedDeclaration": 25,
"src": "99:6:0",
"typeDescriptions":
{
"typeIdentifier": "t_function_external_nonpayable$__$returns$_t_uint256_$_t_uint256_$",
"typeString": "function () external returns (uint256,uint256)"
}
},
"id": 12,
"isConstant": false,
"isLValue": false,
"isPure": false,
"kind": "functionCall",
"lValueRequested": false,
"names": [],
"nodeType": "FunctionCall",
"src": "99:8:0",
"tryCall": true,
"typeDescriptions":
{
"typeIdentifier": "t_function_external_payable$__$returns$__$",
"typeString": "function () payable external"
"typeIdentifier": "t_tuple$_t_uint256_$_t_uint256_$",
"typeString": "tuple(uint256,uint256)"
}
}
\ No newline at end of file
{
"argumentTypes": null,
"id": 13,
"isConstant": false,
"isLValue": false,
"isPure": false,
"lValueRequested": false,
"nodeType": "UnaryOperation",
"operator": "++",
"prefix": false,
"src": "95:3:0",
"subExpression":
{
"argumentTypes": null,
"id": 12,
"name": "x",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 4,
"src": "95:1:0",
"typeDescriptions":
{
"typeIdentifier": "t_uint256",
"typeString": "uint256"
}
},
"typeDescriptions":
{
"typeIdentifier": "t_uint256",
"typeString": "uint256"
}
}
\ No newline at end of file
pragma solidity >=0.4.9 <0.7.0;
contract Set {
library Set {
// We define a new struct datatype that will be used to
// hold its data in the calling contract.
// struct Data { uint flags; }
struct Data { mapping(uint => bool) flags; }
// Note that the first parameter is of type "storage
// reference" and thus only its storage address and not
......@@ -11,35 +11,35 @@ contract Set {
// special feature of library functions. It is idiomatic
// to call the first parameter 'self', if the function can
// be seen as a method of that object.
//function insert(Data memory self, uint value) public
// returns (bool)
//{
// if (self.flags[value])
// return false; // already there
// self.flags[value] = true;
function insert(Data storage self, uint value) public
returns (bool)
{
if (self.flags[value])
return false; // already there
self.flags[value] = true;
// return true;
// }
return true;
}
// function remove(Data memory self, uint value) public
// returns (bool)
// {
// if (!self.flags[value])
// return false; // not there
// self.flags[value] = false;
// return true;
// }
function remove(Data storage self, uint value) public
returns (bool)
{
if (!self.flags[value])
return false; // not there
self.flags[value] = false;
return true;
}
function contains(uint value) public pure
returns (uint)
function contains(Data storage self, uint value) public
returns (bool)
{
return value;
return self.flags[value];
}
}
contract C {
Set x;
Set.Data knownValues;
function register(uint value) public {
// The library functions can be called without a
......@@ -47,18 +47,8 @@ contract C {
// "instance" will be the current contract.
address payable a;
a.send(10 wei);
//if (!Set.insert(knownValues, value))
// revert();
}
function tests2() public {
x = Set(0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c);
uint y = x.contains(103);
if(y == 103){
y++;
} else {
y--;
}
if (!Set.insert(knownValues, value))
revert();
}
// In this contract, we can also directly access knownValues.flags, if we want.
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment