Add all path locations to the IdentifierPath ASTNode

This commit is contained in:
Marenz 2022-06-08 18:13:49 +02:00
parent c3ea86612e
commit 5d2e134378
30 changed files with 281 additions and 8 deletions

View File

@ -587,10 +587,19 @@ private:
class IdentifierPath: public ASTNode
{
public:
IdentifierPath(int64_t _id, SourceLocation const& _location, std::vector<ASTString> _path):
ASTNode(_id, _location), m_path(std::move(_path)) {}
IdentifierPath(
int64_t _id,
SourceLocation const& _location,
std::vector<ASTString> _path,
std::vector<SourceLocation> _pathLocations
):
ASTNode(_id, _location), m_path(std::move(_path)), m_pathLocations(std::move(_pathLocations))
{
solAssert(m_pathLocations.size() == m_path.size());
}
std::vector<ASTString> const& path() const { return m_path; }
std::vector<SourceLocation > const& pathLocations() const { return m_pathLocations; }
IdentifierPathAnnotation& annotation() const override
{
return initAnnotation<IdentifierPathAnnotation>();
@ -600,6 +609,8 @@ public:
void accept(ASTConstVisitor& _visitor) const override;
private:
std::vector<ASTString> m_path;
// Corresponding locations for m_path. Array has same length and indices as m_path.
std::vector<SourceLocation> m_pathLocations;
};
class InheritanceSpecifier: public ASTNode

View File

@ -294,8 +294,14 @@ bool ASTJsonExporter::visit(ContractDefinition const& _node)
bool ASTJsonExporter::visit(IdentifierPath const& _node)
{
Json::Value nameLocations = Json::arrayValue;
for (SourceLocation location: _node.pathLocations())
nameLocations.append(sourceLocationToString(location));
setJsonNode(_node, "IdentifierPath", {
make_pair("name", namePathToString(_node.path())),
make_pair("nameLocations", nameLocations),
make_pair("referencedDeclaration", idOrNull(_node.annotation().referencedDeclaration))
});
return false;

View File

@ -325,6 +325,7 @@ ASTPointer<IdentifierPath> ASTJsonImporter::createIdentifierPath(Json::Value con
astAssert(_node["name"].isString(), "Expected 'name' to be a string!");
vector<ASTString> namePath;
vector<SourceLocation> namePathLocations;
vector<string> strs;
string nameString = member(_node, "name").asString();
boost::algorithm::split(strs, nameString, boost::is_any_of("."));
@ -334,7 +335,23 @@ ASTPointer<IdentifierPath> ASTJsonImporter::createIdentifierPath(Json::Value con
astAssert(!s.empty(), "Expected non-empty string for IdentifierPath element.");
namePath.emplace_back(s);
}
return createASTNode<IdentifierPath>(_node, namePath);
if (_node.isMember("nameLocations") && _node["nameLocations"].isArray())
for (auto const& val: _node["nameLocations"])
namePathLocations.emplace_back(langutil::parseSourceLocation(val.asString(), m_sourceNames));
else
namePathLocations.resize(namePath.size());
astAssert(
namePath.size() == namePathLocations.size(),
"SourceLocations don't match name paths."
);
return createASTNode<IdentifierPath>(
_node,
namePath,
namePathLocations
);
}
ASTPointer<InheritanceSpecifier> ASTJsonImporter::createInheritanceSpecifier(Json::Value const& _node)

View File

@ -1061,14 +1061,24 @@ ASTPointer<IdentifierPath> Parser::parseIdentifierPath()
RecursionGuard recursionGuard(*this);
ASTNodeFactory nodeFactory(*this);
nodeFactory.markEndPosition();
vector<ASTString> identifierPath{*expectIdentifierToken()};
auto [name, nameLocation] = expectIdentifierWithLocation();
vector<ASTString> identifierPath{*name};
vector<SourceLocation> identifierPathLocations{nameLocation};
while (m_scanner->currentToken() == Token::Period)
{
advance();
nodeFactory.markEndPosition();
identifierPath.push_back(*expectIdentifierTokenOrAddress());
tie(name, nameLocation) = expectIdentifierWithLocation();
identifierPath.push_back(*name);
identifierPathLocations.push_back(nameLocation);
}
return nodeFactory.createNode<IdentifierPath>(identifierPath);
return nodeFactory.createNode<IdentifierPath>(identifierPath, identifierPathLocations);
}
ASTPointer<TypeName> Parser::parseTypeNameSuffix(ASTPointer<TypeName> type, ASTNodeFactory& nodeFactory)
@ -2286,9 +2296,16 @@ ASTPointer<TypeName> Parser::typeNameFromIndexAccessStructure(Parser::IndexAcces
else
{
vector<ASTString> path;
vector<SourceLocation> pathLocations;
for (auto const& el: _iap.path)
path.push_back(dynamic_cast<Identifier const&>(*el).name());
type = nodeFactory.createNode<UserDefinedTypeName>(nodeFactory.createNode<IdentifierPath>(path));
{
auto& identifier = dynamic_cast<Identifier const&>(*el);
path.push_back(identifier.name());
pathLocations.push_back(identifier.location());
}
type = nodeFactory.createNode<UserDefinedTypeName>(nodeFactory.createNode<IdentifierPath>(path, pathLocations));
}
for (auto const& lengthExpression: _iap.indices)
{

View File

@ -95,6 +95,10 @@
{
"id": 4,
"name": "S",
"nameLocations":
[
"42:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 3,
"src": "42:1:1"

View File

@ -66,6 +66,10 @@
{
"id": 4,
"name": "S",
"nameLocations":
[
"42:1:1"
],
"nodeType": "IdentifierPath",
"src": "42:1:1"
},

View File

@ -113,6 +113,10 @@
{
"id": 8,
"name": "A",
"nameLocations":
[
"50:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 7,
"src": "50:1:1"
@ -177,6 +181,10 @@
{
"id": 11,
"name": "A",
"nameLocations":
[
"68:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 7,
"src": "68:1:1"

View File

@ -84,6 +84,10 @@
{
"id": 8,
"name": "A",
"nameLocations":
[
"50:1:1"
],
"nodeType": "IdentifierPath",
"src": "50:1:1"
},
@ -131,6 +135,10 @@
{
"id": 11,
"name": "A",
"nameLocations":
[
"68:1:1"
],
"nodeType": "IdentifierPath",
"src": "68:1:1"
},

View File

@ -56,6 +56,10 @@
{
"id": 2,
"name": "A",
"nameLocations":
[
"29:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 1,
"src": "29:1:1"
@ -92,6 +96,10 @@
{
"id": 5,
"name": "B",
"nameLocations":
[
"49:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 4,
"src": "49:1:1"
@ -129,6 +137,10 @@
{
"id": 8,
"name": "C",
"nameLocations":
[
"69:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 7,
"src": "69:1:1"
@ -167,6 +179,10 @@
{
"id": 11,
"name": "D",
"nameLocations":
[
"89:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 10,
"src": "89:1:1"

View File

@ -26,6 +26,10 @@
{
"id": 2,
"name": "A",
"nameLocations":
[
"29:1:1"
],
"nodeType": "IdentifierPath",
"src": "29:1:1"
},
@ -53,6 +57,10 @@
{
"id": 5,
"name": "B",
"nameLocations":
[
"49:1:1"
],
"nodeType": "IdentifierPath",
"src": "49:1:1"
},
@ -80,6 +88,10 @@
{
"id": 8,
"name": "C",
"nameLocations":
[
"69:1:1"
],
"nodeType": "IdentifierPath",
"src": "69:1:1"
},
@ -107,6 +119,10 @@
{
"id": 11,
"name": "D",
"nameLocations":
[
"89:1:1"
],
"nodeType": "IdentifierPath",
"src": "89:1:1"
},

View File

@ -131,6 +131,10 @@
{
"id": 5,
"name": "A",
"nameLocations":
[
"43:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 3,
"src": "43:1:1"

View File

@ -99,6 +99,10 @@
{
"id": 5,
"name": "A",
"nameLocations":
[
"43:1:1"
],
"nodeType": "IdentifierPath",
"src": "43:1:1"
},

View File

@ -65,6 +65,10 @@
{
"id": 4,
"name": "S",
"nameLocations":
[
"50:1:1"
],
"nodeType": "IdentifierPath",
"src": "50:1:1"
},
@ -114,6 +118,10 @@
{
"id": 13,
"name": "error",
"nameLocations":
[
"95:5:1"
],
"nodeType": "IdentifierPath",
"src": "95:5:1"
},
@ -251,6 +259,10 @@
{
"id": 8,
"name": "E",
"nameLocations":
[
"72:1:1"
],
"nodeType": "IdentifierPath",
"src": "72:1:1"
},

View File

@ -44,6 +44,10 @@
{
"id": 2,
"name": "C1",
"nameLocations":
[
"30:2:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 1,
"src": "30:2:1"

View File

@ -26,6 +26,10 @@
{
"id": 2,
"name": "C1",
"nameLocations":
[
"30:2:1"
],
"nodeType": "IdentifierPath",
"src": "30:2:1"
},

View File

@ -87,6 +87,10 @@
{
"id": 5,
"name": "C",
"nameLocations":
[
"48:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 19,
"src": "48:1:1"
@ -201,6 +205,10 @@
{
"id": 14,
"name": "E",
"nameLocations":
[
"106:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 4,
"src": "106:1:1"

View File

@ -68,6 +68,10 @@
{
"id": 5,
"name": "C",
"nameLocations":
[
"48:1:1"
],
"nodeType": "IdentifierPath",
"src": "48:1:1"
},
@ -146,6 +150,10 @@
{
"id": 14,
"name": "E",
"nameLocations":
[
"106:1:1"
],
"nodeType": "IdentifierPath",
"src": "106:1:1"
},

View File

@ -131,6 +131,10 @@
{
"id": 8,
"name": "M",
"nameLocations":
[
"52:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 6,
"src": "52:1:1"

View File

@ -99,6 +99,10 @@
{
"id": 8,
"name": "M",
"nameLocations":
[
"52:1:1"
],
"nodeType": "IdentifierPath",
"src": "52:1:1"
},

View File

@ -131,6 +131,10 @@
{
"id": 8,
"name": "M",
"nameLocations":
[
"52:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 6,
"src": "52:1:1"

View File

@ -99,6 +99,10 @@
{
"id": 8,
"name": "M",
"nameLocations":
[
"52:1:1"
],
"nodeType": "IdentifierPath",
"src": "52:1:1"
},

View File

@ -23,6 +23,11 @@
{
"id": 2,
"name": "NotExisting.X",
"nameLocations":
[
"55:11:1",
"67:1:1"
],
"nodeType": "IdentifierPath",
"src": "55:13:1"
},
@ -58,6 +63,11 @@
{
"id": 4,
"name": "NotExisting.SomeStruct",
"nameLocations":
[
"72:11:1",
"84:10:1"
],
"nodeType": "IdentifierPath",
"src": "72:22:1"
},

View File

@ -86,6 +86,10 @@
{
"id": 6,
"name": "A",
"nameLocations":
[
"72:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 5,
"src": "72:1:1"
@ -200,6 +204,10 @@
{
"id": 17,
"name": "B",
"nameLocations":
[
"167:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 16,
"src": "167:1:1"

View File

@ -62,6 +62,10 @@
{
"id": 6,
"name": "A",
"nameLocations":
[
"72:1:1"
],
"nodeType": "IdentifierPath",
"src": "72:1:1"
},
@ -159,6 +163,10 @@
{
"id": 17,
"name": "B",
"nameLocations":
[
"167:1:1"
],
"nodeType": "IdentifierPath",
"src": "167:1:1"
},

View File

@ -144,6 +144,10 @@
{
"id": 11,
"name": "A",
"nameLocations":
[
"114:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 5,
"src": "114:1:1"
@ -157,6 +161,10 @@
{
"id": 13,
"name": "B",
"nameLocations":
[
"117:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 10,
"src": "117:1:1"
@ -212,6 +220,10 @@
{
"id": 16,
"name": "A",
"nameLocations":
[
"154:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 5,
"src": "154:1:1"
@ -219,6 +231,10 @@
{
"id": 17,
"name": "B",
"nameLocations":
[
"157:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 10,
"src": "157:1:1"

View File

@ -111,6 +111,10 @@
{
"id": 11,
"name": "A",
"nameLocations":
[
"114:1:1"
],
"nodeType": "IdentifierPath",
"src": "114:1:1"
},
@ -123,6 +127,10 @@
{
"id": 13,
"name": "B",
"nameLocations":
[
"117:1:1"
],
"nodeType": "IdentifierPath",
"src": "117:1:1"
},
@ -163,12 +171,20 @@
{
"id": 16,
"name": "A",
"nameLocations":
[
"154:1:1"
],
"nodeType": "IdentifierPath",
"src": "154:1:1"
},
{
"id": 17,
"name": "B",
"nameLocations":
[
"157:1:1"
],
"nodeType": "IdentifierPath",
"src": "157:1:1"
}

View File

@ -103,6 +103,10 @@
{
"id": 7,
"name": "MyAddress",
"nameLocations":
[
"67:9:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 2,
"src": "67:9:1"
@ -153,6 +157,10 @@
{
"id": 11,
"name": "MyUInt",
"nameLocations":
[
"84:6:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 4,
"src": "84:6:1"
@ -287,6 +295,10 @@
{
"id": 21,
"name": "MyAddress",
"nameLocations":
[
"177:9:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 18,
"src": "177:9:1"
@ -314,6 +326,10 @@
{
"id": 23,
"name": "MyUInt",
"nameLocations":
[
"190:6:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 20,
"src": "190:6:1"

View File

@ -69,6 +69,10 @@
{
"id": 7,
"name": "MyAddress",
"nameLocations":
[
"67:9:1"
],
"nodeType": "IdentifierPath",
"src": "67:9:1"
},
@ -108,6 +112,10 @@
{
"id": 11,
"name": "MyUInt",
"nameLocations":
[
"84:6:1"
],
"nodeType": "IdentifierPath",
"src": "84:6:1"
},
@ -213,6 +221,10 @@
{
"id": 21,
"name": "MyAddress",
"nameLocations":
[
"177:9:1"
],
"nodeType": "IdentifierPath",
"src": "177:9:1"
},
@ -230,6 +242,10 @@
{
"id": 23,
"name": "MyUInt",
"nameLocations":
[
"190:6:1"
],
"nodeType": "IdentifierPath",
"src": "190:6:1"
},

View File

@ -27,6 +27,10 @@
{
"id": 1,
"name": "f",
"nameLocations":
[
"7:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 10,
"src": "7:1:1"
@ -161,6 +165,10 @@
{
"id": 11,
"name": "L",
"nameLocations":
[
"72:1:1"
],
"nodeType": "IdentifierPath",
"referencedDeclaration": 4,
"src": "72:1:1"

View File

@ -12,6 +12,10 @@
{
"id": 1,
"name": "f",
"nameLocations":
[
"7:1:1"
],
"nodeType": "IdentifierPath",
"src": "7:1:1"
}
@ -118,6 +122,10 @@
{
"id": 11,
"name": "L",
"nameLocations":
[
"72:1:1"
],
"nodeType": "IdentifierPath",
"src": "72:1:1"
},