1 //===-- SWIG Interface for SBType -------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 namespace lldb {
10 
11     %feature("docstring",
12 "Represents a member of a type in lldb.") SBTypeMember;
13 
14 class SBTypeMember
15 {
16 public:
17     SBTypeMember ();
18 
19     SBTypeMember (const lldb::SBTypeMember& rhs);
20 
21     ~SBTypeMember();
22 
23     bool
24     IsValid() const;
25 
26     explicit operator bool() const;
27 
28     const char *
29     GetName ();
30 
31     lldb::SBType
32     GetType ();
33 
34     uint64_t
35     GetOffsetInBytes();
36 
37     uint64_t
38     GetOffsetInBits();
39 
40     bool
41     IsBitfield();
42 
43     uint32_t
44     GetBitfieldSizeInBits();
45 
46     STRING_EXTENSION_LEVEL(SBTypeMember, lldb::eDescriptionLevelBrief)
47 
48 #ifdef SWIGPYTHON
49     %pythoncode %{
50         name = property(GetName, None, doc='''A read only property that returns the name for this member as a string.''')
51         type = property(GetType, None, doc='''A read only property that returns an lldb object that represents the type (lldb.SBType) for this member.''')
52         byte_offset = property(GetOffsetInBytes, None, doc='''A read only property that returns offset in bytes for this member as an integer.''')
53         bit_offset = property(GetOffsetInBits, None, doc='''A read only property that returns offset in bits for this member as an integer.''')
54         is_bitfield = property(IsBitfield, None, doc='''A read only property that returns true if this member is a bitfield.''')
55         bitfield_bit_size = property(GetBitfieldSizeInBits, None, doc='''A read only property that returns the bitfield size in bits for this member as an integer, or zero if this member is not a bitfield.''')
56     %}
57 #endif
58 
59 protected:
60     std::unique_ptr<lldb_private::TypeMemberImpl> m_opaque_ap;
61 };
62 
63 class SBTypeMemberFunction
64 {
65 public:
66     SBTypeMemberFunction ();
67 
68     SBTypeMemberFunction (const lldb::SBTypeMemberFunction& rhs);
69 
70     ~SBTypeMemberFunction();
71 
72     bool
73     IsValid() const;
74 
75     explicit operator bool() const;
76 
77     const char *
78     GetName ();
79 
80     const char *
81     GetDemangledName ();
82 
83     const char *
84     GetMangledName ();
85 
86     lldb::SBType
87     GetType ();
88 
89     lldb::SBType
90     GetReturnType ();
91 
92     uint32_t
93     GetNumberOfArguments ();
94 
95     lldb::SBType
96     GetArgumentTypeAtIndex (uint32_t);
97 
98     lldb::MemberFunctionKind
99     GetKind();
100 
101     bool
102     GetDescription (lldb::SBStream &description,
103                     lldb::DescriptionLevel description_level);
104 
105     STRING_EXTENSION_LEVEL(SBTypeMemberFunction, lldb::eDescriptionLevelBrief)
106 protected:
107     lldb::TypeMemberFunctionImplSP m_opaque_sp;
108 };
109 
110 %feature("docstring",
111 "Represents a data type in lldb.  The FindFirstType() method of SBTarget/SBModule
112 returns a SBType.
113 
114 SBType supports the eq/ne operator. For example,
115 
116 main.cpp:
117 
118 class Task {
119 public:
120     int id;
121     Task *next;
122     Task(int i, Task *n):
123         id(i),
124         next(n)
125     {}
126 };
127 
128 int main (int argc, char const *argv[])
129 {
130     Task *task_head = new Task(-1, NULL);
131     Task *task1 = new Task(1, NULL);
132     Task *task2 = new Task(2, NULL);
133     Task *task3 = new Task(3, NULL); // Orphaned.
134     Task *task4 = new Task(4, NULL);
135     Task *task5 = new Task(5, NULL);
136 
137     task_head->next = task1;
138     task1->next = task2;
139     task2->next = task4;
140     task4->next = task5;
141 
142     int total = 0;
143     Task *t = task_head;
144     while (t != NULL) {
145         if (t->id >= 0)
146             ++total;
147         t = t->next;
148     }
149     printf('We have a total number of %d tasks\\n', total);
150 
151     // This corresponds to an empty task list.
152     Task *empty_task_head = new Task(-1, NULL);
153 
154     return 0; // Break at this line
155 }
156 
157 find_type.py:
158 
159         # Get the type 'Task'.
160         task_type = target.FindFirstType('Task')
161         self.assertTrue(task_type)
162 
163         # Get the variable 'task_head'.
164         frame0.FindVariable('task_head')
165         task_head_type = task_head.GetType()
166         self.assertTrue(task_head_type.IsPointerType())
167 
168         # task_head_type is 'Task *'.
169         task_pointer_type = task_type.GetPointerType()
170         self.assertTrue(task_head_type == task_pointer_type)
171 
172         # Get the child mmember 'id' from 'task_head'.
173         id = task_head.GetChildMemberWithName('id')
174         id_type = id.GetType()
175 
176         # SBType.GetBasicType() takes an enum 'BasicType' (lldb-enumerations.h).
177         int_type = id_type.GetBasicType(lldb.eBasicTypeInt)
178         # id_type and int_type should be the same type!
179         self.assertTrue(id_type == int_type)
180 
181 ...") SBType;
182 class SBType
183 {
184 public:
185     SBType ();
186 
187     SBType (const lldb::SBType &rhs);
188 
189     ~SBType ();
190 
191     bool
192     IsValid();
193 
194     explicit operator bool() const;
195 
196     uint64_t
197     GetByteSize();
198 
199     bool
200     IsPointerType();
201 
202     bool
203     IsReferenceType();
204 
205     bool
206     IsFunctionType ();
207 
208     bool
209     IsPolymorphicClass ();
210 
211     bool
212     IsArrayType ();
213 
214     bool
215     IsVectorType ();
216 
217     bool
218     IsTypedefType ();
219 
220     bool
221     IsAnonymousType ();
222 
223     lldb::SBType
224     GetPointerType();
225 
226     lldb::SBType
227     GetPointeeType();
228 
229     lldb::SBType
230     GetReferenceType();
231 
232     lldb::SBType
233     SBType::GetTypedefedType();
234 
235     lldb::SBType
236     GetDereferencedType();
237 
238     lldb::SBType
239     GetUnqualifiedType();
240 
241     lldb::SBType
242     GetCanonicalType();
243 
244     lldb::SBType
245     GetArrayElementType ();
246 
247     lldb::SBType
248     GetArrayType (uint64_t size);
249 
250     lldb::SBType
251     GetVectorElementType ();
252 
253     lldb::BasicType
254     GetBasicType();
255 
256     lldb::SBType
257     GetBasicType (lldb::BasicType type);
258 
259     uint32_t
260     GetNumberOfFields ();
261 
262     uint32_t
263     GetNumberOfDirectBaseClasses ();
264 
265     uint32_t
266     GetNumberOfVirtualBaseClasses ();
267 
268     lldb::SBTypeMember
269     GetFieldAtIndex (uint32_t idx);
270 
271     lldb::SBTypeMember
272     GetDirectBaseClassAtIndex (uint32_t idx);
273 
274     lldb::SBTypeMember
275     GetVirtualBaseClassAtIndex (uint32_t idx);
276 
277     lldb::SBTypeEnumMemberList
278     GetEnumMembers();
279 
280     const char*
281     GetName();
282 
283     const char *
284     GetDisplayTypeName ();
285 
286     lldb::TypeClass
287     GetTypeClass ();
288 
289     uint32_t
290     GetNumberOfTemplateArguments ();
291 
292     lldb::SBType
293     GetTemplateArgumentType (uint32_t idx);
294 
295     lldb::TemplateArgumentKind
296     GetTemplateArgumentKind (uint32_t idx);
297 
298     lldb::SBType
299     GetFunctionReturnType ();
300 
301     lldb::SBTypeList
302     GetFunctionArgumentTypes ();
303 
304     uint32_t
305     GetNumberOfMemberFunctions ();
306 
307     lldb::SBTypeMemberFunction
308     GetMemberFunctionAtIndex (uint32_t idx);
309 
310     bool
311     IsTypeComplete ();
312 
313     uint32_t
314     GetTypeFlags ();
315 
316     bool operator==(lldb::SBType &rhs);
317 
318     bool operator!=(lldb::SBType &rhs);
319 
320     STRING_EXTENSION_LEVEL(SBType, lldb::eDescriptionLevelBrief)
321 
322 #ifdef SWIGPYTHON
323     %pythoncode %{
324         def template_arg_array(self):
325             num_args = self.num_template_args
326             if num_args:
327                 template_args = []
328                 for i in range(num_args):
329                     template_args.append(self.GetTemplateArgumentType(i))
330                 return template_args
331             return None
332 
333         name = property(GetName, None, doc='''A read only property that returns the name for this type as a string.''')
334         size = property(GetByteSize, None, doc='''A read only property that returns size in bytes for this type as an integer.''')
335         is_pointer = property(IsPointerType, None, doc='''A read only property that returns a boolean value that indicates if this type is a pointer type.''')
336         is_reference = property(IsReferenceType, None, doc='''A read only property that returns a boolean value that indicates if this type is a reference type.''')
337         is_reference = property(IsReferenceType, None, doc='''A read only property that returns a boolean value that indicates if this type is a function type.''')
338         num_fields = property(GetNumberOfFields, None, doc='''A read only property that returns number of fields in this type as an integer.''')
339         num_bases = property(GetNumberOfDirectBaseClasses, None, doc='''A read only property that returns number of direct base classes in this type as an integer.''')
340         num_vbases = property(GetNumberOfVirtualBaseClasses, None, doc='''A read only property that returns number of virtual base classes in this type as an integer.''')
341         num_template_args = property(GetNumberOfTemplateArguments, None, doc='''A read only property that returns number of template arguments in this type as an integer.''')
342         template_args = property(template_arg_array, None, doc='''A read only property that returns a list() of lldb.SBType objects that represent all template arguments in this type.''')
343         type = property(GetTypeClass, None, doc='''A read only property that returns an lldb enumeration value (see enumerations that start with "lldb.eTypeClass") that represents a classification for this type.''')
344         is_complete = property(IsTypeComplete, None, doc='''A read only property that returns a boolean value that indicates if this type is a complete type (True) or a forward declaration (False).''')
345 
346         def get_bases_array(self):
347             '''An accessor function that returns a list() that contains all direct base classes in a lldb.SBType object.'''
348             bases = []
349             for idx in range(self.GetNumberOfDirectBaseClasses()):
350                 bases.append(self.GetDirectBaseClassAtIndex(idx))
351             return bases
352 
353         def get_vbases_array(self):
354             '''An accessor function that returns a list() that contains all fields in a lldb.SBType object.'''
355             vbases = []
356             for idx in range(self.GetNumberOfVirtualBaseClasses()):
357                 vbases.append(self.GetVirtualBaseClassAtIndex(idx))
358             return vbases
359 
360         def get_fields_array(self):
361             '''An accessor function that returns a list() that contains all fields in a lldb.SBType object.'''
362             fields = []
363             for idx in range(self.GetNumberOfFields()):
364                 fields.append(self.GetFieldAtIndex(idx))
365             return fields
366 
367         def get_members_array(self):
368             '''An accessor function that returns a list() that contains all members (base classes and fields) in a lldb.SBType object in ascending bit offset order.'''
369             members = []
370             bases = self.get_bases_array()
371             fields = self.get_fields_array()
372             vbases = self.get_vbases_array()
373             for base in bases:
374                 bit_offset = base.bit_offset
375                 added = False
376                 for idx, member in enumerate(members):
377                     if member.bit_offset > bit_offset:
378                         members.insert(idx, base)
379                         added = True
380                         break
381                 if not added:
382                     members.append(base)
383             for vbase in vbases:
384                 bit_offset = vbase.bit_offset
385                 added = False
386                 for idx, member in enumerate(members):
387                     if member.bit_offset > bit_offset:
388                         members.insert(idx, vbase)
389                         added = True
390                         break
391                 if not added:
392                     members.append(vbase)
393             for field in fields:
394                 bit_offset = field.bit_offset
395                 added = False
396                 for idx, member in enumerate(members):
397                     if member.bit_offset > bit_offset:
398                         members.insert(idx, field)
399                         added = True
400                         break
401                 if not added:
402                     members.append(field)
403             return members
404 
405         def get_enum_members_array(self):
406             '''An accessor function that returns a list() that contains all enum members in an lldb.SBType object.'''
407             enum_members_list = []
408             sb_enum_members = self.GetEnumMembers()
409             for idx in range(sb_enum_members.GetSize()):
410                 enum_members_list.append(sb_enum_members.GetTypeEnumMemberAtIndex(idx))
411             return enum_members_list
412 
413         bases = property(get_bases_array, None, doc='''A read only property that returns a list() of lldb.SBTypeMember objects that represent all of the direct base classes for this type.''')
414         vbases = property(get_vbases_array, None, doc='''A read only property that returns a list() of lldb.SBTypeMember objects that represent all of the virtual base classes for this type.''')
415         fields = property(get_fields_array, None, doc='''A read only property that returns a list() of lldb.SBTypeMember objects that represent all of the fields for this type.''')
416         members = property(get_members_array, None, doc='''A read only property that returns a list() of all lldb.SBTypeMember objects that represent all of the base classes, virtual base classes and fields for this type in ascending bit offset order.''')
417         enum_members = property(get_enum_members_array, None, doc='''A read only property that returns a list() of all lldb.SBTypeEnumMember objects that represent the enum members for this type.''')
418         %}
419 #endif
420 
421 };
422 
423 %feature("docstring",
424 "Represents a list of SBTypes.  The FindTypes() method of SBTarget/SBModule
425 returns a SBTypeList.
426 
427 SBTypeList supports SBType iteration. For example,
428 
429 main.cpp:
430 
431 class Task {
432 public:
433     int id;
434     Task *next;
435     Task(int i, Task *n):
436         id(i),
437         next(n)
438     {}
439 };
440 
441 ...
442 
443 find_type.py:
444 
445         # Get the type 'Task'.
446         type_list = target.FindTypes('Task')
447         self.assertTrue(len(type_list) == 1)
448         # To illustrate the SBType iteration.
449         for type in type_list:
450             # do something with type
451 
452 ...") SBTypeList;
453 class SBTypeList
454 {
455 public:
456     SBTypeList();
457 
458     bool
459     IsValid();
460 
461     explicit operator bool() const;
462 
463     void
464     Append (lldb::SBType type);
465 
466     lldb::SBType
467     GetTypeAtIndex (uint32_t index);
468 
469     uint32_t
470     GetSize();
471 
472     ~SBTypeList();
473 
474 #ifdef SWIGPYTHON
475     %pythoncode%{
476     def __iter__(self):
477         '''Iterate over all types in a lldb.SBTypeList object.'''
478         return lldb_iter(self, 'GetSize', 'GetTypeAtIndex')
479 
480     def __len__(self):
481         '''Return the number of types in a lldb.SBTypeList object.'''
482         return self.GetSize()
483     %}
484 #endif
485 };
486 
487 } // namespace lldb
488