@@ -264,11 +264,15 @@ Type ASTBuilder::resolveOpaqueType(NodePointer opaqueDescriptor,
264
264
auto moduleNode = findModuleNode (definingDecl);
265
265
if (!moduleNode)
266
266
return Type ();
267
- auto parentModule = findModule (moduleNode);
268
- if (!parentModule )
267
+ auto potentialParentModules = findPotentialModules (moduleNode);
268
+ if (potentialParentModules. empty () )
269
269
return Type ();
270
270
271
- auto opaqueDecl = parentModule->lookupOpaqueResultType (mangledName);
271
+ OpaqueTypeDecl *opaqueDecl = nullptr ;
272
+ for (auto module : potentialParentModules)
273
+ if (auto decl = module ->lookupOpaqueResultType (mangledName))
274
+ opaqueDecl = decl;
275
+
272
276
if (!opaqueDecl)
273
277
return Type ();
274
278
SmallVector<Type, 8 > allArgs;
@@ -1129,17 +1133,11 @@ ASTBuilder::createTypeDecl(NodePointer node,
1129
1133
return dyn_cast<GenericTypeDecl>(DC);
1130
1134
}
1131
1135
1132
- ModuleDecl *ASTBuilder::findModule (NodePointer node) {
1136
+ llvm::ArrayRef<ModuleDecl *>
1137
+ ASTBuilder::findPotentialModules (NodePointer node) {
1133
1138
assert (node->getKind () == Demangle::Node::Kind::Module);
1134
1139
const auto moduleName = node->getText ();
1135
- // Respect the module's ABI name when we're trying to resolve
1136
- // mangled names. But don't touch anything under the Swift stdlib's
1137
- // umbrella.
1138
- if (moduleName != STDLIB_NAME)
1139
- if (auto *Module = Ctx.getLoadedModuleByABIName (moduleName))
1140
- return Module;
1141
-
1142
- return Ctx.getModuleByName (moduleName);
1140
+ return Ctx.getModulesByRealOrABIName (moduleName);
1143
1141
}
1144
1142
1145
1143
Demangle::NodePointer
@@ -1229,8 +1227,17 @@ ASTBuilder::findDeclContext(NodePointer node) {
1229
1227
case Demangle::Node::Kind::BoundGenericTypeAlias:
1230
1228
return findDeclContext (node->getFirstChild ());
1231
1229
1232
- case Demangle::Node::Kind::Module:
1233
- return findModule (node);
1230
+ case Demangle::Node::Kind::Module: {
1231
+ // A Module node is not enough information to find the decl context.
1232
+ // The reason being that the module name in a mangled name can either be
1233
+ // the module's ABI name, which is potentially not unique (due to the
1234
+ // -module-abi-name flag), or the module's real name, if mangling for the
1235
+ // debugger or USR together with the OriginallyDefinedIn attribute for
1236
+ // example.
1237
+ assert (false && " Looked up module as decl context directly!" );
1238
+ auto modules = findPotentialModules (node);
1239
+ return modules.empty () ? nullptr : modules[0 ];
1240
+ }
1234
1241
1235
1242
case Demangle::Node::Kind::Class:
1236
1243
case Demangle::Node::Kind::Enum:
@@ -1243,20 +1250,24 @@ ASTBuilder::findDeclContext(NodePointer node) {
1243
1250
if (declNameNode->getKind () == Demangle::Node::Kind::LocalDeclName) {
1244
1251
// Find the AST node for the defining module.
1245
1252
auto moduleNode = findModuleNode (node);
1246
- if (!moduleNode) return nullptr ;
1253
+ if (!moduleNode)
1254
+ return nullptr ;
1247
1255
1248
- auto module = findModule (moduleNode);
1249
- if (!module ) return nullptr ;
1256
+ auto potentialModules = findPotentialModules (moduleNode);
1257
+ if (potentialModules.empty ())
1258
+ return nullptr ;
1250
1259
1251
1260
// Look up the local type by its mangling.
1252
1261
auto mangling = Demangle::mangleNode (node);
1253
- if (!mangling.isSuccess ()) return nullptr ;
1262
+ if (!mangling.isSuccess ())
1263
+ return nullptr ;
1254
1264
auto mangledName = mangling.result ();
1255
1265
1256
- auto decl = module ->lookupLocalType (mangledName);
1257
- if (!decl) return nullptr ;
1266
+ for (auto *module : potentialModules)
1267
+ if (auto *decl = module ->lookupLocalType (mangledName))
1268
+ return dyn_cast<DeclContext>(decl);
1258
1269
1259
- return dyn_cast<DeclContext>(decl) ;
1270
+ return nullptr ;
1260
1271
}
1261
1272
1262
1273
StringRef name;
@@ -1290,22 +1301,33 @@ ASTBuilder::findDeclContext(NodePointer node) {
1290
1301
}
1291
1302
}
1292
1303
1293
- DeclContext *dc = findDeclContext (node->getChild (0 ));
1294
- if (!dc) {
1304
+ auto child = node->getFirstChild ();
1305
+ if (child->getKind () == Node::Kind::Module) {
1306
+ auto potentialModules = findPotentialModules (child);
1307
+ if (potentialModules.empty ())
1308
+ return nullptr ;
1309
+
1310
+ for (auto *module : potentialModules)
1311
+ if (auto typeDecl = findTypeDecl (module , Ctx.getIdentifier (name),
1312
+ privateDiscriminator, node->getKind ()))
1313
+ return typeDecl;
1295
1314
return nullptr ;
1296
1315
}
1297
1316
1298
- return findTypeDecl (dc, Ctx.getIdentifier (name),
1299
- privateDiscriminator, node->getKind ());
1317
+ if (auto *dc = findDeclContext (child))
1318
+ if (auto typeDecl = findTypeDecl (dc, Ctx.getIdentifier (name),
1319
+ privateDiscriminator, node->getKind ()))
1320
+ return typeDecl;
1321
+
1322
+ return nullptr ;
1300
1323
}
1301
1324
1302
1325
case Demangle::Node::Kind::Global:
1303
1326
return findDeclContext (node->getChild (0 ));
1304
1327
1305
1328
case Demangle::Node::Kind::Extension: {
1306
- auto *moduleDecl = dyn_cast_or_null<ModuleDecl>(
1307
- findDeclContext (node->getChild (0 )));
1308
- if (!moduleDecl)
1329
+ auto moduleDecls = findPotentialModules (node->getFirstChild ());
1330
+ if (moduleDecls.empty ())
1309
1331
return nullptr ;
1310
1332
1311
1333
auto *nominalDecl = dyn_cast_or_null<NominalTypeDecl>(
@@ -1327,14 +1349,23 @@ ASTBuilder::findDeclContext(NodePointer node) {
1327
1349
// If the generic signature is equivalent to that of the nominal type,
1328
1350
// and we're in the same module, it's due to inverse requirements.
1329
1351
// Just return the nominal declaration.
1330
- if (genericSigMatchesNominal &&
1331
- nominalDecl->getParentModule () == moduleDecl) {
1332
- return nominalDecl;
1352
+ for (auto *moduleDecl : moduleDecls) {
1353
+ if (genericSigMatchesNominal &&
1354
+ nominalDecl->getParentModule () == moduleDecl) {
1355
+ return nominalDecl;;
1356
+ }
1333
1357
}
1334
1358
}
1335
1359
1336
1360
for (auto *ext : nominalDecl->getExtensions ()) {
1337
- if (ext->getParentModule () != moduleDecl)
1361
+ bool found = false ;
1362
+ for (ModuleDecl *module : moduleDecls) {
1363
+ if (ext->getParentModule () == module ) {
1364
+ found = true ;
1365
+ break ;
1366
+ }
1367
+ }
1368
+ if (!found)
1338
1369
continue ;
1339
1370
1340
1371
if (!ext->isConstrainedExtension ()) {
0 commit comments