Skip to content

Commit 59282c4

Browse files
authored
Merge pull request #22407 from slavapestov/fix-demangler-circularity
AST: ASTMangler should not depend on ASTDemangler
2 parents d57880e + cc8236f commit 59282c4

File tree

3 files changed

+56
-61
lines changed

3 files changed

+56
-61
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ class ASTMangler : public Mangler {
158158
GenericSignature *signature,
159159
ResilienceExpansion expansion);
160160

161-
std::string mangleTypeForDebugger(Type decl, const DeclContext *DC,
162-
bool verify=false);
161+
std::string mangleTypeForDebugger(Type decl, const DeclContext *DC);
163162

164163
std::string mangleDeclType(const ValueDecl *decl);
165164

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@
4242
#include "llvm/Support/raw_ostream.h"
4343
#include "llvm/Support/CommandLine.h"
4444

45-
#ifndef NDEBUG
46-
#include "swift/AST/ASTDemangler.h"
47-
#endif
48-
4945
using namespace swift;
5046
using namespace swift::Mangle;
5147

@@ -364,30 +360,7 @@ std::string ASTMangler::mangleReabstractionThunkHelper(
364360
return finalize();
365361
}
366362

367-
#ifndef NDEBUG
368-
static bool areTypesReallyEqual(Type lhs, Type rhs) {
369-
// Due to an oversight, escaping and non-escaping @convention(block)
370-
// are mangled identically.
371-
auto eraseEscapingBlock = [](Type t) -> Type {
372-
return t.transform([](Type t) -> Type {
373-
if (auto *fnType = t->getAs<FunctionType>()) {
374-
if (fnType->getExtInfo().getRepresentation()
375-
== FunctionTypeRepresentation::Block) {
376-
return FunctionType::get(fnType->getParams(),
377-
fnType->getResult(),
378-
fnType->getExtInfo().withNoEscape(true));
379-
}
380-
}
381-
return t;
382-
});
383-
};
384-
385-
return eraseEscapingBlock(lhs)->isEqual(eraseEscapingBlock(rhs));
386-
}
387-
#endif
388-
389-
std::string ASTMangler::mangleTypeForDebugger(Type Ty, const DeclContext *DC,
390-
bool verify) {
363+
std::string ASTMangler::mangleTypeForDebugger(Type Ty, const DeclContext *DC) {
391364
PrettyStackTraceType prettyStackTrace(Ty->getASTContext(),
392365
"mangling type for debugger", Ty);
393366

@@ -400,33 +373,7 @@ std::string ASTMangler::mangleTypeForDebugger(Type Ty, const DeclContext *DC,
400373

401374
appendType(Ty);
402375
appendOperator("D");
403-
std::string result = finalize();
404-
405-
if (!verify)
406-
return result;
407-
408-
// Make sure we can reconstruct mangled types for the debugger.
409-
#ifndef NDEBUG
410-
auto &Ctx = Ty->getASTContext();
411-
Type Reconstructed = Demangle::getTypeForMangling(Ctx, result);
412-
if (!Reconstructed) {
413-
llvm::errs() << "Failed to reconstruct type for " << result << "\n";
414-
llvm::errs() << "Original type:\n";
415-
Ty->dump();
416-
abort();
417-
} else if (!Reconstructed->isEqual(Ty)) {
418-
if (!areTypesReallyEqual(Reconstructed, Ty)) {
419-
llvm::errs() << "Incorrect reconstructed type for " << result << "\n";
420-
llvm::errs() << "Original type:\n";
421-
Ty->dump();
422-
llvm::errs() << "Reconstructed type:\n";
423-
Reconstructed->dump();
424-
abort();
425-
}
426-
}
427-
#endif
428-
429-
return result;
376+
return finalize();
430377
}
431378

432379
std::string ASTMangler::mangleDeclType(const ValueDecl *decl) {

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
#include "llvm/Support/raw_ostream.h"
5858
#include "llvm/Transforms/Utils/Local.h"
5959

60+
#ifndef NDEBUG
61+
#include "swift/AST/ASTDemangler.h"
62+
#endif
63+
6064
using namespace swift;
6165
using namespace irgen;
6266

@@ -688,6 +692,28 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
688692
return Size(size);
689693
}
690694

695+
#ifndef NDEBUG
696+
static bool areTypesReallyEqual(Type lhs, Type rhs) {
697+
// Due to an oversight, escaping and non-escaping @convention(block)
698+
// are mangled identically.
699+
auto eraseEscapingBlock = [](Type t) -> Type {
700+
return t.transform([](Type t) -> Type {
701+
if (auto *fnType = t->getAs<FunctionType>()) {
702+
if (fnType->getExtInfo().getRepresentation()
703+
== FunctionTypeRepresentation::Block) {
704+
return FunctionType::get(fnType->getParams(),
705+
fnType->getResult(),
706+
fnType->getExtInfo().withNoEscape(true));
707+
}
708+
}
709+
return t;
710+
});
711+
};
712+
713+
return eraseEscapingBlock(lhs)->isEqual(eraseEscapingBlock(rhs));
714+
}
715+
#endif
716+
691717
StringRef getMangledName(DebugTypeInfo DbgTy) {
692718
if (MetadataTypeDecl && DbgTy.getDecl() == MetadataTypeDecl)
693719
return BumpAllocatedString(DbgTy.getDecl()->getName().str());
@@ -697,10 +723,33 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
697723
Ty = Ty->mapTypeOutOfContext();
698724

699725
Mangle::ASTMangler Mangler;
700-
std::string Name = Mangler.mangleTypeForDebugger(
701-
Ty, DbgTy.getDeclContext(),
702-
!Opts.DisableRoundTripDebugTypes);
703-
return BumpAllocatedString(Name);
726+
std::string Result = Mangler.mangleTypeForDebugger(
727+
Ty, DbgTy.getDeclContext());
728+
729+
if (!Opts.DisableRoundTripDebugTypes) {
730+
// Make sure we can reconstruct mangled types for the debugger.
731+
#ifndef NDEBUG
732+
auto &Ctx = Ty->getASTContext();
733+
Type Reconstructed = Demangle::getTypeForMangling(Ctx, Result);
734+
if (!Reconstructed) {
735+
llvm::errs() << "Failed to reconstruct type for " << Result << "\n";
736+
llvm::errs() << "Original type:\n";
737+
Ty->dump();
738+
abort();
739+
} else if (!Reconstructed->isEqual(Ty)) {
740+
if (!areTypesReallyEqual(Reconstructed, Ty)) {
741+
llvm::errs() << "Incorrect reconstructed type for " << Result << "\n";
742+
llvm::errs() << "Original type:\n";
743+
Ty->dump();
744+
llvm::errs() << "Reconstructed type:\n";
745+
Reconstructed->dump();
746+
abort();
747+
}
748+
}
749+
#endif
750+
}
751+
752+
return BumpAllocatedString(Result);
704753
}
705754

706755
llvm::DIDerivedType *createMemberType(DebugTypeInfo DbgTy, StringRef Name,

0 commit comments

Comments
 (0)