Skip to content

Commit 50617d8

Browse files
committed
Remove SwiftASTContext from SwiftMetatype_SummaryProvider
This is done primarily for performance reasons. The change is not entirely NFC, the type produced by the reflection API is more verbose, but also more accurate. (cherry picked from commit 763ecf6)
1 parent 1511e98 commit 50617d8

File tree

8 files changed

+73
-35
lines changed

8 files changed

+73
-35
lines changed
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/SwiftLanguageRuntimeDynamicTypeResolution.cpp

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,6 +1760,46 @@ 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+
17631803
llvm::Optional<lldb::addr_t>
17641804
SwiftLanguageRuntimeImpl::GetTypeMetadataForTypeNameAndFrame(
17651805
StringRef mdvar_name, StackFrame &frame) {
@@ -2381,11 +2421,16 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress(
23812421
in_value, use_dynamic, class_type_or_name, address, static_value_type);
23822422
else if (type_info.AnySet(eTypeIsPack))
23832423
success = GetDynamicTypeAndAddress_Pack(in_value, val_type, use_dynamic,
2384-
class_type_or_name, address, static_value_type);
2424+
class_type_or_name, address,
2425+
static_value_type);
23852426
else if (type_info.AnySet(eTypeIsClass) ||
23862427
type_info.AllSet(eTypeIsBuiltIn | eTypeIsPointer | eTypeHasValue))
23872428
success = GetDynamicTypeAndAddress_Class(in_value, val_type, use_dynamic,
2388-
class_type_or_name, address, static_value_type);
2429+
class_type_or_name, address,
2430+
static_value_type);
2431+
else if (type_info.AllSet(eTypeIsMetatype | eTypeIsProtocol))
2432+
success = GetDynamicTypeAndAddress_ExistentialMetatype(
2433+
in_value, val_type, use_dynamic, class_type_or_name, address);
23892434
else if (type_info.AnySet(eTypeIsProtocol))
23902435
success = GetDynamicTypeAndAddress_Protocol(in_value, val_type, use_dynamic,
23912436
class_type_or_name, address);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,12 @@ class SwiftLanguageRuntimeImpl {
352352
bool use_local_buffer,
353353
lldb::addr_t existential_address);
354354
#endif
355+
356+
bool GetDynamicTypeAndAddress_ExistentialMetatype(
357+
ValueObject &in_value, CompilerType meta_type,
358+
lldb::DynamicValueType use_dynamic, TypeAndOrName &class_type_or_name,
359+
Address &address);
360+
355361
bool GetDynamicTypeAndAddress_Value(ValueObject &in_value,
356362
CompilerType &bound_type,
357363
lldb::DynamicValueType use_dynamic,

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

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5440,6 +5440,8 @@ SwiftASTContext::GetTypeInfo(opaque_compiler_type_t type,
54405440
swift_flags |= eTypeHasChildren | eTypeIsStructUnion | eTypeIsProtocol;
54415441
break;
54425442
case swift::TypeKind::ExistentialMetatype:
5443+
swift_flags |= eTypeIsProtocol;
5444+
LLVM_FALLTHROUGH;
54435445
case swift::TypeKind::Metatype:
54445446
swift_flags |= eTypeIsMetatype | eTypeHasValue;
54455447
break;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,8 @@ TypeSystemSwiftTypeRef::CollectTypeInfo(swift::Demangle::Demangler &dem,
12951295
break;
12961296

12971297
case Node::Kind::ExistentialMetatype:
1298+
swift_flags |= eTypeIsProtocol;
1299+
LLVM_FALLTHROUGH;
12981300
case Node::Kind::Metatype:
12991301
swift_flags |= eTypeIsMetatype | eTypeHasValue;
13001302
break;
@@ -3361,6 +3363,7 @@ bool TypeSystemSwiftTypeRef::IsExistentialType(
33613363
if (!node || node->getNumChildren() != 1)
33623364
return false;
33633365
switch (node->getKind()) {
3366+
case Node::Kind::ExistentialMetatype:
33643367
case Node::Kind::Protocol:
33653368
case Node::Kind::ProtocolList:
33663369
return true;

lldb/test/API/lang/swift/bridged_metatype/TestSwiftBridgedMetatype.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,4 @@ def test_swift_bridged_metatype(self):
2525
)
2626

2727
var_k = self.frame().FindVariable("k")
28-
if sys.platform.startswith("linux"):
29-
lldbutil.check_variable(self, var_k, False, "Foundation.NSString")
30-
else:
31-
lldbutil.check_variable(self, var_k, False, "NSString")
28+
lldbutil.check_variable(self, var_k, False, "@thick NSString.Type")

lldb/test/API/lang/swift/metatype/TestSwiftMetatype.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_metatype(self):
4343
var_t = frame.FindVariable("t")
4444
var_p = frame.FindVariable("p")
4545
lldbutil.check_variable(self, var_s, False, "String")
46-
lldbutil.check_variable(self, var_c, False, "a.D")
47-
lldbutil.check_variable(self, var_f, False, "(Int) -> Int")
46+
lldbutil.check_variable(self, var_c, False, "@thick a.D.Type")
47+
lldbutil.check_variable(self, var_f, False, '@thick ((Int) -> Int).Type')
4848
lldbutil.check_variable(self, var_t, False, "(Int, Int, String)")
4949
lldbutil.check_variable(self, var_p, False, "a.P")

0 commit comments

Comments
 (0)