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