Skip to content

Commit e952eeb

Browse files
authored
Merge pull request #59236 from slavapestov/opaque-result-type-debug-info-mangling-5.7
AST: Fix debug info mangling of opaque result types with @_originallyDefinedIn [5.7]
2 parents 90fc51f + 0dbccc6 commit e952eeb

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ASTMangler : public Mangler {
4343

4444
/// If enabled, non-canonical types are allowed and type alias types get a
4545
/// special mangling.
46-
bool DWARFMangling;
46+
bool DWARFMangling = false;
4747

4848
/// If enabled, entities that ought to have names but don't get a placeholder.
4949
///
@@ -72,6 +72,12 @@ class ASTMangler : public Mangler {
7272
/// function types.
7373
bool Preconcurrency = false;
7474

75+
/// If enabled, declarations annotated with @_originallyDefinedIn are mangled
76+
/// as if they're part of their original module. Disabled for debug mangling,
77+
/// because lldb wants to find declarations in the modules they're currently
78+
/// defined in.
79+
bool RespectOriginallyDefinedIn = true;
80+
7581
public:
7682
using SymbolicReferent = llvm::PointerUnion<const NominalTypeDecl *,
7783
const OpaqueTypeDecl *>;
@@ -110,8 +116,13 @@ class ASTMangler : public Mangler {
110116
BackDeploymentFallback,
111117
};
112118

113-
ASTMangler(bool DWARFMangling = false)
114-
: DWARFMangling(DWARFMangling) {}
119+
/// lldb overrides the defaulted argument to 'true'.
120+
ASTMangler(bool DWARFMangling = false) {
121+
if (DWARFMangling) {
122+
DWARFMangling = true;
123+
RespectOriginallyDefinedIn = false;
124+
}
125+
}
115126

116127
void addTypeSubstitution(Type type, GenericSignature sig) {
117128
type = dropProtocolsFromAssociatedTypes(type, sig);

lib/AST/ASTMangler.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ std::string ASTMangler::mangleTypeForDebugger(Type Ty, GenericSignature sig) {
633633
"mangling type for debugger", Ty);
634634

635635
DWARFMangling = true;
636+
RespectOriginallyDefinedIn = false;
636637
OptimizeProtocolNames = false;
637638
beginMangling();
638639

@@ -651,6 +652,7 @@ std::string ASTMangler::mangleTypeForTypeName(Type type) {
651652

652653
std::string ASTMangler::mangleDeclType(const ValueDecl *decl) {
653654
DWARFMangling = true;
655+
RespectOriginallyDefinedIn = false;
654656
beginMangling();
655657

656658
appendDeclType(decl);
@@ -742,6 +744,7 @@ std::string ASTMangler::mangleTypeAsContextUSR(const NominalTypeDecl *type) {
742744

743745
std::string ASTMangler::mangleTypeAsUSR(Type Ty) {
744746
DWARFMangling = true;
747+
RespectOriginallyDefinedIn = false;
745748
beginMangling();
746749

747750
Ty = getTypeForDWARFMangling(Ty);
@@ -758,6 +761,7 @@ std::string ASTMangler::mangleTypeAsUSR(Type Ty) {
758761

759762
std::string ASTMangler::mangleAnyDecl(const ValueDecl *Decl, bool prefix) {
760763
DWARFMangling = true;
764+
RespectOriginallyDefinedIn = false;
761765
if (prefix) {
762766
beginMangling();
763767
} else {
@@ -1032,6 +1036,14 @@ void ASTMangler::appendOpaqueDeclName(const OpaqueTypeDecl *opaqueDecl) {
10321036
if (canSymbolicReference(opaqueDecl)) {
10331037
appendSymbolicReference(opaqueDecl);
10341038
} else if (auto namingDecl = opaqueDecl->getNamingDecl()) {
1039+
// Set this to true temporarily, even if we're doing DWARF
1040+
// mangling for debug info, where it is false. Otherwise,
1041+
// the mangled opaque result type name will not be able to
1042+
// be looked up, since we rely on an exact match with the
1043+
// ABI name.
1044+
llvm::SaveAndRestore<bool> savedRespectOriginallyDefinedIn(
1045+
RespectOriginallyDefinedIn, true);
1046+
10351047
appendEntity(namingDecl);
10361048
appendOperator("QO");
10371049
} else {
@@ -2269,10 +2281,11 @@ void ASTMangler::appendModule(const ModuleDecl *module,
22692281
// Use the module real name in mangling; this is the physical name
22702282
// of the module on-disk, which can be different if -module-alias is
22712283
// used.
2284+
//
22722285
// For example, if a module Foo has 'import Bar', and '-module-alias Bar=Baz'
22732286
// was passed, the name 'Baz' will be used for mangling besides loading.
22742287
StringRef ModName = module->getRealName().str();
2275-
if (!DWARFMangling &&
2288+
if (RespectOriginallyDefinedIn &&
22762289
module->getABIName() != module->getName()) { // check if the ABI name is set
22772290
ModName = module->getABIName().str();
22782291
}
@@ -2281,7 +2294,7 @@ void ASTMangler::appendModule(const ModuleDecl *module,
22812294
if (ModName == STDLIB_NAME) {
22822295
if (useModuleName.empty()) {
22832296
appendOperator("s");
2284-
} else if (DWARFMangling) {
2297+
} else if (!RespectOriginallyDefinedIn) {
22852298
appendOperator("s");
22862299
} else {
22872300
appendIdentifier(useModuleName);
@@ -2298,11 +2311,11 @@ void ASTMangler::appendModule(const ModuleDecl *module,
22982311
return appendOperator("SC");
22992312
}
23002313

2301-
// Enabling DWARFMangling indicate the mangled names are not part of the ABI,
2302-
// probably used by the debugger or IDE (USR). These mangled names will not be
2303-
// demangled successfully if we use the original module name instead of the
2304-
// actual module name.
2305-
if (!useModuleName.empty() && !DWARFMangling)
2314+
// Disabling RespectOriginallyDefinedIn indicate the mangled names are not part
2315+
// of the ABI, probably used by the debugger or IDE (USR). These mangled names
2316+
// will not be demangled successfully if we use the original module name instead
2317+
// of the actual module name.
2318+
if (!useModuleName.empty() && RespectOriginallyDefinedIn)
23062319
appendIdentifier(useModuleName);
23072320
else
23082321
appendIdentifier(ModName);

lib/SIL/IR/SILType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ bool SILType::isNoReturnFunction(SILModule &M,
152152
}
153153

154154
std::string SILType::getMangledName() const {
155-
Mangle::ASTMangler mangler(false/*use dwarf mangling*/);
155+
Mangle::ASTMangler mangler;
156156
return mangler.mangleTypeWithoutPrefix(getASTType());
157157
}
158158

test/ModuleInterface/originally-defined-attr-opaque-result.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// RUN: %target-swift-frontend -typecheck -emit-module-interface-path %t/CoreVegetable.swiftinterface %S/Inputs/CoreVegetable.swift -disable-availability-checking -enable-library-evolution -swift-version 5
44
// RUN: %target-swift-frontend -typecheck -emit-module-interface-path %t/CoreChef.swiftinterface -module-name CoreChef %s -I %t -disable-availability-checking -enable-library-evolution -swift-version 5 -DLIB
55

6+
// Also build the module itself with -g to exercise debug info round tripping.
7+
// RUN: %target-swift-frontend -emit-ir -g %s -I %t -disable-availability-checking
8+
69
// RUN: %FileCheck %s < %t/CoreChef.swiftinterface
710

811
// REQUIRES: OS=macosx

0 commit comments

Comments
 (0)