From 05194730596defb41a02503538fe1f9ecd538ff7 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 11 Mar 2021 14:39:27 +0100 Subject: [PATCH] Add "kind" field to ModifierInvocation AST json element. --- Changelog.md | 2 + libsolidity/ast/ASTJsonConverter.cpp | 13 +- .../ASTJSON/base_constructor_call.json | 216 ++++++++++++++++++ .../ASTJSON/base_constructor_call.sol | 4 + .../base_constructor_call_parseOnly.json | 166 ++++++++++++++ .../ASTJSON/modifier_definition.json | 1 + .../ASTJSON/modifier_invocation.json | 1 + 7 files changed, 401 insertions(+), 2 deletions(-) create mode 100644 test/libsolidity/ASTJSON/base_constructor_call.json create mode 100644 test/libsolidity/ASTJSON/base_constructor_call.sol create mode 100644 test/libsolidity/ASTJSON/base_constructor_call_parseOnly.json diff --git a/Changelog.md b/Changelog.md index 3af5e5301..b7698d8e7 100644 --- a/Changelog.md +++ b/Changelog.md @@ -19,6 +19,8 @@ Bugfixes: * SMTChecker: Fix internal error on pushing to ``string`` casted to ``bytes``. AST Changes: + * ModifierInvocation: Add ``kind`` field which can be ``modifierInvocation`` or ``baseConstructorSpecifier``. + ### 0.8.2 (2021-03-02) diff --git a/libsolidity/ast/ASTJsonConverter.cpp b/libsolidity/ast/ASTJsonConverter.cpp index ce9ec4eee..5f769b048 100644 --- a/libsolidity/ast/ASTJsonConverter.cpp +++ b/libsolidity/ast/ASTJsonConverter.cpp @@ -453,10 +453,19 @@ bool ASTJsonConverter::visit(ModifierDefinition const& _node) bool ASTJsonConverter::visit(ModifierInvocation const& _node) { - setJsonNode(_node, "ModifierInvocation", { + std::vector> attributes{ make_pair("modifierName", toJson(_node.name())), make_pair("arguments", _node.arguments() ? toJson(*_node.arguments()) : Json::nullValue) - }); + }; + if (Declaration const* declaration = _node.name().annotation().referencedDeclaration) + { + if (dynamic_cast(declaration)) + attributes.emplace_back("kind", "modifierInvocation"); + else if (FunctionDefinition const* function = dynamic_cast(declaration)) + if (function->isConstructor()) + attributes.emplace_back("kind", "baseConstructorSpecifier"); + } + setJsonNode(_node, "ModifierInvocation", move(attributes)); return false; } diff --git a/test/libsolidity/ASTJSON/base_constructor_call.json b/test/libsolidity/ASTJSON/base_constructor_call.json new file mode 100644 index 000000000..fd11b622f --- /dev/null +++ b/test/libsolidity/ASTJSON/base_constructor_call.json @@ -0,0 +1,216 @@ +{ + "absolutePath": "a", + "exportedSymbols": + { + "A": + [ + 7 + ], + "C": + [ + 17 + ] + }, + "id": 18, + "nodeType": "SourceUnit", + "nodes": + [ + { + "abstract": false, + "baseContracts": [], + "contractDependencies": [], + "contractKind": "contract", + "fullyImplemented": true, + "id": 7, + "linearizedBaseContracts": + [ + 7 + ], + "name": "A", + "nameLocation": "9:1:1", + "nodeType": "ContractDefinition", + "nodes": + [ + { + "body": + { + "id": 5, + "nodeType": "Block", + "src": "31:2:1", + "statements": [] + }, + "id": 6, + "implemented": true, + "kind": "constructor", + "modifiers": [], + "name": "", + "nameLocation": "-1:-1:-1", + "nodeType": "FunctionDefinition", + "parameters": + { + "id": 3, + "nodeType": "ParameterList", + "parameters": + [ + { + "constant": false, + "id": 2, + "mutability": "mutable", + "name": "", + "nameLocation": "-1:-1:-1", + "nodeType": "VariableDeclaration", + "scope": 6, + "src": "25:4:1", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": + { + "typeIdentifier": "t_uint256", + "typeString": "uint256" + }, + "typeName": + { + "id": 1, + "name": "uint", + "nodeType": "ElementaryTypeName", + "src": "25:4:1", + "typeDescriptions": + { + "typeIdentifier": "t_uint256", + "typeString": "uint256" + } + }, + "visibility": "internal" + } + ], + "src": "24:6:1" + }, + "returnParameters": + { + "id": 4, + "nodeType": "ParameterList", + "parameters": [], + "src": "31:0:1" + }, + "scope": 7, + "src": "13:20:1", + "stateMutability": "nonpayable", + "virtual": false, + "visibility": "public" + } + ], + "scope": 18, + "src": "0:35:1" + }, + { + "abstract": false, + "baseContracts": + [ + { + "baseName": + { + "id": 8, + "name": "A", + "nodeType": "IdentifierPath", + "referencedDeclaration": 7, + "src": "50:1:1" + }, + "id": 9, + "nodeType": "InheritanceSpecifier", + "src": "50:1:1" + } + ], + "contractDependencies": + [ + 7 + ], + "contractKind": "contract", + "fullyImplemented": true, + "id": 17, + "linearizedBaseContracts": + [ + 17, + 7 + ], + "name": "C", + "nameLocation": "45:1:1", + "nodeType": "ContractDefinition", + "nodes": + [ + { + "body": + { + "id": 15, + "nodeType": "Block", + "src": "73:2:1", + "statements": [] + }, + "id": 16, + "implemented": true, + "kind": "constructor", + "modifiers": + [ + { + "arguments": + [ + { + "hexValue": "32", + "id": 12, + "isConstant": false, + "isLValue": false, + "isPure": true, + "kind": "number", + "lValueRequested": false, + "nodeType": "Literal", + "src": "70:1:1", + "typeDescriptions": + { + "typeIdentifier": "t_rational_2_by_1", + "typeString": "int_const 2" + }, + "value": "2" + } + ], + "id": 13, + "modifierName": + { + "id": 11, + "name": "A", + "nodeType": "IdentifierPath", + "referencedDeclaration": 7, + "src": "68:1:1" + }, + "nodeType": "ModifierInvocation", + "src": "68:4:1" + } + ], + "name": "", + "nameLocation": "-1:-1:-1", + "nodeType": "FunctionDefinition", + "parameters": + { + "id": 10, + "nodeType": "ParameterList", + "parameters": [], + "src": "65:2:1" + }, + "returnParameters": + { + "id": 14, + "nodeType": "ParameterList", + "parameters": [], + "src": "73:0:1" + }, + "scope": 17, + "src": "54:21:1", + "stateMutability": "nonpayable", + "virtual": false, + "visibility": "public" + } + ], + "scope": 18, + "src": "36:41:1" + } + ], + "src": "0:78:1" +} diff --git a/test/libsolidity/ASTJSON/base_constructor_call.sol b/test/libsolidity/ASTJSON/base_constructor_call.sol new file mode 100644 index 000000000..9e3b82259 --- /dev/null +++ b/test/libsolidity/ASTJSON/base_constructor_call.sol @@ -0,0 +1,4 @@ +contract A { constructor(uint) {} } +contract C is A { constructor() A(2) {} } + +// ---- diff --git a/test/libsolidity/ASTJSON/base_constructor_call_parseOnly.json b/test/libsolidity/ASTJSON/base_constructor_call_parseOnly.json new file mode 100644 index 000000000..61854cf9c --- /dev/null +++ b/test/libsolidity/ASTJSON/base_constructor_call_parseOnly.json @@ -0,0 +1,166 @@ +{ + "absolutePath": "a", + "id": 18, + "nodeType": "SourceUnit", + "nodes": + [ + { + "abstract": false, + "baseContracts": [], + "contractDependencies": [], + "contractKind": "contract", + "id": 7, + "name": "A", + "nameLocation": "9:1:1", + "nodeType": "ContractDefinition", + "nodes": + [ + { + "body": + { + "id": 5, + "nodeType": "Block", + "src": "31:2:1", + "statements": [] + }, + "id": 6, + "implemented": true, + "kind": "constructor", + "modifiers": [], + "name": "", + "nameLocation": "-1:-1:-1", + "nodeType": "FunctionDefinition", + "parameters": + { + "id": 3, + "nodeType": "ParameterList", + "parameters": + [ + { + "constant": false, + "id": 2, + "mutability": "mutable", + "name": "", + "nameLocation": "-1:-1:-1", + "nodeType": "VariableDeclaration", + "src": "25:4:1", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": {}, + "typeName": + { + "id": 1, + "name": "uint", + "nodeType": "ElementaryTypeName", + "src": "25:4:1", + "typeDescriptions": {} + }, + "visibility": "internal" + } + ], + "src": "24:6:1" + }, + "returnParameters": + { + "id": 4, + "nodeType": "ParameterList", + "parameters": [], + "src": "31:0:1" + }, + "src": "13:20:1", + "stateMutability": "nonpayable", + "virtual": false + } + ], + "src": "0:35:1" + }, + { + "abstract": false, + "baseContracts": + [ + { + "baseName": + { + "id": 8, + "name": "A", + "nodeType": "IdentifierPath", + "src": "50:1:1" + }, + "id": 9, + "nodeType": "InheritanceSpecifier", + "src": "50:1:1" + } + ], + "contractDependencies": [], + "contractKind": "contract", + "id": 17, + "name": "C", + "nameLocation": "45:1:1", + "nodeType": "ContractDefinition", + "nodes": + [ + { + "body": + { + "id": 15, + "nodeType": "Block", + "src": "73:2:1", + "statements": [] + }, + "id": 16, + "implemented": true, + "kind": "constructor", + "modifiers": + [ + { + "arguments": + [ + { + "hexValue": "32", + "id": 12, + "kind": "number", + "nodeType": "Literal", + "src": "70:1:1", + "typeDescriptions": {}, + "value": "2" + } + ], + "id": 13, + "modifierName": + { + "id": 11, + "name": "A", + "nodeType": "IdentifierPath", + "src": "68:1:1" + }, + "nodeType": "ModifierInvocation", + "src": "68:4:1" + } + ], + "name": "", + "nameLocation": "-1:-1:-1", + "nodeType": "FunctionDefinition", + "parameters": + { + "id": 10, + "nodeType": "ParameterList", + "parameters": [], + "src": "65:2:1" + }, + "returnParameters": + { + "id": 14, + "nodeType": "ParameterList", + "parameters": [], + "src": "73:0:1" + }, + "src": "54:21:1", + "stateMutability": "nonpayable", + "virtual": false + } + ], + "src": "36:41:1" + } + ], + "src": "0:78:1" +} diff --git a/test/libsolidity/ASTJSON/modifier_definition.json b/test/libsolidity/ASTJSON/modifier_definition.json index 7e4bdc3b7..b20afd429 100644 --- a/test/libsolidity/ASTJSON/modifier_definition.json +++ b/test/libsolidity/ASTJSON/modifier_definition.json @@ -125,6 +125,7 @@ } ], "id": 10, + "kind": "modifierInvocation", "modifierName": { "id": 8, diff --git a/test/libsolidity/ASTJSON/modifier_invocation.json b/test/libsolidity/ASTJSON/modifier_invocation.json index 7e4bdc3b7..b20afd429 100644 --- a/test/libsolidity/ASTJSON/modifier_invocation.json +++ b/test/libsolidity/ASTJSON/modifier_invocation.json @@ -125,6 +125,7 @@ } ], "id": 10, + "kind": "modifierInvocation", "modifierName": { "id": 8,