Skip to content

Commit 5352e2d

Browse files
committed
print types in subscripts
1 parent 047351d commit 5352e2d

File tree

2 files changed

+74
-9
lines changed

2 files changed

+74
-9
lines changed

lib/Demangling/NodePrinter.cpp

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <cassert>
2222
#include <cstdio>
2323
#include <cstdlib>
24+
#include <vector>
2425

2526
using namespace swift;
2627
using namespace Demangle;
@@ -3211,6 +3212,32 @@ void NodePrinter::printEntityType(NodePointer Entity, NodePointer type,
32113212
}
32123213
}
32133214

3215+
NodePointer matchSequenceOfKinds(NodePointer start,
3216+
std::vector<std::tuple<Node::Kind, size_t>> pattern) {
3217+
if (start != nullptr) {
3218+
NodePointer current = start;
3219+
size_t idx = 0;
3220+
while (idx < pattern.size()) {
3221+
std::tuple<Node::Kind, size_t> next = pattern[idx];
3222+
idx += 1;
3223+
NodePointer nextChild = current->getChild(std::get<1>(next));
3224+
if (nextChild != nullptr &&
3225+
nextChild->getKind() == std::get<0>(next)) {
3226+
current = nextChild;
3227+
} else {
3228+
return nullptr;
3229+
}
3230+
}
3231+
if (idx == pattern.size()) {
3232+
return current;
3233+
} else {
3234+
return nullptr;
3235+
}
3236+
} else {
3237+
return nullptr;
3238+
}
3239+
}
3240+
32143241
std::string Demangle::keypathSourceString(const char *MangledName,
32153242
size_t MangledNameLength) {
32163243
std::string invalid = "";
@@ -3227,29 +3254,68 @@ std::string Demangle::keypathSourceString(const char *MangledName,
32273254
switch (child->getKind()) {
32283255
case Node::Kind::Subscript: {
32293256
std::string subscriptText = "subscript(";
3257+
std::vector<std::string> argumentTypeNames;
3258+
// Multiple arguments case
3259+
NodePointer argList = matchSequenceOfKinds(child, {
3260+
std::make_pair(Node::Kind::Type, 2),
3261+
std::make_pair(Node::Kind::FunctionType, 0),
3262+
std::make_pair(Node::Kind::ArgumentTuple, 0),
3263+
std::make_pair(Node::Kind::Type, 0),
3264+
std::make_pair(Node::Kind::Tuple, 0),
3265+
});
3266+
if (argList != nullptr) {
3267+
size_t numArgumentTypes = argList->getNumChildren();
3268+
size_t idx = 0;
3269+
while (idx < numArgumentTypes) {
3270+
NodePointer argumentType = argList->getChild(idx);
3271+
idx += 1;
3272+
if (argumentType->getKind() == Node::Kind::TupleElement) {
3273+
argumentType = argumentType->getChild(0)->getChild(0)->getChild(1);
3274+
if (argumentType->getKind() == Node::Kind::Identifier) {
3275+
argumentTypeNames.push_back(std::string(argumentType->getText()));
3276+
continue;
3277+
}
3278+
}
3279+
argumentTypeNames.push_back("<Unknown>");
3280+
}
3281+
} else {
3282+
// Case where there is a single argument
3283+
argList = matchSequenceOfKinds(child, {
3284+
std::make_pair(Node::Kind::Type, 2),
3285+
std::make_pair(Node::Kind::FunctionType, 0),
3286+
std::make_pair(Node::Kind::ArgumentTuple, 0),
3287+
std::make_pair(Node::Kind::Type, 0),
3288+
});
3289+
if (argList != nullptr) {
3290+
argumentTypeNames.push_back(std::string(argList->getChild(0)->getChild(1)->getText()));
3291+
}
3292+
}
32303293
child = child->getChild(1);
3294+
size_t idx = 0;
3295+
// There is an argument label:
32313296
if (child != nullptr) {
3232-
// There is an argument label:
32333297
if (child->getKind() == Node::Kind::LabelList) {
3234-
size_t idx = 0;
32353298
size_t numChildren = child->getNumChildren();
32363299
if (numChildren == 0) {
3237-
subscriptText += unlabelledArg;
3300+
subscriptText += unlabelledArg + argumentTypeNames[0];
32383301
} else {
32393302
while (idx < numChildren) {
32403303
Node *argChild = child->getChild(idx);
32413304
idx += 1;
32423305
if (argChild->getKind() == Node::Kind::Identifier) {
3243-
subscriptText += std::string(argChild->getText()) + ": ";
3306+
subscriptText += std::string(argChild->getText()) + ": " + argumentTypeNames[idx - 1];
3307+
if (idx != numChildren) {
3308+
subscriptText += " ";
3309+
}
32443310
} else if (argChild->getKind() == Node::Kind::FirstElementMarker ||
32453311
argChild->getKind() == Node::Kind::VariadicMarker) {
3246-
subscriptText += unlabelledArg;
3312+
subscriptText += unlabelledArg + argumentTypeNames[idx - 1];
32473313
}
32483314
}
32493315
}
32503316
}
32513317
} else {
3252-
subscriptText += unlabelledArg;
3318+
subscriptText += unlabelledArg + argumentTypeNames[0];
32533319
}
32543320
return subscriptText + ")";
32553321
}
@@ -3273,7 +3339,6 @@ std::string Demangle::keypathSourceString(const char *MangledName,
32733339
}
32743340
default:
32753341
return invalid;
3276-
32773342
}
32783343
}
32793344
}

test/Interpreter/keypath.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ generic_class_constrained_keypath(GenericController(5))
7676
// CHECK: {{\\Controller\.secondLabel!\.text|\\Controller\.<computed 0x.* \(Optional<MyLabel>\)>!\.<computed 0x.* \(String\)>}}
7777
print(\Controller.secondLabel!.text)
7878

79-
// CHECK: {{\\Controller\.subscript\(_: \)|\\Controller\.<computed 0x.* \(String\)>}}
79+
// CHECK: {{\\Controller\.subscript\(_: String\)|\\Controller\.<computed 0x.* \(String\)>}}
8080
print(\Controller["abc"])
8181
// CHECK: \S.a
8282
print(\S.a)
83-
// CHECK: {{\\Controller\.subscript\(int: str: _: \)|\\Controller\.<computed 0x.* \(Int\)>}}
83+
// CHECK: {{\\Controller\.subscript\(int: Int str: String _: Int\)|\\Controller\.<computed 0x.* \(Int\)>}}
8484
print(\Controller[int: 0, str: "", 0])
8585
// CHECK: {{\\Controller\.thirdLabel|\\Controller\.<computed 0x.* \(Optional<MyLabel>\)>}}
8686
print(\Controller.thirdLabel)

0 commit comments

Comments
 (0)