Skip to content

Commit d676de5

Browse files
committed
[lldb] Factor out iteration over runtime types from GetChildCompilerTypeAtIndex()
This patch introduces a new class SwiftRuntimeTypeVisitor that exists to unify iteration over runtime type information. The visitor callback has closure parameters that can be called to make additional expensive queries on a child. TODO: This is not the final evolution step. - We probably should remove the "depth" parameter entirely and implement the access path computation for GetIndexOfChildMemberWithName at a different layer. - We could cache the results for the last execution context. Relanding with an off-by-one error fixed in GetExistentialSyntheticChildren that was caught by ASAN! (cherry picked from commit 4c6a22a)
1 parent 0380a21 commit d676de5

File tree

7 files changed

+28
-17
lines changed

7 files changed

+28
-17
lines changed

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeDynamicTypeResolution.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,16 +667,30 @@ GetExistentialSyntheticChildren(TypeSystemSwiftTypeRef &ts,
667667
llvm::dyn_cast<swift::reflection::ProtocolCompositionTypeRef>(tr);
668668
if (!protocol_composition_tr)
669669
return children;
670-
if (ti && (llvm::isa<swift::reflection::ReferenceTypeInfo>(ti) ||
671-
llvm::isa<swift::reflection::RecordTypeInfo>(ti))) {
670+
if (!ti)
671+
return children;
672+
auto *rti = llvm::dyn_cast<swift::reflection::RecordTypeInfo>(ti);
673+
if (rti || llvm::isa<swift::reflection::ReferenceTypeInfo>(ti)) {
672674
TypeSystemSwiftTypeRefSP ts_sp = ts.GetTypeSystemSwiftTypeRef();
673675
children.push_back({"object", [=]() {
674676
if (auto *super_class_tr =
675677
protocol_composition_tr->getSuperclass())
676678
return GetTypeFromTypeRef(*ts_sp, super_class_tr);
677679
else
678-
return ts_sp->GetRawPointerType();
679-
}});
680+
return rti ? ts_sp->GetBuiltinUnknownObjectType()
681+
: ts_sp->GetBuiltinRawPointerType();
682+
}});
683+
// We replaced "object" with a more specific type.
684+
if (rti) {
685+
auto &fields = rti->getFields();
686+
for (unsigned i = 1; i < fields.size(); ++i) {
687+
TypeSystemSwiftTypeRefSP ts_sp = ts.GetTypeSystemSwiftTypeRef();
688+
auto *type_ref = fields[i].TR;
689+
children.push_back({fields[i].Name, [=]() {
690+
return GetTypeFromTypeRef(*ts_sp, type_ref);
691+
}});
692+
}
693+
}
680694
}
681695
if (ti && llvm::isa<swift::reflection::RecordTypeInfo>(ti)) {
682696
auto &fields =

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8623,10 +8623,6 @@ std::string SwiftASTContext::GetSwiftName(const clang::Decl *clang_decl,
86238623
return {};
86248624
}
86258625

8626-
CompilerType SwiftASTContext::GetBuiltinRawPointerType() {
8627-
return GetTypeFromMangledTypename(ConstString("$sBpD"));
8628-
}
8629-
86308626
CompilerType
86318627
SwiftASTContext::ConvertClangTypeToSwiftType(CompilerType clang_type) {
86328628
auto ts = GetTypeSystemSwiftTypeRef();

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,6 @@ class SwiftASTContext : public TypeSystemSwift {
412412
std::string GetSwiftName(const clang::Decl *clang_decl,
413413
TypeSystemClang &clang_typesystem) override;
414414

415-
CompilerType GetBuiltinRawPointerType() override;
416415
CompilerType GetBuiltinIntType();
417416

418417
/// Attempts to convert a Clang type into a Swift type.

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ bool TypeSystemSwift::IsScalarType(opaque_compiler_type_t type) {
9898
return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
9999
}
100100

101+
CompilerType TypeSystemSwift::GetBuiltinRawPointerType() {
102+
return GetTypeFromMangledTypename(ConstString("$sBpD"));
103+
}
104+
105+
CompilerType TypeSystemSwift::GetBuiltinUnknownObjectType() {
106+
return GetTypeFromMangledTypename(ConstString("$sBOD"));
107+
}
108+
101109
bool TypeSystemSwift::ShouldTreatScalarValueAsAddress(
102110
opaque_compiler_type_t type) {
103111
return Flags(GetTypeInfo(type, nullptr))

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwift.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ class TypeSystemSwift : public TypeSystem {
197197
virtual std::string GetSwiftName(const clang::Decl *clang_decl,
198198
TypeSystemClang &clang_typesystem) = 0;
199199

200-
virtual CompilerType GetBuiltinRawPointerType() = 0;
200+
CompilerType GetBuiltinRawPointerType();
201+
CompilerType GetBuiltinUnknownObjectType();
201202

202203
/// Attempts to convert a Clang type into a Swift type.
203204
/// For example, int is converted to Int32.

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,11 +1444,6 @@ TypeSystemSwiftTypeRef::GetSwiftName(const clang::Decl *clang_decl,
14441444
return {};
14451445
}
14461446

1447-
CompilerType TypeSystemSwiftTypeRef::GetBuiltinRawPointerType() {
1448-
return GetTypeFromMangledTypename(ConstString("$sBpD"));
1449-
}
1450-
1451-
14521447
static bool IsImportedType(swift::Demangle::NodePointer node) {
14531448
if (!node)
14541449
return false;

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,6 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
412412
std::string GetSwiftName(const clang::Decl *clang_decl,
413413
TypeSystemClang &clang_typesystem) override;
414414

415-
CompilerType GetBuiltinRawPointerType() override;
416-
417415
/// Wrap \p node as \p Global(TypeMangling(node)), remangle the type
418416
/// and create a CompilerType from it.
419417
CompilerType RemangleAsType(swift::Demangle::Demangler &dem,

0 commit comments

Comments
 (0)