Skip to content

Commit 46b41a3

Browse files
authored
Merge pull request #85288 from slavapestov/narrow-cxx-roundtrip-carveout
IRGen: Only skip round-trip demangler check for types that involve C++ types
2 parents 0f58eb2 + d486919 commit 46b41a3

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

lib/AST/Module.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,18 @@ void ModuleDecl::lookupMember(SmallVectorImpl<ValueDecl*> &results,
10111011
} else if (privateDiscriminator.empty()) {
10121012
auto newEnd = std::remove_if(results.begin()+oldSize, results.end(),
10131013
[](const ValueDecl *VD) -> bool {
1014-
return VD->getFormalAccess() <= AccessLevel::FilePrivate;
1014+
// FIXME: The ClangImporter sometimes generates private declarations but
1015+
// doesn't give their file unit a private discriminator so they mangle
1016+
// incorrectly.
1017+
//
1018+
// The hasClangNode() carveout makes the ASTDemangler work properly in
1019+
// this case.
1020+
//
1021+
// We should fix things up so that such declarations also mangle with a
1022+
// a private discriminator, in which case this entry point will be called
1023+
// with the right parameters so that this isn't needed.
1024+
return (VD->getFormalAccess() <= AccessLevel::FilePrivate &&
1025+
!VD->hasClangNode());
10151026
});
10161027
results.erase(newEnd, results.end());
10171028

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,12 +1075,11 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
10751075
CanonicalName.clear();
10761076
}
10771077

1078-
bool IsTypeOriginallyDefinedIn =
1079-
containsOriginallyDefinedIn(DbgTy.getType());
1080-
// TODO(https://github.com/apple/swift/issues/57699): We currently cannot round trip some C++ types.
1078+
bool IsTypeOriginallyDefinedIn = containsOriginallyDefinedIn(DbgTy.getType());
1079+
bool IsCxxType = containsCxxType(DbgTy.getType());
10811080
// There's no way to round trip when respecting @_originallyDefinedIn for a type.
1082-
if (!Opts.DisableRoundTripDebugTypes &&
1083-
!Ty->getASTContext().LangOpts.EnableCXXInterop && !IsTypeOriginallyDefinedIn) {
1081+
// TODO(https://github.com/apple/swift/issues/57699): We currently cannot round trip some C++ types.
1082+
if (!Opts.DisableRoundTripDebugTypes && !IsTypeOriginallyDefinedIn && !IsCxxType) {
10841083
// Make sure we can reconstruct mangled types for the debugger.
10851084
auto &Ctx = Ty->getASTContext();
10861085
Type Reconstructed = Demangle::getTypeForMangling(Ctx, SugaredName, Sig);
@@ -2504,6 +2503,42 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
25042503
return Walker.visitedOriginallyDefinedIn;
25052504
}
25062505

2506+
/// Returns true if the type contains an imported C++ type. Due to
2507+
/// various unimplemented features these cannot round-trip through
2508+
/// the ASTDemangler.
2509+
///
2510+
/// FIXME: Get these cases working with the ASTDemangler instead.
2511+
bool containsCxxType(Type T) {
2512+
return T.findIf([&](Type t) -> bool {
2513+
if (auto *decl = t->getAnyNominal()) {
2514+
if (auto *clangDecl = decl->getClangDecl()) {
2515+
// Lookup of template instantiations is not implemented.
2516+
if (isa<clang::ClassTemplateSpecializationDecl>(clangDecl))
2517+
return true;
2518+
2519+
// Lookup of types in weird contexts is not implemented.
2520+
if (isa<clang::EnumDecl>(clangDecl) ||
2521+
isa<clang::CXXRecordDecl>(clangDecl)) {
2522+
auto *dc = clangDecl->getDeclContext();
2523+
while (!isa<clang::TranslationUnitDecl>(dc)) {
2524+
// ... in namespaces,
2525+
if (isa<clang::NamespaceDecl>(dc))
2526+
return true;
2527+
2528+
// ... or inside other types.
2529+
if (isa<clang::CXXRecordDecl>(dc))
2530+
return true;
2531+
2532+
dc = dc->getParent();
2533+
}
2534+
}
2535+
}
2536+
}
2537+
2538+
return false;
2539+
});
2540+
}
2541+
25072542
/// Returns the decl of the type's parent chain annotated by
25082543
/// @_originallyDefinedIn. Returns null if no type is annotated.
25092544
NominalTypeDecl *getDeclAnnotatedByOriginallyDefinedIn(DebugTypeInfo DbgTy) {

0 commit comments

Comments
 (0)