Skip to content

Commit 70ca05f

Browse files
authored
Merge pull request #13183 from ethereum/identifierPath_locations
Add all path locations to the IdentifierPath ASTNode
2 parents a2a88af + 5d2e134 commit 70ca05f

30 files changed

+281
-8
lines changed

libsolidity/ast/AST.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,19 @@ class ContractDefinition: public Declaration, public StructurallyDocumented, pub
587587
class IdentifierPath: public ASTNode
588588
{
589589
public:
590-
IdentifierPath(int64_t _id, SourceLocation const& _location, std::vector<ASTString> _path):
591-
ASTNode(_id, _location), m_path(std::move(_path)) {}
590+
IdentifierPath(
591+
int64_t _id,
592+
SourceLocation const& _location,
593+
std::vector<ASTString> _path,
594+
std::vector<SourceLocation> _pathLocations
595+
):
596+
ASTNode(_id, _location), m_path(std::move(_path)), m_pathLocations(std::move(_pathLocations))
597+
{
598+
solAssert(m_pathLocations.size() == m_path.size());
599+
}
592600

593601
std::vector<ASTString> const& path() const { return m_path; }
602+
std::vector<SourceLocation > const& pathLocations() const { return m_pathLocations; }
594603
IdentifierPathAnnotation& annotation() const override
595604
{
596605
return initAnnotation<IdentifierPathAnnotation>();
@@ -600,6 +609,8 @@ class IdentifierPath: public ASTNode
600609
void accept(ASTConstVisitor& _visitor) const override;
601610
private:
602611
std::vector<ASTString> m_path;
612+
// Corresponding locations for m_path. Array has same length and indices as m_path.
613+
std::vector<SourceLocation> m_pathLocations;
603614
};
604615

605616
class InheritanceSpecifier: public ASTNode

libsolidity/ast/ASTJsonExporter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,14 @@ bool ASTJsonExporter::visit(ContractDefinition const& _node)
294294

295295
bool ASTJsonExporter::visit(IdentifierPath const& _node)
296296
{
297+
Json::Value nameLocations = Json::arrayValue;
298+
299+
for (SourceLocation location: _node.pathLocations())
300+
nameLocations.append(sourceLocationToString(location));
301+
297302
setJsonNode(_node, "IdentifierPath", {
298303
make_pair("name", namePathToString(_node.path())),
304+
make_pair("nameLocations", nameLocations),
299305
make_pair("referencedDeclaration", idOrNull(_node.annotation().referencedDeclaration))
300306
});
301307
return false;

libsolidity/ast/ASTJsonImporter.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ ASTPointer<IdentifierPath> ASTJsonImporter::createIdentifierPath(Json::Value con
325325
astAssert(_node["name"].isString(), "Expected 'name' to be a string!");
326326

327327
vector<ASTString> namePath;
328+
vector<SourceLocation> namePathLocations;
328329
vector<string> strs;
329330
string nameString = member(_node, "name").asString();
330331
boost::algorithm::split(strs, nameString, boost::is_any_of("."));
@@ -334,7 +335,23 @@ ASTPointer<IdentifierPath> ASTJsonImporter::createIdentifierPath(Json::Value con
334335
astAssert(!s.empty(), "Expected non-empty string for IdentifierPath element.");
335336
namePath.emplace_back(s);
336337
}
337-
return createASTNode<IdentifierPath>(_node, namePath);
338+
339+
if (_node.isMember("nameLocations") && _node["nameLocations"].isArray())
340+
for (auto const& val: _node["nameLocations"])
341+
namePathLocations.emplace_back(langutil::parseSourceLocation(val.asString(), m_sourceNames));
342+
else
343+
namePathLocations.resize(namePath.size());
344+
345+
astAssert(
346+
namePath.size() == namePathLocations.size(),
347+
"SourceLocations don't match name paths."
348+
);
349+
350+
return createASTNode<IdentifierPath>(
351+
_node,
352+
namePath,
353+
namePathLocations
354+
);
338355
}
339356

340357
ASTPointer<InheritanceSpecifier> ASTJsonImporter::createInheritanceSpecifier(Json::Value const& _node)

libsolidity/parsing/Parser.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,14 +1061,24 @@ ASTPointer<IdentifierPath> Parser::parseIdentifierPath()
10611061
RecursionGuard recursionGuard(*this);
10621062
ASTNodeFactory nodeFactory(*this);
10631063
nodeFactory.markEndPosition();
1064-
vector<ASTString> identifierPath{*expectIdentifierToken()};
1064+
1065+
auto [name, nameLocation] = expectIdentifierWithLocation();
1066+
1067+
vector<ASTString> identifierPath{*name};
1068+
vector<SourceLocation> identifierPathLocations{nameLocation};
1069+
10651070
while (m_scanner->currentToken() == Token::Period)
10661071
{
10671072
advance();
1073+
10681074
nodeFactory.markEndPosition();
1069-
identifierPath.push_back(*expectIdentifierTokenOrAddress());
1075+
1076+
tie(name, nameLocation) = expectIdentifierWithLocation();
1077+
1078+
identifierPath.push_back(*name);
1079+
identifierPathLocations.push_back(nameLocation);
10701080
}
1071-
return nodeFactory.createNode<IdentifierPath>(identifierPath);
1081+
return nodeFactory.createNode<IdentifierPath>(identifierPath, identifierPathLocations);
10721082
}
10731083

10741084
ASTPointer<TypeName> Parser::parseTypeNameSuffix(ASTPointer<TypeName> type, ASTNodeFactory& nodeFactory)
@@ -2286,9 +2296,16 @@ ASTPointer<TypeName> Parser::typeNameFromIndexAccessStructure(Parser::IndexAcces
22862296
else
22872297
{
22882298
vector<ASTString> path;
2299+
vector<SourceLocation> pathLocations;
2300+
22892301
for (auto const& el: _iap.path)
2290-
path.push_back(dynamic_cast<Identifier const&>(*el).name());
2291-
type = nodeFactory.createNode<UserDefinedTypeName>(nodeFactory.createNode<IdentifierPath>(path));
2302+
{
2303+
auto& identifier = dynamic_cast<Identifier const&>(*el);
2304+
path.push_back(identifier.name());
2305+
pathLocations.push_back(identifier.location());
2306+
}
2307+
2308+
type = nodeFactory.createNode<UserDefinedTypeName>(nodeFactory.createNode<IdentifierPath>(path, pathLocations));
22922309
}
22932310
for (auto const& lengthExpression: _iap.indices)
22942311
{

test/libsolidity/ASTJSON/assembly/slot_offset.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@
9595
{
9696
"id": 4,
9797
"name": "S",
98+
"nameLocations":
99+
[
100+
"42:1:1"
101+
],
98102
"nodeType": "IdentifierPath",
99103
"referencedDeclaration": 3,
100104
"src": "42:1:1"

test/libsolidity/ASTJSON/assembly/slot_offset_parseOnly.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
{
6767
"id": 4,
6868
"name": "S",
69+
"nameLocations":
70+
[
71+
"42:1:1"
72+
],
6973
"nodeType": "IdentifierPath",
7074
"src": "42:1:1"
7175
},

test/libsolidity/ASTJSON/base_constructor_call.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@
113113
{
114114
"id": 8,
115115
"name": "A",
116+
"nameLocations":
117+
[
118+
"50:1:1"
119+
],
116120
"nodeType": "IdentifierPath",
117121
"referencedDeclaration": 7,
118122
"src": "50:1:1"
@@ -177,6 +181,10 @@
177181
{
178182
"id": 11,
179183
"name": "A",
184+
"nameLocations":
185+
[
186+
"68:1:1"
187+
],
180188
"nodeType": "IdentifierPath",
181189
"referencedDeclaration": 7,
182190
"src": "68:1:1"

test/libsolidity/ASTJSON/base_constructor_call_parseOnly.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@
8484
{
8585
"id": 8,
8686
"name": "A",
87+
"nameLocations":
88+
[
89+
"50:1:1"
90+
],
8791
"nodeType": "IdentifierPath",
8892
"src": "50:1:1"
8993
},
@@ -131,6 +135,10 @@
131135
{
132136
"id": 11,
133137
"name": "A",
138+
"nameLocations":
139+
[
140+
"68:1:1"
141+
],
134142
"nodeType": "IdentifierPath",
135143
"src": "68:1:1"
136144
},

test/libsolidity/ASTJSON/contract_dep_order.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
{
5757
"id": 2,
5858
"name": "A",
59+
"nameLocations":
60+
[
61+
"29:1:1"
62+
],
5963
"nodeType": "IdentifierPath",
6064
"referencedDeclaration": 1,
6165
"src": "29:1:1"
@@ -92,6 +96,10 @@
9296
{
9397
"id": 5,
9498
"name": "B",
99+
"nameLocations":
100+
[
101+
"49:1:1"
102+
],
95103
"nodeType": "IdentifierPath",
96104
"referencedDeclaration": 4,
97105
"src": "49:1:1"
@@ -129,6 +137,10 @@
129137
{
130138
"id": 8,
131139
"name": "C",
140+
"nameLocations":
141+
[
142+
"69:1:1"
143+
],
132144
"nodeType": "IdentifierPath",
133145
"referencedDeclaration": 7,
134146
"src": "69:1:1"
@@ -167,6 +179,10 @@
167179
{
168180
"id": 11,
169181
"name": "D",
182+
"nameLocations":
183+
[
184+
"89:1:1"
185+
],
170186
"nodeType": "IdentifierPath",
171187
"referencedDeclaration": 10,
172188
"src": "89:1:1"

test/libsolidity/ASTJSON/contract_dep_order_parseOnly.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
{
2727
"id": 2,
2828
"name": "A",
29+
"nameLocations":
30+
[
31+
"29:1:1"
32+
],
2933
"nodeType": "IdentifierPath",
3034
"src": "29:1:1"
3135
},
@@ -53,6 +57,10 @@
5357
{
5458
"id": 5,
5559
"name": "B",
60+
"nameLocations":
61+
[
62+
"49:1:1"
63+
],
5664
"nodeType": "IdentifierPath",
5765
"src": "49:1:1"
5866
},
@@ -80,6 +88,10 @@
8088
{
8189
"id": 8,
8290
"name": "C",
91+
"nameLocations":
92+
[
93+
"69:1:1"
94+
],
8395
"nodeType": "IdentifierPath",
8496
"src": "69:1:1"
8597
},
@@ -107,6 +119,10 @@
107119
{
108120
"id": 11,
109121
"name": "D",
122+
"nameLocations":
123+
[
124+
"89:1:1"
125+
],
110126
"nodeType": "IdentifierPath",
111127
"src": "89:1:1"
112128
},

0 commit comments

Comments
 (0)