Skip to content

Commit 1f42bfb

Browse files
committed
[lldb] Thread llvm::Expected through ReflectionContext
1 parent 3c84c55 commit 1f42bfb

File tree

4 files changed

+385
-251
lines changed

4 files changed

+385
-251
lines changed

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

Lines changed: 89 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
411413
private:
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

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,29 @@ class ReflectionContextInterface {
8383
ReadELF(swift::remote::RemoteAddress ImageStart,
8484
std::optional<llvm::sys::MemoryBlock> FileBuffer,
8585
llvm::SmallVector<llvm::StringRef, 1> likely_module_names = {}) = 0;
86-
virtual const swift::reflection::TypeRef *GetTypeRefOrNull(
87-
llvm::StringRef mangled_type_name,
88-
swift::reflection::DescriptorFinder *descriptor_finder) = 0;
89-
virtual const swift::reflection::TypeRef *GetTypeRefOrNull(
90-
swift::Demangle::Demangler &dem, swift::Demangle::NodePointer node,
91-
swift::reflection::DescriptorFinder *descriptor_finder) = 0;
92-
virtual const swift::reflection::TypeInfo *GetClassInstanceTypeInfo(
93-
const swift::reflection::TypeRef *type_ref,
86+
virtual llvm::Expected<const swift::reflection::TypeRef &>
87+
GetTypeRef(llvm::StringRef mangled_type_name,
88+
swift::reflection::DescriptorFinder *descriptor_finder) = 0;
89+
virtual llvm::Expected<const swift::reflection::TypeRef &>
90+
GetTypeRef(swift::Demangle::Demangler &dem, swift::Demangle::NodePointer node,
91+
swift::reflection::DescriptorFinder *descriptor_finder) = 0;
92+
virtual llvm::Expected<const swift::reflection::RecordTypeInfo &>
93+
GetClassInstanceTypeInfo(
94+
const swift::reflection::TypeRef &type_ref,
9495
swift::remote::TypeInfoProvider *provider,
9596
swift::reflection::DescriptorFinder *descriptor_finder) = 0;
96-
virtual const swift::reflection::TypeInfo *
97-
GetTypeInfo(const swift::reflection::TypeRef *type_ref,
97+
virtual llvm::Expected<const swift::reflection::TypeInfo &>
98+
GetTypeInfo(const swift::reflection::TypeRef &type_ref,
9899
swift::remote::TypeInfoProvider *provider,
99100
swift::reflection::DescriptorFinder *descriptor_finder) = 0;
100-
virtual const swift::reflection::TypeInfo *GetTypeInfoFromInstance(
101+
virtual llvm::Expected<const swift::reflection::TypeInfo &>
102+
GetTypeInfoFromInstance(
101103
lldb::addr_t instance, swift::remote::TypeInfoProvider *provider,
102104
swift::reflection::DescriptorFinder *descriptor_finder) = 0;
103105
virtual swift::remote::MemoryReader &GetReader() = 0;
104-
virtual const swift::reflection::TypeRef *LookupSuperclass(
105-
const swift::reflection::TypeRef *tr,
106-
swift::reflection::DescriptorFinder *descriptor_finder) = 0;
106+
virtual const swift::reflection::TypeRef *
107+
LookupSuperclass(const swift::reflection::TypeRef &tr,
108+
swift::reflection::DescriptorFinder *descriptor_finder) = 0;
107109
virtual bool
108110
ForEachSuperClassType(swift::remote::TypeInfoProvider *tip,
109111
swift::reflection::DescriptorFinder *descriptor_finder,
@@ -131,23 +133,23 @@ class ReflectionContextInterface {
131133
const swift::reflection::TypeRef *enum_type_ref,
132134
swift::remote::TypeInfoProvider *provider,
133135
swift::reflection::DescriptorFinder *descriptor_finder) = 0;
134-
virtual const swift::reflection::TypeRef *
136+
virtual llvm::Expected<const swift::reflection::TypeRef &>
135137
LookupTypeWitness(const std::string &MangledTypeName,
136138
const std::string &Member, StringRef Protocol) = 0;
137139
virtual swift::reflection::ConformanceCollectionResult
138140
GetAllConformances() = 0;
139-
virtual const swift::reflection::TypeRef *ReadTypeFromMetadata(
140-
lldb::addr_t metadata_address,
141-
swift::reflection::DescriptorFinder *descriptor_finder,
142-
bool skip_artificial_subclasses = false) = 0;
143-
virtual const swift::reflection::TypeRef *ReadTypeFromInstance(
144-
lldb::addr_t instance_address,
145-
swift::reflection::DescriptorFinder *descriptor_finder,
146-
bool skip_artificial_subclasses = false) = 0;
141+
virtual llvm::Expected<const swift::reflection::TypeRef &>
142+
ReadTypeFromMetadata(lldb::addr_t metadata_address,
143+
swift::reflection::DescriptorFinder *descriptor_finder,
144+
bool skip_artificial_subclasses = false) = 0;
145+
virtual llvm::Expected<const swift::reflection::TypeRef &>
146+
ReadTypeFromInstance(lldb::addr_t instance_address,
147+
swift::reflection::DescriptorFinder *descriptor_finder,
148+
bool skip_artificial_subclasses = false) = 0;
147149
virtual std::optional<bool> IsValueInlinedInExistentialContainer(
148150
swift::remote::RemoteAddress existential_address) = 0;
149-
virtual const swift::reflection::TypeRef *ApplySubstitutions(
150-
const swift::reflection::TypeRef *type_ref,
151+
virtual llvm::Expected<const swift::reflection::TypeRef &> ApplySubstitutions(
152+
const swift::reflection::TypeRef &type_ref,
151153
swift::reflection::GenericArgumentMap substitutions,
152154
swift::reflection::DescriptorFinder *descriptor_finder) = 0;
153155
virtual swift::remote::RemoteAbsolutePointer

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,13 +531,12 @@ class SwiftLanguageRuntime : public LanguageRuntime {
531531
llvm::ArrayRef<swift::reflection::FieldInfo> fields);
532532

533533
/// Use the reflection context to build a TypeRef object.
534-
const swift::reflection::TypeRef *
534+
llvm::Expected<const swift::reflection::TypeRef &>
535535
GetTypeRef(CompilerType type, TypeSystemSwiftTypeRef *module_holder);
536536

537-
private:
538537
/// Ask Remote Mirrors for the type info about a Swift type.
539538
/// This will return a nullptr if the lookup fails.
540-
const swift::reflection::TypeInfo *
539+
llvm::Expected<const swift::reflection::TypeInfo &>
541540
GetSwiftRuntimeTypeInfo(CompilerType type, ExecutionContextScope *exe_scope,
542541
swift::reflection::TypeRef const **out_tr = nullptr);
543542

0 commit comments

Comments
 (0)