@@ -150,6 +150,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
150
150
llvm::DenseMap<const void *, llvm::TrackingMDNodeRef> DIModuleCache;
151
151
llvm::StringMap<llvm::TrackingMDNodeRef> DIFileCache;
152
152
llvm::StringMap<llvm::TrackingMDNodeRef> RuntimeErrorFnCache;
153
+ llvm::StringSet<> OriginallyDefinedInTypes;
153
154
TrackingDIRefMap DIRefMap;
154
155
TrackingDIRefMap InnerTypeCache;
155
156
// / \}
@@ -972,7 +973,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
972
973
return SizeInBits;
973
974
}
974
975
975
- StringRef getMangledName (DebugTypeInfo DbgTy) {
976
+ StringRef getMangledName (DebugTypeInfo DbgTy, bool RespectOriginallyDefinedIn ) {
976
977
if (DbgTy.isMetadataType ())
977
978
return MetadataTypeDeclCache.find (DbgTy.getDecl ()->getName ().str ())
978
979
->getKey ();
@@ -1024,11 +1025,12 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
1024
1025
IGM.getSILModule ());
1025
1026
1026
1027
Mangle::ASTMangler Mangler;
1027
- std::string Result = Mangler.mangleTypeForDebugger (Ty, Sig);
1028
+ std::string Result = Mangler.mangleTypeForDebugger (Ty, Sig, RespectOriginallyDefinedIn );
1028
1029
1029
1030
// TODO(https://github.com/apple/swift/issues/57699): We currently cannot round trip some C++ types.
1031
+ // There's no way to round trip when respecting @_originallyDefinedIn for a type.
1030
1032
if (!Opts.DisableRoundTripDebugTypes &&
1031
- !Ty->getASTContext ().LangOpts .EnableCXXInterop ) {
1033
+ !Ty->getASTContext ().LangOpts .EnableCXXInterop && !RespectOriginallyDefinedIn ) {
1032
1034
// Make sure we can reconstruct mangled types for the debugger.
1033
1035
auto &Ctx = Ty->getASTContext ();
1034
1036
Type Reconstructed = Demangle::getTypeForMangling (Ctx, Result, Sig);
@@ -1197,8 +1199,9 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
1197
1199
auto DbgTy = DebugTypeInfo::getFromTypeInfo (
1198
1200
UnsubstitutedTy, IGM.getTypeInfoForUnlowered (UnsubstitutedTy), IGM);
1199
1201
Mangle::ASTMangler Mangler;
1200
- std::string DeclTypeMangledName = Mangler.mangleTypeForDebugger (
1201
- UnsubstitutedTy->mapTypeOutOfContext (), {});
1202
+ std::string DeclTypeMangledName =
1203
+ Mangler.mangleTypeForDebugger (UnsubstitutedTy->mapTypeOutOfContext (),
1204
+ {}, /* RespectOriginallyDefinedIn=*/ true );
1202
1205
if (DeclTypeMangledName == MangledName) {
1203
1206
return createUnsubstitutedVariantType (DbgTy, Decl, MangledName, Scope,
1204
1207
File, 0 , Flags);
@@ -1256,7 +1259,8 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
1256
1259
UnsubstitutedTy, IGM.getTypeInfoForUnlowered (UnsubstitutedTy), IGM);
1257
1260
Mangle::ASTMangler Mangler;
1258
1261
std::string DeclTypeMangledName =
1259
- Mangler.mangleTypeForDebugger (UnsubstitutedTy->mapTypeOutOfContext (), {});
1262
+ Mangler.mangleTypeForDebugger (UnsubstitutedTy->mapTypeOutOfContext (), {},
1263
+ /* RespectOriginallyDefinedIn=*/ true );
1260
1264
if (DeclTypeMangledName == MangledName) {
1261
1265
return createUnsubstitutedGenericStructOrClassType (
1262
1266
DbgTy, Decl, UnsubstitutedTy, Scope, File, Line, Flags, nullptr ,
@@ -1506,7 +1510,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
1506
1510
// / anchor any typedefs that may appear in parameters so they can be
1507
1511
// / resolved in the debugger without needing to query the Swift module.
1508
1512
llvm::DINodeArray
1509
- collectGenericParams (NominalOrBoundGenericNominalType *BGT) {
1513
+ collectGenericParams (NominalOrBoundGenericNominalType *BGT, bool AsForwardDeclarations = false ) {
1510
1514
1511
1515
// Collect the generic args from the type and its parent.
1512
1516
std::vector<Type> GenericArgs;
@@ -1521,7 +1525,8 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
1521
1525
SmallVector<llvm::Metadata *, 16 > TemplateParams;
1522
1526
for (auto Arg : GenericArgs) {
1523
1527
DebugTypeInfo ParamDebugType;
1524
- if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes)
1528
+ if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes &&
1529
+ !AsForwardDeclarations)
1525
1530
// For the DwarfTypes level don't generate just a forward declaration
1526
1531
// for the generic type parameters.
1527
1532
ParamDebugType = DebugTypeInfo::getFromTypeInfo (
@@ -1789,26 +1794,6 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
1789
1794
}
1790
1795
1791
1796
llvm::DIType *SpecificationOf = nullptr ;
1792
- if (auto *TypeDecl = DbgTy.getType ()->getNominalOrBoundGenericNominal ()) {
1793
- // If this is a nominal type that has the @_originallyDefinedIn attribute,
1794
- // IRGenDebugInfo emits a forward declaration of the type as a child
1795
- // of the original module, and the type with a specification pointing to
1796
- // the forward declaraation. We do this so LLDB has enough information to
1797
- // both find the type in reflection metadata (the parent module name) and
1798
- // find it in the swiftmodule (the module name in the type mangled name).
1799
- if (auto Attribute =
1800
- TypeDecl->getAttrs ().getAttribute <OriginallyDefinedInAttr>()) {
1801
- auto Identifier = IGM.getSILModule ().getASTContext ().getIdentifier (
1802
- Attribute->OriginalModuleName );
1803
-
1804
- void *Key = (void *)Identifier.get ();
1805
- auto InnerScope =
1806
- getOrCreateModule (Key, TheCU, Attribute->OriginalModuleName , {});
1807
- SpecificationOf = DBuilder.createForwardDecl (
1808
- llvm::dwarf::DW_TAG_structure_type, TypeDecl->getNameStr (),
1809
- InnerScope, File, 0 , llvm::dwarf::DW_LANG_Swift, 0 , 0 );
1810
- }
1811
- }
1812
1797
1813
1798
// Here goes!
1814
1799
switch (BaseTy->getKind ()) {
@@ -2338,6 +2323,45 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
2338
2323
}
2339
2324
}
2340
2325
2326
+ // / If this is a nominal type that has the @_originallyDefinedIn
2327
+ // / attribute, IRGenDebugInfo emits an imported declaration of the type as
2328
+ // / a child of the real module. We do this so LLDB has enough
2329
+ // / information to both find the type in reflection metadata (the module name
2330
+ // / in the type's mangled name), and find it in the swiftmodule (the type's
2331
+ // / imported declaration's parent module name).
2332
+ void handleOriginallyDefinedIn (DebugTypeInfo DbgTy, llvm::DIType *DITy,
2333
+ StringRef MangledName, llvm::DIFile *File) {
2334
+ if (OriginallyDefinedInTypes.contains (MangledName))
2335
+ return ;
2336
+
2337
+ auto Type = DbgTy.getType ();
2338
+ auto *TypeDecl = Type->getNominalOrBoundGenericNominal ();
2339
+ if (!TypeDecl)
2340
+ return ;
2341
+
2342
+ // Force the generation of the generic type parameters as forward
2343
+ // declarations, as those types might be annotated with
2344
+ // @_originallyDefinedIn.
2345
+ if (auto *BoundDecl = llvm::dyn_cast<BoundGenericType>(DbgTy.getType ()))
2346
+ collectGenericParams (BoundDecl, /* AsForwardDeclarations=*/ true );
2347
+
2348
+ // Find the outermost type, since only those can have @_originallyDefinedIn
2349
+ // attached to them.
2350
+ NominalTypeDecl *ParentDecl = TypeDecl;
2351
+ while (llvm::isa_and_nonnull<NominalTypeDecl>(ParentDecl->getParent ()))
2352
+ ParentDecl = llvm::cast<NominalTypeDecl>(ParentDecl->getParent ());
2353
+
2354
+ auto Attribute =
2355
+ ParentDecl->getAttrs ().getAttribute <OriginallyDefinedInAttr>();
2356
+ if (!Attribute)
2357
+ return ;
2358
+
2359
+ // Find the module the imported declaration should be emitted under, this is the real module the type lives under.
2360
+ auto RealModule = getOrCreateContext (ParentDecl->getParent ());
2361
+ DBuilder.createImportedDeclaration (RealModule, DITy, File, 0 , MangledName);
2362
+ OriginallyDefinedInTypes.insert (MangledName);
2363
+ }
2364
+
2341
2365
llvm::DIType *getOrCreateType (DebugTypeInfo DbgTy,
2342
2366
llvm::DIScope *Scope = nullptr ) {
2343
2367
// Is this an empty type?
@@ -2357,7 +2381,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
2357
2381
StringRef MangledName;
2358
2382
llvm::MDString *UID = nullptr ;
2359
2383
if (canMangle (DbgTy.getType ())) {
2360
- MangledName = getMangledName (DbgTy);
2384
+ MangledName = getMangledName (DbgTy, /* RespectOriginallyDefinedIn= */ true );
2361
2385
UID = llvm::MDString::get (IGM.getLLVMContext (), MangledName);
2362
2386
if (llvm::Metadata *CachedTy = DIRefMap.lookup (UID)) {
2363
2387
auto DITy = cast<llvm::DIType>(CachedTy);
@@ -2382,7 +2406,18 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
2382
2406
ClangDecl = AliasDecl->getClangDecl ();
2383
2407
} else if (auto *ND = DbgTy.getType ()->getNominalOrBoundGenericNominal ()) {
2384
2408
TypeDecl = ND;
2385
- Context = ND->getParent ();
2409
+ // If this is an originally defined in type, we want to emit this type's scope
2410
+ // to be the ABI module.
2411
+ if (auto Attribute =
2412
+ ND->getAttrs ().getAttribute <OriginallyDefinedInAttr>()) {
2413
+ auto Identifier = IGM.getSILModule ().getASTContext ().getIdentifier (
2414
+ Attribute->OriginalModuleName );
2415
+ void *Key = (void *)Identifier.get ();
2416
+ Scope =
2417
+ getOrCreateModule (Key, TheCU, Attribute->OriginalModuleName , {});
2418
+ } else {
2419
+ Context = ND->getParent ();
2420
+ }
2386
2421
ClangDecl = ND->getClangDecl ();
2387
2422
} else if (auto BNO = dyn_cast<BuiltinType>(DbgTy.getType ())) {
2388
2423
Context = BNO->getASTContext ().TheBuiltinModule ;
@@ -2431,6 +2466,8 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
2431
2466
FwdDeclTypes.emplace_back (
2432
2467
std::piecewise_construct, std::make_tuple (MangledName),
2433
2468
std::make_tuple (static_cast <llvm::Metadata *>(FwdDecl)));
2469
+
2470
+ handleOriginallyDefinedIn (DbgTy, FwdDecl, MangledName, getFile (Scope));
2434
2471
return FwdDecl;
2435
2472
}
2436
2473
llvm::DIType *DITy = createType (DbgTy, MangledName, Scope, getFile (Scope));
@@ -2459,6 +2496,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
2459
2496
// Store it in the cache.
2460
2497
DITypeCache.insert ({DbgTy.getType (), llvm::TrackingMDNodeRef (DITy)});
2461
2498
2499
+ handleOriginallyDefinedIn (DbgTy, DITy, MangledName, getFile (Scope));
2462
2500
return DITy;
2463
2501
}
2464
2502
};
0 commit comments