13
13
//
14
14
// ===----------------------------------------------------------------------===//
15
15
16
+ #include < sstream>
17
+
18
+ #include " DWARFDebugInfo.h"
16
19
#include " DWARFASTParserSwift.h"
17
20
18
21
#include " DWARFDIE.h"
19
- #include " DWARFDebugInfo.h"
20
22
21
- #include " Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h"
22
23
#include " Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h"
23
-
24
24
#include " swift/RemoteInspection/TypeLowering.h"
25
25
26
+ #include " lldb/Utility/LLDBLog.h"
27
+ #include " lldb/Utility/Log.h"
28
+ #include " lldb/Target/Target.h"
26
29
27
30
using namespace lldb ;
28
31
using namespace lldb_private ;
@@ -38,30 +41,46 @@ getTypeAndDie(TypeSystemSwiftTypeRef &ts,
38
41
swift::Demangle::Demangler dem;
39
42
swift::Demangle::NodePointer node = TR->getDemangling (dem);
40
43
auto type = ts.RemangleAsType (dem, node);
41
- if (!type)
44
+ if (!type) {
45
+ if (auto log = GetLog (LLDBLog::Types)) {
46
+ std::stringstream ss;
47
+ TR->dump (ss);
48
+ LLDB_LOG (log , " Could not find type for typeref: {0}" , ss.str ());
49
+ }
42
50
return {};
51
+ }
43
52
44
53
auto *dwarf = llvm::cast_or_null<SymbolFileDWARF>(ts.GetSymbolFile ());
45
54
if (!dwarf)
46
55
return {};
47
56
auto lldb_type = ts.FindTypeInModule (type.GetOpaqueQualType ());
48
- if (!lldb_type)
57
+ if (!lldb_type) {
49
58
// TODO: for embedded Swift this is fine but consult other modules here for
50
59
// general case?
60
+ LLDB_LOGV (GetLog (LLDBLog::Types), " Could not find type {0} in module" ,
61
+ type.GetMangledTypeName ());
51
62
return {};
63
+ }
52
64
auto die = dwarf->GetDIE (lldb_type->GetID ());
53
65
return {{type, die}};
54
66
}
55
67
56
68
static std::optional<swift::reflection::FieldDescriptorKind>
57
- getFieldDescriptorKindForDie (DWARFDIE &die) {
58
- if (die.Tag () == DW_TAG_structure_type) {
59
- if (die.HasChildren () && die.GetFirstChild ().Tag () == llvm::dwarf::DW_TAG_variant_part)
60
- return swift::reflection::FieldDescriptorKind::Enum;
69
+ getFieldDescriptorKindForDie (CompilerType type) {
70
+ auto type_class = type.GetTypeClass ();
71
+ switch (type_class) {
72
+ case lldb::eTypeClassClass:
73
+ return swift::reflection::FieldDescriptorKind::Class;
74
+ case lldb::eTypeClassStruct:
61
75
return swift::reflection::FieldDescriptorKind::Struct;
76
+ case lldb::eTypeClassEnumeration:
77
+ return swift::reflection::FieldDescriptorKind::Enum;
78
+ default :
79
+ LLDB_LOG (GetLog (LLDBLog::Types),
80
+ " Could not determine file descriptor kind for type: {0}" ,
81
+ type.GetMangledTypeName ());
82
+ return {};
62
83
}
63
- // TODO: handle more cases, for now we only support structs and enums.
64
- return {};
65
84
}
66
85
67
86
namespace {
@@ -108,20 +127,23 @@ class DWARFFieldDescriptorImpl : public swift::reflection::FieldDescriptorBase {
108
127
TypeSystemSwiftTypeRef &m_type_system;
109
128
ConstString m_mangled_name;
110
129
DIERef m_die_ref;
130
+ NodePointer m_superclass_node;
111
131
112
132
public:
113
133
DWARFFieldDescriptorImpl (swift::reflection::FieldDescriptorKind kind,
114
- bool has_superclass ,
134
+ NodePointer superclass_node ,
115
135
TypeSystemSwiftTypeRef &type_system,
116
136
ConstString mangled_name, DIERef die_ref)
117
- : swift::reflection::FieldDescriptorBase(kind, has_superclass),
137
+ : swift::reflection::FieldDescriptorBase(kind,
138
+ superclass_node != nullptr ),
118
139
m_type_system (type_system), m_mangled_name(mangled_name),
119
- m_die_ref(die_ref) {}
140
+ m_die_ref(die_ref), m_superclass_node(superclass_node) {}
120
141
121
142
~DWARFFieldDescriptorImpl () override = default ;
122
143
123
- // TODO: implement this.
124
- swift::Demangle::NodePointer demangleSuperclass () override { return nullptr ; }
144
+ swift::Demangle::NodePointer demangleSuperclass () override {
145
+ return m_superclass_node;
146
+ }
125
147
126
148
std::vector<std::unique_ptr<swift::reflection::FieldRecordBase>>
127
149
getFieldRecords () override {
@@ -139,17 +161,22 @@ class DWARFFieldDescriptorImpl : public swift::reflection::FieldDescriptorBase {
139
161
140
162
switch (Kind) {
141
163
case swift::reflection::FieldDescriptorKind::Struct:
142
- return getFieldRecordsFromStruct (die, dwarf_parser);
164
+ case swift::reflection::FieldDescriptorKind::Class:
165
+ return getFieldRecordsFromStructOrClass (die, dwarf_parser);
143
166
case swift::reflection::FieldDescriptorKind::Enum:
144
167
return getFieldRecordsFromEnum (die, dwarf_parser);
145
168
default :
146
169
// TODO: handle more cases.
170
+ LLDB_LOG (GetLog (LLDBLog::Types),
171
+ " Trying to get field records of unexpected kind: {0}" ,
172
+ (uint8_t )Kind);
173
+ assert (false && " Trying to get field records of unexpected kind" );
147
174
return {};
148
175
}
149
176
}
150
177
151
178
std::vector<std::unique_ptr<swift::reflection::FieldRecordBase>>
152
- getFieldRecordsFromStruct (const DWARFDIE &die,
179
+ getFieldRecordsFromStructOrClass (const DWARFDIE &die,
153
180
plugin::dwarf::DWARFASTParser *dwarf_parser) {
154
181
std::vector<std::unique_ptr<swift::reflection::FieldRecordBase>> fields;
155
182
for (DWARFDIE child_die : die.children ()) {
@@ -221,12 +248,14 @@ DWARFASTParserSwift::getBuiltinTypeDescriptor(
221
248
return nullptr ;
222
249
auto &[type, die] = *pair;
223
250
224
- if (die.Tag () == llvm::dwarf::DW_TAG_structure_type) {
225
- auto child = die.GetFirstChild ();
226
- if (child.Tag () != llvm::dwarf::DW_TAG_variant_part)
251
+ if (!TypeSystemSwiftTypeRef::IsBuiltinType (type)) {
252
+ if (die.Tag () == llvm::dwarf::DW_TAG_structure_type) {
253
+ auto child = die.GetFirstChild ();
254
+ if (child.Tag () != llvm::dwarf::DW_TAG_variant_part)
255
+ return nullptr ;
256
+ } else if (die.Tag () != llvm::dwarf::DW_TAG_base_type)
227
257
return nullptr ;
228
- } else if (die.Tag () != llvm::dwarf::DW_TAG_base_type)
229
- return nullptr ;
258
+ }
230
259
231
260
auto byte_size =
232
261
die.GetAttributeValueAsUnsigned (DW_AT_byte_size, LLDB_INVALID_ADDRESS);
@@ -249,6 +278,35 @@ DWARFASTParserSwift::getBuiltinTypeDescriptor(
249
278
type.GetMangledTypeName ());
250
279
}
251
280
281
+ namespace {
282
+ DWARFDIE FindSuperClassDIE (DWARFDIE &die) {
283
+ const auto inheritance_die_it =
284
+ llvm::find_if (die.children (), [&](const DWARFDIE &child_die) {
285
+ return child_die.Tag () == llvm::dwarf::DW_TAG_inheritance;
286
+ });
287
+
288
+ if (inheritance_die_it == die.children ().end ())
289
+ return {};
290
+
291
+ auto inheritance_die = *inheritance_die_it;
292
+ const auto superclass_type_die =
293
+ inheritance_die.GetAttributeValueAsReferenceDIE (llvm::dwarf::DW_AT_type);
294
+ return superclass_type_die;
295
+ }
296
+ } // namespace
297
+
298
+ NodePointer DWARFASTParserSwift::GetCanonicalDemangleTree (DWARFDIE &die) {
299
+ const auto name = StringRef (
300
+ die.GetAttributeValueAsString (llvm::dwarf::DW_AT_linkage_name, " " ));
301
+
302
+ if (name.empty ())
303
+ return nullptr ;
304
+
305
+ auto *node =
306
+ m_swift_typesystem.GetCanonicalDemangleTree (m_dem, name);
307
+ return node;
308
+ }
309
+
252
310
std::unique_ptr<swift::reflection::FieldDescriptorBase>
253
311
DWARFASTParserSwift::getFieldDescriptor (const swift::reflection::TypeRef *TR) {
254
312
if (!Target::GetGlobalProperties ().GetSwiftEnableFullDwarfDebugging ())
@@ -260,12 +318,14 @@ DWARFASTParserSwift::getFieldDescriptor(const swift::reflection::TypeRef *TR) {
260
318
auto [type, die] = *pair;
261
319
if (!die)
262
320
return nullptr ;
263
- auto kind = getFieldDescriptorKindForDie (die );
321
+ auto kind = getFieldDescriptorKindForDie (type );
264
322
if (!kind)
265
323
return nullptr ;
266
- // TODO: encode this in DWARF, maybe as a DW_AT_containing_type?
267
- bool has_superclass = false ;
324
+
325
+ DWARFDIE superclass_die = FindSuperClassDIE (die);
326
+ NodePointer superclass_pointer = GetCanonicalDemangleTree (superclass_die);
327
+
268
328
return std::make_unique<DWARFFieldDescriptorImpl>(
269
- *kind, has_superclass , m_swift_typesystem, type.GetMangledTypeName (),
329
+ *kind, superclass_pointer , m_swift_typesystem, type.GetMangledTypeName (),
270
330
*die.GetDIERef ());
271
331
}
0 commit comments