1 //===-- SBType.cpp --------------------------------------------------------===//
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 #include "lldb/API/SBType.h"
10 #include "SBReproducerPrivate.h"
11 #include "lldb/API/SBDefines.h"
12 #include "lldb/API/SBModule.h"
13 #include "lldb/API/SBStream.h"
14 #include "lldb/API/SBTypeEnumMember.h"
15 #include "lldb/Core/Mangled.h"
16 #include "lldb/Symbol/CompilerType.h"
17 #include "lldb/Symbol/Type.h"
18 #include "lldb/Symbol/TypeSystem.h"
19 #include "lldb/Utility/ConstString.h"
20 #include "lldb/Utility/Stream.h"
21 
22 #include "llvm/ADT/APSInt.h"
23 
24 #include <memory>
25 
26 using namespace lldb;
27 using namespace lldb_private;
28 
29 SBType::SBType() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBType); }
30 
31 SBType::SBType(const CompilerType &type)
32     : m_opaque_sp(new TypeImpl(
33           CompilerType(type.GetTypeSystem(), type.GetOpaqueQualType()))) {}
34 
35 SBType::SBType(const lldb::TypeSP &type_sp)
36     : m_opaque_sp(new TypeImpl(type_sp)) {}
37 
38 SBType::SBType(const lldb::TypeImplSP &type_impl_sp)
39     : m_opaque_sp(type_impl_sp) {}
40 
41 SBType::SBType(const SBType &rhs) {
42   LLDB_RECORD_CONSTRUCTOR(SBType, (const lldb::SBType &), rhs);
43 
44   if (this != &rhs) {
45     m_opaque_sp = rhs.m_opaque_sp;
46   }
47 }
48 
49 // SBType::SBType (TypeImpl* impl) :
50 //    m_opaque_up(impl)
51 //{}
52 //
53 bool SBType::operator==(SBType &rhs) {
54   LLDB_RECORD_METHOD(bool, SBType, operator==,(lldb::SBType &), rhs);
55 
56   if (!IsValid())
57     return !rhs.IsValid();
58 
59   if (!rhs.IsValid())
60     return false;
61 
62   return *m_opaque_sp.get() == *rhs.m_opaque_sp.get();
63 }
64 
65 bool SBType::operator!=(SBType &rhs) {
66   LLDB_RECORD_METHOD(bool, SBType, operator!=,(lldb::SBType &), rhs);
67 
68   if (!IsValid())
69     return rhs.IsValid();
70 
71   if (!rhs.IsValid())
72     return true;
73 
74   return *m_opaque_sp.get() != *rhs.m_opaque_sp.get();
75 }
76 
77 lldb::TypeImplSP SBType::GetSP() { return m_opaque_sp; }
78 
79 void SBType::SetSP(const lldb::TypeImplSP &type_impl_sp) {
80   m_opaque_sp = type_impl_sp;
81 }
82 
83 SBType &SBType::operator=(const SBType &rhs) {
84   LLDB_RECORD_METHOD(lldb::SBType &, SBType, operator=,(const lldb::SBType &),
85                      rhs);
86 
87   if (this != &rhs) {
88     m_opaque_sp = rhs.m_opaque_sp;
89   }
90   return LLDB_RECORD_RESULT(*this);
91 }
92 
93 SBType::~SBType() = default;
94 
95 TypeImpl &SBType::ref() {
96   if (m_opaque_sp.get() == nullptr)
97     m_opaque_sp = std::make_shared<TypeImpl>();
98   return *m_opaque_sp;
99 }
100 
101 const TypeImpl &SBType::ref() const {
102   // "const SBAddress &addr" should already have checked "addr.IsValid()" prior
103   // to calling this function. In case you didn't we will assert and die to let
104   // you know.
105   assert(m_opaque_sp.get());
106   return *m_opaque_sp;
107 }
108 
109 bool SBType::IsValid() const {
110   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBType, IsValid);
111   return this->operator bool();
112 }
113 SBType::operator bool() const {
114   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBType, operator bool);
115 
116   if (m_opaque_sp.get() == nullptr)
117     return false;
118 
119   return m_opaque_sp->IsValid();
120 }
121 
122 uint64_t SBType::GetByteSize() {
123   LLDB_RECORD_METHOD_NO_ARGS(uint64_t, SBType, GetByteSize);
124 
125   if (IsValid())
126     if (llvm::Optional<uint64_t> size =
127             m_opaque_sp->GetCompilerType(false).GetByteSize(nullptr))
128       return *size;
129   return 0;
130 }
131 
132 bool SBType::IsPointerType() {
133   LLDB_RECORD_METHOD_NO_ARGS(bool, SBType, IsPointerType);
134 
135   if (!IsValid())
136     return false;
137   return m_opaque_sp->GetCompilerType(true).IsPointerType();
138 }
139 
140 bool SBType::IsArrayType() {
141   LLDB_RECORD_METHOD_NO_ARGS(bool, SBType, IsArrayType);
142 
143   if (!IsValid())
144     return false;
145   return m_opaque_sp->GetCompilerType(true).IsArrayType(nullptr, nullptr,
146                                                         nullptr);
147 }
148 
149 bool SBType::IsVectorType() {
150   LLDB_RECORD_METHOD_NO_ARGS(bool, SBType, IsVectorType);
151 
152   if (!IsValid())
153     return false;
154   return m_opaque_sp->GetCompilerType(true).IsVectorType(nullptr, nullptr);
155 }
156 
157 bool SBType::IsReferenceType() {
158   LLDB_RECORD_METHOD_NO_ARGS(bool, SBType, IsReferenceType);
159 
160   if (!IsValid())
161     return false;
162   return m_opaque_sp->GetCompilerType(true).IsReferenceType();
163 }
164 
165 SBType SBType::GetPointerType() {
166   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBType, SBType, GetPointerType);
167 
168   if (!IsValid())
169     return LLDB_RECORD_RESULT(SBType());
170 
171   return LLDB_RECORD_RESULT(
172       SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetPointerType()))));
173 }
174 
175 SBType SBType::GetPointeeType() {
176   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBType, SBType, GetPointeeType);
177 
178   if (!IsValid())
179     return LLDB_RECORD_RESULT(SBType());
180   return LLDB_RECORD_RESULT(
181       SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetPointeeType()))));
182 }
183 
184 SBType SBType::GetReferenceType() {
185   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBType, SBType, GetReferenceType);
186 
187   if (!IsValid())
188     return LLDB_RECORD_RESULT(SBType());
189   return LLDB_RECORD_RESULT(
190       SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetReferenceType()))));
191 }
192 
193 SBType SBType::GetTypedefedType() {
194   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBType, SBType, GetTypedefedType);
195 
196   if (!IsValid())
197     return LLDB_RECORD_RESULT(SBType());
198   return LLDB_RECORD_RESULT(
199       SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetTypedefedType()))));
200 }
201 
202 SBType SBType::GetDereferencedType() {
203   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBType, SBType, GetDereferencedType);
204 
205   if (!IsValid())
206     return LLDB_RECORD_RESULT(SBType());
207   return LLDB_RECORD_RESULT(
208       SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetDereferencedType()))));
209 }
210 
211 SBType SBType::GetArrayElementType() {
212   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBType, SBType, GetArrayElementType);
213 
214   if (!IsValid())
215     return LLDB_RECORD_RESULT(SBType());
216   return LLDB_RECORD_RESULT(SBType(TypeImplSP(new TypeImpl(
217       m_opaque_sp->GetCompilerType(true).GetArrayElementType(nullptr)))));
218 }
219 
220 SBType SBType::GetArrayType(uint64_t size) {
221   LLDB_RECORD_METHOD(lldb::SBType, SBType, GetArrayType, (uint64_t), size);
222 
223   if (!IsValid())
224     return LLDB_RECORD_RESULT(SBType());
225   return LLDB_RECORD_RESULT(SBType(TypeImplSP(
226       new TypeImpl(m_opaque_sp->GetCompilerType(true).GetArrayType(size)))));
227 }
228 
229 SBType SBType::GetVectorElementType() {
230   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBType, SBType, GetVectorElementType);
231 
232   SBType type_sb;
233   if (IsValid()) {
234     CompilerType vector_element_type;
235     if (m_opaque_sp->GetCompilerType(true).IsVectorType(&vector_element_type,
236                                                         nullptr))
237       type_sb.SetSP(TypeImplSP(new TypeImpl(vector_element_type)));
238   }
239   return LLDB_RECORD_RESULT(type_sb);
240 }
241 
242 bool SBType::IsFunctionType() {
243   LLDB_RECORD_METHOD_NO_ARGS(bool, SBType, IsFunctionType);
244 
245   if (!IsValid())
246     return false;
247   return m_opaque_sp->GetCompilerType(true).IsFunctionType();
248 }
249 
250 bool SBType::IsPolymorphicClass() {
251   LLDB_RECORD_METHOD_NO_ARGS(bool, SBType, IsPolymorphicClass);
252 
253   if (!IsValid())
254     return false;
255   return m_opaque_sp->GetCompilerType(true).IsPolymorphicClass();
256 }
257 
258 bool SBType::IsTypedefType() {
259   LLDB_RECORD_METHOD_NO_ARGS(bool, SBType, IsTypedefType);
260 
261   if (!IsValid())
262     return false;
263   return m_opaque_sp->GetCompilerType(true).IsTypedefType();
264 }
265 
266 bool SBType::IsAnonymousType() {
267   LLDB_RECORD_METHOD_NO_ARGS(bool, SBType, IsAnonymousType);
268 
269   if (!IsValid())
270     return false;
271   return m_opaque_sp->GetCompilerType(true).IsAnonymousType();
272 }
273 
274 bool SBType::IsScopedEnumerationType() {
275   LLDB_RECORD_METHOD_NO_ARGS(bool, SBType, IsScopedEnumerationType);
276 
277   if (!IsValid())
278     return false;
279   return m_opaque_sp->GetCompilerType(true).IsScopedEnumerationType();
280 }
281 
282 lldb::SBType SBType::GetFunctionReturnType() {
283   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBType, SBType, GetFunctionReturnType);
284 
285   if (IsValid()) {
286     CompilerType return_type(
287         m_opaque_sp->GetCompilerType(true).GetFunctionReturnType());
288     if (return_type.IsValid())
289       return LLDB_RECORD_RESULT(SBType(return_type));
290   }
291   return LLDB_RECORD_RESULT(lldb::SBType());
292 }
293 
294 lldb::SBTypeList SBType::GetFunctionArgumentTypes() {
295   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBTypeList, SBType,
296                              GetFunctionArgumentTypes);
297 
298   SBTypeList sb_type_list;
299   if (IsValid()) {
300     CompilerType func_type(m_opaque_sp->GetCompilerType(true));
301     size_t count = func_type.GetNumberOfFunctionArguments();
302     for (size_t i = 0; i < count; i++) {
303       sb_type_list.Append(SBType(func_type.GetFunctionArgumentAtIndex(i)));
304     }
305   }
306   return LLDB_RECORD_RESULT(sb_type_list);
307 }
308 
309 uint32_t SBType::GetNumberOfMemberFunctions() {
310   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBType, GetNumberOfMemberFunctions);
311 
312   if (IsValid()) {
313     return m_opaque_sp->GetCompilerType(true).GetNumMemberFunctions();
314   }
315   return 0;
316 }
317 
318 lldb::SBTypeMemberFunction SBType::GetMemberFunctionAtIndex(uint32_t idx) {
319   LLDB_RECORD_METHOD(lldb::SBTypeMemberFunction, SBType,
320                      GetMemberFunctionAtIndex, (uint32_t), idx);
321 
322   SBTypeMemberFunction sb_func_type;
323   if (IsValid())
324     sb_func_type.reset(new TypeMemberFunctionImpl(
325         m_opaque_sp->GetCompilerType(true).GetMemberFunctionAtIndex(idx)));
326   return LLDB_RECORD_RESULT(sb_func_type);
327 }
328 
329 lldb::SBType SBType::GetUnqualifiedType() {
330   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBType, SBType, GetUnqualifiedType);
331 
332   if (!IsValid())
333     return LLDB_RECORD_RESULT(SBType());
334   return LLDB_RECORD_RESULT(
335       SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetUnqualifiedType()))));
336 }
337 
338 lldb::SBType SBType::GetCanonicalType() {
339   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBType, SBType, GetCanonicalType);
340 
341   if (IsValid())
342     return LLDB_RECORD_RESULT(
343         SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetCanonicalType()))));
344   return LLDB_RECORD_RESULT(SBType());
345 }
346 
347 SBType SBType::GetEnumerationIntegerType() {
348   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBType, SBType, GetEnumerationIntegerType);
349 
350   if (IsValid()) {
351     return LLDB_RECORD_RESULT(
352         SBType(m_opaque_sp->GetCompilerType(true).GetEnumerationIntegerType()));
353   }
354   return LLDB_RECORD_RESULT(SBType());
355 }
356 
357 lldb::BasicType SBType::GetBasicType() {
358   LLDB_RECORD_METHOD_NO_ARGS(lldb::BasicType, SBType, GetBasicType);
359 
360   if (IsValid())
361     return m_opaque_sp->GetCompilerType(false).GetBasicTypeEnumeration();
362   return eBasicTypeInvalid;
363 }
364 
365 SBType SBType::GetBasicType(lldb::BasicType basic_type) {
366   LLDB_RECORD_METHOD(lldb::SBType, SBType, GetBasicType, (lldb::BasicType),
367                      basic_type);
368 
369   if (IsValid() && m_opaque_sp->IsValid())
370     return LLDB_RECORD_RESULT(SBType(
371         m_opaque_sp->GetTypeSystem(false)->GetBasicTypeFromAST(basic_type)));
372   return LLDB_RECORD_RESULT(SBType());
373 }
374 
375 uint32_t SBType::GetNumberOfDirectBaseClasses() {
376   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBType, GetNumberOfDirectBaseClasses);
377 
378   if (IsValid())
379     return m_opaque_sp->GetCompilerType(true).GetNumDirectBaseClasses();
380   return 0;
381 }
382 
383 uint32_t SBType::GetNumberOfVirtualBaseClasses() {
384   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBType, GetNumberOfVirtualBaseClasses);
385 
386   if (IsValid())
387     return m_opaque_sp->GetCompilerType(true).GetNumVirtualBaseClasses();
388   return 0;
389 }
390 
391 uint32_t SBType::GetNumberOfFields() {
392   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBType, GetNumberOfFields);
393 
394   if (IsValid())
395     return m_opaque_sp->GetCompilerType(true).GetNumFields();
396   return 0;
397 }
398 
399 bool SBType::GetDescription(SBStream &description,
400                             lldb::DescriptionLevel description_level) {
401   LLDB_RECORD_METHOD(bool, SBType, GetDescription,
402                      (lldb::SBStream &, lldb::DescriptionLevel), description,
403                      description_level);
404 
405   Stream &strm = description.ref();
406 
407   if (m_opaque_sp) {
408     m_opaque_sp->GetDescription(strm, description_level);
409   } else
410     strm.PutCString("No value");
411 
412   return true;
413 }
414 
415 SBTypeMember SBType::GetDirectBaseClassAtIndex(uint32_t idx) {
416   LLDB_RECORD_METHOD(lldb::SBTypeMember, SBType, GetDirectBaseClassAtIndex,
417                      (uint32_t), idx);
418 
419   SBTypeMember sb_type_member;
420   if (IsValid()) {
421     uint32_t bit_offset = 0;
422     CompilerType base_class_type =
423         m_opaque_sp->GetCompilerType(true).GetDirectBaseClassAtIndex(
424             idx, &bit_offset);
425     if (base_class_type.IsValid())
426       sb_type_member.reset(new TypeMemberImpl(
427           TypeImplSP(new TypeImpl(base_class_type)), bit_offset));
428   }
429   return LLDB_RECORD_RESULT(sb_type_member);
430 }
431 
432 SBTypeMember SBType::GetVirtualBaseClassAtIndex(uint32_t idx) {
433   LLDB_RECORD_METHOD(lldb::SBTypeMember, SBType, GetVirtualBaseClassAtIndex,
434                      (uint32_t), idx);
435 
436   SBTypeMember sb_type_member;
437   if (IsValid()) {
438     uint32_t bit_offset = 0;
439     CompilerType base_class_type =
440         m_opaque_sp->GetCompilerType(true).GetVirtualBaseClassAtIndex(
441             idx, &bit_offset);
442     if (base_class_type.IsValid())
443       sb_type_member.reset(new TypeMemberImpl(
444           TypeImplSP(new TypeImpl(base_class_type)), bit_offset));
445   }
446   return LLDB_RECORD_RESULT(sb_type_member);
447 }
448 
449 SBTypeEnumMemberList SBType::GetEnumMembers() {
450   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBTypeEnumMemberList, SBType,
451                              GetEnumMembers);
452 
453   SBTypeEnumMemberList sb_enum_member_list;
454   if (IsValid()) {
455     CompilerType this_type(m_opaque_sp->GetCompilerType(true));
456     if (this_type.IsValid()) {
457       this_type.ForEachEnumerator([&sb_enum_member_list](
458                                       const CompilerType &integer_type,
459                                       ConstString name,
460                                       const llvm::APSInt &value) -> bool {
461         SBTypeEnumMember enum_member(
462             lldb::TypeEnumMemberImplSP(new TypeEnumMemberImpl(
463                 lldb::TypeImplSP(new TypeImpl(integer_type)), name, value)));
464         sb_enum_member_list.Append(enum_member);
465         return true; // Keep iterating
466       });
467     }
468   }
469   return LLDB_RECORD_RESULT(sb_enum_member_list);
470 }
471 
472 SBTypeMember SBType::GetFieldAtIndex(uint32_t idx) {
473   LLDB_RECORD_METHOD(lldb::SBTypeMember, SBType, GetFieldAtIndex, (uint32_t),
474                      idx);
475 
476   SBTypeMember sb_type_member;
477   if (IsValid()) {
478     CompilerType this_type(m_opaque_sp->GetCompilerType(false));
479     if (this_type.IsValid()) {
480       uint64_t bit_offset = 0;
481       uint32_t bitfield_bit_size = 0;
482       bool is_bitfield = false;
483       std::string name_sstr;
484       CompilerType field_type(this_type.GetFieldAtIndex(
485           idx, name_sstr, &bit_offset, &bitfield_bit_size, &is_bitfield));
486       if (field_type.IsValid()) {
487         ConstString name;
488         if (!name_sstr.empty())
489           name.SetCString(name_sstr.c_str());
490         sb_type_member.reset(
491             new TypeMemberImpl(TypeImplSP(new TypeImpl(field_type)), bit_offset,
492                                name, bitfield_bit_size, is_bitfield));
493       }
494     }
495   }
496   return LLDB_RECORD_RESULT(sb_type_member);
497 }
498 
499 bool SBType::IsTypeComplete() {
500   LLDB_RECORD_METHOD_NO_ARGS(bool, SBType, IsTypeComplete);
501 
502   if (!IsValid())
503     return false;
504   return m_opaque_sp->GetCompilerType(false).IsCompleteType();
505 }
506 
507 uint32_t SBType::GetTypeFlags() {
508   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBType, GetTypeFlags);
509 
510   if (!IsValid())
511     return 0;
512   return m_opaque_sp->GetCompilerType(true).GetTypeInfo();
513 }
514 
515 lldb::SBModule SBType::GetModule() {
516   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBModule, SBType, GetModule);
517 
518   lldb::SBModule sb_module;
519   if (!IsValid())
520     return LLDB_RECORD_RESULT(sb_module);
521 
522   sb_module.SetSP(m_opaque_sp->GetModule());
523   return LLDB_RECORD_RESULT(sb_module);
524 }
525 
526 const char *SBType::GetName() {
527   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBType, GetName);
528 
529   if (!IsValid())
530     return "";
531   return m_opaque_sp->GetName().GetCString();
532 }
533 
534 const char *SBType::GetDisplayTypeName() {
535   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBType, GetDisplayTypeName);
536 
537   if (!IsValid())
538     return "";
539   return m_opaque_sp->GetDisplayTypeName().GetCString();
540 }
541 
542 lldb::TypeClass SBType::GetTypeClass() {
543   LLDB_RECORD_METHOD_NO_ARGS(lldb::TypeClass, SBType, GetTypeClass);
544 
545   if (IsValid())
546     return m_opaque_sp->GetCompilerType(true).GetTypeClass();
547   return lldb::eTypeClassInvalid;
548 }
549 
550 uint32_t SBType::GetNumberOfTemplateArguments() {
551   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBType, GetNumberOfTemplateArguments);
552 
553   if (IsValid())
554     return m_opaque_sp->GetCompilerType(false).GetNumTemplateArguments();
555   return 0;
556 }
557 
558 lldb::SBType SBType::GetTemplateArgumentType(uint32_t idx) {
559   LLDB_RECORD_METHOD(lldb::SBType, SBType, GetTemplateArgumentType, (uint32_t),
560                      idx);
561 
562   if (!IsValid())
563     return LLDB_RECORD_RESULT(SBType());
564 
565   CompilerType type;
566   switch(GetTemplateArgumentKind(idx)) {
567     case eTemplateArgumentKindType:
568       type = m_opaque_sp->GetCompilerType(false).GetTypeTemplateArgument(idx);
569       break;
570     case eTemplateArgumentKindIntegral:
571       type = m_opaque_sp->GetCompilerType(false)
572                  .GetIntegralTemplateArgument(idx)
573                  ->type;
574       break;
575     default:
576       break;
577   }
578   if (type.IsValid())
579     return LLDB_RECORD_RESULT(SBType(type));
580   return LLDB_RECORD_RESULT(SBType());
581 }
582 
583 lldb::TemplateArgumentKind SBType::GetTemplateArgumentKind(uint32_t idx) {
584   LLDB_RECORD_METHOD(lldb::TemplateArgumentKind, SBType,
585                      GetTemplateArgumentKind, (uint32_t), idx);
586 
587   if (IsValid())
588     return m_opaque_sp->GetCompilerType(false).GetTemplateArgumentKind(idx);
589   return eTemplateArgumentKindNull;
590 }
591 
592 SBTypeList::SBTypeList() : m_opaque_up(new TypeListImpl()) {
593   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBTypeList);
594 }
595 
596 SBTypeList::SBTypeList(const SBTypeList &rhs)
597     : m_opaque_up(new TypeListImpl()) {
598   LLDB_RECORD_CONSTRUCTOR(SBTypeList, (const lldb::SBTypeList &), rhs);
599 
600   for (uint32_t i = 0, rhs_size = const_cast<SBTypeList &>(rhs).GetSize();
601        i < rhs_size; i++)
602     Append(const_cast<SBTypeList &>(rhs).GetTypeAtIndex(i));
603 }
604 
605 bool SBTypeList::IsValid() {
606   LLDB_RECORD_METHOD_NO_ARGS(bool, SBTypeList, IsValid);
607   return this->operator bool();
608 }
609 SBTypeList::operator bool() const {
610   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTypeList, operator bool);
611 
612   return (m_opaque_up != nullptr);
613 }
614 
615 SBTypeList &SBTypeList::operator=(const SBTypeList &rhs) {
616   LLDB_RECORD_METHOD(lldb::SBTypeList &,
617                      SBTypeList, operator=,(const lldb::SBTypeList &), rhs);
618 
619   if (this != &rhs) {
620     m_opaque_up = std::make_unique<TypeListImpl>();
621     for (uint32_t i = 0, rhs_size = const_cast<SBTypeList &>(rhs).GetSize();
622          i < rhs_size; i++)
623       Append(const_cast<SBTypeList &>(rhs).GetTypeAtIndex(i));
624   }
625   return LLDB_RECORD_RESULT(*this);
626 }
627 
628 void SBTypeList::Append(SBType type) {
629   LLDB_RECORD_METHOD(void, SBTypeList, Append, (lldb::SBType), type);
630 
631   if (type.IsValid())
632     m_opaque_up->Append(type.m_opaque_sp);
633 }
634 
635 SBType SBTypeList::GetTypeAtIndex(uint32_t index) {
636   LLDB_RECORD_METHOD(lldb::SBType, SBTypeList, GetTypeAtIndex, (uint32_t),
637                      index);
638 
639   if (m_opaque_up)
640     return LLDB_RECORD_RESULT(SBType(m_opaque_up->GetTypeAtIndex(index)));
641   return LLDB_RECORD_RESULT(SBType());
642 }
643 
644 uint32_t SBTypeList::GetSize() {
645   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBTypeList, GetSize);
646 
647   return m_opaque_up->GetSize();
648 }
649 
650 SBTypeList::~SBTypeList() = default;
651 
652 SBTypeMember::SBTypeMember() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBTypeMember); }
653 
654 SBTypeMember::~SBTypeMember() = default;
655 
656 SBTypeMember::SBTypeMember(const SBTypeMember &rhs) {
657   LLDB_RECORD_CONSTRUCTOR(SBTypeMember, (const lldb::SBTypeMember &), rhs);
658 
659   if (this != &rhs) {
660     if (rhs.IsValid())
661       m_opaque_up = std::make_unique<TypeMemberImpl>(rhs.ref());
662   }
663 }
664 
665 lldb::SBTypeMember &SBTypeMember::operator=(const lldb::SBTypeMember &rhs) {
666   LLDB_RECORD_METHOD(lldb::SBTypeMember &,
667                      SBTypeMember, operator=,(const lldb::SBTypeMember &), rhs);
668 
669   if (this != &rhs) {
670     if (rhs.IsValid())
671       m_opaque_up = std::make_unique<TypeMemberImpl>(rhs.ref());
672   }
673   return LLDB_RECORD_RESULT(*this);
674 }
675 
676 bool SBTypeMember::IsValid() const {
677   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTypeMember, IsValid);
678   return this->operator bool();
679 }
680 SBTypeMember::operator bool() const {
681   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTypeMember, operator bool);
682 
683   return m_opaque_up.get();
684 }
685 
686 const char *SBTypeMember::GetName() {
687   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBTypeMember, GetName);
688 
689   if (m_opaque_up)
690     return m_opaque_up->GetName().GetCString();
691   return nullptr;
692 }
693 
694 SBType SBTypeMember::GetType() {
695   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBType, SBTypeMember, GetType);
696 
697   SBType sb_type;
698   if (m_opaque_up) {
699     sb_type.SetSP(m_opaque_up->GetTypeImpl());
700   }
701   return LLDB_RECORD_RESULT(sb_type);
702 }
703 
704 uint64_t SBTypeMember::GetOffsetInBytes() {
705   LLDB_RECORD_METHOD_NO_ARGS(uint64_t, SBTypeMember, GetOffsetInBytes);
706 
707   if (m_opaque_up)
708     return m_opaque_up->GetBitOffset() / 8u;
709   return 0;
710 }
711 
712 uint64_t SBTypeMember::GetOffsetInBits() {
713   LLDB_RECORD_METHOD_NO_ARGS(uint64_t, SBTypeMember, GetOffsetInBits);
714 
715   if (m_opaque_up)
716     return m_opaque_up->GetBitOffset();
717   return 0;
718 }
719 
720 bool SBTypeMember::IsBitfield() {
721   LLDB_RECORD_METHOD_NO_ARGS(bool, SBTypeMember, IsBitfield);
722 
723   if (m_opaque_up)
724     return m_opaque_up->GetIsBitfield();
725   return false;
726 }
727 
728 uint32_t SBTypeMember::GetBitfieldSizeInBits() {
729   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBTypeMember, GetBitfieldSizeInBits);
730 
731   if (m_opaque_up)
732     return m_opaque_up->GetBitfieldBitSize();
733   return 0;
734 }
735 
736 bool SBTypeMember::GetDescription(lldb::SBStream &description,
737                                   lldb::DescriptionLevel description_level) {
738   LLDB_RECORD_METHOD(bool, SBTypeMember, GetDescription,
739                      (lldb::SBStream &, lldb::DescriptionLevel), description,
740                      description_level);
741 
742   Stream &strm = description.ref();
743 
744   if (m_opaque_up) {
745     const uint32_t bit_offset = m_opaque_up->GetBitOffset();
746     const uint32_t byte_offset = bit_offset / 8u;
747     const uint32_t byte_bit_offset = bit_offset % 8u;
748     const char *name = m_opaque_up->GetName().GetCString();
749     if (byte_bit_offset)
750       strm.Printf("+%u + %u bits: (", byte_offset, byte_bit_offset);
751     else
752       strm.Printf("+%u: (", byte_offset);
753 
754     TypeImplSP type_impl_sp(m_opaque_up->GetTypeImpl());
755     if (type_impl_sp)
756       type_impl_sp->GetDescription(strm, description_level);
757 
758     strm.Printf(") %s", name);
759     if (m_opaque_up->GetIsBitfield()) {
760       const uint32_t bitfield_bit_size = m_opaque_up->GetBitfieldBitSize();
761       strm.Printf(" : %u", bitfield_bit_size);
762     }
763   } else {
764     strm.PutCString("No value");
765   }
766   return true;
767 }
768 
769 void SBTypeMember::reset(TypeMemberImpl *type_member_impl) {
770   m_opaque_up.reset(type_member_impl);
771 }
772 
773 TypeMemberImpl &SBTypeMember::ref() {
774   if (m_opaque_up == nullptr)
775     m_opaque_up = std::make_unique<TypeMemberImpl>();
776   return *m_opaque_up;
777 }
778 
779 const TypeMemberImpl &SBTypeMember::ref() const { return *m_opaque_up; }
780 
781 SBTypeMemberFunction::SBTypeMemberFunction() {
782   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBTypeMemberFunction);
783 }
784 
785 SBTypeMemberFunction::~SBTypeMemberFunction() = default;
786 
787 SBTypeMemberFunction::SBTypeMemberFunction(const SBTypeMemberFunction &rhs)
788     : m_opaque_sp(rhs.m_opaque_sp) {
789   LLDB_RECORD_CONSTRUCTOR(SBTypeMemberFunction,
790                           (const lldb::SBTypeMemberFunction &), rhs);
791 }
792 
793 lldb::SBTypeMemberFunction &SBTypeMemberFunction::
794 operator=(const lldb::SBTypeMemberFunction &rhs) {
795   LLDB_RECORD_METHOD(
796       lldb::SBTypeMemberFunction &,
797       SBTypeMemberFunction, operator=,(const lldb::SBTypeMemberFunction &),
798       rhs);
799 
800   if (this != &rhs)
801     m_opaque_sp = rhs.m_opaque_sp;
802   return LLDB_RECORD_RESULT(*this);
803 }
804 
805 bool SBTypeMemberFunction::IsValid() const {
806   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTypeMemberFunction, IsValid);
807   return this->operator bool();
808 }
809 SBTypeMemberFunction::operator bool() const {
810   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTypeMemberFunction, operator bool);
811 
812   return m_opaque_sp.get();
813 }
814 
815 const char *SBTypeMemberFunction::GetName() {
816   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBTypeMemberFunction, GetName);
817 
818   if (m_opaque_sp)
819     return m_opaque_sp->GetName().GetCString();
820   return nullptr;
821 }
822 
823 const char *SBTypeMemberFunction::GetDemangledName() {
824   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBTypeMemberFunction,
825                              GetDemangledName);
826 
827   if (m_opaque_sp) {
828     ConstString mangled_str = m_opaque_sp->GetMangledName();
829     if (mangled_str) {
830       Mangled mangled(mangled_str);
831       return mangled.GetDemangledName().GetCString();
832     }
833   }
834   return nullptr;
835 }
836 
837 const char *SBTypeMemberFunction::GetMangledName() {
838   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBTypeMemberFunction,
839                              GetMangledName);
840 
841   if (m_opaque_sp)
842     return m_opaque_sp->GetMangledName().GetCString();
843   return nullptr;
844 }
845 
846 SBType SBTypeMemberFunction::GetType() {
847   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBType, SBTypeMemberFunction, GetType);
848 
849   SBType sb_type;
850   if (m_opaque_sp) {
851     sb_type.SetSP(lldb::TypeImplSP(new TypeImpl(m_opaque_sp->GetType())));
852   }
853   return LLDB_RECORD_RESULT(sb_type);
854 }
855 
856 lldb::SBType SBTypeMemberFunction::GetReturnType() {
857   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBType, SBTypeMemberFunction, GetReturnType);
858 
859   SBType sb_type;
860   if (m_opaque_sp) {
861     sb_type.SetSP(lldb::TypeImplSP(new TypeImpl(m_opaque_sp->GetReturnType())));
862   }
863   return LLDB_RECORD_RESULT(sb_type);
864 }
865 
866 uint32_t SBTypeMemberFunction::GetNumberOfArguments() {
867   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBTypeMemberFunction,
868                              GetNumberOfArguments);
869 
870   if (m_opaque_sp)
871     return m_opaque_sp->GetNumArguments();
872   return 0;
873 }
874 
875 lldb::SBType SBTypeMemberFunction::GetArgumentTypeAtIndex(uint32_t i) {
876   LLDB_RECORD_METHOD(lldb::SBType, SBTypeMemberFunction, GetArgumentTypeAtIndex,
877                      (uint32_t), i);
878 
879   SBType sb_type;
880   if (m_opaque_sp) {
881     sb_type.SetSP(
882         lldb::TypeImplSP(new TypeImpl(m_opaque_sp->GetArgumentAtIndex(i))));
883   }
884   return LLDB_RECORD_RESULT(sb_type);
885 }
886 
887 lldb::MemberFunctionKind SBTypeMemberFunction::GetKind() {
888   LLDB_RECORD_METHOD_NO_ARGS(lldb::MemberFunctionKind, SBTypeMemberFunction,
889                              GetKind);
890 
891   if (m_opaque_sp)
892     return m_opaque_sp->GetKind();
893   return lldb::eMemberFunctionKindUnknown;
894 }
895 
896 bool SBTypeMemberFunction::GetDescription(
897     lldb::SBStream &description, lldb::DescriptionLevel description_level) {
898   LLDB_RECORD_METHOD(bool, SBTypeMemberFunction, GetDescription,
899                      (lldb::SBStream &, lldb::DescriptionLevel), description,
900                      description_level);
901 
902   Stream &strm = description.ref();
903 
904   if (m_opaque_sp)
905     return m_opaque_sp->GetDescription(strm);
906 
907   return false;
908 }
909 
910 void SBTypeMemberFunction::reset(TypeMemberFunctionImpl *type_member_impl) {
911   m_opaque_sp.reset(type_member_impl);
912 }
913 
914 TypeMemberFunctionImpl &SBTypeMemberFunction::ref() {
915   if (!m_opaque_sp)
916     m_opaque_sp = std::make_shared<TypeMemberFunctionImpl>();
917   return *m_opaque_sp.get();
918 }
919 
920 const TypeMemberFunctionImpl &SBTypeMemberFunction::ref() const {
921   return *m_opaque_sp.get();
922 }
923 
924 namespace lldb_private {
925 namespace repro {
926 
927 template <>
928 void RegisterMethods<SBType>(Registry &R) {
929   LLDB_REGISTER_CONSTRUCTOR(SBType, ());
930   LLDB_REGISTER_CONSTRUCTOR(SBType, (const lldb::SBType &));
931   LLDB_REGISTER_METHOD(bool, SBType, operator==,(lldb::SBType &));
932   LLDB_REGISTER_METHOD(bool, SBType, operator!=,(lldb::SBType &));
933   LLDB_REGISTER_METHOD(lldb::SBType &,
934                        SBType, operator=,(const lldb::SBType &));
935   LLDB_REGISTER_METHOD_CONST(bool, SBType, IsValid, ());
936   LLDB_REGISTER_METHOD_CONST(bool, SBType, operator bool, ());
937   LLDB_REGISTER_METHOD(uint64_t, SBType, GetByteSize, ());
938   LLDB_REGISTER_METHOD(bool, SBType, IsPointerType, ());
939   LLDB_REGISTER_METHOD(bool, SBType, IsArrayType, ());
940   LLDB_REGISTER_METHOD(bool, SBType, IsVectorType, ());
941   LLDB_REGISTER_METHOD(bool, SBType, IsReferenceType, ());
942   LLDB_REGISTER_METHOD(lldb::SBType, SBType, GetPointerType, ());
943   LLDB_REGISTER_METHOD(lldb::SBType, SBType, GetPointeeType, ());
944   LLDB_REGISTER_METHOD(lldb::SBType, SBType, GetReferenceType, ());
945   LLDB_REGISTER_METHOD(lldb::SBType, SBType, GetTypedefedType, ());
946   LLDB_REGISTER_METHOD(lldb::SBType, SBType, GetDereferencedType, ());
947   LLDB_REGISTER_METHOD(lldb::SBType, SBType, GetArrayElementType, ());
948   LLDB_REGISTER_METHOD(lldb::SBType, SBType, GetArrayType, (uint64_t));
949   LLDB_REGISTER_METHOD(lldb::SBType, SBType, GetVectorElementType, ());
950   LLDB_REGISTER_METHOD(bool, SBType, IsFunctionType, ());
951   LLDB_REGISTER_METHOD(bool, SBType, IsPolymorphicClass, ());
952   LLDB_REGISTER_METHOD(bool, SBType, IsTypedefType, ());
953   LLDB_REGISTER_METHOD(bool, SBType, IsAnonymousType, ());
954   LLDB_REGISTER_METHOD(bool, SBType, IsScopedEnumerationType, ());
955   LLDB_REGISTER_METHOD(lldb::SBType, SBType, GetFunctionReturnType, ());
956   LLDB_REGISTER_METHOD(lldb::SBTypeList, SBType, GetFunctionArgumentTypes,
957                        ());
958   LLDB_REGISTER_METHOD(uint32_t, SBType, GetNumberOfMemberFunctions, ());
959   LLDB_REGISTER_METHOD(lldb::SBTypeMemberFunction, SBType,
960                        GetMemberFunctionAtIndex, (uint32_t));
961   LLDB_REGISTER_METHOD(lldb::SBType, SBType, GetUnqualifiedType, ());
962   LLDB_REGISTER_METHOD(lldb::SBType, SBType, GetCanonicalType, ());
963   LLDB_REGISTER_METHOD(lldb::SBType, SBType, GetEnumerationIntegerType, ());
964   LLDB_REGISTER_METHOD(lldb::BasicType, SBType, GetBasicType, ());
965   LLDB_REGISTER_METHOD(lldb::SBType, SBType, GetBasicType, (lldb::BasicType));
966   LLDB_REGISTER_METHOD(uint32_t, SBType, GetNumberOfDirectBaseClasses, ());
967   LLDB_REGISTER_METHOD(uint32_t, SBType, GetNumberOfVirtualBaseClasses, ());
968   LLDB_REGISTER_METHOD(uint32_t, SBType, GetNumberOfFields, ());
969   LLDB_REGISTER_METHOD(bool, SBType, GetDescription,
970                        (lldb::SBStream &, lldb::DescriptionLevel));
971   LLDB_REGISTER_METHOD(lldb::SBTypeMember, SBType, GetDirectBaseClassAtIndex,
972                        (uint32_t));
973   LLDB_REGISTER_METHOD(lldb::SBTypeMember, SBType, GetVirtualBaseClassAtIndex,
974                        (uint32_t));
975   LLDB_REGISTER_METHOD(lldb::SBTypeEnumMemberList, SBType, GetEnumMembers,
976                        ());
977   LLDB_REGISTER_METHOD(lldb::SBTypeMember, SBType, GetFieldAtIndex,
978                        (uint32_t));
979   LLDB_REGISTER_METHOD(bool, SBType, IsTypeComplete, ());
980   LLDB_REGISTER_METHOD(uint32_t, SBType, GetTypeFlags, ());
981   LLDB_REGISTER_METHOD(lldb::SBModule, SBType, GetModule, ());
982   LLDB_REGISTER_METHOD(const char *, SBType, GetName, ());
983   LLDB_REGISTER_METHOD(const char *, SBType, GetDisplayTypeName, ());
984   LLDB_REGISTER_METHOD(lldb::TypeClass, SBType, GetTypeClass, ());
985   LLDB_REGISTER_METHOD(uint32_t, SBType, GetNumberOfTemplateArguments, ());
986   LLDB_REGISTER_METHOD(lldb::SBType, SBType, GetTemplateArgumentType,
987                        (uint32_t));
988   LLDB_REGISTER_METHOD(lldb::TemplateArgumentKind, SBType,
989                        GetTemplateArgumentKind, (uint32_t));
990   LLDB_REGISTER_CONSTRUCTOR(SBTypeList, ());
991   LLDB_REGISTER_CONSTRUCTOR(SBTypeList, (const lldb::SBTypeList &));
992   LLDB_REGISTER_METHOD(bool, SBTypeList, IsValid, ());
993   LLDB_REGISTER_METHOD_CONST(bool, SBTypeList, operator bool, ());
994   LLDB_REGISTER_METHOD(lldb::SBTypeList &,
995                        SBTypeList, operator=,(const lldb::SBTypeList &));
996   LLDB_REGISTER_METHOD(void, SBTypeList, Append, (lldb::SBType));
997   LLDB_REGISTER_METHOD(lldb::SBType, SBTypeList, GetTypeAtIndex, (uint32_t));
998   LLDB_REGISTER_METHOD(uint32_t, SBTypeList, GetSize, ());
999   LLDB_REGISTER_CONSTRUCTOR(SBTypeMember, ());
1000   LLDB_REGISTER_CONSTRUCTOR(SBTypeMember, (const lldb::SBTypeMember &));
1001   LLDB_REGISTER_METHOD(lldb::SBTypeMember &,
1002                        SBTypeMember, operator=,(const lldb::SBTypeMember &));
1003   LLDB_REGISTER_METHOD_CONST(bool, SBTypeMember, IsValid, ());
1004   LLDB_REGISTER_METHOD_CONST(bool, SBTypeMember, operator bool, ());
1005   LLDB_REGISTER_METHOD(const char *, SBTypeMember, GetName, ());
1006   LLDB_REGISTER_METHOD(lldb::SBType, SBTypeMember, GetType, ());
1007   LLDB_REGISTER_METHOD(uint64_t, SBTypeMember, GetOffsetInBytes, ());
1008   LLDB_REGISTER_METHOD(uint64_t, SBTypeMember, GetOffsetInBits, ());
1009   LLDB_REGISTER_METHOD(bool, SBTypeMember, IsBitfield, ());
1010   LLDB_REGISTER_METHOD(uint32_t, SBTypeMember, GetBitfieldSizeInBits, ());
1011   LLDB_REGISTER_METHOD(bool, SBTypeMember, GetDescription,
1012                        (lldb::SBStream &, lldb::DescriptionLevel));
1013   LLDB_REGISTER_CONSTRUCTOR(SBTypeMemberFunction, ());
1014   LLDB_REGISTER_CONSTRUCTOR(SBTypeMemberFunction,
1015                             (const lldb::SBTypeMemberFunction &));
1016   LLDB_REGISTER_METHOD(
1017       lldb::SBTypeMemberFunction &,
1018       SBTypeMemberFunction, operator=,(const lldb::SBTypeMemberFunction &));
1019   LLDB_REGISTER_METHOD_CONST(bool, SBTypeMemberFunction, IsValid, ());
1020   LLDB_REGISTER_METHOD_CONST(bool, SBTypeMemberFunction, operator bool, ());
1021   LLDB_REGISTER_METHOD(const char *, SBTypeMemberFunction, GetName, ());
1022   LLDB_REGISTER_METHOD(const char *, SBTypeMemberFunction, GetDemangledName,
1023                        ());
1024   LLDB_REGISTER_METHOD(const char *, SBTypeMemberFunction, GetMangledName,
1025                        ());
1026   LLDB_REGISTER_METHOD(lldb::SBType, SBTypeMemberFunction, GetType, ());
1027   LLDB_REGISTER_METHOD(lldb::SBType, SBTypeMemberFunction, GetReturnType, ());
1028   LLDB_REGISTER_METHOD(uint32_t, SBTypeMemberFunction, GetNumberOfArguments,
1029                        ());
1030   LLDB_REGISTER_METHOD(lldb::SBType, SBTypeMemberFunction,
1031                        GetArgumentTypeAtIndex, (uint32_t));
1032   LLDB_REGISTER_METHOD(lldb::MemberFunctionKind, SBTypeMemberFunction,
1033                        GetKind, ());
1034   LLDB_REGISTER_METHOD(bool, SBTypeMemberFunction, GetDescription,
1035                        (lldb::SBStream &, lldb::DescriptionLevel));
1036 }
1037 
1038 }
1039 }
1040