Skip to content

[pull] swift/release/5.4 from apple:swift/release/5.4 #1067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lldb/include/lldb/Symbol/CompilerType.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class CompilerType {
std::function<bool(const CompilerType &integer_type, ConstString name,
const llvm::APSInt &value)> const &callback) const;

uint32_t GetNumFields() const;
uint32_t GetNumFields(ExecutionContext *exe_ctx = nullptr) const;

CompilerType GetFieldAtIndex(size_t idx, std::string &name,
uint64_t *bit_offset_ptr,
Expand Down
3 changes: 2 additions & 1 deletion lldb/include/lldb/Symbol/TypeSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ class TypeSystem : public PluginInterface {
ConstString name,
const llvm::APSInt &value)> const &callback) {}

virtual uint32_t GetNumFields(lldb::opaque_compiler_type_t type) = 0;
virtual uint32_t GetNumFields(lldb::opaque_compiler_type_t type,
ExecutionContext *exe_ctx = nullptr) = 0;

virtual CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type,
size_t idx, std::string &name,
Expand Down
4 changes: 4 additions & 0 deletions lldb/include/lldb/Target/SwiftLanguageRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ class SwiftLanguageRuntime : public LanguageRuntime {
bool &child_is_deref_of_parent, ValueObject *valobj,
uint64_t &language_flags);

/// Ask Remote Mirrors about the fields of a composite type.
llvm::Optional<unsigned> GetNumFields(CompilerType type,
ExecutionContext *exe_ctx);

/// Ask Remote Mirrors for the size of a Swift type.
llvm::Optional<uint64_t> GetBitSize(CompilerType type,
ExecutionContextScope *exe_scope);
Expand Down
3 changes: 2 additions & 1 deletion lldb/source/Plugins/Language/Swift/SwiftLanguage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,8 @@ SwiftLanguage::GetHardcodedSynthetics() {
is_imported = true;
}

if (is_imported && type.GetNumFields() == 0)
ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
if (is_imported && type.GetNumFields(&exe_ctx) == 0)
return true;
if (valobj.IsBaseClass() && type.IsRuntimeGeneratedType()) {
auto parent(valobj.GetParent());
Expand Down
3 changes: 2 additions & 1 deletion lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5416,7 +5416,8 @@ void TypeSystemClang::ForEachEnumerator(

#pragma mark Aggregate Types

uint32_t TypeSystemClang::GetNumFields(lldb::opaque_compiler_type_t type) {
uint32_t TypeSystemClang::GetNumFields(lldb::opaque_compiler_type_t type,
ExecutionContext *exe_ctx) {
if (!type)
return 0;

Expand Down
3 changes: 2 additions & 1 deletion lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,8 @@ class TypeSystemClang : public TypeSystem {
ConstString name,
const llvm::APSInt &value)> const &callback) override;

uint32_t GetNumFields(lldb::opaque_compiler_type_t type) override;
uint32_t GetNumFields(lldb::opaque_compiler_type_t type,
ExecutionContext *exe_ctx = nullptr) override;

CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type, size_t idx,
std::string &name, uint64_t *bit_offset_ptr,
Expand Down
3 changes: 2 additions & 1 deletion lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6384,7 +6384,8 @@ SwiftASTContext::GetNumDirectBaseClasses(opaque_compiler_type_t opaque_type) {
return 0;
}

uint32_t SwiftASTContext::GetNumFields(opaque_compiler_type_t type) {
uint32_t SwiftASTContext::GetNumFields(opaque_compiler_type_t type,
ExecutionContext *exe_ctx) {
VALID_OR_RETURN(0);

if (!type)
Expand Down
3 changes: 2 additions & 1 deletion lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ class SwiftASTContext : public TypeSystemSwift {
bool omit_empty_base_classes,
const ExecutionContext *exe_ctx) override;

uint32_t GetNumFields(lldb::opaque_compiler_type_t type) override;
uint32_t GetNumFields(lldb::opaque_compiler_type_t type,
ExecutionContext *exe_ctx = nullptr) override;

CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type, size_t idx,
std::string &name, uint64_t *bit_offset_ptr,
Expand Down
26 changes: 24 additions & 2 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2231,9 +2231,31 @@ TypeSystemSwiftTypeRef::GetNumChildren(opaque_compiler_type_t type,
ReconstructType(type), omit_empty_base_classes, exe_ctx);
}

uint32_t TypeSystemSwiftTypeRef::GetNumFields(opaque_compiler_type_t type) {
return m_swift_ast_context->GetNumFields(ReconstructType(type));
uint32_t TypeSystemSwiftTypeRef::GetNumFields(opaque_compiler_type_t type,
ExecutionContext *exe_ctx) {
if (exe_ctx)
if (auto *runtime = SwiftLanguageRuntime::Get(exe_ctx->GetProcessSP()))
if (auto num_fields =
runtime->GetNumFields(GetCanonicalType(type), exe_ctx))
// Use a lambda to intercept & unwrap the `Optional` return value from
// `SwiftLanguageRuntime::GetNumFields`.
return [&] {
auto impl = [&]() -> llvm::Optional<uint32_t> {
if (!type)
return 0;
return num_fields;
};
VALIDATE_AND_RETURN(impl, GetNumFields, type,
(ReconstructType(type), exe_ctx));
}().getValue();

LLDB_LOGF(GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES),
"Using SwiftASTContext::GetNumFields fallback for type %s",
AsMangledName(type));

return m_swift_ast_context->GetNumFields(ReconstructType(type), exe_ctx);
}

CompilerType TypeSystemSwiftTypeRef::GetFieldAtIndex(
opaque_compiler_type_t type, size_t idx, std::string &name,
uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
uint32_t GetNumChildren(lldb::opaque_compiler_type_t type,
bool omit_empty_base_classes,
const ExecutionContext *exe_ctx) override;
uint32_t GetNumFields(lldb::opaque_compiler_type_t type) override;
uint32_t GetNumFields(lldb::opaque_compiler_type_t type,
ExecutionContext *exe_ctx = nullptr) override;
CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type, size_t idx,
std::string &name, uint64_t *bit_offset_ptr,
uint32_t *bitfield_bit_size_ptr,
Expand Down
4 changes: 2 additions & 2 deletions lldb/source/Symbol/CompilerType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,10 @@ void CompilerType::ForEachEnumerator(
return m_type_system->ForEachEnumerator(m_type, callback);
}

uint32_t CompilerType::GetNumFields() const {
uint32_t CompilerType::GetNumFields(ExecutionContext *exe_ctx) const {
if (!IsValid())
return 0;
return m_type_system->GetNumFields(m_type);
return m_type_system->GetNumFields(m_type, exe_ctx);
}

CompilerType CompilerType::GetFieldAtIndex(size_t idx, std::string &name,
Expand Down
12 changes: 12 additions & 0 deletions lldb/source/Target/SwiftLanguageRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ class SwiftLanguageRuntimeStub {
return {};
}

llvm::Optional<unsigned> GetNumFields(CompilerType type,
ExecutionContext *exe_ctx) {
STUB_LOG();
return {};
}

bool GetObjectDescription(Stream &str, ValueObject &object) {
STUB_LOG();
return false;
Expand Down Expand Up @@ -2185,6 +2191,12 @@ CompilerType SwiftLanguageRuntime::GetChildCompilerTypeAtIndex(
language_flags);
}

llvm::Optional<unsigned>
SwiftLanguageRuntime::GetNumFields(CompilerType type,
ExecutionContext *exe_ctx) {
FORWARD(GetNumFields, type, exe_ctx);
}

bool SwiftLanguageRuntime::GetObjectDescription(Stream &str,
ValueObject &object) {
FORWARD(GetObjectDescription, str, object);
Expand Down
64 changes: 64 additions & 0 deletions lldb/source/Target/SwiftLanguageRuntimeDynamicTypeResolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,70 @@ SwiftLanguageRuntimeImpl::GetNumChildren(CompilerType type,
return {};
}

llvm::Optional<unsigned>
SwiftLanguageRuntimeImpl::GetNumFields(CompilerType type,
ExecutionContext *exe_ctx) {
auto *ts =
llvm::dyn_cast_or_null<TypeSystemSwiftTypeRef>(type.GetTypeSystem());
if (!ts)
return {};

using namespace swift::reflection;
// Try the static type metadata.
const TypeRef *tr = nullptr;
auto *ti = GetTypeInfo(type, exe_ctx->GetFramePtr(), &tr);
// Structs and Tuples.
switch (ti->getKind()) {
case TypeInfoKind::Record: {
// Structs and Tuples.
auto *rti = llvm::cast<RecordTypeInfo>(ti);
switch (rti->getRecordKind()) {
case RecordKind::ExistentialMetatype:
case RecordKind::ThickFunction:
// There are two fields, `function` and `context`, but they're not exposed
// by lldb.
return 0;
case RecordKind::OpaqueExistential:
// `OpaqueExistential` is documented as:
// An existential is a three-word buffer followed by value metadata...
// The buffer is exposed as fields named `payload_data_{0,1,2}`, and
// the number of fields are increased to match.
return rti->getNumFields() + 3;
default:
return rti->getNumFields();
}
}
case TypeInfoKind::Enum: {
auto *eti = llvm::cast<EnumTypeInfo>(ti);
return eti->getNumPayloadCases();
}
case TypeInfoKind::Reference: {
// Objects.
auto *rti = llvm::cast<ReferenceTypeInfo>(ti);
switch (rti->getReferenceKind()) {
case ReferenceKind::Weak:
case ReferenceKind::Unowned:
case ReferenceKind::Unmanaged:
if (auto referent = GetWeakReferent(*ts, type))
return referent.GetNumFields(exe_ctx);
return 0;
case ReferenceKind::Strong:
TypeConverter tc(GetReflectionContext()->getBuilder());
LLDBTypeInfoProvider tip(*this, *ts);
auto *cti = tc.getClassInstanceTypeInfo(tr, 0, &tip);
if (auto *rti = llvm::dyn_cast_or_null<RecordTypeInfo>(cti)) {
return rti->getNumFields();
}

return {};
}
}
default:
// FIXME: Implement more cases.
return {};
}
}

/// Return the base name of the topmost nominal type.
static llvm::StringRef GetBaseName(swift::Demangle::NodePointer node) {
if (!node)
Expand Down
3 changes: 3 additions & 0 deletions lldb/source/Target/SwiftLanguageRuntimeImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ class SwiftLanguageRuntimeImpl {
llvm::Optional<unsigned> GetNumChildren(CompilerType type,
ValueObject *valobj);

llvm::Optional<unsigned> GetNumFields(CompilerType type,
ExecutionContext *exe_ctx);

llvm::Optional<size_t> GetIndexOfChildMemberWithName(
CompilerType type, llvm::StringRef name, ExecutionContext *exe_ctx,
bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes);
Expand Down