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