@@ -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);
@@ -1506,7 +1508,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
1506
1508
// / anchor any typedefs that may appear in parameters so they can be
1507
1509
// / resolved in the debugger without needing to query the Swift module.
1508
1510
llvm::DINodeArray
1509
- collectGenericParams (NominalOrBoundGenericNominalType *BGT) {
1511
+ collectGenericParams (NominalOrBoundGenericNominalType *BGT, bool AsForwardDeclarations = false ) {
1510
1512
1511
1513
// Collect the generic args from the type and its parent.
1512
1514
std::vector<Type> GenericArgs;
@@ -1521,7 +1523,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
1521
1523
SmallVector<llvm::Metadata *, 16 > TemplateParams;
1522
1524
for (auto Arg : GenericArgs) {
1523
1525
DebugTypeInfo ParamDebugType;
1524
- if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes)
1526
+ if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes && !Arg-> is <GenericTypeParamType>() && !AsForwardDeclarations )
1525
1527
// For the DwarfTypes level don't generate just a forward declaration
1526
1528
// for the generic type parameters.
1527
1529
ParamDebugType = DebugTypeInfo::getFromTypeInfo (
@@ -1789,26 +1791,6 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
1789
1791
}
1790
1792
1791
1793
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
1794
1813
1795
// Here goes!
1814
1796
switch (BaseTy->getKind ()) {
@@ -2338,6 +2320,45 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
2338
2320
}
2339
2321
}
2340
2322
2323
+ // / If this is a nominal type that has the @_originallyDefinedIn
2324
+ // / attribute, IRGenDebugInfo emits an imported declaration of the type as
2325
+ // / a child of the real module. We do this so LLDB has enough
2326
+ // / information to both find the type in reflection metadata (the module name
2327
+ // / in the type's mangled name), and find it in the swiftmodule (the type's
2328
+ // / imported declaration's parent module name).
2329
+ void handleOriginallyDefinedIn (DebugTypeInfo DbgTy, llvm::DIType *DITy,
2330
+ StringRef MangledName, llvm::DIFile *File) {
2331
+ if (OriginallyDefinedInTypes.contains (MangledName))
2332
+ return ;
2333
+
2334
+ auto Type = DbgTy.getType ();
2335
+ auto *TypeDecl = Type->getNominalOrBoundGenericNominal ();
2336
+ if (!TypeDecl)
2337
+ return ;
2338
+
2339
+ // Force the generation of the generic type parameters as forward
2340
+ // declarations, as those types might be annotated with
2341
+ // @_originallyDefinedIn.
2342
+ if (auto *BoundDecl = llvm::dyn_cast<BoundGenericType>(DbgTy.getType ()))
2343
+ collectGenericParams (BoundDecl, /* AsForwardDeclarations=*/ true );
2344
+
2345
+ // Find the outermost type, since only those can have @_originallyDefinedIn
2346
+ // attached to them.
2347
+ NominalTypeDecl *ParentDecl = TypeDecl;
2348
+ while (llvm::isa_and_nonnull<NominalTypeDecl>(ParentDecl->getParent ()))
2349
+ ParentDecl = llvm::cast<NominalTypeDecl>(ParentDecl->getParent ());
2350
+
2351
+ auto Attribute =
2352
+ ParentDecl->getAttrs ().getAttribute <OriginallyDefinedInAttr>();
2353
+ if (!Attribute)
2354
+ return ;
2355
+
2356
+ // Find the module the imported declaration should be emitted under, this is the real module the type lives under.
2357
+ auto RealModule = getOrCreateContext (ParentDecl->getParent ());
2358
+ DBuilder.createImportedDeclaration (RealModule, DITy, File, 0 , MangledName);
2359
+ OriginallyDefinedInTypes.insert (MangledName);
2360
+ }
2361
+
2341
2362
llvm::DIType *getOrCreateType (DebugTypeInfo DbgTy,
2342
2363
llvm::DIScope *Scope = nullptr ) {
2343
2364
// Is this an empty type?
@@ -2357,7 +2378,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
2357
2378
StringRef MangledName;
2358
2379
llvm::MDString *UID = nullptr ;
2359
2380
if (canMangle (DbgTy.getType ())) {
2360
- MangledName = getMangledName (DbgTy);
2381
+ MangledName = getMangledName (DbgTy, /* RespectOriginallyDefinedIn= */ true );
2361
2382
UID = llvm::MDString::get (IGM.getLLVMContext (), MangledName);
2362
2383
if (llvm::Metadata *CachedTy = DIRefMap.lookup (UID)) {
2363
2384
auto DITy = cast<llvm::DIType>(CachedTy);
@@ -2382,7 +2403,18 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
2382
2403
ClangDecl = AliasDecl->getClangDecl ();
2383
2404
} else if (auto *ND = DbgTy.getType ()->getNominalOrBoundGenericNominal ()) {
2384
2405
TypeDecl = ND;
2385
- Context = ND->getParent ();
2406
+ // If this is an originally defined in type, we want to emit this type's scope
2407
+ // to be the ABI module.
2408
+ if (auto Attribute =
2409
+ ND->getAttrs ().getAttribute <OriginallyDefinedInAttr>()) {
2410
+ auto Identifier = IGM.getSILModule ().getASTContext ().getIdentifier (
2411
+ Attribute->OriginalModuleName );
2412
+ void *Key = (void *)Identifier.get ();
2413
+ Scope =
2414
+ getOrCreateModule (Key, TheCU, Attribute->OriginalModuleName , {});
2415
+ } else {
2416
+ Context = ND->getParent ();
2417
+ }
2386
2418
ClangDecl = ND->getClangDecl ();
2387
2419
} else if (auto BNO = dyn_cast<BuiltinType>(DbgTy.getType ())) {
2388
2420
Context = BNO->getASTContext ().TheBuiltinModule ;
@@ -2431,6 +2463,8 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
2431
2463
FwdDeclTypes.emplace_back (
2432
2464
std::piecewise_construct, std::make_tuple (MangledName),
2433
2465
std::make_tuple (static_cast <llvm::Metadata *>(FwdDecl)));
2466
+
2467
+ handleOriginallyDefinedIn (DbgTy, FwdDecl, MangledName, getFile (Scope));
2434
2468
return FwdDecl;
2435
2469
}
2436
2470
llvm::DIType *DITy = createType (DbgTy, MangledName, Scope, getFile (Scope));
@@ -2459,6 +2493,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
2459
2493
// Store it in the cache.
2460
2494
DITypeCache.insert ({DbgTy.getType (), llvm::TrackingMDNodeRef (DITy)});
2461
2495
2496
+ handleOriginallyDefinedIn (DbgTy, DITy, MangledName, getFile (Scope));
2462
2497
return DITy;
2463
2498
}
2464
2499
};
0 commit comments