Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,10 @@ class SwiftLanguageRuntime : public LanguageRuntime {
llvm::Expected<const swift::reflection::TypeRef &>
GetTypeRef(CompilerType type, TypeSystemSwiftTypeRef *module_holder);

/// Returns a non-null type system or an error.
static llvm::Expected<TypeSystemSwiftTypeRefSP>
GetReflectionTypeSystem(CompilerType for_type, ExecutionContext exe_ctx);

/// Ask Remote Mirrors for the type info about a Swift type.
/// This will return a nullptr if the lookup fails.
llvm::Expected<const swift::reflection::TypeInfo &>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

#include "LLDBMemoryReader.h"
#include "Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h"
#include "ReflectionContextInterface.h"
#include "SwiftLanguageRuntime.h"
#include "SwiftMetadataCache.h"
Expand All @@ -31,6 +32,7 @@
#include "lldb/ValueObject/ValueObjectCast.h"
#include "lldb/ValueObject/ValueObjectMemory.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Casting.h"

#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTMangler.h"
Expand Down Expand Up @@ -607,13 +609,14 @@ std::optional<uint64_t> SwiftLanguageRuntime::GetMemberVariableOffset(
namespace {

CompilerType GetTypeFromTypeRef(TypeSystemSwiftTypeRef &ts,
const swift::reflection::TypeRef *type_ref) {
const swift::reflection::TypeRef *type_ref,
swift::Mangle::ManglingFlavor flavor) {
if (!type_ref)
return {};
swift::Demangle::Demangler dem;
swift::Demangle::NodePointer node = type_ref->getDemangling(dem);
// TODO: the mangling flavor should come from the TypeRef.
return ts.RemangleAsType(dem, node, ts.GetManglingFlavor());
return ts.RemangleAsType(dem, node, flavor);
}

struct ExistentialSyntheticChild {
Expand All @@ -627,7 +630,8 @@ struct ExistentialSyntheticChild {
llvm::SmallVector<ExistentialSyntheticChild, 4>
GetExistentialSyntheticChildren(TypeSystemSwiftTypeRef &ts,
const swift::reflection::TypeRef *tr,
const swift::reflection::TypeInfo *ti) {
const swift::reflection::TypeInfo *ti,
swift::Mangle::ManglingFlavor flavor) {
llvm::SmallVector<ExistentialSyntheticChild, 4> children;
auto *protocol_composition_tr =
llvm::dyn_cast<swift::reflection::ProtocolCompositionTypeRef>(tr);
Expand All @@ -641,7 +645,8 @@ GetExistentialSyntheticChildren(TypeSystemSwiftTypeRef &ts,
children.push_back({"object", [=]() {
if (auto *super_class_tr =
protocol_composition_tr->getSuperclass())
return GetTypeFromTypeRef(*ts_sp, super_class_tr);
return GetTypeFromTypeRef(*ts_sp, super_class_tr,
flavor);
else
return rti ? ts_sp->GetBuiltinUnknownObjectType()
: ts_sp->GetBuiltinRawPointerType();
Expand All @@ -652,9 +657,9 @@ GetExistentialSyntheticChildren(TypeSystemSwiftTypeRef &ts,
for (unsigned i = 1; i < fields.size(); ++i) {
TypeSystemSwiftTypeRefSP ts_sp = ts.GetTypeSystemSwiftTypeRef();
auto *type_ref = fields[i].TR;
children.push_back({fields[i].Name, [=]() {
return GetTypeFromTypeRef(*ts_sp, type_ref);
}});
children.push_back(
{fields[i].Name,
[=]() { return GetTypeFromTypeRef(*ts_sp, type_ref, flavor); }});
}
}
}
Expand Down Expand Up @@ -702,12 +707,20 @@ CompilerType GetTypedefedTypeRecursive(CompilerType type) {
class SwiftRuntimeTypeVisitor {
SwiftLanguageRuntime &m_runtime;
ExecutionContext m_exe_ctx;
swift::Mangle::ManglingFlavor m_flavor =
swift::Mangle::ManglingFlavor::Default;
CompilerType m_type;
ValueObject *m_valobj = nullptr;
bool m_hide_superclass = false;
bool m_include_clang_types = false;
bool m_visit_superclass = false;

void SetFlavor() {
if (auto ts_sp =
m_type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwiftTypeRef>())
m_flavor = ts_sp->GetManglingFlavor(&m_exe_ctx);
}

public:
struct ChildInfo {
uint32_t byte_size = 0;
Expand All @@ -732,6 +745,7 @@ class SwiftRuntimeTypeVisitor {
: m_runtime(runtime), m_type(type), m_valobj(valobj) {
if (valobj)
m_exe_ctx = valobj->GetExecutionContextRef();
SetFlavor();
}
SwiftRuntimeTypeVisitor(SwiftLanguageRuntime &runtime, CompilerType type,
ExecutionContextScope *exe_scope,
Expand All @@ -740,6 +754,7 @@ class SwiftRuntimeTypeVisitor {
m_include_clang_types(include_clang_types) {
if (exe_scope)
exe_scope->CalculateExecutionContext(m_exe_ctx);
SetFlavor();
}
SwiftRuntimeTypeVisitor(SwiftLanguageRuntime &runtime, CompilerType type,
ExecutionContext *exe_ctx, bool hide_superclass,
Expand All @@ -749,6 +764,7 @@ class SwiftRuntimeTypeVisitor {
m_visit_superclass(visit_superclass) {
if (exe_ctx)
m_exe_ctx = *exe_ctx;
SetFlavor();
}
llvm::Error VisitAllChildren(VisitCallback callback) {
return VisitImpl({}, callback).takeError();
Expand Down Expand Up @@ -776,11 +792,11 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,

const unsigned success = 0;
bool count_only = !visit_callback;
auto ts_sp =
m_type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwiftTypeRef>();
if (!ts_sp)
return llvm::createStringError("no type system");
auto &ts = *ts_sp;
auto ts_or_err =
SwiftLanguageRuntime::GetReflectionTypeSystem(m_type, m_exe_ctx);
if (!ts_or_err)
return ts_or_err.takeError();
auto &ts = *ts_or_err->get();

// Deal with the LLDB-only SILPackType variant.
if (auto pack_type_info = ts.IsSILPackType(m_type)) {
Expand Down Expand Up @@ -871,7 +887,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
field_type = tuple->element_type;
else {
if (!field_type)
field_type = GetTypeFromTypeRef(ts, field.TR);
field_type = GetTypeFromTypeRef(ts, field.TR, m_flavor);
}
auto get_info = [&]() -> llvm::Expected<ChildInfo> {
ChildInfo child;
Expand Down Expand Up @@ -968,7 +984,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
if (rti->getRecordKind() ==
swift::reflection::RecordKind::ClassExistential) {
// Compatibility with SwiftASTContext.
auto children = GetExistentialSyntheticChildren(ts, tr, ti);
auto children = GetExistentialSyntheticChildren(ts, tr, ti, m_flavor);
if (count_only)
return children.size();
auto visit_existential = [&](ExistentialSyntheticChild c, unsigned idx) {
Expand Down Expand Up @@ -1019,7 +1035,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
llvm::dyn_cast_or_null<swift::reflection::ReferenceTypeInfo>(ti)) {
// Is this an Existential?
unsigned i = 0;
auto children = GetExistentialSyntheticChildren(ts, tr, ti);
auto children = GetExistentialSyntheticChildren(ts, tr, ti, m_flavor);
if (children.size()) {
if (count_only)
return children.size();
Expand Down Expand Up @@ -1109,7 +1125,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
if (auto *super_tr = reflection_ctx->LookupSuperclass(
*tr, ts.GetDescriptorFinder()))
if (auto error = visit_callback(
GetTypeFromTypeRef(ts, super_tr), depth,
GetTypeFromTypeRef(ts, super_tr, m_flavor), depth,
[]() -> std::string { return "<base class>"; },
[]() -> llvm::Expected<ChildInfo> {
return ChildInfo();
Expand Down Expand Up @@ -1143,8 +1159,9 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
return ChildInfo();
};

if (auto error = visit_callback(GetTypeFromTypeRef(ts, super_tr), 0,
get_name, get_info))
if (auto error =
visit_callback(GetTypeFromTypeRef(ts, super_tr, m_flavor), 0,
get_name, get_info))
return error;
}

Expand Down Expand Up @@ -1263,7 +1280,7 @@ SwiftRuntimeTypeVisitor::VisitImpl(std::optional<unsigned> visit_only,
return success;
}

CompilerType super_type = GetTypeFromTypeRef(ts, type_ref);
CompilerType super_type = GetTypeFromTypeRef(ts, type_ref, m_flavor);
auto get_name = [&]() -> std::string {
auto child_name = super_type.GetTypeName().GetStringRef().str();
// FIXME: This should be fixed in GetDisplayTypeName instead!
Expand Down Expand Up @@ -1471,6 +1488,8 @@ SwiftLanguageRuntime::ProjectEnum(ValueObject &valobj) {
if (!ti_or_err)
return ti_or_err.takeError();
auto *ti = &*ti_or_err;
auto flavor =
SwiftLanguageRuntime::GetManglingFlavor(enum_type.GetMangledTypeName());

auto project_indirect_enum =
[&](uint64_t offset, std::string name) -> llvm::Expected<ValueObjectSP> {
Expand All @@ -1496,13 +1515,14 @@ SwiftLanguageRuntime::ProjectEnum(ValueObject &valobj) {
auto &field = rti->getFields()[0];
auto *type_ref = field.TR;
payload += field.Offset;
payload_type = GetTypeFromTypeRef(ts, type_ref);
payload_type = GetTypeFromTypeRef(ts, type_ref, flavor);
break;
}
case swift::reflection::RecordKind::Tuple: {
std::vector<TypeSystemSwift::TupleElement> elts;
for (auto &field : rti->getFields())
elts.emplace_back(ConstString(), GetTypeFromTypeRef(ts, field.TR));
elts.emplace_back(ConstString(),
GetTypeFromTypeRef(ts, field.TR, flavor));
payload_type = ts.CreateTupleType(elts);
break;
}
Expand Down Expand Up @@ -1570,7 +1590,7 @@ SwiftLanguageRuntime::ProjectEnum(ValueObject &valobj) {
if (is_indirect_enum)
return project_indirect_enum(field_info.Offset, field_info.Name);

CompilerType projected_type = GetTypeFromTypeRef(ts, field_info.TR);
CompilerType projected_type = GetTypeFromTypeRef(ts, field_info.TR, flavor);
if (field_info.Offset != 0) {
assert(false);
return llvm::createStringError("enum with unexpected offset");
Expand Down Expand Up @@ -1678,7 +1698,9 @@ CompilerType SwiftLanguageRuntime::GetBaseClass(CompilerType class_ty) {
}
auto *super_tr = reflection_ctx->LookupSuperclass(
*type_ref_or_err, tr_ts->GetDescriptorFinder());
return GetTypeFromTypeRef(*tr_ts, super_tr);
auto flavor =
SwiftLanguageRuntime::GetManglingFlavor(class_ty.GetMangledTypeName());
return GetTypeFromTypeRef(*tr_ts, super_tr, flavor);
}

bool SwiftLanguageRuntime::ForEachSuperClassType(
Expand Down Expand Up @@ -3362,6 +3384,42 @@ SwiftLanguageRuntime::GetTypeRef(CompilerType type,
return type_ref_or_err;
}

llvm::Expected<TypeSystemSwiftTypeRefSP>
SwiftLanguageRuntime::GetReflectionTypeSystem(CompilerType for_type,
ExecutionContext exe_ctx) {
auto ts_sp = for_type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>();
if (!ts_sp)
return llvm::createStringError("not a Swift type");

// The TypeSystemSwiftTypeRefForExpressions doesn't have a SymbolFile,
// so any DWARF lookups for Embedded Swift fail.
//
// FIXME: It's unclear whether this is safe to do in a non-LTO Swift program.
if (auto *tr_ts =
llvm::dyn_cast_or_null<TypeSystemSwiftTypeRefForExpressions>(
ts_sp.get())) {
if (tr_ts->GetManglingFlavor(&exe_ctx) ==
swift::Mangle::ManglingFlavor::Embedded) {
if (auto *frame = exe_ctx.GetFramePtr()) {
auto &sc = frame->GetSymbolContext(eSymbolContextModule);
if (sc.module_sp) {
auto ts_or_err =
sc.module_sp->GetTypeSystemForLanguage(eLanguageTypeSwift);
if (!ts_or_err)
return ts_or_err.takeError();
if (auto *tr_ts =
llvm::dyn_cast_or_null<TypeSystemSwift>(ts_or_err->get()))
ts_sp = tr_ts->GetTypeSystemSwiftTypeRef();
}
}
}
}
auto tr_ts = ts_sp->GetTypeSystemSwiftTypeRef();
if (!tr_ts)
return llvm::createStringError("no Swift typesystem");
return tr_ts;
}

llvm::Expected<const swift::reflection::TypeInfo &>
SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo(
CompilerType type, ExecutionContextScope *exe_scope,
Expand All @@ -3372,14 +3430,17 @@ SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo(
"[SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo] Getting "
"type info for type: {0}",
type.GetMangledTypeName());

auto ts_sp = type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>();
if (!ts_sp)
return llvm::createStringError("not a Swift type");
auto tr_ts = ts_sp->GetTypeSystemSwiftTypeRef();
if (!tr_ts)
return llvm::createStringError("no Swift typesystem");
auto &ts = *tr_ts;
StackFrame *frame = nullptr;
ExecutionContext exe_ctx;
if (exe_scope) {
frame = exe_scope->CalculateStackFrame().get();
if (frame)
frame->CalculateExecutionContext(exe_ctx);
}
auto ts_or_err = GetReflectionTypeSystem(type, exe_ctx);
if (!ts_or_err)
return ts_or_err.takeError();
auto &ts = *ts_or_err->get();

// Resolve all type aliases.
type = type.GetCanonicalType();
Expand All @@ -3393,15 +3454,13 @@ SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo(
// Resolve all generic type parameters in the type for the current
// frame. Generic parameter binding has to happen in the scratch
// context.
if (exe_scope)
if (StackFrame *frame = exe_scope->CalculateStackFrame().get()) {
ExecutionContext exe_ctx;
frame->CalculateExecutionContext(exe_ctx);
auto bound_type_or_err = BindGenericTypeParameters(*frame, type);
if (!bound_type_or_err)
return bound_type_or_err.takeError();
type = *bound_type_or_err;
}
if (frame) {
frame->CalculateExecutionContext(exe_ctx);
auto bound_type_or_err = BindGenericTypeParameters(*frame, type);
if (!bound_type_or_err)
return bound_type_or_err.takeError();
type = *bound_type_or_err;
}

// BindGenericTypeParameters imports the type into the scratch
// context, but we need to resolve (any DWARF links in) the typeref
Expand All @@ -3419,7 +3478,7 @@ SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo(

LLDBTypeInfoProvider provider(*this, ts);
return reflection_ctx->GetTypeInfo(*type_ref_or_err, &provider,
tr_ts->GetDescriptorFinder());
ts.GetDescriptorFinder());
}

bool SwiftLanguageRuntime::IsStoredInlineInBuffer(CompilerType type) {
Expand Down Expand Up @@ -3555,7 +3614,7 @@ SwiftLanguageRuntime::ResolveTypeAlias(CompilerType alias) {
type_ref = &*type_ref_or_err;
}

CompilerType resolved = GetTypeFromTypeRef(*tr_ts, type_ref);
CompilerType resolved = GetTypeFromTypeRef(*tr_ts, type_ref, flavor);
LLDB_LOG(GetLog(LLDBLog::Types),
"Resolved type alias {0} = {1} using reflection metadata.",
alias.GetMangledTypeName(), resolved.GetMangledTypeName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,9 @@ lldb::TypeSP DWARFASTParserSwift::ParseTypeFromDWARF(const SymbolContext &sc,
if (TypeSP desugared_type = get_type(die)) {
// For a typedef, store the once desugared type as the name.
CompilerType type = desugared_type->GetForwardCompilerType();
if (auto swift_ast_ctx =
if (auto ts =
type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>())
preferred_name =
swift_ast_ctx->GetMangledTypeName(type.GetOpaqueQualType());
preferred_name = ts->GetMangledTypeName(type.GetOpaqueQualType());
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserSwift.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class DWARFASTParserSwift : public lldb_private::plugin::dwarf::DWARFASTParser,

virtual ~DWARFASTParserSwift();

static std::pair<lldb::TypeSP, lldb_private::CompilerType>
ResolveTypeAlias(lldb_private::CompilerType alias);
lldb::TypeSP ParseTypeFromDWARF(const lldb_private::SymbolContext &sc,
const DWARFDIE &die,
bool *type_is_new_ptr) override;
Expand Down
Loading