From ee710eadd4f550fa5b33ec7e31677b046b4abc98 Mon Sep 17 00:00:00 2001 From: Marenz Date: Tue, 14 Jun 2022 16:02:05 +0200 Subject: [PATCH] Add location for member in MemberAccess ASTNode --- libsolidity/ast/AST.h | 12 ++++++++++-- libsolidity/ast/ASTJsonExporter.cpp | 1 + libsolidity/ast/ASTJsonImporter.cpp | 7 ++++++- libsolidity/parsing/Parser.cpp | 7 +++++-- test/libsolidity/ASTJSON/enum_value_declaration.json | 1 + .../ASTJSON/enum_value_declaration_parseOnly.json | 1 + 6 files changed, 24 insertions(+), 5 deletions(-) diff --git a/libsolidity/ast/AST.h b/libsolidity/ast/AST.h index 9c4c10b2f..8c1bf938e 100644 --- a/libsolidity/ast/AST.h +++ b/libsolidity/ast/AST.h @@ -2190,19 +2190,27 @@ public: int64_t _id, SourceLocation const& _location, ASTPointer _expression, - ASTPointer _memberName + ASTPointer _memberName, + SourceLocation _memberLocation ): - Expression(_id, _location), m_expression(std::move(_expression)), m_memberName(std::move(_memberName)) {} + Expression(_id, _location), + m_expression(std::move(_expression)), + m_memberName(std::move(_memberName)), + m_memberLocation(std::move(_memberLocation)) + {} + void accept(ASTVisitor& _visitor) override; void accept(ASTConstVisitor& _visitor) const override; Expression const& expression() const { return *m_expression; } ASTString const& memberName() const { return *m_memberName; } + SourceLocation const& memberLocation() const { return m_memberLocation; } MemberAccessAnnotation& annotation() const override; private: ASTPointer m_expression; ASTPointer m_memberName; + SourceLocation m_memberLocation; }; /** diff --git a/libsolidity/ast/ASTJsonExporter.cpp b/libsolidity/ast/ASTJsonExporter.cpp index dfd4a2f93..82ac4ab29 100644 --- a/libsolidity/ast/ASTJsonExporter.cpp +++ b/libsolidity/ast/ASTJsonExporter.cpp @@ -889,6 +889,7 @@ bool ASTJsonExporter::visit(MemberAccess const& _node) { std::vector> attributes = { make_pair("memberName", _node.memberName()), + make_pair("memberLocation", Json::Value(sourceLocationToString(_node.memberLocation()))), make_pair("expression", toJson(_node.expression())), make_pair("referencedDeclaration", idOrNull(_node.annotation().referencedDeclaration)), }; diff --git a/libsolidity/ast/ASTJsonImporter.cpp b/libsolidity/ast/ASTJsonImporter.cpp index 0f692376c..c4a342672 100644 --- a/libsolidity/ast/ASTJsonImporter.cpp +++ b/libsolidity/ast/ASTJsonImporter.cpp @@ -931,10 +931,15 @@ ASTPointer ASTJsonImporter::createNewExpression(Json::Value const ASTPointer ASTJsonImporter::createMemberAccess(Json::Value const& _node) { + SourceLocation memberLocation; + if (member(_node, "memberLocation").isString()) + memberLocation = solidity::langutil::parseSourceLocation(_node["memberLocation"].asString(), m_sourceNames); + return createASTNode( _node, convertJsonToASTNode(member(_node, "expression")), - memberAsASTString(_node, "memberName") + memberAsASTString(_node, "memberName"), + std::move(memberLocation) ); } diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 15e9f6c53..37ef1af49 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -1897,7 +1897,9 @@ ASTPointer Parser::parseLeftHandSideExpression( { advance(); nodeFactory.markEndPosition(); - expression = nodeFactory.createNode(expression, expectIdentifierTokenOrAddress()); + SourceLocation memberLocation = currentLocation(); + ASTPointer memberName = expectIdentifierTokenOrAddress(); + expression = nodeFactory.createNode(expression, std::move(memberName), std::move(memberLocation)); break; } case Token::LParen: @@ -2335,7 +2337,8 @@ ASTPointer Parser::expressionFromIndexAccessStructure( Identifier const& identifier = dynamic_cast(*_iap.path[i]); expression = nodeFactory.createNode( expression, - make_shared(identifier.name()) + make_shared(identifier.name()), + identifier.location() ); } for (auto const& index: _iap.indices) diff --git a/test/libsolidity/ASTJSON/enum_value_declaration.json b/test/libsolidity/ASTJSON/enum_value_declaration.json index 3e6e7c24c..acdcc736e 100644 --- a/test/libsolidity/ASTJSON/enum_value_declaration.json +++ b/test/libsolidity/ASTJSON/enum_value_declaration.json @@ -70,6 +70,7 @@ "isLValue": false, "isPure": true, "lValueRequested": false, + "memberLocation": "57:1:1", "memberName": "X", "nodeType": "MemberAccess", "referencedDeclaration": 1, diff --git a/test/libsolidity/ASTJSON/enum_value_declaration_parseOnly.json b/test/libsolidity/ASTJSON/enum_value_declaration_parseOnly.json index f19bb2d9b..8fa574fa7 100644 --- a/test/libsolidity/ASTJSON/enum_value_declaration_parseOnly.json +++ b/test/libsolidity/ASTJSON/enum_value_declaration_parseOnly.json @@ -49,6 +49,7 @@ "typeDescriptions": {} }, "id": 10, + "memberLocation": "57:1:1", "memberName": "X", "nodeType": "MemberAccess", "src": "55:3:1",