@@ -130,17 +130,12 @@ class TargetReflectionContext : public ReflectionContextInterface {
130
130
return id;
131
131
}
132
132
133
- const swift::reflection::TypeRef * GetTypeRefOrNull (
134
- StringRef mangled_type_name,
135
- swift::reflection::DescriptorFinder *descriptor_finder) override {
133
+ llvm::Expected< const swift::reflection::TypeRef &>
134
+ GetTypeRef ( StringRef mangled_type_name,
135
+ swift::reflection::DescriptorFinder *descriptor_finder) override {
136
136
swift::Demangle::Demangler dem;
137
137
swift::Demangle::NodePointer node = dem.demangleSymbol (mangled_type_name);
138
- const swift::reflection::TypeRef *type_ref =
139
- GetTypeRefOrNull (dem, node, descriptor_finder);
140
- if (!type_ref)
141
- LLDB_LOG (GetLog (LLDBLog::Types), " Could not find typeref for type {0}" ,
142
- mangled_type_name);
143
- return type_ref;
138
+ return GetTypeRef (dem, node, descriptor_finder);
144
139
}
145
140
146
141
// / Sets the descriptor finder, and on scope exit clears it out.
@@ -151,100 +146,98 @@ class TargetReflectionContext : public ReflectionContextInterface {
151
146
[&]() { m_forwader.PopExternalDescriptorFinder (); });
152
147
}
153
148
154
- const swift::reflection::TypeRef * GetTypeRefOrNull (
155
- swift::Demangle::Demangler &dem, swift::Demangle::NodePointer node,
156
- swift::reflection::DescriptorFinder *descriptor_finder) override {
149
+ llvm::Expected< const swift::reflection::TypeRef &>
150
+ GetTypeRef ( swift::Demangle::Demangler &dem, swift::Demangle::NodePointer node,
151
+ swift::reflection::DescriptorFinder *descriptor_finder) override {
157
152
auto on_exit = PushDescriptorFinderAndPopOnExit (descriptor_finder);
158
153
auto type_ref_or_err =
159
154
swift::Demangle::decodeMangledType (m_reflection_ctx.getBuilder (), node);
160
- if (type_ref_or_err.isError ()) {
161
- LLDB_LOG (GetLog (LLDBLog::Types),
162
- " Could not find typeref: decode mangled type failed. Error: {0}" ,
163
- type_ref_or_err.getError ()->copyErrorString ());
164
- return nullptr ;
165
- }
166
- return type_ref_or_err.getType ();
167
- }
168
-
169
- const swift::reflection::RecordTypeInfo *GetClassInstanceTypeInfo (
170
- const swift::reflection::TypeRef *type_ref,
155
+ if (type_ref_or_err.isError ())
156
+ return llvm::createStringError (
157
+ type_ref_or_err.getError ()->copyErrorString ());
158
+ auto *tr = type_ref_or_err.getType ();
159
+ if (!tr)
160
+ return llvm::createStringError (
161
+ " decoder returned nullptr typeref but no error" );
162
+ return *tr;
163
+ }
164
+
165
+ llvm::Expected<const swift::reflection::RecordTypeInfo &>
166
+ GetClassInstanceTypeInfo (
167
+ const swift::reflection::TypeRef &type_ref,
171
168
swift::remote::TypeInfoProvider *provider,
172
169
swift::reflection::DescriptorFinder *descriptor_finder) override {
173
170
auto on_exit = PushDescriptorFinderAndPopOnExit (descriptor_finder);
174
- if (!type_ref)
175
- return nullptr ;
176
-
177
171
auto start =
178
- m_reflection_ctx.computeUnalignedFieldStartOffset (type_ref, provider);
172
+ m_reflection_ctx.computeUnalignedFieldStartOffset (& type_ref, provider);
179
173
if (!start) {
180
- if (auto *log = GetLog (LLDBLog::Types)) {
181
- std::stringstream ss;
182
- type_ref->dump (ss);
183
- LLDB_LOG (log, " Could not compute start field offset for typeref: " ,
184
- ss.str ());
185
- }
186
- return nullptr ;
174
+ std::stringstream ss;
175
+ type_ref.dump (ss);
176
+ return llvm::createStringError (
177
+ " Could not compute start field offset for typeref: " + ss.str ());
187
178
}
188
179
189
- return m_type_converter.getClassInstanceTypeInfo (type_ref, *start,
190
- provider);
180
+ auto *rti =
181
+ m_type_converter.getClassInstanceTypeInfo (&type_ref, *start, provider);
182
+ if (!rti)
183
+ return llvm::createStringError (
184
+ " converter returned nullptr typeinfo but no error" );
185
+ return *rti;
191
186
}
192
187
193
- const swift::reflection::TypeInfo *
194
- GetTypeInfo (const swift::reflection::TypeRef * type_ref,
188
+ llvm::Expected< const swift::reflection::TypeInfo &>
189
+ GetTypeInfo (const swift::reflection::TypeRef & type_ref,
195
190
swift::remote::TypeInfoProvider *provider,
196
191
swift::reflection::DescriptorFinder *descriptor_finder) override {
197
192
auto on_exit = PushDescriptorFinderAndPopOnExit (descriptor_finder);
198
- if (!type_ref)
199
- return nullptr ;
200
193
201
194
Log *log (GetLog (LLDBLog::Types));
202
195
if (log && log->GetVerbose ()) {
203
196
std::stringstream ss;
204
- type_ref-> dump (ss);
197
+ type_ref. dump (ss);
205
198
LLDB_LOG (log,
206
199
" [TargetReflectionContext[{0:x}]::getTypeInfo] Getting type "
207
200
" info for typeref {1}" ,
208
201
provider ? provider->getId () : 0 , ss.str ());
209
202
}
210
203
211
- auto type_info = m_reflection_ctx.getTypeInfo (type_ref, provider);
212
- if (log && !type_info) {
204
+ auto type_info = m_reflection_ctx.getTypeInfo (& type_ref, provider);
205
+ if (!type_info) {
213
206
std::stringstream ss;
214
- type_ref->dump (ss);
215
- LLDB_LOG (log,
216
- " [TargetReflectionContext::getTypeInfo] Could not get "
217
- " type info for typeref {0}" ,
218
- ss.str ());
207
+ type_ref.dump (ss);
208
+ return llvm::createStringError (" Could not get type info for typeref: " +
209
+ ss.str ());
219
210
}
220
211
221
- if (type_info && log && log->GetVerbose ()) {
212
+ if (log && log->GetVerbose ()) {
222
213
std::stringstream ss;
223
214
type_info->dump (ss);
224
215
LLDB_LOG (log,
225
- " [TargetReflectionContext::getTypeInfo] Found "
226
- " type info {0}" ,
216
+ " [TargetReflectionContext::getTypeInfo] Found type info {0}" ,
227
217
ss.str ());
228
218
}
229
- return type_info;
219
+ return * type_info;
230
220
}
231
221
232
- const swift::reflection::TypeInfo * GetTypeInfoFromInstance (
222
+ llvm::Expected< const swift::reflection::TypeInfo &> GetTypeInfoFromInstance (
233
223
lldb::addr_t instance, swift::remote::TypeInfoProvider *provider,
234
224
swift::reflection::DescriptorFinder *descriptor_finder) override {
235
225
auto on_exit = PushDescriptorFinderAndPopOnExit (descriptor_finder);
236
- return m_reflection_ctx.getInstanceTypeInfo (instance, provider);
226
+ auto *ti = m_reflection_ctx.getInstanceTypeInfo (instance, provider);
227
+ if (!ti)
228
+ return llvm::createStringError (" could not get instance type info" );
229
+ return *ti;
237
230
}
238
231
239
232
swift::reflection::MemoryReader &GetReader () override {
240
233
return m_reflection_ctx.getReader ();
241
234
}
242
235
243
236
const swift::reflection::TypeRef *LookupSuperclass (
244
- const swift::reflection::TypeRef * tr,
237
+ const swift::reflection::TypeRef & tr,
245
238
swift::reflection::DescriptorFinder *descriptor_finder) override {
246
239
auto on_exit = PushDescriptorFinderAndPopOnExit (descriptor_finder);
247
- return m_reflection_ctx.getBuilder ().lookupSuperclass (tr);
240
+ return m_reflection_ctx.getBuilder ().lookupSuperclass (& tr);
248
241
}
249
242
250
243
bool
@@ -254,12 +247,15 @@ class TargetReflectionContext : public ReflectionContextInterface {
254
247
std::function<bool (SuperClassType)> fn) override {
255
248
while (tr) {
256
249
if (fn ({[=]() -> const swift::reflection::RecordTypeInfo * {
257
- return GetRecordTypeInfo (tr, tip, descriptor_finder);
250
+ auto ti_or_err = GetRecordTypeInfo (*tr, tip, descriptor_finder);
251
+ LLDB_LOG_ERRORV (GetLog (LLDBLog::Types), ti_or_err.takeError (),
252
+ " ForEachSuperClassType: {0}" );
253
+ return &*ti_or_err;
258
254
},
259
255
[=]() -> const swift::reflection::TypeRef * { return tr; }}))
260
256
return true ;
261
257
262
- tr = LookupSuperclass (tr, descriptor_finder);
258
+ tr = LookupSuperclass (* tr, descriptor_finder);
263
259
}
264
260
return false ;
265
261
}
@@ -323,11 +319,13 @@ class TargetReflectionContext : public ReflectionContextInterface {
323
319
existential_address, existential_tr);
324
320
}
325
321
326
- const swift::reflection::TypeRef *
322
+ llvm::Expected< const swift::reflection::TypeRef &>
327
323
LookupTypeWitness (const std::string &MangledTypeName,
328
324
const std::string &Member, StringRef Protocol) override {
329
- return m_type_converter.getBuilder ().lookupTypeWitness (MangledTypeName,
330
- Member, Protocol);
325
+ if (auto *tr = m_type_converter.getBuilder ().lookupTypeWitness (
326
+ MangledTypeName, Member, Protocol))
327
+ return *tr;
328
+ return llvm::createStringError (" could not lookup type witness" );
331
329
}
332
330
swift::reflection::ConformanceCollectionResult GetAllConformances () override {
333
331
swift::reflection::TypeRefBuilder &b = m_type_converter.getBuilder ();
@@ -336,31 +334,33 @@ class TargetReflectionContext : public ReflectionContextInterface {
336
334
return b.collectAllConformances <swift::NoObjCInterop, PointerSize>();
337
335
}
338
336
339
- const swift::reflection::TypeRef *
337
+ llvm::Expected< const swift::reflection::TypeRef &>
340
338
ReadTypeFromMetadata (lldb::addr_t metadata_address,
341
339
swift::reflection::DescriptorFinder *descriptor_finder,
342
340
bool skip_artificial_subclasses) override {
343
341
auto on_exit = PushDescriptorFinderAndPopOnExit (descriptor_finder);
344
- return m_reflection_ctx.readTypeFromMetadata (metadata_address,
345
- skip_artificial_subclasses);
342
+ if (auto *tr = m_reflection_ctx.readTypeFromMetadata (
343
+ metadata_address, skip_artificial_subclasses))
344
+ return *tr;
345
+ return llvm::createStringError (" could not read type from metadata" );
346
346
}
347
347
348
- const swift::reflection::TypeRef *
348
+ llvm::Expected< const swift::reflection::TypeRef &>
349
349
ReadTypeFromInstance (lldb::addr_t instance_address,
350
350
swift::reflection::DescriptorFinder *descriptor_finder,
351
351
bool skip_artificial_subclasses) override {
352
352
auto on_exit = PushDescriptorFinderAndPopOnExit (descriptor_finder);
353
353
auto metadata_address =
354
354
m_reflection_ctx.readMetadataFromInstance (instance_address);
355
- if (!metadata_address) {
356
- LLDB_LOG (GetLog (LLDBLog::Types),
357
- " could not read heap metadata for object at {0:x}" ,
358
- instance_address);
359
- return nullptr ;
360
- }
355
+ if (!metadata_address)
356
+ return llvm::createStringError (
357
+ llvm::formatv (" could not read heap metadata for object at {0:x}" ,
358
+ instance_address));
361
359
362
- return m_reflection_ctx.readTypeFromMetadata (*metadata_address,
363
- skip_artificial_subclasses);
360
+ if (auto *tr = m_reflection_ctx.readTypeFromMetadata (
361
+ *metadata_address, skip_artificial_subclasses))
362
+ return *tr;
363
+ return llvm::createStringError (" could not read type from metadata" );
364
364
}
365
365
366
366
std::optional<bool > IsValueInlinedInExistentialContainer (
@@ -369,12 +369,14 @@ class TargetReflectionContext : public ReflectionContextInterface {
369
369
existential_address);
370
370
}
371
371
372
- const swift::reflection::TypeRef * ApplySubstitutions (
373
- const swift::reflection::TypeRef * type_ref,
372
+ llvm::Expected< const swift::reflection::TypeRef &> ApplySubstitutions (
373
+ const swift::reflection::TypeRef & type_ref,
374
374
swift::reflection::GenericArgumentMap substitutions,
375
375
swift::reflection::DescriptorFinder *descriptor_finder) override {
376
376
auto on_exit = PushDescriptorFinderAndPopOnExit (descriptor_finder);
377
- return type_ref->subst (m_reflection_ctx.getBuilder (), substitutions);
377
+ if (auto *tr = type_ref.subst (m_reflection_ctx.getBuilder (), substitutions))
378
+ return *tr;
379
+ return llvm::createStringError (" failed to apply substitutions" );
378
380
}
379
381
380
382
swift::remote::RemoteAbsolutePointer
@@ -411,23 +413,24 @@ class TargetReflectionContext : public ReflectionContextInterface {
411
413
private:
412
414
// / Return a description of the layout of the record (classes, structs and
413
415
// / tuples) type given its typeref.
414
- const swift::reflection::RecordTypeInfo *
415
- GetRecordTypeInfo (const swift::reflection::TypeRef * type_ref,
416
+ llvm::Expected< const swift::reflection::RecordTypeInfo &>
417
+ GetRecordTypeInfo (const swift::reflection::TypeRef & type_ref,
416
418
swift::remote::TypeInfoProvider *tip,
417
419
swift::reflection::DescriptorFinder *descriptor_finder) {
418
- auto *type_info = GetTypeInfo (type_ref, tip, descriptor_finder);
420
+ auto type_info_or_err = GetTypeInfo (type_ref, tip, descriptor_finder);
421
+ if (!type_info_or_err)
422
+ return type_info_or_err.takeError ();
423
+ auto *type_info = &*type_info_or_err;
419
424
if (auto record_type_info =
420
425
llvm::dyn_cast_or_null<swift::reflection::RecordTypeInfo>(
421
426
type_info))
422
- return record_type_info;
427
+ return * record_type_info;
423
428
if (llvm::isa_and_nonnull<swift::reflection::ReferenceTypeInfo>(type_info))
424
429
return GetClassInstanceTypeInfo (type_ref, tip, descriptor_finder);
425
- if (auto *log = GetLog (LLDBLog::Types)) {
426
- std::stringstream ss;
427
- type_ref->dump (ss);
428
- LLDB_LOG (log, " Could not get record type info for typeref: " , ss.str ());
429
- }
430
- return nullptr ;
430
+ std::stringstream ss;
431
+ type_ref.dump (ss);
432
+ return llvm::createStringError (
433
+ " Could not get record type info for typeref: " + ss.str ());
431
434
}
432
435
};
433
436
} // namespace
0 commit comments