Skip to content

Commit 57f3bfc

Browse files
authored
Merge pull request #3188 from augusto2112/reconstruct-type-hierarchy
[lldb] Find public Objective-C class when reconstructing type
2 parents 577bf6d + 33bf8f7 commit 57f3bfc

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4305,8 +4305,11 @@ CompilerType SwiftASTContext::GetAsClangType(ConstString mangled_name) {
43054305
// that look like they might be come from Objective-C (or C) as
43064306
// Clang types. LLDB's Objective-C part is very robust against
43074307
// malformed object pointers, so this isn't very risky.
4308+
Module *module = GetModule();
4309+
if (!module)
4310+
return {};
43084311
auto type_system_or_err =
4309-
GetModule()->GetTypeSystemForLanguage(eLanguageTypeObjC);
4312+
module->GetTypeSystemForLanguage(eLanguageTypeObjC);
43104313
if (!type_system_or_err) {
43114314
llvm::consumeError(type_system_or_err.takeError());
43124315
return {};
@@ -4327,8 +4330,10 @@ CompilerType SwiftASTContext::GetAsClangType(ConstString mangled_name) {
43274330
// Import the Clang type into the Clang context.
43284331
if (!clang_type)
43294332
return {};
4330-
clang_type =
4331-
clang_ast_parser->GetClangASTImporter().CopyType(*clang_ctx, clang_type);
4333+
4334+
if (clang_type.GetTypeSystem() != clang_ctx)
4335+
clang_type = clang_ast_parser->GetClangASTImporter().CopyType(*clang_ctx,
4336+
clang_type);
43324337
// Swift doesn't know pointers. Convert top-level
43334338
// Objective-C object types to object pointers for Clang.
43344339
auto qual_type =
@@ -4401,6 +4406,31 @@ swift::TypeBase *SwiftASTContext::ReconstructType(ConstString mangled_typename,
44014406
*ast_ctx, mangled_typename.GetStringRef())
44024407
.getPointer();
44034408

4409+
// Objective-C classes sometimes have private subclasses that are invisible to the Swift compiler because they are declared and defined in a .m file. If we can't reconstruct an ObjC type, walk up the type hierarchy until we find something we can import, or until we run out of types
4410+
while (!found_type) {
4411+
CompilerType clang_type = GetAsClangType(mangled_typename);
4412+
if (!clang_type)
4413+
break;
4414+
4415+
auto *clang_ctx =
4416+
llvm::dyn_cast_or_null<TypeSystemClang>(clang_type.GetTypeSystem());
4417+
if (!clang_ctx)
4418+
break;
4419+
auto *interface_decl = TypeSystemClang::GetAsObjCInterfaceDecl(clang_type);
4420+
if (!interface_decl)
4421+
break;
4422+
auto *super_interface_decl = interface_decl->getSuperClass();
4423+
if (!super_interface_decl)
4424+
break;
4425+
CompilerType super_type = clang_ctx->GetTypeForDecl(super_interface_decl);
4426+
if (!super_type)
4427+
break;
4428+
auto super_mangled_typename = super_type.GetMangledTypeName();
4429+
found_type = swift::Demangle::getTypeForMangling(
4430+
*ast_ctx, super_mangled_typename.GetStringRef())
4431+
.getPointer();
4432+
}
4433+
44044434
if (found_type) {
44054435
swift::TypeBase *ast_type =
44064436
ConvertSILFunctionTypesToASTFunctionTypes(found_type).getPointer();

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2780,14 +2780,9 @@ bool TypeSystemSwiftTypeRef::IsImportedType(opaque_compiler_type_t type,
27802780
return true;
27812781
};
27822782
FALLBACK(IsImportedType, (ReconstructType(type), original_type));
2783-
// Dont compare the results if there is no ClangImporter in the SwiftASTContext.
2784-
const auto &props = ModuleList::GetGlobalModuleListProperties();
2785-
if (!props.GetUseSwiftClangImporter())
2786-
return impl();
2787-
2788-
VALIDATE_AND_RETURN(impl, IsImportedType, type,
2789-
(ReconstructType(type), nullptr),
2790-
(ReconstructType(type), original_type));
2783+
// We can't validate the result because ReconstructType may call this
2784+
// function, causing an infinite loop.
2785+
return impl();
27912786
}
27922787

27932788
bool TypeSystemSwiftTypeRef::IsExistentialType(

0 commit comments

Comments
 (0)