Skip to content

Commit 42340bb

Browse files
Merge pull request #7598 from adrian-prantl/cherry-pick-116361792-5.10
Cherry pick 116361792 5.10
2 parents bfce165 + c5a4eee commit 42340bb

File tree

12 files changed

+144
-87
lines changed

12 files changed

+144
-87
lines changed

lldb/source/Plugins/Language/Swift/SwiftArray.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h"
1616
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
17+
#include "Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h"
1718
#include "lldb/Core/ValueObjectConstResult.h"
1819
#include "lldb/DataFormatters/FormattersHelpers.h"
1920
#include "lldb/Target/Process.h"
@@ -22,8 +23,6 @@
2223
// FIXME: we should not need this
2324
#include "Plugins/Language/ObjC/Cocoa.h"
2425

25-
#include "llvm/ADT/StringRef.h"
26-
2726
using namespace lldb;
2827
using namespace lldb_private;
2928
using namespace lldb_private::formatters;
@@ -281,7 +280,10 @@ bool SwiftSyntheticFrontEndBufferHandler::IsValid() {
281280
}
282281

283282
std::unique_ptr<SwiftArrayBufferHandler>
284-
SwiftArrayBufferHandler::CreateBufferHandler(ValueObject &valobj) {
283+
SwiftArrayBufferHandler::CreateBufferHandler(ValueObject &static_valobj) {
284+
lldb::ValueObjectSP valobj_sp =
285+
static_valobj.GetDynamicValue(lldb::eDynamicCanRunTarget);
286+
ValueObject &valobj = valobj_sp ? *valobj_sp : static_valobj;
285287
llvm::StringRef valobj_typename(
286288
valobj.GetCompilerType().GetTypeName().AsCString(""));
287289

@@ -325,22 +327,24 @@ SwiftArrayBufferHandler::CreateBufferHandler(ValueObject &valobj) {
325327
if (error.Fail() || argmetadata_ptr == LLDB_INVALID_ADDRESS)
326328
return nullptr;
327329

330+
// Get the type of the array elements.
331+
CompilerType argument_type;
332+
auto scratch_ctx_reader = valobj.GetSwiftScratchContext();
333+
if (!scratch_ctx_reader)
334+
return nullptr;
335+
auto *ts = scratch_ctx_reader->get();
336+
if (!ts)
337+
return nullptr;
328338
auto *swift_runtime = SwiftLanguageRuntime::Get(process_sp);
329339
if (!swift_runtime)
330340
return nullptr;
331341

332-
CompilerType argument_type;
333-
334-
SwiftLanguageRuntime::MetadataPromiseSP promise_sp(
335-
swift_runtime->GetMetadataPromise(argmetadata_ptr, valobj));
336-
if (promise_sp)
337-
if (CompilerType type = promise_sp->FulfillTypePromise())
338-
if (auto type_system =
339-
type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>())
340-
argument_type =
341-
type_system->GetGenericArgumentType(type.GetOpaqueQualType(), 0);
342+
if (CompilerType type =
343+
swift_runtime->GetTypeFromMetadata(*ts, argmetadata_ptr))
344+
if (auto ts = type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>())
345+
argument_type = ts->GetGenericArgumentType(type.GetOpaqueQualType(), 0);
342346

343-
if (!argument_type.IsValid())
347+
if (!argument_type)
344348
return nullptr;
345349

346350
auto handler = std::unique_ptr<SwiftArrayBufferHandler>(
Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
//===-- SwiftMetatype.cpp ---------------------------------------*- C++ -*-===//
1+
//===-- SwiftMetatype.cpp -------------------------------------------------===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -12,12 +12,7 @@
1212

1313
#include "SwiftMetatype.h"
1414
#include "Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h"
15-
#include "lldb/Core/Mangled.h"
1615
#include "lldb/Symbol/CompilerType.h"
17-
#include "lldb/Target/Process.h"
18-
19-
#include "swift/AST/Type.h"
20-
#include "swift/AST/Types.h"
2116

2217
using namespace lldb;
2318
using namespace lldb_private;
@@ -26,30 +21,18 @@ using namespace lldb_private::formatters::swift;
2621

2722
bool lldb_private::formatters::swift::SwiftMetatype_SummaryProvider(
2823
ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
24+
ConstString name;
2925
lldb::addr_t metadata_ptr = valobj.GetPointerValue();
3026
if (metadata_ptr == LLDB_INVALID_ADDRESS || metadata_ptr == 0) {
3127
CompilerType compiler_metatype_type(valobj.GetCompilerType());
3228
CompilerType instancetype =
3329
TypeSystemSwift::GetInstanceType(compiler_metatype_type);
34-
35-
const char *ptr = instancetype.GetDisplayTypeName().AsCString(nullptr);
36-
if (ptr && *ptr) {
37-
stream.Printf("%s", ptr);
38-
return true;
39-
}
40-
} else {
41-
auto swift_runtime = SwiftLanguageRuntime::Get(valobj.GetProcessSP());
42-
if (!swift_runtime)
43-
return false;
44-
SwiftLanguageRuntime::MetadataPromiseSP metadata_promise_sp =
45-
swift_runtime->GetMetadataPromise(metadata_ptr, valobj);
46-
if (!metadata_promise_sp)
47-
return false;
48-
if (CompilerType resolved_type =
49-
metadata_promise_sp->FulfillTypePromise()) {
50-
stream.Printf("%s", resolved_type.GetDisplayTypeName().AsCString());
51-
return true;
52-
}
30+
name = instancetype.GetDisplayTypeName();
31+
} else if (CompilerType meta_type = valobj.GetCompilerType()) {
32+
name = meta_type.GetDisplayTypeName();
5333
}
54-
return false;
34+
if (!name)
35+
return false;
36+
stream << name;
37+
return true;
5538
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,7 @@ class SwiftLanguageRuntimeStub {
265265
return addr;
266266
}
267267

268-
SwiftLanguageRuntime::MetadataPromiseSP
269-
GetMetadataPromise(lldb::addr_t addr, ValueObject &for_object) {
268+
CompilerType GetTypeFromMetadata(TypeSystemSwift &tss, Address addr) {
270269
STUB_LOG();
271270
return {};
272271
}
@@ -2373,10 +2372,9 @@ lldb::addr_t SwiftLanguageRuntime::FixupAddress(lldb::addr_t addr,
23732372
FORWARD(FixupAddress, addr, type, error);
23742373
}
23752374

2376-
SwiftLanguageRuntime::MetadataPromiseSP
2377-
SwiftLanguageRuntime::GetMetadataPromise(lldb::addr_t addr,
2378-
ValueObject &for_object) {
2379-
FORWARD(GetMetadataPromise, addr, for_object);
2375+
CompilerType SwiftLanguageRuntime::GetTypeFromMetadata(TypeSystemSwift &tss,
2376+
Address addr) {
2377+
FORWARD(GetTypeFromMetadata, tss, addr);
23802378
}
23812379

23822380
bool SwiftLanguageRuntime::IsStoredInlineInBuffer(CompilerType type) {

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ class SwiftLanguageRuntime : public LanguageRuntime {
7373
std::unique_ptr<SwiftLanguageRuntimeImpl> m_impl;
7474

7575
public:
76-
class MetadataPromise;
77-
typedef std::shared_ptr<MetadataPromise> MetadataPromiseSP;
78-
7976
static char ID;
8077

8178
bool isA(const void *ClassID) const override {
@@ -237,24 +234,7 @@ class SwiftLanguageRuntime : public LanguageRuntime {
237234
CompilerType GetConcreteType(ExecutionContextScope *exe_scope,
238235
ConstString abstract_type_name) override;
239236

240-
/// A proxy object to support lazy binding of Archetypes.
241-
class MetadataPromise {
242-
friend class SwiftLanguageRuntimeImpl;
243-
244-
MetadataPromise(ValueObject &, SwiftLanguageRuntimeImpl &, lldb::addr_t);
245-
246-
lldb::ValueObjectSP m_for_object_sp;
247-
SwiftLanguageRuntimeImpl &m_swift_runtime;
248-
lldb::addr_t m_metadata_location;
249-
llvm::Optional<swift::MetadataKind> m_metadata_kind;
250-
llvm::Optional<CompilerType> m_compiler_type;
251-
252-
public:
253-
CompilerType FulfillTypePromise(Status *error = nullptr);
254-
};
255-
256-
MetadataPromiseSP GetMetadataPromise(lldb::addr_t addr,
257-
ValueObject &for_object);
237+
CompilerType GetTypeFromMetadata(TypeSystemSwift &tss, Address address);
258238
/// Build the artificial type metadata variable name for \p swift_type.
259239
static bool GetAbstractTypeName(StreamString &name, swift::Type swift_type);
260240

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

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,6 +1760,65 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Protocol(
17601760
return true;
17611761
}
17621762

1763+
bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_ExistentialMetatype(
1764+
ValueObject &in_value, CompilerType meta_type,
1765+
lldb::DynamicValueType use_dynamic, TypeAndOrName &class_type_or_name,
1766+
Address &address) {
1767+
// Resolve the dynamic type of the metatype.
1768+
AddressType address_type;
1769+
lldb::addr_t ptr = in_value.GetPointerValue(&address_type);
1770+
if (ptr == LLDB_INVALID_ADDRESS || ptr == 0)
1771+
return false;
1772+
1773+
ThreadSafeReflectionContext reflection_ctx = GetReflectionContext();
1774+
if (!reflection_ctx)
1775+
return false;
1776+
1777+
const swift::reflection::TypeRef *type_ref =
1778+
reflection_ctx->ReadTypeFromMetadata(ptr);
1779+
1780+
auto tss = meta_type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>();
1781+
if (!tss)
1782+
return false;
1783+
auto &ts = tss->GetTypeSystemSwiftTypeRef();
1784+
1785+
using namespace swift::Demangle;
1786+
Demangler dem;
1787+
NodePointer node = type_ref->getDemangling(dem);
1788+
// Wrap the resolved type in a metatype again for the data formatter to
1789+
// recognize.
1790+
if (!node || node->getKind() != Node::Kind::Type)
1791+
return false;
1792+
NodePointer wrapped = dem.createNode(Node::Kind::Type);
1793+
NodePointer meta = dem.createNode(Node::Kind::Metatype);
1794+
meta->addChild(node, dem);
1795+
wrapped->addChild(meta,dem);
1796+
1797+
meta_type = ts.GetTypeSystemSwiftTypeRef().RemangleAsType(dem, wrapped);
1798+
class_type_or_name.SetCompilerType(meta_type);
1799+
address.SetRawAddress(ptr);
1800+
return true;
1801+
}
1802+
1803+
CompilerType SwiftLanguageRuntimeImpl::GetTypeFromMetadata(TypeSystemSwift &ts,
1804+
Address address) {
1805+
lldb::addr_t ptr = address.GetLoadAddress(&GetProcess().GetTarget());
1806+
if (ptr == LLDB_INVALID_ADDRESS)
1807+
return {};
1808+
1809+
ThreadSafeReflectionContext reflection_ctx = GetReflectionContext();
1810+
if (!reflection_ctx)
1811+
return {};
1812+
1813+
const swift::reflection::TypeRef *type_ref =
1814+
reflection_ctx->ReadTypeFromMetadata(ptr);
1815+
1816+
using namespace swift::Demangle;
1817+
Demangler dem;
1818+
NodePointer node = type_ref->getDemangling(dem);
1819+
return ts.GetTypeSystemSwiftTypeRef().RemangleAsType(dem, node);
1820+
}
1821+
17631822
llvm::Optional<lldb::addr_t>
17641823
SwiftLanguageRuntimeImpl::GetTypeMetadataForTypeNameAndFrame(
17651824
StringRef mdvar_name, StackFrame &frame) {
@@ -2381,11 +2440,16 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress(
23812440
in_value, use_dynamic, class_type_or_name, address, static_value_type);
23822441
else if (type_info.AnySet(eTypeIsPack))
23832442
success = GetDynamicTypeAndAddress_Pack(in_value, val_type, use_dynamic,
2384-
class_type_or_name, address, static_value_type);
2443+
class_type_or_name, address,
2444+
static_value_type);
23852445
else if (type_info.AnySet(eTypeIsClass) ||
23862446
type_info.AllSet(eTypeIsBuiltIn | eTypeIsPointer | eTypeHasValue))
23872447
success = GetDynamicTypeAndAddress_Class(in_value, val_type, use_dynamic,
2388-
class_type_or_name, address, static_value_type);
2448+
class_type_or_name, address,
2449+
static_value_type);
2450+
else if (type_info.AllSet(eTypeIsMetatype | eTypeIsProtocol))
2451+
success = GetDynamicTypeAndAddress_ExistentialMetatype(
2452+
in_value, val_type, use_dynamic, class_type_or_name, address);
23892453
else if (type_info.AnySet(eTypeIsProtocol))
23902454
success = GetDynamicTypeAndAddress_Protocol(in_value, val_type, use_dynamic,
23912455
class_type_or_name, address);
@@ -2702,7 +2766,7 @@ SwiftLanguageRuntimeImpl::GetConcreteType(ExecutionContextScope *exe_scope,
27022766
if (!frame)
27032767
return CompilerType();
27042768

2705-
SwiftLanguageRuntime::MetadataPromiseSP promise_sp(
2769+
SwiftLanguageRuntimeImpl::MetadataPromiseSP promise_sp(
27062770
GetPromiseForTypeNameAndFrame(abstract_type_name.GetCString(), frame));
27072771
if (!promise_sp)
27082772
return CompilerType();

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeImpl.h

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ class SwiftLanguageRuntimeImpl {
110110
llvm::Optional<size_t> GetBitAlignment(CompilerType type,
111111
ExecutionContextScope *exe_scope);
112112

113-
SwiftLanguageRuntime::MetadataPromiseSP
114-
GetMetadataPromise(lldb::addr_t addr, ValueObject &for_object);
113+
CompilerType GetTypeFromMetadata(TypeSystemSwift &tss, Address address);
115114

116115
llvm::Optional<uint64_t>
117116
GetMemberVariableOffsetRemoteAST(CompilerType instance_type,
@@ -352,6 +351,12 @@ class SwiftLanguageRuntimeImpl {
352351
bool use_local_buffer,
353352
lldb::addr_t existential_address);
354353
#endif
354+
355+
bool GetDynamicTypeAndAddress_ExistentialMetatype(
356+
ValueObject &in_value, CompilerType meta_type,
357+
lldb::DynamicValueType use_dynamic, TypeAndOrName &class_type_or_name,
358+
Address &address);
359+
355360
bool GetDynamicTypeAndAddress_Value(ValueObject &in_value,
356361
CompilerType &bound_type,
357362
lldb::DynamicValueType use_dynamic,
@@ -370,7 +375,26 @@ class SwiftLanguageRuntimeImpl {
370375
Address &address,
371376
Value::ValueType &value_type);
372377

373-
SwiftLanguageRuntime::MetadataPromiseSP
378+
/// A proxy object to support lazy binding of Archetypes.
379+
class MetadataPromise {
380+
friend class SwiftLanguageRuntimeImpl;
381+
382+
MetadataPromise(ValueObject &, SwiftLanguageRuntimeImpl &, lldb::addr_t);
383+
384+
lldb::ValueObjectSP m_for_object_sp;
385+
SwiftLanguageRuntimeImpl &m_swift_runtime;
386+
lldb::addr_t m_metadata_location;
387+
llvm::Optional<swift::MetadataKind> m_metadata_kind;
388+
llvm::Optional<CompilerType> m_compiler_type;
389+
390+
public:
391+
CompilerType FulfillTypePromise(Status *error = nullptr);
392+
};
393+
typedef std::shared_ptr<MetadataPromise> MetadataPromiseSP;
394+
395+
MetadataPromiseSP GetMetadataPromise(lldb::addr_t addr,
396+
ValueObject &for_object);
397+
MetadataPromiseSP
374398
GetPromiseForTypeNameAndFrame(const char *type_name, StackFrame *frame);
375399

376400
llvm::Optional<lldb::addr_t>
@@ -401,7 +425,7 @@ class SwiftLanguageRuntimeImpl {
401425
std::shared_ptr<LLDBMemoryReader> m_memory_reader_sp;
402426

403427
llvm::DenseMap<std::pair<swift::ASTContext *, lldb::addr_t>,
404-
SwiftLanguageRuntime::MetadataPromiseSP>
428+
MetadataPromiseSP>
405429
m_promises_map;
406430

407431
llvm::DenseMap<swift::ASTContext *,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ std::string SwiftLanguageRuntime::DemangleSymbolAsString(
705705
options.ShowPrivateDiscriminators = false;
706706
options.DisplayExtensionContexts = false;
707707
options.DisplayLocalNameContexts = false;
708+
options.ShowFunctionArgumentTypes = true;
708709
break;
709710
case eDisplayTypeName:
710711
options = swift::Demangle::DemangleOptions::SimplifiedUIDemangleOptions();
@@ -714,6 +715,7 @@ std::string SwiftLanguageRuntime::DemangleSymbolAsString(
714715
options.DisplayModuleNames = true;
715716
options.DisplayLocalNameContexts = false;
716717
options.DisplayDebuggerGeneratedModule = false;
718+
options.ShowFunctionArgumentTypes = true;
717719
break;
718720
}
719721

0 commit comments

Comments
 (0)