@@ -264,25 +264,26 @@ 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);
272
- if (!opaqueDecl)
273
- return Type ();
274
- SmallVector<Type, 8 > allArgs;
275
- for (auto argSet : args) {
276
- allArgs.append (argSet.begin (), argSet.end ());
277
- }
271
+ for (ModuleDecl *module : potentialParentModules) {
272
+ auto opaqueDecl = module ->lookupOpaqueResultType (mangledName);
273
+ if (!opaqueDecl)
274
+ continue ;
275
+ SmallVector<Type, 8 > allArgs;
276
+ for (auto argSet : args) {
277
+ allArgs.append (argSet.begin (), argSet.end ());
278
+ }
278
279
279
- SubstitutionMap subs = createSubstitutionMapFromGenericArgs (
280
- opaqueDecl->getGenericSignature (), allArgs,
281
- LookUpConformanceInModule ());
282
- Type interfaceType = opaqueDecl->getOpaqueGenericParams ()[ordinal];
283
- return OpaqueTypeArchetypeType::get (opaqueDecl, interfaceType, subs);
280
+ SubstitutionMap subs = createSubstitutionMapFromGenericArgs (
281
+ opaqueDecl->getGenericSignature (), allArgs,
282
+ LookUpConformanceInModule ());
283
+ Type interfaceType = opaqueDecl->getOpaqueGenericParams ()[ordinal];
284
+ return OpaqueTypeArchetypeType::get (opaqueDecl, interfaceType, subs);
285
+ }
284
286
}
285
-
286
287
// TODO: named opaque types
287
288
return Type ();
288
289
}
@@ -1123,17 +1124,11 @@ ASTBuilder::createTypeDecl(NodePointer node,
1123
1124
return dyn_cast<GenericTypeDecl>(DC);
1124
1125
}
1125
1126
1126
- ModuleDecl *ASTBuilder::findModule (NodePointer node) {
1127
+ llvm::SmallVector<ModuleDecl *, 1 >
1128
+ ASTBuilder::findPotentialModules (NodePointer node) {
1127
1129
assert (node->getKind () == Demangle::Node::Kind::Module);
1128
1130
const auto moduleName = node->getText ();
1129
- // Respect the module's ABI name when we're trying to resolve
1130
- // mangled names. But don't touch anything under the Swift stdlib's
1131
- // umbrella.
1132
- if (moduleName != STDLIB_NAME)
1133
- if (auto *Module = Ctx.getLoadedModuleByABIName (moduleName))
1134
- return Module;
1135
-
1136
- return Ctx.getModuleByName (moduleName);
1131
+ return Ctx.getModulesByRealOrABIName (moduleName);
1137
1132
}
1138
1133
1139
1134
Demangle::NodePointer
@@ -1223,8 +1218,17 @@ ASTBuilder::findDeclContext(NodePointer node) {
1223
1218
case Demangle::Node::Kind::BoundGenericTypeAlias:
1224
1219
return findDeclContext (node->getFirstChild ());
1225
1220
1226
- case Demangle::Node::Kind::Module:
1227
- return findModule (node);
1221
+ case Demangle::Node::Kind::Module: {
1222
+ // A Module node is not enough information to find the decl context.
1223
+ // The reason being that the module name in a mangled name can either be
1224
+ // the module's ABI name, which is potentially not unique (due to the
1225
+ // -module-abi-name flag), or the module's real name, if mangling for the
1226
+ // debugger or USR together with the OriginallyDefinedIn attribute for
1227
+ // example.
1228
+ assert (false && " Looked up module as decl context directly!" );
1229
+ auto modules = findPotentialModules (node);
1230
+ return modules.empty () ? nullptr : modules[0 ];
1231
+ }
1228
1232
1229
1233
case Demangle::Node::Kind::Class:
1230
1234
case Demangle::Node::Kind::Enum:
@@ -1237,20 +1241,24 @@ ASTBuilder::findDeclContext(NodePointer node) {
1237
1241
if (declNameNode->getKind () == Demangle::Node::Kind::LocalDeclName) {
1238
1242
// Find the AST node for the defining module.
1239
1243
auto moduleNode = findModuleNode (node);
1240
- if (!moduleNode) return nullptr ;
1244
+ if (!moduleNode)
1245
+ return nullptr ;
1241
1246
1242
- auto module = findModule (moduleNode);
1243
- if (!module ) return nullptr ;
1247
+ auto potentialModules = findPotentialModules (moduleNode);
1248
+ if (potentialModules.empty ())
1249
+ return nullptr ;
1244
1250
1245
1251
// Look up the local type by its mangling.
1246
1252
auto mangling = Demangle::mangleNode (node);
1247
- if (!mangling.isSuccess ()) return nullptr ;
1253
+ if (!mangling.isSuccess ())
1254
+ return nullptr ;
1248
1255
auto mangledName = mangling.result ();
1249
1256
1250
- auto decl = module ->lookupLocalType (mangledName);
1251
- if (!decl) return nullptr ;
1257
+ for (auto *module : potentialModules)
1258
+ if (auto *decl = module ->lookupLocalType (mangledName))
1259
+ return dyn_cast<DeclContext>(decl);
1252
1260
1253
- return dyn_cast<DeclContext>(decl) ;
1261
+ return nullptr ;
1254
1262
}
1255
1263
1256
1264
StringRef name;
@@ -1272,7 +1280,7 @@ ASTBuilder::findDeclContext(NodePointer node) {
1272
1280
1273
1281
// Ignore any other decl-name productions for now.
1274
1282
} else {
1275
- return nullptr ;
1283
+ return nullptr ;;
1276
1284
}
1277
1285
1278
1286
// Do some special logic for foreign type declarations.
@@ -1284,22 +1292,33 @@ ASTBuilder::findDeclContext(NodePointer node) {
1284
1292
}
1285
1293
}
1286
1294
1287
- DeclContext *dc = findDeclContext (node->getChild (0 ));
1288
- if (!dc) {
1295
+ auto child = node->getFirstChild ();
1296
+ if (child->getKind () == Node::Kind::Module) {
1297
+ auto potentialModules = findPotentialModules (child);
1298
+ if (potentialModules.empty ())
1299
+ return nullptr ;
1300
+
1301
+ for (auto *module : potentialModules)
1302
+ if (auto typeDecl = findTypeDecl (module , Ctx.getIdentifier (name),
1303
+ privateDiscriminator, node->getKind ()))
1304
+ return typeDecl;
1289
1305
return nullptr ;
1290
1306
}
1291
1307
1292
- return findTypeDecl (dc, Ctx.getIdentifier (name),
1293
- privateDiscriminator, node->getKind ());
1308
+ if (auto *dc = findDeclContext (child))
1309
+ if (auto typeDecl = findTypeDecl (dc, Ctx.getIdentifier (name),
1310
+ privateDiscriminator, node->getKind ()))
1311
+ return typeDecl;
1312
+
1313
+ return nullptr ;
1294
1314
}
1295
1315
1296
1316
case Demangle::Node::Kind::Global:
1297
1317
return findDeclContext (node->getChild (0 ));
1298
1318
1299
1319
case Demangle::Node::Kind::Extension: {
1300
- auto *moduleDecl = dyn_cast_or_null<ModuleDecl>(
1301
- findDeclContext (node->getChild (0 )));
1302
- if (!moduleDecl)
1320
+ auto moduleDecls = findPotentialModules (node->getFirstChild ());
1321
+ if (moduleDecls.empty ())
1303
1322
return nullptr ;
1304
1323
1305
1324
auto *nominalDecl = dyn_cast_or_null<NominalTypeDecl>(
@@ -1321,39 +1340,43 @@ ASTBuilder::findDeclContext(NodePointer node) {
1321
1340
// If the generic signature is equivalent to that of the nominal type,
1322
1341
// and we're in the same module, it's due to inverse requirements.
1323
1342
// Just return the nominal declaration.
1324
- if (genericSigMatchesNominal &&
1325
- nominalDecl->getParentModule () == moduleDecl) {
1326
- return nominalDecl;
1343
+ for (auto *moduleDecl : moduleDecls) {
1344
+ if (genericSigMatchesNominal &&
1345
+ nominalDecl->getParentModule () == moduleDecl) {
1346
+ return nominalDecl;
1347
+ }
1327
1348
}
1328
1349
}
1329
1350
1330
- for (auto *ext : nominalDecl->getExtensions ()) {
1331
- if (ext->getParentModule () != moduleDecl)
1332
- continue ;
1333
-
1334
- if (!ext->isConstrainedExtension ()) {
1335
- if (!genericSig || genericSigMatchesNominal)
1336
- return ext;
1351
+ for (auto *moduleDecl : moduleDecls) {
1352
+ for (auto *ext : nominalDecl->getExtensions ()) {
1353
+ if (ext->getParentModule () != moduleDecl)
1354
+ continue ;
1337
1355
1338
- continue ;
1339
- }
1356
+ if (!ext->isConstrainedExtension ()) {
1357
+ if (!genericSig || genericSigMatchesNominal)
1358
+ return ext;
1340
1359
1341
- if (!ext-> isWrittenWithConstraints () && !genericSig)
1342
- return ext;
1360
+ continue ;
1361
+ }
1343
1362
1344
- auto extSig = ext->getGenericSignature ().getCanonicalSignature ();
1345
- if (extSig == genericSig) {
1346
- return ext;
1347
- }
1363
+ if (!ext->isWrittenWithConstraints () && !genericSig)
1364
+ return ext;
1348
1365
1349
- // If the extension mangling doesn't include a generic signature, it
1350
- // might be because the nominal type suppresses conformance.
1351
- if (!genericSig) {
1352
- SmallVector<Requirement, 2 > requirements;
1353
- SmallVector<InverseRequirement, 2 > inverses;
1354
- extSig->getRequirementsWithInverses (requirements, inverses);
1355
- if (requirements.empty ())
1366
+ auto extSig = ext->getGenericSignature ().getCanonicalSignature ();
1367
+ if (extSig == genericSig) {
1356
1368
return ext;
1369
+ }
1370
+
1371
+ // If the extension mangling doesn't include a generic signature, it
1372
+ // might be because the nominal type suppresses conformance.
1373
+ if (!genericSig) {
1374
+ SmallVector<Requirement, 2 > requirements;
1375
+ SmallVector<InverseRequirement, 2 > inverses;
1376
+ extSig->getRequirementsWithInverses (requirements, inverses);
1377
+ if (requirements.empty ())
1378
+ return ext;
1379
+ }
1357
1380
}
1358
1381
}
1359
1382
0 commit comments