21
21
#include < cassert>
22
22
#include < cstdio>
23
23
#include < cstdlib>
24
+ #include < vector>
24
25
25
26
using namespace swift ;
26
27
using namespace Demangle ;
@@ -3211,6 +3212,32 @@ void NodePrinter::printEntityType(NodePointer Entity, NodePointer type,
3211
3212
}
3212
3213
}
3213
3214
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
+
3214
3241
std::string Demangle::keypathSourceString (const char *MangledName,
3215
3242
size_t MangledNameLength) {
3216
3243
std::string invalid = " " ;
@@ -3227,29 +3254,68 @@ std::string Demangle::keypathSourceString(const char *MangledName,
3227
3254
switch (child->getKind ()) {
3228
3255
case Node::Kind::Subscript: {
3229
3256
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
+ }
3230
3293
child = child->getChild (1 );
3294
+ size_t idx = 0 ;
3295
+ // There is an argument label:
3231
3296
if (child != nullptr ) {
3232
- // There is an argument label:
3233
3297
if (child->getKind () == Node::Kind::LabelList) {
3234
- size_t idx = 0 ;
3235
3298
size_t numChildren = child->getNumChildren ();
3236
3299
if (numChildren == 0 ) {
3237
- subscriptText += unlabelledArg;
3300
+ subscriptText += unlabelledArg + argumentTypeNames[ 0 ] ;
3238
3301
} else {
3239
3302
while (idx < numChildren) {
3240
3303
Node *argChild = child->getChild (idx);
3241
3304
idx += 1 ;
3242
3305
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
+ }
3244
3310
} else if (argChild->getKind () == Node::Kind::FirstElementMarker ||
3245
3311
argChild->getKind () == Node::Kind::VariadicMarker) {
3246
- subscriptText += unlabelledArg;
3312
+ subscriptText += unlabelledArg + argumentTypeNames[idx - 1 ] ;
3247
3313
}
3248
3314
}
3249
3315
}
3250
3316
}
3251
3317
} else {
3252
- subscriptText += unlabelledArg;
3318
+ subscriptText += unlabelledArg + argumentTypeNames[ 0 ] ;
3253
3319
}
3254
3320
return subscriptText + " )" ;
3255
3321
}
@@ -3273,7 +3339,6 @@ std::string Demangle::keypathSourceString(const char *MangledName,
3273
3339
}
3274
3340
default :
3275
3341
return invalid;
3276
-
3277
3342
}
3278
3343
}
3279
3344
}
0 commit comments