Skip to content

Commit 553b0e9

Browse files
committed
Revert "Merge pull request swiftlang#78104 from augusto2112/orig-defined-in"
This reverts commit dccc52d, reversing changes made to 2571e05.
1 parent e87c1c3 commit 553b0e9

9 files changed

+58
-232
lines changed

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ std::string ASTMangler::mangleTypeForDebugger(Type Ty, GenericSignature sig) {
717717
Ty);
718718

719719
DWARFMangling = true;
720-
RespectOriginallyDefinedIn = true;
720+
RespectOriginallyDefinedIn = false;
721721
OptimizeProtocolNames = false;
722722
beginMangling();
723723

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ const ExternalSourceLocs *Decl::getSerializedLocs() const {
979979
StringRef Decl::getAlternateModuleName() const {
980980
for (auto *Att: Attrs) {
981981
if (auto *OD = dyn_cast<OriginallyDefinedInAttr>(Att)) {
982-
if (!OD->isInvalid() && OD->isActivePlatform(getASTContext())) {
982+
if (OD->isActivePlatform(getASTContext())) {
983983
return OD->OriginalModuleName;
984984
}
985985
}

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 33 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,13 @@
2323
#include "IRBuilder.h"
2424
#include "swift/AST/ASTDemangler.h"
2525
#include "swift/AST/ASTMangler.h"
26-
#include "swift/AST/Attr.h"
27-
#include "swift/AST/Decl.h"
2826
#include "swift/AST/Expr.h"
2927
#include "swift/AST/GenericEnvironment.h"
3028
#include "swift/AST/IRGenOptions.h"
3129
#include "swift/AST/Module.h"
3230
#include "swift/AST/ModuleLoader.h"
33-
#include "swift/AST/ParameterList.h"
3431
#include "swift/AST/Pattern.h"
3532
#include "swift/AST/TypeDifferenceVisitor.h"
36-
#include "swift/AST/TypeWalker.h"
3733
#include "swift/Basic/Assertions.h"
3834
#include "swift/Basic/Compiler.h"
3935
#include "swift/Basic/SourceManager.h"
@@ -141,7 +137,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
141137
return Line == other.Line && Column == other.Column && File == other.File;
142138
}
143139
};
144-
140+
145141
/// Various caches.
146142
/// \{
147143
llvm::StringSet<> VarNames;
@@ -154,7 +150,6 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
154150
llvm::DenseMap<const void *, llvm::TrackingMDNodeRef> DIModuleCache;
155151
llvm::StringMap<llvm::TrackingMDNodeRef> DIFileCache;
156152
llvm::StringMap<llvm::TrackingMDNodeRef> RuntimeErrorFnCache;
157-
llvm::StringSet<> OriginallyDefinedInTypes;
158153
TrackingDIRefMap DIRefMap;
159154
TrackingDIRefMap InnerTypeCache;
160155
/// \}
@@ -1031,12 +1026,9 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
10311026
Mangle::ASTMangler Mangler(IGM.Context);
10321027
std::string Result = Mangler.mangleTypeForDebugger(Ty, Sig);
10331028

1034-
bool IsTypeOriginallyDefinedIn =
1035-
containsOriginallyDefinedIn(DbgTy.getType());
10361029
// 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.
10381030
if (!Opts.DisableRoundTripDebugTypes &&
1039-
!Ty->getASTContext().LangOpts.EnableCXXInterop && !IsTypeOriginallyDefinedIn) {
1031+
!Ty->getASTContext().LangOpts.EnableCXXInterop) {
10401032
// Make sure we can reconstruct mangled types for the debugger.
10411033
auto &Ctx = Ty->getASTContext();
10421034
Type Reconstructed = Demangle::getTypeForMangling(Ctx, Result, Sig);
@@ -1515,7 +1507,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
15151507
/// anchor any typedefs that may appear in parameters so they can be
15161508
/// resolved in the debugger without needing to query the Swift module.
15171509
llvm::DINodeArray
1518-
collectGenericParams(NominalOrBoundGenericNominalType *BGT, bool AsForwardDeclarations = false) {
1510+
collectGenericParams(NominalOrBoundGenericNominalType *BGT) {
15191511

15201512
// Collect the generic args from the type and its parent.
15211513
std::vector<Type> GenericArgs;
@@ -1530,8 +1522,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
15301522
SmallVector<llvm::Metadata *, 16> TemplateParams;
15311523
for (auto Arg : GenericArgs) {
15321524
DebugTypeInfo ParamDebugType;
1533-
if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes &&
1534-
!AsForwardDeclarations)
1525+
if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes)
15351526
// For the DwarfTypes level don't generate just a forward declaration
15361527
// for the generic type parameters.
15371528
ParamDebugType = DebugTypeInfo::getFromTypeInfo(
@@ -1799,19 +1790,39 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
17991790
}
18001791

18011792
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+
}
18021813

18031814
// Here goes!
18041815
switch (BaseTy->getKind()) {
18051816
case TypeKind::BuiltinUnboundGeneric:
18061817
llvm_unreachable("not a real type");
1807-
1818+
18081819
case TypeKind::BuiltinFixedArray: {
18091820
// TODO: provide proper array debug info
18101821
unsigned FwdDeclLine = 0;
18111822
return createOpaqueStruct(Scope, "Builtin.FixedArray", MainFile, FwdDeclLine,
18121823
SizeInBits, AlignInBits, Flags, MangledName);
18131824
}
1814-
1825+
18151826
case TypeKind::BuiltinPackIndex:
18161827
case TypeKind::BuiltinInteger: {
18171828
Encoding = llvm::dwarf::DW_ATE_unsigned;
@@ -1835,7 +1846,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
18351846
break;
18361847
}
18371848

1838-
case TypeKind::BuiltinNativeObject:
1849+
case TypeKind::BuiltinNativeObject:
18391850
case TypeKind::BuiltinBridgeObject:
18401851
case TypeKind::BuiltinRawPointer:
18411852
case TypeKind::BuiltinRawUnsafeContinuation:
@@ -1917,7 +1928,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
19171928
assert(SizeInBits ==
19181929
CI.getTargetInfo().getPointerWidth(clang::LangAS::Default));
19191930
if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes) {
1920-
if (ClassTy->isSpecialized())
1931+
if (ClassTy->isSpecialized())
19211932
return createSpecializedStructOrClassType(
19221933
ClassTy, Decl, Scope, L.File, L.Line, SizeInBits, AlignInBits,
19231934
Flags, MangledName);
@@ -2000,7 +2011,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
20002011
auto L = getFileAndLocation(Decl);
20012012
unsigned FwdDeclLine = 0;
20022013

2003-
if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes)
2014+
if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes)
20042015
return createSpecializedStructOrClassType(
20052016
ClassTy, Decl, Scope, L.File, L.Line, SizeInBits, AlignInBits,
20062017
Flags, MangledName);
@@ -2304,8 +2315,8 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
23042315
unsigned CachedSizeInBits = getSizeInBits(CachedType);
23052316
if ((SizeInBits && CachedSizeInBits != *SizeInBits) ||
23062317
(!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.
23092320
if (DbgTy.getType()->isSpecialized() && SizeInBits && *SizeInBits > 0 &&
23102321
CachedSizeInBits == 0)
23112322
return true;
@@ -2325,7 +2336,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
23252336
/// needed to correctly calculate the layout of more complex types built on
23262337
/// top of them.
23272338
void createSpecialStlibBuiltinTypes() {
2328-
if (Opts.DebugInfoLevel <= IRGenDebugInfoLevel::ASTTypes)
2339+
if (Opts.DebugInfoLevel <= IRGenDebugInfoLevel::ASTTypes)
23292340
return;
23302341
for (auto BuiltinType: IGM.getOrCreateSpecialStlibBuiltinTypes()) {
23312342
auto DbgTy = DebugTypeInfo::getFromTypeInfo(
@@ -2334,103 +2345,6 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
23342345
}
23352346
}
23362347

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-
24342348
llvm::DIType *getOrCreateType(DebugTypeInfo DbgTy,
24352349
llvm::DIScope *Scope = nullptr) {
24362350
// Is this an empty type?
@@ -2475,18 +2389,7 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
24752389
ClangDecl = AliasDecl->getClangDecl();
24762390
} else if (auto *ND = DbgTy.getType()->getNominalOrBoundGenericNominal()) {
24772391
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();
24902393
ClangDecl = ND->getClangDecl();
24912394
} else if (auto BNO = dyn_cast<BuiltinType>(DbgTy.getType())) {
24922395
Context = BNO->getASTContext().TheBuiltinModule;
@@ -2535,8 +2438,6 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
25352438
FwdDeclTypes.emplace_back(
25362439
std::piecewise_construct, std::make_tuple(MangledName),
25372440
std::make_tuple(static_cast<llvm::Metadata *>(FwdDecl)));
2538-
2539-
handleOriginallyDefinedIn(DbgTy, FwdDecl, MangledName, getFile(Scope));
25402441
return FwdDecl;
25412442
}
25422443
llvm::DIType *DITy = createType(DbgTy, MangledName, Scope, getFile(Scope));
@@ -2565,7 +2466,6 @@ createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
25652466
// Store it in the cache.
25662467
DITypeCache.insert({DbgTy.getType(), llvm::TrackingMDNodeRef(DITy)});
25672468

2568-
handleOriginallyDefinedIn(DbgTy, DITy, MangledName, getFile(Scope));
25692469
return DITy;
25702470
}
25712471
};
Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,2 @@
1-
@_originallyDefinedIn(
2-
module: "Barn", iOS 2.0, macOS 2.0, tvOS 2.0, watchOS 2.0)
3-
@available(iOS 1.0, macOS 1.0, tvOS 1.0, watchOS 1.0, *)
4-
public struct Horse {
5-
public init() {}
6-
}
7-
8-
@_originallyDefinedIn(
9-
module: "Barn", iOS 2.0, macOS 2.0, tvOS 2.0, watchOS 2.0)
10-
@available(iOS 1.0, macOS 1.0, tvOS 1.0, watchOS 1.0, *)
11-
public class Cow {
12-
public init() {}
13-
}
14-
15-
16-
@_originallyDefinedIn(
17-
module: "Barn", iOS 2.0, macOS 2.0, tvOS 2.0, watchOS 2.0)
18-
@available(iOS 1.0, macOS 1.0, tvOS 1.0, watchOS 1.0, *)
19-
public class Sheep {
20-
public init() {}
21-
}
22-
1+
@available(macOS 10, *)
2+
@_originallyDefinedIn(module: "Barn", macOS 10.1) public struct Horse {}
Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/local_type_originally_defined_in_other.swiftmodule %S/Inputs/local_type_originally_defined_in_other.swift
3-
// RUN: %target-swift-frontend -I%t -g -emit-ir %s -o - | %FileCheck %s
4-
// REQUIRES: OS=macosx
3+
// RUN: %target-swift-frontend -I%t -g -emit-ir %s
54

65
import local_type_originally_defined_in_other
76

8-
public func definedInOtherModule() {
9-
let s = Sheep()
10-
// CHECK: DICompositeType(tag: DW_TAG_structure_type, name: "Sheep"{{.*}}identifier: "$s4Barn5SheepCD
11-
}
127
public func localTypeAliasTest(horse: Horse) {
138
// The local type mangling for 'A' mentions 'Horse', which must
149
// be mangled using it's current module name, and not the
@@ -18,21 +13,4 @@ public func localTypeAliasTest(horse: Horse) {
1813

1914
let info = UnsafeMutablePointer<A>.allocate(capacity: 1)
2015
_ = info
21-
// CHECK: DIDerivedType(tag: DW_TAG_typedef, name: "$s32local_type_originally_defined_in0A13TypeAliasTest5horsey4Barn5HorseV_tF1AL_aD"
22-
}
23-
24-
public func localTypeAliasTest() -> Horse {
25-
typealias B = Int
26-
27-
let info = UnsafeMutablePointer<B>.allocate(capacity: 1)
28-
_ = info
29-
return Horse()
30-
// CHECK: DIDerivedType(tag: DW_TAG_typedef, name: "$s32local_type_originally_defined_in0A13TypeAliasTest4Barn5HorseVyF1BL_aD"
31-
}
32-
33-
public func localTypeAliasTestGeneric<T: Cow>(cow: T) {
34-
typealias C = Int
35-
36-
let info = UnsafeMutablePointer<C>.allocate(capacity: 1)
37-
_ = info
38-
}
16+
}

test/DebugInfo/module_abi_name.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,11 @@
33
class SomeClass {}
44
// CHECK: DICompositeType(tag: DW_TAG_structure_type, name: "SomeClass",{{.*}}runtimeLang: DW_LANG_Swift, identifier: "$s7Goodbye9SomeClassCD"
55

6+
@available(macOS 10.13, *)
7+
@_originallyDefinedIn(module: "ThirdModule", OSX 10.12)
8+
class DefinedElsewhere {}
9+
// CHECK: DICompositeType(tag: DW_TAG_structure_type, name: "DefinedElsewhere",{{.*}}runtimeLang: DW_LANG_Swift, identifier: "$s7Goodbye16DefinedElsewhereCD")
10+
611
let v1 = SomeClass()
12+
let v2 = DefinedElsewhere()
713

0 commit comments

Comments
 (0)