@@ -130,17 +130,12 @@ class TargetReflectionContext : public ReflectionContextInterface {
130130 return id;
131131 }
132132
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 {
136136 swift::Demangle::Demangler dem;
137137 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);
144139 }
145140
146141 // / Sets the descriptor finder, and on scope exit clears it out.
@@ -151,100 +146,98 @@ class TargetReflectionContext : public ReflectionContextInterface {
151146 [&]() { m_forwader.PopExternalDescriptorFinder (); });
152147 }
153148
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 {
157152 auto on_exit = PushDescriptorFinderAndPopOnExit (descriptor_finder);
158153 auto type_ref_or_err =
159154 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,
171168 swift::remote::TypeInfoProvider *provider,
172169 swift::reflection::DescriptorFinder *descriptor_finder) override {
173170 auto on_exit = PushDescriptorFinderAndPopOnExit (descriptor_finder);
174- if (!type_ref)
175- return nullptr ;
176-
177171 auto start =
178- m_reflection_ctx.computeUnalignedFieldStartOffset (type_ref, provider);
172+ m_reflection_ctx.computeUnalignedFieldStartOffset (& type_ref, provider);
179173 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 ());
187178 }
188179
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;
191186 }
192187
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,
195190 swift::remote::TypeInfoProvider *provider,
196191 swift::reflection::DescriptorFinder *descriptor_finder) override {
197192 auto on_exit = PushDescriptorFinderAndPopOnExit (descriptor_finder);
198- if (!type_ref)
199- return nullptr ;
200193
201194 Log *log (GetLog (LLDBLog::Types));
202195 if (log && log->GetVerbose ()) {
203196 std::stringstream ss;
204- type_ref-> dump (ss);
197+ type_ref. dump (ss);
205198 LLDB_LOG (log,
206199 " [TargetReflectionContext[{0:x}]::getTypeInfo] Getting type "
207200 " info for typeref {1}" ,
208201 provider ? provider->getId () : 0 , ss.str ());
209202 }
210203
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) {
213206 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 ());
219210 }
220211
221- if (type_info && log && log->GetVerbose ()) {
212+ if (log && log->GetVerbose ()) {
222213 std::stringstream ss;
223214 type_info->dump (ss);
224215 LLDB_LOG (log,
225- " [TargetReflectionContext::getTypeInfo] Found "
226- " type info {0}" ,
216+ " [TargetReflectionContext::getTypeInfo] Found type info {0}" ,
227217 ss.str ());
228218 }
229- return type_info;
219+ return * type_info;
230220 }
231221
232- const swift::reflection::TypeInfo * GetTypeInfoFromInstance (
222+ llvm::Expected< const swift::reflection::TypeInfo &> GetTypeInfoFromInstance (
233223 lldb::addr_t instance, swift::remote::TypeInfoProvider *provider,
234224 swift::reflection::DescriptorFinder *descriptor_finder) override {
235225 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;
237230 }
238231
239232 swift::reflection::MemoryReader &GetReader () override {
240233 return m_reflection_ctx.getReader ();
241234 }
242235
243236 const swift::reflection::TypeRef *LookupSuperclass (
244- const swift::reflection::TypeRef * tr,
237+ const swift::reflection::TypeRef & tr,
245238 swift::reflection::DescriptorFinder *descriptor_finder) override {
246239 auto on_exit = PushDescriptorFinderAndPopOnExit (descriptor_finder);
247- return m_reflection_ctx.getBuilder ().lookupSuperclass (tr);
240+ return m_reflection_ctx.getBuilder ().lookupSuperclass (& tr);
248241 }
249242
250243 bool
@@ -254,12 +247,15 @@ class TargetReflectionContext : public ReflectionContextInterface {
254247 std::function<bool (SuperClassType)> fn) override {
255248 while (tr) {
256249 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;
258254 },
259255 [=]() -> const swift::reflection::TypeRef * { return tr; }}))
260256 return true ;
261257
262- tr = LookupSuperclass (tr, descriptor_finder);
258+ tr = LookupSuperclass (* tr, descriptor_finder);
263259 }
264260 return false ;
265261 }
@@ -323,11 +319,13 @@ class TargetReflectionContext : public ReflectionContextInterface {
323319 existential_address, existential_tr);
324320 }
325321
326- const swift::reflection::TypeRef *
322+ llvm::Expected< const swift::reflection::TypeRef &>
327323 LookupTypeWitness (const std::string &MangledTypeName,
328324 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" );
331329 }
332330 swift::reflection::ConformanceCollectionResult GetAllConformances () override {
333331 swift::reflection::TypeRefBuilder &b = m_type_converter.getBuilder ();
@@ -336,31 +334,33 @@ class TargetReflectionContext : public ReflectionContextInterface {
336334 return b.collectAllConformances <swift::NoObjCInterop, PointerSize>();
337335 }
338336
339- const swift::reflection::TypeRef *
337+ llvm::Expected< const swift::reflection::TypeRef &>
340338 ReadTypeFromMetadata (lldb::addr_t metadata_address,
341339 swift::reflection::DescriptorFinder *descriptor_finder,
342340 bool skip_artificial_subclasses) override {
343341 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" );
346346 }
347347
348- const swift::reflection::TypeRef *
348+ llvm::Expected< const swift::reflection::TypeRef &>
349349 ReadTypeFromInstance (lldb::addr_t instance_address,
350350 swift::reflection::DescriptorFinder *descriptor_finder,
351351 bool skip_artificial_subclasses) override {
352352 auto on_exit = PushDescriptorFinderAndPopOnExit (descriptor_finder);
353353 auto metadata_address =
354354 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));
361359
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" );
364364 }
365365
366366 std::optional<bool > IsValueInlinedInExistentialContainer (
@@ -369,12 +369,14 @@ class TargetReflectionContext : public ReflectionContextInterface {
369369 existential_address);
370370 }
371371
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,
374374 swift::reflection::GenericArgumentMap substitutions,
375375 swift::reflection::DescriptorFinder *descriptor_finder) override {
376376 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" );
378380 }
379381
380382 swift::remote::RemoteAbsolutePointer
@@ -411,23 +413,24 @@ class TargetReflectionContext : public ReflectionContextInterface {
411413private:
412414 // / Return a description of the layout of the record (classes, structs and
413415 // / 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,
416418 swift::remote::TypeInfoProvider *tip,
417419 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;
419424 if (auto record_type_info =
420425 llvm::dyn_cast_or_null<swift::reflection::RecordTypeInfo>(
421426 type_info))
422- return record_type_info;
427+ return * record_type_info;
423428 if (llvm::isa_and_nonnull<swift::reflection::ReferenceTypeInfo>(type_info))
424429 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 ());
431434 }
432435};
433436} // namespace
0 commit comments