15
15
#include " lldb/Utility/LLDBLog.h"
16
16
#include " lldb/Utility/Log.h"
17
17
#include " swift/Demangling/Demangle.h"
18
+ #include " swift/RemoteInspection/DescriptorFinder.h"
18
19
19
20
using namespace lldb ;
20
21
using namespace lldb_private ;
21
22
22
23
namespace {
23
24
25
+ // / The descriptor finder needs to be an instance variable of the
26
+ // / TypeRefBuilder, but we would still want to swap out the descriptor finder,
27
+ // / as they are tied to each type system typeref's symbol file. This class's
28
+ // / only purpose is to allow this swapping.
29
+ struct DescriptorFinderForwarder : public swift ::reflection::DescriptorFinder {
30
+ DescriptorFinderForwarder () = default ;
31
+ ~DescriptorFinderForwarder () override = default ;
32
+
33
+ std::unique_ptr<swift::reflection::BuiltinTypeDescriptorBase>
34
+ getBuiltinTypeDescriptor (const swift::reflection::TypeRef *TR) override {
35
+ if (m_descriptor_finder)
36
+ return m_descriptor_finder->getBuiltinTypeDescriptor (TR);
37
+ return nullptr ;
38
+ }
39
+
40
+ std::unique_ptr<swift::reflection::FieldDescriptorBase>
41
+ getFieldDescriptor (const swift::reflection::TypeRef *TR) override {
42
+ if (m_descriptor_finder)
43
+ return m_descriptor_finder->getFieldDescriptor (TR);
44
+ return nullptr ;
45
+ }
46
+
47
+ void SetExternalDescriptorFinder (
48
+ swift::reflection::DescriptorFinder *desciptor_finder) {
49
+ m_descriptor_finder = desciptor_finder;
50
+ }
51
+
52
+ void ClearExternalDescriptorFinder () { m_descriptor_finder = nullptr ; }
53
+
54
+ private:
55
+ swift::reflection::DescriptorFinder *m_descriptor_finder = nullptr ;
56
+ };
57
+
24
58
// / An implementation of the generic ReflectionContextInterface that
25
59
// / is templatized on target pointer width and specialized to either
26
60
// / 32-bit or 64-bit pointers, with and without ObjC interoperability.
27
61
template <typename ReflectionContext>
28
- class TargetReflectionContext
29
- : public ReflectionContextInterface {
62
+ class TargetReflectionContext : public ReflectionContextInterface {
63
+ DescriptorFinderForwarder m_forwader;
30
64
ReflectionContext m_reflection_ctx;
31
65
swift::reflection::TypeConverter m_type_converter;
32
66
33
67
public:
34
68
TargetReflectionContext (
35
69
std::shared_ptr<swift::reflection::MemoryReader> reader,
36
70
SwiftMetadataCache *swift_metadata_cache)
37
- : m_reflection_ctx(reader, swift_metadata_cache),
71
+ : m_reflection_ctx(reader, swift_metadata_cache, &m_forwader ),
38
72
m_type_converter (m_reflection_ctx.getBuilder()) {}
39
73
40
74
llvm::Optional<uint32_t > AddImage (
@@ -59,20 +93,31 @@ class TargetReflectionContext
59
93
likely_module_names);
60
94
}
61
95
62
- const swift::reflection::TypeRef *
63
- GetTypeRefOrNull (StringRef mangled_type_name) override {
96
+ const swift::reflection::TypeRef *GetTypeRefOrNull (
97
+ StringRef mangled_type_name,
98
+ swift::reflection::DescriptorFinder *descriptor_finder) override {
64
99
swift::Demangle::Demangler dem;
65
100
swift::Demangle::NodePointer node = dem.demangleSymbol (mangled_type_name);
66
- const swift::reflection::TypeRef *type_ref = GetTypeRefOrNull (dem, node);
101
+ const swift::reflection::TypeRef *type_ref =
102
+ GetTypeRefOrNull (dem, node, descriptor_finder);
67
103
if (!type_ref)
68
104
LLDB_LOG (GetLog (LLDBLog::Types), " Could not find typeref for type: {0}" ,
69
105
mangled_type_name);
70
106
return type_ref;
71
107
}
72
108
73
- virtual const swift::reflection::TypeRef *
74
- GetTypeRefOrNull (swift::Demangle::Demangler &dem,
75
- swift::Demangle::NodePointer node) override {
109
+ // / Sets the descriptor finder, and on scope exit clears it out.
110
+ auto SetDescriptorFinderAndClearOnExit (
111
+ swift::reflection::DescriptorFinder *descriptor_finder) {
112
+ m_forwader.SetExternalDescriptorFinder (descriptor_finder);
113
+ return llvm::make_scope_exit (
114
+ [&]() { m_forwader.ClearExternalDescriptorFinder (); });
115
+ }
116
+
117
+ const swift::reflection::TypeRef *GetTypeRefOrNull (
118
+ swift::Demangle::Demangler &dem, swift::Demangle::NodePointer node,
119
+ swift::reflection::DescriptorFinder *descriptor_finder) override {
120
+ auto on_exit = SetDescriptorFinderAndClearOnExit (descriptor_finder);
76
121
auto type_ref_or_err =
77
122
swift::Demangle::decodeMangledType (m_reflection_ctx.getBuilder (), node);
78
123
if (type_ref_or_err.isError ()) {
@@ -84,17 +129,21 @@ class TargetReflectionContext
84
129
return type_ref_or_err.getType ();
85
130
}
86
131
87
- const swift::reflection::TypeInfo *
88
- GetClassInstanceTypeInfo (const swift::reflection::TypeRef *type_ref,
89
- swift::remote::TypeInfoProvider *provider) override {
132
+ const swift::reflection::TypeInfo *GetClassInstanceTypeInfo (
133
+ const swift::reflection::TypeRef *type_ref,
134
+ swift::remote::TypeInfoProvider *provider,
135
+ swift::reflection::DescriptorFinder *descriptor_finder) override {
136
+ auto on_exit = SetDescriptorFinderAndClearOnExit (descriptor_finder);
90
137
if (!type_ref)
91
138
return nullptr ;
92
139
return m_type_converter.getClassInstanceTypeInfo (type_ref, 0 , provider);
93
140
}
94
141
95
142
const swift::reflection::TypeInfo *
96
143
GetTypeInfo (const swift::reflection::TypeRef *type_ref,
97
- swift::remote::TypeInfoProvider *provider) override {
144
+ swift::remote::TypeInfoProvider *provider,
145
+ swift::reflection::DescriptorFinder *descriptor_finder) override {
146
+ auto on_exit = SetDescriptorFinderAndClearOnExit (descriptor_finder);
98
147
if (!type_ref)
99
148
return nullptr ;
100
149
@@ -128,24 +177,30 @@ class TargetReflectionContext
128
177
return type_info;
129
178
}
130
179
131
- const swift::reflection::TypeInfo *
132
- GetTypeInfoFromInstance (lldb::addr_t instance,
133
- swift::remote::TypeInfoProvider *provider) override {
180
+ const swift::reflection::TypeInfo *GetTypeInfoFromInstance (
181
+ lldb::addr_t instance, swift::remote::TypeInfoProvider *provider,
182
+ swift::reflection::DescriptorFinder *descriptor_finder) override {
183
+ auto on_exit = SetDescriptorFinderAndClearOnExit (descriptor_finder);
134
184
return m_reflection_ctx.getInstanceTypeInfo (instance, provider);
135
185
}
136
186
137
187
swift::reflection::MemoryReader &GetReader () override {
138
188
return m_reflection_ctx.getReader ();
139
189
}
140
190
141
- const swift::reflection::TypeRef *
142
- LookupSuperclass (const swift::reflection::TypeRef *tr) override {
191
+ const swift::reflection::TypeRef *LookupSuperclass (
192
+ const swift::reflection::TypeRef *tr,
193
+ swift::reflection::DescriptorFinder *descriptor_finder) override {
194
+ auto on_exit = SetDescriptorFinderAndClearOnExit (descriptor_finder);
143
195
return m_reflection_ctx.getBuilder ().lookupSuperclass (tr);
144
196
}
145
197
146
- bool ForEachSuperClassType (swift::remote::TypeInfoProvider *tip,
147
- lldb::addr_t pointer,
148
- std::function<bool (SuperClassType)> fn) override {
198
+ bool
199
+ ForEachSuperClassType (swift::remote::TypeInfoProvider *tip,
200
+ swift::reflection::DescriptorFinder *descriptor_finder,
201
+ lldb::addr_t pointer,
202
+ std::function<bool (SuperClassType)> fn) override {
203
+ auto on_exit = SetDescriptorFinderAndClearOnExit (descriptor_finder);
149
204
// Guard against faulty self-referential metadata.
150
205
unsigned limit = 256 ;
151
206
auto md_ptr = m_reflection_ctx.readMetadataFromInstance (pointer);
@@ -175,10 +230,12 @@ class TargetReflectionContext
175
230
return false ;
176
231
}
177
232
178
- llvm::Optional<int32_t >
179
- ProjectEnumValue (swift::remote::RemoteAddress enum_addr,
180
- const swift::reflection::TypeRef *enum_type_ref,
181
- swift::remote::TypeInfoProvider *provider) override {
233
+ llvm::Optional<int32_t > ProjectEnumValue (
234
+ swift::remote::RemoteAddress enum_addr,
235
+ const swift::reflection::TypeRef *enum_type_ref,
236
+ swift::remote::TypeInfoProvider *provider,
237
+ swift::reflection::DescriptorFinder *descriptor_finder) override {
238
+ auto on_exit = SetDescriptorFinderAndClearOnExit (descriptor_finder);
182
239
int32_t case_idx;
183
240
if (m_reflection_ctx.projectEnumValue (enum_addr, enum_type_ref, &case_idx,
184
241
provider))
@@ -190,21 +247,27 @@ class TargetReflectionContext
190
247
swift::reflection::RemoteAddress>>
191
248
ProjectExistentialAndUnwrapClass (
192
249
swift::reflection::RemoteAddress existential_address,
193
- const swift::reflection::TypeRef &existential_tr) override {
250
+ const swift::reflection::TypeRef &existential_tr,
251
+ swift::reflection::DescriptorFinder *descriptor_finder) override {
252
+ auto on_exit = SetDescriptorFinderAndClearOnExit (descriptor_finder);
194
253
return m_reflection_ctx.projectExistentialAndUnwrapClass (
195
254
existential_address, existential_tr);
196
255
}
197
256
198
257
const swift::reflection::TypeRef *
199
258
ReadTypeFromMetadata (lldb::addr_t metadata_address,
259
+ swift::reflection::DescriptorFinder *descriptor_finder,
200
260
bool skip_artificial_subclasses) override {
261
+ auto on_exit = SetDescriptorFinderAndClearOnExit (descriptor_finder);
201
262
return m_reflection_ctx.readTypeFromMetadata (metadata_address,
202
263
skip_artificial_subclasses);
203
264
}
204
265
205
266
const swift::reflection::TypeRef *
206
267
ReadTypeFromInstance (lldb::addr_t instance_address,
268
+ swift::reflection::DescriptorFinder *descriptor_finder,
207
269
bool skip_artificial_subclasses) override {
270
+ auto on_exit = SetDescriptorFinderAndClearOnExit (descriptor_finder);
208
271
auto metadata_address =
209
272
m_reflection_ctx.readMetadataFromInstance (instance_address);
210
273
if (!metadata_address) {
@@ -226,12 +289,14 @@ class TargetReflectionContext
226
289
227
290
const swift::reflection::TypeRef *ApplySubstitutions (
228
291
const swift::reflection::TypeRef *type_ref,
229
- swift::reflection::GenericArgumentMap substitutions) override {
292
+ swift::reflection::GenericArgumentMap substitutions,
293
+ swift::reflection::DescriptorFinder *descriptor_finder) override {
294
+ auto on_exit = SetDescriptorFinderAndClearOnExit (descriptor_finder);
230
295
return type_ref->subst (m_reflection_ctx.getBuilder (), substitutions);
231
296
}
232
297
233
- swift::remote::RemoteAbsolutePointer StripSignedPointer (
234
- swift::remote::RemoteAbsolutePointer pointer) override {
298
+ swift::remote::RemoteAbsolutePointer
299
+ StripSignedPointer ( swift::remote::RemoteAbsolutePointer pointer) override {
235
300
return m_reflection_ctx.stripSignedPointer (pointer);
236
301
}
237
302
};
0 commit comments