23
23
#include " IRBuilder.h"
24
24
#include " swift/AST/ASTDemangler.h"
25
25
#include " swift/AST/ASTMangler.h"
26
- #include " swift/AST/Attr.h"
27
- #include " swift/AST/Decl.h"
28
26
#include " swift/AST/Expr.h"
29
27
#include " swift/AST/GenericEnvironment.h"
30
28
#include " swift/AST/IRGenOptions.h"
31
29
#include " swift/AST/Module.h"
32
30
#include " swift/AST/ModuleLoader.h"
33
- #include " swift/AST/ParameterList.h"
34
31
#include " swift/AST/Pattern.h"
35
32
#include " swift/AST/TypeDifferenceVisitor.h"
36
- #include " swift/AST/TypeWalker.h"
37
33
#include " swift/Basic/Assertions.h"
38
34
#include " swift/Basic/Compiler.h"
39
35
#include " swift/Basic/SourceManager.h"
@@ -141,7 +137,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
141
137
return Line == other.Line && Column == other.Column && File == other.File ;
142
138
}
143
139
};
144
-
140
+
145
141
// / Various caches.
146
142
// / \{
147
143
llvm::StringSet<> VarNames;
@@ -154,7 +150,6 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
154
150
llvm::DenseMap<const void *, llvm::TrackingMDNodeRef> DIModuleCache;
155
151
llvm::StringMap<llvm::TrackingMDNodeRef> DIFileCache;
156
152
llvm::StringMap<llvm::TrackingMDNodeRef> RuntimeErrorFnCache;
157
- llvm::StringSet<> OriginallyDefinedInTypes;
158
153
TrackingDIRefMap DIRefMap;
159
154
TrackingDIRefMap InnerTypeCache;
160
155
// / \}
@@ -1031,12 +1026,9 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
1031
1026
Mangle::ASTMangler Mangler (IGM.Context );
1032
1027
std::string Result = Mangler.mangleTypeForDebugger (Ty, Sig);
1033
1028
1034
- bool IsTypeOriginallyDefinedIn =
1035
- containsOriginallyDefinedIn (DbgTy.getType ());
1036
1029
// TODO(https://github.com/apple/swift/issues/57699): We currently cannot round trip some C++ types.
1037
- // There's no way to round trip when respecting @_originallyDefinedIn for a type.
1038
1030
if (!Opts.DisableRoundTripDebugTypes &&
1039
- !Ty->getASTContext ().LangOpts .EnableCXXInterop && !IsTypeOriginallyDefinedIn ) {
1031
+ !Ty->getASTContext ().LangOpts .EnableCXXInterop ) {
1040
1032
// Make sure we can reconstruct mangled types for the debugger.
1041
1033
auto &Ctx = Ty->getASTContext ();
1042
1034
Type Reconstructed = Demangle::getTypeForMangling (Ctx, Result, Sig);
@@ -1515,7 +1507,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
1515
1507
// / anchor any typedefs that may appear in parameters so they can be
1516
1508
// / resolved in the debugger without needing to query the Swift module.
1517
1509
llvm::DINodeArray
1518
- collectGenericParams (NominalOrBoundGenericNominalType *BGT, bool AsForwardDeclarations = false ) {
1510
+ collectGenericParams (NominalOrBoundGenericNominalType *BGT) {
1519
1511
1520
1512
// Collect the generic args from the type and its parent.
1521
1513
std::vector<Type> GenericArgs;
@@ -1530,8 +1522,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
1530
1522
SmallVector<llvm::Metadata *, 16 > TemplateParams;
1531
1523
for (auto Arg : GenericArgs) {
1532
1524
DebugTypeInfo ParamDebugType;
1533
- if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes &&
1534
- !AsForwardDeclarations)
1525
+ if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes)
1535
1526
// For the DwarfTypes level don't generate just a forward declaration
1536
1527
// for the generic type parameters.
1537
1528
ParamDebugType = DebugTypeInfo::getFromTypeInfo (
@@ -1799,19 +1790,39 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
1799
1790
}
1800
1791
1801
1792
llvm::DIType *SpecificationOf = nullptr ;
1793
+ if (auto *TypeDecl = DbgTy.getType ()->getNominalOrBoundGenericNominal ()) {
1794
+ // If this is a nominal type that has the @_originallyDefinedIn attribute,
1795
+ // IRGenDebugInfo emits a forward declaration of the type as a child
1796
+ // of the original module, and the type with a specification pointing to
1797
+ // the forward declaraation. We do this so LLDB has enough information to
1798
+ // both find the type in reflection metadata (the parent module name) and
1799
+ // find it in the swiftmodule (the module name in the type mangled name).
1800
+ if (auto Attribute =
1801
+ TypeDecl->getAttrs ().getAttribute <OriginallyDefinedInAttr>()) {
1802
+ auto Identifier = IGM.getSILModule ().getASTContext ().getIdentifier (
1803
+ Attribute->OriginalModuleName );
1804
+
1805
+ void *Key = (void *)Identifier.get ();
1806
+ auto InnerScope =
1807
+ getOrCreateModule (Key, TheCU, Attribute->OriginalModuleName , {});
1808
+ SpecificationOf = DBuilder.createForwardDecl (
1809
+ llvm::dwarf::DW_TAG_structure_type, TypeDecl->getNameStr (),
1810
+ InnerScope, File, 0 , llvm::dwarf::DW_LANG_Swift, 0 , 0 );
1811
+ }
1812
+ }
1802
1813
1803
1814
// Here goes!
1804
1815
switch (BaseTy->getKind ()) {
1805
1816
case TypeKind::BuiltinUnboundGeneric:
1806
1817
llvm_unreachable (" not a real type" );
1807
-
1818
+
1808
1819
case TypeKind::BuiltinFixedArray: {
1809
1820
// TODO: provide proper array debug info
1810
1821
unsigned FwdDeclLine = 0 ;
1811
1822
return createOpaqueStruct (Scope, " Builtin.FixedArray" , MainFile, FwdDeclLine,
1812
1823
SizeInBits, AlignInBits, Flags, MangledName);
1813
1824
}
1814
-
1825
+
1815
1826
case TypeKind::BuiltinPackIndex:
1816
1827
case TypeKind::BuiltinInteger: {
1817
1828
Encoding = llvm::dwarf::DW_ATE_unsigned;
@@ -1835,7 +1846,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
1835
1846
break ;
1836
1847
}
1837
1848
1838
- case TypeKind::BuiltinNativeObject:
1849
+ case TypeKind::BuiltinNativeObject:
1839
1850
case TypeKind::BuiltinBridgeObject:
1840
1851
case TypeKind::BuiltinRawPointer:
1841
1852
case TypeKind::BuiltinRawUnsafeContinuation:
@@ -1917,7 +1928,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
1917
1928
assert (SizeInBits ==
1918
1929
CI.getTargetInfo ().getPointerWidth (clang::LangAS::Default));
1919
1930
if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes) {
1920
- if (ClassTy->isSpecialized ())
1931
+ if (ClassTy->isSpecialized ())
1921
1932
return createSpecializedStructOrClassType (
1922
1933
ClassTy, Decl, Scope, L.File , L.Line , SizeInBits, AlignInBits,
1923
1934
Flags, MangledName);
@@ -2000,7 +2011,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
2000
2011
auto L = getFileAndLocation (Decl);
2001
2012
unsigned FwdDeclLine = 0 ;
2002
2013
2003
- if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes)
2014
+ if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes)
2004
2015
return createSpecializedStructOrClassType (
2005
2016
ClassTy, Decl, Scope, L.File , L.Line , SizeInBits, AlignInBits,
2006
2017
Flags, MangledName);
@@ -2304,8 +2315,8 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
2304
2315
unsigned CachedSizeInBits = getSizeInBits (CachedType);
2305
2316
if ((SizeInBits && CachedSizeInBits != *SizeInBits) ||
2306
2317
(!SizeInBits && CachedSizeInBits)) {
2307
- // In some situation a specialized type is emitted with size 0, even if
2308
- // the real type has a size.
2318
+ // In some situation a specialized type is emitted with size 0, even if the real
2319
+ // type has a size.
2309
2320
if (DbgTy.getType ()->isSpecialized () && SizeInBits && *SizeInBits > 0 &&
2310
2321
CachedSizeInBits == 0 )
2311
2322
return true ;
@@ -2325,7 +2336,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
2325
2336
// / needed to correctly calculate the layout of more complex types built on
2326
2337
// / top of them.
2327
2338
void createSpecialStlibBuiltinTypes () {
2328
- if (Opts.DebugInfoLevel <= IRGenDebugInfoLevel::ASTTypes)
2339
+ if (Opts.DebugInfoLevel <= IRGenDebugInfoLevel::ASTTypes)
2329
2340
return ;
2330
2341
for (auto BuiltinType: IGM.getOrCreateSpecialStlibBuiltinTypes ()) {
2331
2342
auto DbgTy = DebugTypeInfo::getFromTypeInfo (
@@ -2334,103 +2345,6 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
2334
2345
}
2335
2346
}
2336
2347
2337
- // / A TypeWalker that finds if a given type's mangling is affected by an
2338
- // / @_originallyDefinedIn annotation.
2339
- struct OriginallyDefinedInFinder : public TypeWalker {
2340
- bool visitedOriginallyDefinedIn = false ;
2341
-
2342
- TypeWalker::Action walkToTypePre (Type T) override {
2343
- if (visitedOriginallyDefinedIn)
2344
- return TypeWalker::Action::Stop;
2345
-
2346
- // A typealias inside a function used that function's signature as part of
2347
- // its mangling, so check if any types in the generic signature are
2348
- // annotated with @_originallyDefinedIn.
2349
- if (auto *TAT = llvm::dyn_cast<TypeAliasType>(T)) {
2350
- auto D = TAT->getDecl ()->getDeclContext ();
2351
- if (auto AFD = llvm::dyn_cast<AbstractFunctionDecl>(D)) {
2352
- OriginallyDefinedInFinder InnerWalker;
2353
- AFD->getInterfaceType ().walk (InnerWalker);
2354
- if (InnerWalker.visitedOriginallyDefinedIn ) {
2355
- visitedOriginallyDefinedIn = true ;
2356
- return TypeWalker::Action::Stop;
2357
- }
2358
- }
2359
- }
2360
-
2361
- auto *TypeDecl = T->getNominalOrBoundGenericNominal ();
2362
- if (!TypeDecl)
2363
- return TypeWalker::Action::Continue;
2364
-
2365
- NominalTypeDecl *ParentDecl = TypeDecl;
2366
- while (llvm::isa_and_nonnull<NominalTypeDecl>(ParentDecl->getParent ()))
2367
- ParentDecl = llvm::cast<NominalTypeDecl>(ParentDecl->getParent ());
2368
-
2369
- if (ParentDecl->getAttrs ().hasAttribute <OriginallyDefinedInAttr>()) {
2370
- visitedOriginallyDefinedIn = true ;
2371
- return TypeWalker::Action::Stop;
2372
- }
2373
-
2374
- return TypeWalker::Action::Continue;
2375
- }
2376
- };
2377
-
2378
- // / Returns true if the type's mangled name is affected by an
2379
- // / @_originallyDefinedIn annotation. This annotation can be on the type
2380
- // / itself, one of its generic arguments, etc.
2381
- bool containsOriginallyDefinedIn (Type T) {
2382
- OriginallyDefinedInFinder Walker;
2383
- T.walk (Walker);
2384
- return Walker.visitedOriginallyDefinedIn ;
2385
- }
2386
-
2387
- // / Returns the decl of the type's parent chain annotated by
2388
- // / @_originallyDefinedIn. Returns null if no type is annotated.
2389
- NominalTypeDecl *getDeclAnnotatedByOriginallyDefinedIn (DebugTypeInfo DbgTy) {
2390
- auto Type = DbgTy.getType ();
2391
- auto *TypeDecl = Type->getNominalOrBoundGenericNominal ();
2392
- if (!TypeDecl)
2393
- return nullptr ;
2394
-
2395
- // Find the outermost type, since only those can have @_originallyDefinedIn
2396
- // attached to them.
2397
- NominalTypeDecl *ParentDecl = TypeDecl;
2398
- while (llvm::isa_and_nonnull<NominalTypeDecl>(ParentDecl->getParent ()))
2399
- ParentDecl = llvm::cast<NominalTypeDecl>(ParentDecl->getParent ());
2400
-
2401
- if (ParentDecl->getAttrs ().hasAttribute <OriginallyDefinedInAttr>())
2402
- return ParentDecl;;
2403
-
2404
- return nullptr ;
2405
- }
2406
-
2407
- // / If this is a nominal type that has the @_originallyDefinedIn
2408
- // / attribute, IRGenDebugInfo emits an imported declaration of the type as
2409
- // / a child of the real module. We do this so LLDB has enough
2410
- // / information to both find the type in reflection metadata (the module name
2411
- // / in the type's mangled name), and find it in the swiftmodule (the type's
2412
- // / imported declaration's parent module name).
2413
- void handleOriginallyDefinedIn (DebugTypeInfo DbgTy, llvm::DIType *DITy,
2414
- StringRef MangledName, llvm::DIFile *File) {
2415
- if (OriginallyDefinedInTypes.contains (MangledName))
2416
- return ;
2417
-
2418
- // Force the generation of the generic type parameters as forward
2419
- // declarations, as those types might be annotated with
2420
- // @_originallyDefinedIn.
2421
- if (auto *BoundDecl = llvm::dyn_cast<BoundGenericType>(DbgTy.getType ()))
2422
- collectGenericParams (BoundDecl, /* AsForwardDeclarations=*/ true );
2423
-
2424
- NominalTypeDecl *OriginallyDefinedInDecl = getDeclAnnotatedByOriginallyDefinedIn (DbgTy);
2425
- if (!OriginallyDefinedInDecl)
2426
- return ;
2427
-
2428
- // Emit the imported declaration under the real swiftmodule the type lives on.
2429
- auto RealModule = getOrCreateContext (OriginallyDefinedInDecl->getParent ());
2430
- DBuilder.createImportedDeclaration (RealModule, DITy, File, 0 , MangledName);
2431
- OriginallyDefinedInTypes.insert (MangledName);
2432
- }
2433
-
2434
2348
llvm::DIType *getOrCreateType (DebugTypeInfo DbgTy,
2435
2349
llvm::DIScope *Scope = nullptr ) {
2436
2350
// Is this an empty type?
@@ -2475,18 +2389,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
2475
2389
ClangDecl = AliasDecl->getClangDecl ();
2476
2390
} else if (auto *ND = DbgTy.getType ()->getNominalOrBoundGenericNominal ()) {
2477
2391
TypeDecl = ND;
2478
- // If this is an originally defined in type, we want to emit this type's
2479
- // scope to be the ABI module.
2480
- if (auto Attribute =
2481
- ND->getAttrs ().getAttribute <OriginallyDefinedInAttr>()) {
2482
- auto Identifier = IGM.getSILModule ().getASTContext ().getIdentifier (
2483
- Attribute->OriginalModuleName );
2484
- void *Key = (void *)Identifier.get ();
2485
- Scope =
2486
- getOrCreateModule (Key, TheCU, Attribute->OriginalModuleName , {});
2487
- } else {
2488
- Context = ND->getParent ();
2489
- }
2392
+ Context = ND->getParent ();
2490
2393
ClangDecl = ND->getClangDecl ();
2491
2394
} else if (auto BNO = dyn_cast<BuiltinType>(DbgTy.getType ())) {
2492
2395
Context = BNO->getASTContext ().TheBuiltinModule ;
@@ -2535,8 +2438,6 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
2535
2438
FwdDeclTypes.emplace_back (
2536
2439
std::piecewise_construct, std::make_tuple (MangledName),
2537
2440
std::make_tuple (static_cast <llvm::Metadata *>(FwdDecl)));
2538
-
2539
- handleOriginallyDefinedIn (DbgTy, FwdDecl, MangledName, getFile (Scope));
2540
2441
return FwdDecl;
2541
2442
}
2542
2443
llvm::DIType *DITy = createType (DbgTy, MangledName, Scope, getFile (Scope));
@@ -2565,7 +2466,6 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
2565
2466
// Store it in the cache.
2566
2467
DITypeCache.insert ({DbgTy.getType (), llvm::TrackingMDNodeRef (DITy)});
2567
2468
2568
- handleOriginallyDefinedIn (DbgTy, DITy, MangledName, getFile (Scope));
2569
2469
return DITy;
2570
2470
}
2571
2471
};
0 commit comments