]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/API/SBType.cpp
Vendor import of lldb trunk r351319 (just before the release_80 branch
[FreeBSD/FreeBSD.git] / source / API / SBType.cpp
1 //===-- SBType.cpp ----------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/API/SBType.h"
11 #include "lldb/API/SBDefines.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/API/SBTypeEnumMember.h"
14 #include "lldb/Core/Mangled.h"
15 #include "lldb/Symbol/CompilerType.h"
16 #include "lldb/Symbol/Type.h"
17 #include "lldb/Symbol/TypeSystem.h"
18 #include "lldb/Utility/ConstString.h"
19 #include "lldb/Utility/Log.h"
20 #include "lldb/Utility/Stream.h"
21
22 #include "llvm/ADT/APSInt.h"
23
24 using namespace lldb;
25 using namespace lldb_private;
26
27 SBType::SBType() : m_opaque_sp() {}
28
29 SBType::SBType(const CompilerType &type)
30     : m_opaque_sp(new TypeImpl(
31           CompilerType(type.GetTypeSystem(), type.GetOpaqueQualType()))) {}
32
33 SBType::SBType(const lldb::TypeSP &type_sp)
34     : m_opaque_sp(new TypeImpl(type_sp)) {}
35
36 SBType::SBType(const lldb::TypeImplSP &type_impl_sp)
37     : m_opaque_sp(type_impl_sp) {}
38
39 SBType::SBType(const SBType &rhs) : m_opaque_sp() {
40   if (this != &rhs) {
41     m_opaque_sp = rhs.m_opaque_sp;
42   }
43 }
44
45 // SBType::SBType (TypeImpl* impl) :
46 //    m_opaque_ap(impl)
47 //{}
48 //
49 bool SBType::operator==(SBType &rhs) {
50   if (!IsValid())
51     return !rhs.IsValid();
52
53   if (!rhs.IsValid())
54     return false;
55
56   return *m_opaque_sp.get() == *rhs.m_opaque_sp.get();
57 }
58
59 bool SBType::operator!=(SBType &rhs) {
60   if (!IsValid())
61     return rhs.IsValid();
62
63   if (!rhs.IsValid())
64     return true;
65
66   return *m_opaque_sp.get() != *rhs.m_opaque_sp.get();
67 }
68
69 lldb::TypeImplSP SBType::GetSP() { return m_opaque_sp; }
70
71 void SBType::SetSP(const lldb::TypeImplSP &type_impl_sp) {
72   m_opaque_sp = type_impl_sp;
73 }
74
75 SBType &SBType::operator=(const SBType &rhs) {
76   if (this != &rhs) {
77     m_opaque_sp = rhs.m_opaque_sp;
78   }
79   return *this;
80 }
81
82 SBType::~SBType() {}
83
84 TypeImpl &SBType::ref() {
85   if (m_opaque_sp.get() == NULL)
86     m_opaque_sp.reset(new TypeImpl());
87   return *m_opaque_sp;
88 }
89
90 const TypeImpl &SBType::ref() const {
91   // "const SBAddress &addr" should already have checked "addr.IsValid()" prior
92   // to calling this function. In case you didn't we will assert and die to let
93   // you know.
94   assert(m_opaque_sp.get());
95   return *m_opaque_sp;
96 }
97
98 bool SBType::IsValid() const {
99   if (m_opaque_sp.get() == NULL)
100     return false;
101
102   return m_opaque_sp->IsValid();
103 }
104
105 uint64_t SBType::GetByteSize() {
106   if (IsValid())
107     if (llvm::Optional<uint64_t> size =
108             m_opaque_sp->GetCompilerType(false).GetByteSize(nullptr))
109       return *size;
110   return 0;
111 }
112
113 bool SBType::IsPointerType() {
114   if (!IsValid())
115     return false;
116   return m_opaque_sp->GetCompilerType(true).IsPointerType();
117 }
118
119 bool SBType::IsArrayType() {
120   if (!IsValid())
121     return false;
122   return m_opaque_sp->GetCompilerType(true).IsArrayType(nullptr, nullptr,
123                                                         nullptr);
124 }
125
126 bool SBType::IsVectorType() {
127   if (!IsValid())
128     return false;
129   return m_opaque_sp->GetCompilerType(true).IsVectorType(nullptr, nullptr);
130 }
131
132 bool SBType::IsReferenceType() {
133   if (!IsValid())
134     return false;
135   return m_opaque_sp->GetCompilerType(true).IsReferenceType();
136 }
137
138 SBType SBType::GetPointerType() {
139   if (!IsValid())
140     return SBType();
141
142   return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetPointerType())));
143 }
144
145 SBType SBType::GetPointeeType() {
146   if (!IsValid())
147     return SBType();
148   return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetPointeeType())));
149 }
150
151 SBType SBType::GetReferenceType() {
152   if (!IsValid())
153     return SBType();
154   return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetReferenceType())));
155 }
156
157 SBType SBType::GetTypedefedType() {
158   if (!IsValid())
159     return SBType();
160   return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetTypedefedType())));
161 }
162
163 SBType SBType::GetDereferencedType() {
164   if (!IsValid())
165     return SBType();
166   return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetDereferencedType())));
167 }
168
169 SBType SBType::GetArrayElementType() {
170   if (!IsValid())
171     return SBType();
172   return SBType(TypeImplSP(
173       new TypeImpl(m_opaque_sp->GetCompilerType(true).GetArrayElementType())));
174 }
175
176 SBType SBType::GetArrayType(uint64_t size) {
177   if (!IsValid())
178     return SBType();
179   return SBType(TypeImplSP(
180       new TypeImpl(m_opaque_sp->GetCompilerType(true).GetArrayType(size))));
181 }
182
183 SBType SBType::GetVectorElementType() {
184   SBType type_sb;
185   if (IsValid()) {
186     CompilerType vector_element_type;
187     if (m_opaque_sp->GetCompilerType(true).IsVectorType(&vector_element_type,
188                                                         nullptr))
189       type_sb.SetSP(TypeImplSP(new TypeImpl(vector_element_type)));
190   }
191   return type_sb;
192 }
193
194 bool SBType::IsFunctionType() {
195   if (!IsValid())
196     return false;
197   return m_opaque_sp->GetCompilerType(true).IsFunctionType();
198 }
199
200 bool SBType::IsPolymorphicClass() {
201   if (!IsValid())
202     return false;
203   return m_opaque_sp->GetCompilerType(true).IsPolymorphicClass();
204 }
205
206 bool SBType::IsTypedefType() {
207   if (!IsValid())
208     return false;
209   return m_opaque_sp->GetCompilerType(true).IsTypedefType();
210 }
211
212 bool SBType::IsAnonymousType() {
213   if (!IsValid())
214     return false;
215   return m_opaque_sp->GetCompilerType(true).IsAnonymousType();
216 }
217
218 lldb::SBType SBType::GetFunctionReturnType() {
219   if (IsValid()) {
220     CompilerType return_type(
221         m_opaque_sp->GetCompilerType(true).GetFunctionReturnType());
222     if (return_type.IsValid())
223       return SBType(return_type);
224   }
225   return lldb::SBType();
226 }
227
228 lldb::SBTypeList SBType::GetFunctionArgumentTypes() {
229   SBTypeList sb_type_list;
230   if (IsValid()) {
231     CompilerType func_type(m_opaque_sp->GetCompilerType(true));
232     size_t count = func_type.GetNumberOfFunctionArguments();
233     for (size_t i = 0; i < count; i++) {
234       sb_type_list.Append(SBType(func_type.GetFunctionArgumentAtIndex(i)));
235     }
236   }
237   return sb_type_list;
238 }
239
240 uint32_t SBType::GetNumberOfMemberFunctions() {
241   if (IsValid()) {
242     return m_opaque_sp->GetCompilerType(true).GetNumMemberFunctions();
243   }
244   return 0;
245 }
246
247 lldb::SBTypeMemberFunction SBType::GetMemberFunctionAtIndex(uint32_t idx) {
248   SBTypeMemberFunction sb_func_type;
249   if (IsValid())
250     sb_func_type.reset(new TypeMemberFunctionImpl(
251         m_opaque_sp->GetCompilerType(true).GetMemberFunctionAtIndex(idx)));
252   return sb_func_type;
253 }
254
255 lldb::SBType SBType::GetUnqualifiedType() {
256   if (!IsValid())
257     return SBType();
258   return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetUnqualifiedType())));
259 }
260
261 lldb::SBType SBType::GetCanonicalType() {
262   if (IsValid())
263     return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetCanonicalType())));
264   return SBType();
265 }
266
267 lldb::BasicType SBType::GetBasicType() {
268   if (IsValid())
269     return m_opaque_sp->GetCompilerType(false).GetBasicTypeEnumeration();
270   return eBasicTypeInvalid;
271 }
272
273 SBType SBType::GetBasicType(lldb::BasicType basic_type) {
274   if (IsValid() && m_opaque_sp->IsValid())
275     return SBType(
276         m_opaque_sp->GetTypeSystem(false)->GetBasicTypeFromAST(basic_type));
277   return SBType();
278 }
279
280 uint32_t SBType::GetNumberOfDirectBaseClasses() {
281   if (IsValid())
282     return m_opaque_sp->GetCompilerType(true).GetNumDirectBaseClasses();
283   return 0;
284 }
285
286 uint32_t SBType::GetNumberOfVirtualBaseClasses() {
287   if (IsValid())
288     return m_opaque_sp->GetCompilerType(true).GetNumVirtualBaseClasses();
289   return 0;
290 }
291
292 uint32_t SBType::GetNumberOfFields() {
293   if (IsValid())
294     return m_opaque_sp->GetCompilerType(true).GetNumFields();
295   return 0;
296 }
297
298 bool SBType::GetDescription(SBStream &description,
299                             lldb::DescriptionLevel description_level) {
300   Stream &strm = description.ref();
301
302   if (m_opaque_sp) {
303     m_opaque_sp->GetDescription(strm, description_level);
304   } else
305     strm.PutCString("No value");
306
307   return true;
308 }
309
310 SBTypeMember SBType::GetDirectBaseClassAtIndex(uint32_t idx) {
311   SBTypeMember sb_type_member;
312   if (IsValid()) {
313     uint32_t bit_offset = 0;
314     CompilerType base_class_type =
315         m_opaque_sp->GetCompilerType(true).GetDirectBaseClassAtIndex(
316             idx, &bit_offset);
317     if (base_class_type.IsValid())
318       sb_type_member.reset(new TypeMemberImpl(
319           TypeImplSP(new TypeImpl(base_class_type)), bit_offset));
320   }
321   return sb_type_member;
322 }
323
324 SBTypeMember SBType::GetVirtualBaseClassAtIndex(uint32_t idx) {
325   SBTypeMember sb_type_member;
326   if (IsValid()) {
327     uint32_t bit_offset = 0;
328     CompilerType base_class_type =
329         m_opaque_sp->GetCompilerType(true).GetVirtualBaseClassAtIndex(
330             idx, &bit_offset);
331     if (base_class_type.IsValid())
332       sb_type_member.reset(new TypeMemberImpl(
333           TypeImplSP(new TypeImpl(base_class_type)), bit_offset));
334   }
335   return sb_type_member;
336 }
337
338 SBTypeEnumMemberList SBType::GetEnumMembers() {
339   SBTypeEnumMemberList sb_enum_member_list;
340   if (IsValid()) {
341     CompilerType this_type(m_opaque_sp->GetCompilerType(true));
342     if (this_type.IsValid()) {
343       this_type.ForEachEnumerator([&sb_enum_member_list](
344                                       const CompilerType &integer_type,
345                                       const ConstString &name,
346                                       const llvm::APSInt &value) -> bool {
347         SBTypeEnumMember enum_member(
348             lldb::TypeEnumMemberImplSP(new TypeEnumMemberImpl(
349                 lldb::TypeImplSP(new TypeImpl(integer_type)), name, value)));
350         sb_enum_member_list.Append(enum_member);
351         return true; // Keep iterating
352       });
353     }
354   }
355   return sb_enum_member_list;
356 }
357
358 SBTypeMember SBType::GetFieldAtIndex(uint32_t idx) {
359   SBTypeMember sb_type_member;
360   if (IsValid()) {
361     CompilerType this_type(m_opaque_sp->GetCompilerType(false));
362     if (this_type.IsValid()) {
363       uint64_t bit_offset = 0;
364       uint32_t bitfield_bit_size = 0;
365       bool is_bitfield = false;
366       std::string name_sstr;
367       CompilerType field_type(this_type.GetFieldAtIndex(
368           idx, name_sstr, &bit_offset, &bitfield_bit_size, &is_bitfield));
369       if (field_type.IsValid()) {
370         ConstString name;
371         if (!name_sstr.empty())
372           name.SetCString(name_sstr.c_str());
373         sb_type_member.reset(
374             new TypeMemberImpl(TypeImplSP(new TypeImpl(field_type)), bit_offset,
375                                name, bitfield_bit_size, is_bitfield));
376       }
377     }
378   }
379   return sb_type_member;
380 }
381
382 bool SBType::IsTypeComplete() {
383   if (!IsValid())
384     return false;
385   return m_opaque_sp->GetCompilerType(false).IsCompleteType();
386 }
387
388 uint32_t SBType::GetTypeFlags() {
389   if (!IsValid())
390     return 0;
391   return m_opaque_sp->GetCompilerType(true).GetTypeInfo();
392 }
393
394 const char *SBType::GetName() {
395   if (!IsValid())
396     return "";
397   return m_opaque_sp->GetName().GetCString();
398 }
399
400 const char *SBType::GetDisplayTypeName() {
401   if (!IsValid())
402     return "";
403   return m_opaque_sp->GetDisplayTypeName().GetCString();
404 }
405
406 lldb::TypeClass SBType::GetTypeClass() {
407   if (IsValid())
408     return m_opaque_sp->GetCompilerType(true).GetTypeClass();
409   return lldb::eTypeClassInvalid;
410 }
411
412 uint32_t SBType::GetNumberOfTemplateArguments() {
413   if (IsValid())
414     return m_opaque_sp->GetCompilerType(false).GetNumTemplateArguments();
415   return 0;
416 }
417
418 lldb::SBType SBType::GetTemplateArgumentType(uint32_t idx) {
419   if (!IsValid())
420     return SBType();
421
422   CompilerType type;
423   switch(GetTemplateArgumentKind(idx)) {
424     case eTemplateArgumentKindType:
425       type = m_opaque_sp->GetCompilerType(false).GetTypeTemplateArgument(idx);
426       break;
427     case eTemplateArgumentKindIntegral:
428       type = m_opaque_sp->GetCompilerType(false)
429                  .GetIntegralTemplateArgument(idx)
430                  ->type;
431       break;
432     default:
433       break;
434   }
435   if (type.IsValid())
436     return SBType(type);
437   return SBType();
438 }
439
440 lldb::TemplateArgumentKind SBType::GetTemplateArgumentKind(uint32_t idx) {
441   if (IsValid())
442     return m_opaque_sp->GetCompilerType(false).GetTemplateArgumentKind(idx);
443   return eTemplateArgumentKindNull;
444 }
445
446 SBTypeList::SBTypeList() : m_opaque_ap(new TypeListImpl()) {}
447
448 SBTypeList::SBTypeList(const SBTypeList &rhs)
449     : m_opaque_ap(new TypeListImpl()) {
450   for (uint32_t i = 0, rhs_size = const_cast<SBTypeList &>(rhs).GetSize();
451        i < rhs_size; i++)
452     Append(const_cast<SBTypeList &>(rhs).GetTypeAtIndex(i));
453 }
454
455 bool SBTypeList::IsValid() { return (m_opaque_ap != NULL); }
456
457 SBTypeList &SBTypeList::operator=(const SBTypeList &rhs) {
458   if (this != &rhs) {
459     m_opaque_ap.reset(new TypeListImpl());
460     for (uint32_t i = 0, rhs_size = const_cast<SBTypeList &>(rhs).GetSize();
461          i < rhs_size; i++)
462       Append(const_cast<SBTypeList &>(rhs).GetTypeAtIndex(i));
463   }
464   return *this;
465 }
466
467 void SBTypeList::Append(SBType type) {
468   if (type.IsValid())
469     m_opaque_ap->Append(type.m_opaque_sp);
470 }
471
472 SBType SBTypeList::GetTypeAtIndex(uint32_t index) {
473   if (m_opaque_ap)
474     return SBType(m_opaque_ap->GetTypeAtIndex(index));
475   return SBType();
476 }
477
478 uint32_t SBTypeList::GetSize() { return m_opaque_ap->GetSize(); }
479
480 SBTypeList::~SBTypeList() {}
481
482 SBTypeMember::SBTypeMember() : m_opaque_ap() {}
483
484 SBTypeMember::~SBTypeMember() {}
485
486 SBTypeMember::SBTypeMember(const SBTypeMember &rhs) : m_opaque_ap() {
487   if (this != &rhs) {
488     if (rhs.IsValid())
489       m_opaque_ap.reset(new TypeMemberImpl(rhs.ref()));
490   }
491 }
492
493 lldb::SBTypeMember &SBTypeMember::operator=(const lldb::SBTypeMember &rhs) {
494   if (this != &rhs) {
495     if (rhs.IsValid())
496       m_opaque_ap.reset(new TypeMemberImpl(rhs.ref()));
497   }
498   return *this;
499 }
500
501 bool SBTypeMember::IsValid() const { return m_opaque_ap.get(); }
502
503 const char *SBTypeMember::GetName() {
504   if (m_opaque_ap)
505     return m_opaque_ap->GetName().GetCString();
506   return NULL;
507 }
508
509 SBType SBTypeMember::GetType() {
510   SBType sb_type;
511   if (m_opaque_ap) {
512     sb_type.SetSP(m_opaque_ap->GetTypeImpl());
513   }
514   return sb_type;
515 }
516
517 uint64_t SBTypeMember::GetOffsetInBytes() {
518   if (m_opaque_ap)
519     return m_opaque_ap->GetBitOffset() / 8u;
520   return 0;
521 }
522
523 uint64_t SBTypeMember::GetOffsetInBits() {
524   if (m_opaque_ap)
525     return m_opaque_ap->GetBitOffset();
526   return 0;
527 }
528
529 bool SBTypeMember::IsBitfield() {
530   if (m_opaque_ap)
531     return m_opaque_ap->GetIsBitfield();
532   return false;
533 }
534
535 uint32_t SBTypeMember::GetBitfieldSizeInBits() {
536   if (m_opaque_ap)
537     return m_opaque_ap->GetBitfieldBitSize();
538   return 0;
539 }
540
541 bool SBTypeMember::GetDescription(lldb::SBStream &description,
542                                   lldb::DescriptionLevel description_level) {
543   Stream &strm = description.ref();
544
545   if (m_opaque_ap) {
546     const uint32_t bit_offset = m_opaque_ap->GetBitOffset();
547     const uint32_t byte_offset = bit_offset / 8u;
548     const uint32_t byte_bit_offset = bit_offset % 8u;
549     const char *name = m_opaque_ap->GetName().GetCString();
550     if (byte_bit_offset)
551       strm.Printf("+%u + %u bits: (", byte_offset, byte_bit_offset);
552     else
553       strm.Printf("+%u: (", byte_offset);
554
555     TypeImplSP type_impl_sp(m_opaque_ap->GetTypeImpl());
556     if (type_impl_sp)
557       type_impl_sp->GetDescription(strm, description_level);
558
559     strm.Printf(") %s", name);
560     if (m_opaque_ap->GetIsBitfield()) {
561       const uint32_t bitfield_bit_size = m_opaque_ap->GetBitfieldBitSize();
562       strm.Printf(" : %u", bitfield_bit_size);
563     }
564   } else {
565     strm.PutCString("No value");
566   }
567   return true;
568 }
569
570 void SBTypeMember::reset(TypeMemberImpl *type_member_impl) {
571   m_opaque_ap.reset(type_member_impl);
572 }
573
574 TypeMemberImpl &SBTypeMember::ref() {
575   if (m_opaque_ap == NULL)
576     m_opaque_ap.reset(new TypeMemberImpl());
577   return *m_opaque_ap;
578 }
579
580 const TypeMemberImpl &SBTypeMember::ref() const { return *m_opaque_ap; }
581
582 SBTypeMemberFunction::SBTypeMemberFunction() : m_opaque_sp() {}
583
584 SBTypeMemberFunction::~SBTypeMemberFunction() {}
585
586 SBTypeMemberFunction::SBTypeMemberFunction(const SBTypeMemberFunction &rhs)
587     : m_opaque_sp(rhs.m_opaque_sp) {}
588
589 lldb::SBTypeMemberFunction &SBTypeMemberFunction::
590 operator=(const lldb::SBTypeMemberFunction &rhs) {
591   if (this != &rhs)
592     m_opaque_sp = rhs.m_opaque_sp;
593   return *this;
594 }
595
596 bool SBTypeMemberFunction::IsValid() const { return m_opaque_sp.get(); }
597
598 const char *SBTypeMemberFunction::GetName() {
599   if (m_opaque_sp)
600     return m_opaque_sp->GetName().GetCString();
601   return NULL;
602 }
603
604 const char *SBTypeMemberFunction::GetDemangledName() {
605   if (m_opaque_sp) {
606     ConstString mangled_str = m_opaque_sp->GetMangledName();
607     if (mangled_str) {
608       Mangled mangled(mangled_str, true);
609       return mangled.GetDemangledName(mangled.GuessLanguage()).GetCString();
610     }
611   }
612   return NULL;
613 }
614
615 const char *SBTypeMemberFunction::GetMangledName() {
616   if (m_opaque_sp)
617     return m_opaque_sp->GetMangledName().GetCString();
618   return NULL;
619 }
620
621 SBType SBTypeMemberFunction::GetType() {
622   SBType sb_type;
623   if (m_opaque_sp) {
624     sb_type.SetSP(lldb::TypeImplSP(new TypeImpl(m_opaque_sp->GetType())));
625   }
626   return sb_type;
627 }
628
629 lldb::SBType SBTypeMemberFunction::GetReturnType() {
630   SBType sb_type;
631   if (m_opaque_sp) {
632     sb_type.SetSP(lldb::TypeImplSP(new TypeImpl(m_opaque_sp->GetReturnType())));
633   }
634   return sb_type;
635 }
636
637 uint32_t SBTypeMemberFunction::GetNumberOfArguments() {
638   if (m_opaque_sp)
639     return m_opaque_sp->GetNumArguments();
640   return 0;
641 }
642
643 lldb::SBType SBTypeMemberFunction::GetArgumentTypeAtIndex(uint32_t i) {
644   SBType sb_type;
645   if (m_opaque_sp) {
646     sb_type.SetSP(
647         lldb::TypeImplSP(new TypeImpl(m_opaque_sp->GetArgumentAtIndex(i))));
648   }
649   return sb_type;
650 }
651
652 lldb::MemberFunctionKind SBTypeMemberFunction::GetKind() {
653   if (m_opaque_sp)
654     return m_opaque_sp->GetKind();
655   return lldb::eMemberFunctionKindUnknown;
656 }
657
658 bool SBTypeMemberFunction::GetDescription(
659     lldb::SBStream &description, lldb::DescriptionLevel description_level) {
660   Stream &strm = description.ref();
661
662   if (m_opaque_sp)
663     return m_opaque_sp->GetDescription(strm);
664
665   return false;
666 }
667
668 void SBTypeMemberFunction::reset(TypeMemberFunctionImpl *type_member_impl) {
669   m_opaque_sp.reset(type_member_impl);
670 }
671
672 TypeMemberFunctionImpl &SBTypeMemberFunction::ref() {
673   if (!m_opaque_sp)
674     m_opaque_sp.reset(new TypeMemberFunctionImpl());
675   return *m_opaque_sp.get();
676 }
677
678 const TypeMemberFunctionImpl &SBTypeMemberFunction::ref() const {
679   return *m_opaque_sp.get();
680 }