1 //===-- CompilerType.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/Symbol/CompilerType.h"
11 
12 #include "lldb/Core/Debugger.h"
13 #include "lldb/Core/StreamFile.h"
14 #include "lldb/Symbol/ClangASTContext.h"
15 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
16 #include "lldb/Symbol/Type.h"
17 #include "lldb/Target/ExecutionContext.h"
18 #include "lldb/Target/Process.h"
19 #include "lldb/Utility/ConstString.h"
20 #include "lldb/Utility/DataBufferHeap.h"
21 #include "lldb/Utility/DataExtractor.h"
22 #include "lldb/Utility/Scalar.h"
23 #include "lldb/Utility/Stream.h"
24 #include "lldb/Utility/StreamString.h"
25 
26 #include <iterator>
27 #include <mutex>
28 
29 using namespace lldb;
30 using namespace lldb_private;
31 
32 CompilerType::CompilerType(TypeSystem *type_system,
33                            lldb::opaque_compiler_type_t type)
34     : m_type(type), m_type_system(type_system) {}
35 
36 CompilerType::CompilerType(clang::ASTContext *ast, clang::QualType qual_type)
37     : m_type(qual_type.getAsOpaquePtr()),
38       m_type_system(ClangASTContext::GetASTContext(ast)) {
39 #ifdef LLDB_CONFIGURATION_DEBUG
40   if (m_type)
41     assert(m_type_system != nullptr);
42 #endif
43 }
44 
45 CompilerType::~CompilerType() {}
46 
47 //----------------------------------------------------------------------
48 // Tests
49 //----------------------------------------------------------------------
50 
51 bool CompilerType::IsAggregateType() const {
52   if (IsValid())
53     return m_type_system->IsAggregateType(m_type);
54   return false;
55 }
56 
57 bool CompilerType::IsAnonymousType() const {
58   if (IsValid())
59     return m_type_system->IsAnonymousType(m_type);
60   return false;
61 }
62 
63 bool CompilerType::IsArrayType(CompilerType *element_type_ptr, uint64_t *size,
64                                bool *is_incomplete) const {
65   if (IsValid())
66     return m_type_system->IsArrayType(m_type, element_type_ptr, size,
67                                       is_incomplete);
68 
69   if (element_type_ptr)
70     element_type_ptr->Clear();
71   if (size)
72     *size = 0;
73   if (is_incomplete)
74     *is_incomplete = false;
75   return false;
76 }
77 
78 bool CompilerType::IsVectorType(CompilerType *element_type,
79                                 uint64_t *size) const {
80   if (IsValid())
81     return m_type_system->IsVectorType(m_type, element_type, size);
82   return false;
83 }
84 
85 bool CompilerType::IsRuntimeGeneratedType() const {
86   if (IsValid())
87     return m_type_system->IsRuntimeGeneratedType(m_type);
88   return false;
89 }
90 
91 bool CompilerType::IsCharType() const {
92   if (IsValid())
93     return m_type_system->IsCharType(m_type);
94   return false;
95 }
96 
97 bool CompilerType::IsCompleteType() const {
98   if (IsValid())
99     return m_type_system->IsCompleteType(m_type);
100   return false;
101 }
102 
103 bool CompilerType::IsConst() const {
104   if (IsValid())
105     return m_type_system->IsConst(m_type);
106   return false;
107 }
108 
109 bool CompilerType::IsCStringType(uint32_t &length) const {
110   if (IsValid())
111     return m_type_system->IsCStringType(m_type, length);
112   return false;
113 }
114 
115 bool CompilerType::IsFunctionType(bool *is_variadic_ptr) const {
116   if (IsValid())
117     return m_type_system->IsFunctionType(m_type, is_variadic_ptr);
118   return false;
119 }
120 
121 // Used to detect "Homogeneous Floating-point Aggregates"
122 uint32_t
123 CompilerType::IsHomogeneousAggregate(CompilerType *base_type_ptr) const {
124   if (IsValid())
125     return m_type_system->IsHomogeneousAggregate(m_type, base_type_ptr);
126   return 0;
127 }
128 
129 size_t CompilerType::GetNumberOfFunctionArguments() const {
130   if (IsValid())
131     return m_type_system->GetNumberOfFunctionArguments(m_type);
132   return 0;
133 }
134 
135 CompilerType
136 CompilerType::GetFunctionArgumentAtIndex(const size_t index) const {
137   if (IsValid())
138     return m_type_system->GetFunctionArgumentAtIndex(m_type, index);
139   return CompilerType();
140 }
141 
142 bool CompilerType::IsFunctionPointerType() const {
143   if (IsValid())
144     return m_type_system->IsFunctionPointerType(m_type);
145   return false;
146 }
147 
148 bool CompilerType::IsBlockPointerType(
149     CompilerType *function_pointer_type_ptr) const {
150   if (IsValid())
151     return m_type_system->IsBlockPointerType(m_type, function_pointer_type_ptr);
152   return 0;
153 }
154 
155 bool CompilerType::IsIntegerType(bool &is_signed) const {
156   if (IsValid())
157     return m_type_system->IsIntegerType(m_type, is_signed);
158   return false;
159 }
160 
161 bool CompilerType::IsEnumerationType(bool &is_signed) const {
162   if (IsValid())
163     return m_type_system->IsEnumerationType(m_type, is_signed);
164   return false;
165 }
166 
167 bool CompilerType::IsIntegerOrEnumerationType(bool &is_signed) const {
168   return IsIntegerType(is_signed) || IsEnumerationType(is_signed);
169 }
170 
171 bool CompilerType::IsPointerType(CompilerType *pointee_type) const {
172   if (IsValid()) {
173     return m_type_system->IsPointerType(m_type, pointee_type);
174   }
175   if (pointee_type)
176     pointee_type->Clear();
177   return false;
178 }
179 
180 bool CompilerType::IsPointerOrReferenceType(CompilerType *pointee_type) const {
181   if (IsValid()) {
182     return m_type_system->IsPointerOrReferenceType(m_type, pointee_type);
183   }
184   if (pointee_type)
185     pointee_type->Clear();
186   return false;
187 }
188 
189 bool CompilerType::IsReferenceType(CompilerType *pointee_type,
190                                    bool *is_rvalue) const {
191   if (IsValid()) {
192     return m_type_system->IsReferenceType(m_type, pointee_type, is_rvalue);
193   }
194   if (pointee_type)
195     pointee_type->Clear();
196   return false;
197 }
198 
199 bool CompilerType::ShouldTreatScalarValueAsAddress() const {
200   if (IsValid())
201     return m_type_system->ShouldTreatScalarValueAsAddress(m_type);
202   return false;
203 }
204 
205 bool CompilerType::IsFloatingPointType(uint32_t &count,
206                                        bool &is_complex) const {
207   if (IsValid()) {
208     return m_type_system->IsFloatingPointType(m_type, count, is_complex);
209   }
210   count = 0;
211   is_complex = false;
212   return false;
213 }
214 
215 bool CompilerType::IsDefined() const {
216   if (IsValid())
217     return m_type_system->IsDefined(m_type);
218   return true;
219 }
220 
221 bool CompilerType::IsPolymorphicClass() const {
222   if (IsValid()) {
223     return m_type_system->IsPolymorphicClass(m_type);
224   }
225   return false;
226 }
227 
228 bool CompilerType::IsPossibleDynamicType(CompilerType *dynamic_pointee_type,
229                                          bool check_cplusplus,
230                                          bool check_objc) const {
231   if (IsValid())
232     return m_type_system->IsPossibleDynamicType(m_type, dynamic_pointee_type,
233                                                 check_cplusplus, check_objc);
234   return false;
235 }
236 
237 bool CompilerType::IsScalarType() const {
238   if (!IsValid())
239     return false;
240 
241   return m_type_system->IsScalarType(m_type);
242 }
243 
244 bool CompilerType::IsTypedefType() const {
245   if (!IsValid())
246     return false;
247   return m_type_system->IsTypedefType(m_type);
248 }
249 
250 bool CompilerType::IsVoidType() const {
251   if (!IsValid())
252     return false;
253   return m_type_system->IsVoidType(m_type);
254 }
255 
256 bool CompilerType::IsPointerToScalarType() const {
257   if (!IsValid())
258     return false;
259 
260   return IsPointerType() && GetPointeeType().IsScalarType();
261 }
262 
263 bool CompilerType::IsArrayOfScalarType() const {
264   CompilerType element_type;
265   if (IsArrayType(&element_type, nullptr, nullptr))
266     return element_type.IsScalarType();
267   return false;
268 }
269 
270 bool CompilerType::IsBeingDefined() const {
271   if (!IsValid())
272     return false;
273   return m_type_system->IsBeingDefined(m_type);
274 }
275 
276 //----------------------------------------------------------------------
277 // Type Completion
278 //----------------------------------------------------------------------
279 
280 bool CompilerType::GetCompleteType() const {
281   if (!IsValid())
282     return false;
283   return m_type_system->GetCompleteType(m_type);
284 }
285 
286 //----------------------------------------------------------------------
287 // AST related queries
288 //----------------------------------------------------------------------
289 size_t CompilerType::GetPointerByteSize() const {
290   if (m_type_system)
291     return m_type_system->GetPointerByteSize();
292   return 0;
293 }
294 
295 ConstString CompilerType::GetConstQualifiedTypeName() const {
296   return GetConstTypeName();
297 }
298 
299 ConstString CompilerType::GetConstTypeName() const {
300   if (IsValid()) {
301     ConstString type_name(GetTypeName());
302     if (type_name)
303       return type_name;
304   }
305   return ConstString("<invalid>");
306 }
307 
308 ConstString CompilerType::GetTypeName() const {
309   if (IsValid()) {
310     return m_type_system->GetTypeName(m_type);
311   }
312   return ConstString("<invalid>");
313 }
314 
315 ConstString CompilerType::GetDisplayTypeName() const { return GetTypeName(); }
316 
317 uint32_t CompilerType::GetTypeInfo(
318     CompilerType *pointee_or_element_compiler_type) const {
319   if (!IsValid())
320     return 0;
321 
322   return m_type_system->GetTypeInfo(m_type, pointee_or_element_compiler_type);
323 }
324 
325 lldb::LanguageType CompilerType::GetMinimumLanguage() {
326   if (!IsValid())
327     return lldb::eLanguageTypeC;
328 
329   return m_type_system->GetMinimumLanguage(m_type);
330 }
331 
332 lldb::TypeClass CompilerType::GetTypeClass() const {
333   if (!IsValid())
334     return lldb::eTypeClassInvalid;
335 
336   return m_type_system->GetTypeClass(m_type);
337 }
338 
339 void CompilerType::SetCompilerType(TypeSystem *type_system,
340                                    lldb::opaque_compiler_type_t type) {
341   m_type_system = type_system;
342   m_type = type;
343 }
344 
345 void CompilerType::SetCompilerType(clang::ASTContext *ast,
346                                    clang::QualType qual_type) {
347   m_type_system = ClangASTContext::GetASTContext(ast);
348   m_type = qual_type.getAsOpaquePtr();
349 }
350 
351 unsigned CompilerType::GetTypeQualifiers() const {
352   if (IsValid())
353     return m_type_system->GetTypeQualifiers(m_type);
354   return 0;
355 }
356 
357 //----------------------------------------------------------------------
358 // Creating related types
359 //----------------------------------------------------------------------
360 
361 CompilerType CompilerType::GetArrayElementType(uint64_t *stride) const {
362   if (IsValid()) {
363     return m_type_system->GetArrayElementType(m_type, stride);
364   }
365   return CompilerType();
366 }
367 
368 CompilerType CompilerType::GetArrayType(uint64_t size) const {
369   if (IsValid()) {
370     return m_type_system->GetArrayType(m_type, size);
371   }
372   return CompilerType();
373 }
374 
375 CompilerType CompilerType::GetCanonicalType() const {
376   if (IsValid())
377     return m_type_system->GetCanonicalType(m_type);
378   return CompilerType();
379 }
380 
381 CompilerType CompilerType::GetFullyUnqualifiedType() const {
382   if (IsValid())
383     return m_type_system->GetFullyUnqualifiedType(m_type);
384   return CompilerType();
385 }
386 
387 int CompilerType::GetFunctionArgumentCount() const {
388   if (IsValid()) {
389     return m_type_system->GetFunctionArgumentCount(m_type);
390   }
391   return -1;
392 }
393 
394 CompilerType CompilerType::GetFunctionArgumentTypeAtIndex(size_t idx) const {
395   if (IsValid()) {
396     return m_type_system->GetFunctionArgumentTypeAtIndex(m_type, idx);
397   }
398   return CompilerType();
399 }
400 
401 CompilerType CompilerType::GetFunctionReturnType() const {
402   if (IsValid()) {
403     return m_type_system->GetFunctionReturnType(m_type);
404   }
405   return CompilerType();
406 }
407 
408 size_t CompilerType::GetNumMemberFunctions() const {
409   if (IsValid()) {
410     return m_type_system->GetNumMemberFunctions(m_type);
411   }
412   return 0;
413 }
414 
415 TypeMemberFunctionImpl CompilerType::GetMemberFunctionAtIndex(size_t idx) {
416   if (IsValid()) {
417     return m_type_system->GetMemberFunctionAtIndex(m_type, idx);
418   }
419   return TypeMemberFunctionImpl();
420 }
421 
422 CompilerType CompilerType::GetNonReferenceType() const {
423   if (IsValid())
424     return m_type_system->GetNonReferenceType(m_type);
425   return CompilerType();
426 }
427 
428 CompilerType CompilerType::GetPointeeType() const {
429   if (IsValid()) {
430     return m_type_system->GetPointeeType(m_type);
431   }
432   return CompilerType();
433 }
434 
435 CompilerType CompilerType::GetPointerType() const {
436   if (IsValid()) {
437     return m_type_system->GetPointerType(m_type);
438   }
439   return CompilerType();
440 }
441 
442 CompilerType CompilerType::GetLValueReferenceType() const {
443   if (IsValid())
444     return m_type_system->GetLValueReferenceType(m_type);
445   else
446     return CompilerType();
447 }
448 
449 CompilerType CompilerType::GetRValueReferenceType() const {
450   if (IsValid())
451     return m_type_system->GetRValueReferenceType(m_type);
452   else
453     return CompilerType();
454 }
455 
456 CompilerType CompilerType::AddConstModifier() const {
457   if (IsValid())
458     return m_type_system->AddConstModifier(m_type);
459   else
460     return CompilerType();
461 }
462 
463 CompilerType CompilerType::AddVolatileModifier() const {
464   if (IsValid())
465     return m_type_system->AddVolatileModifier(m_type);
466   else
467     return CompilerType();
468 }
469 
470 CompilerType CompilerType::AddRestrictModifier() const {
471   if (IsValid())
472     return m_type_system->AddRestrictModifier(m_type);
473   else
474     return CompilerType();
475 }
476 
477 CompilerType
478 CompilerType::CreateTypedef(const char *name,
479                             const CompilerDeclContext &decl_ctx) const {
480   if (IsValid())
481     return m_type_system->CreateTypedef(m_type, name, decl_ctx);
482   else
483     return CompilerType();
484 }
485 
486 CompilerType CompilerType::GetTypedefedType() const {
487   if (IsValid())
488     return m_type_system->GetTypedefedType(m_type);
489   else
490     return CompilerType();
491 }
492 
493 //----------------------------------------------------------------------
494 // Create related types using the current type's AST
495 //----------------------------------------------------------------------
496 
497 CompilerType
498 CompilerType::GetBasicTypeFromAST(lldb::BasicType basic_type) const {
499   if (IsValid())
500     return m_type_system->GetBasicTypeFromAST(basic_type);
501   return CompilerType();
502 }
503 //----------------------------------------------------------------------
504 // Exploring the type
505 //----------------------------------------------------------------------
506 
507 uint64_t CompilerType::GetBitSize(ExecutionContextScope *exe_scope) const {
508   if (IsValid()) {
509     return m_type_system->GetBitSize(m_type, exe_scope);
510   }
511   return 0;
512 }
513 
514 uint64_t CompilerType::GetByteSize(ExecutionContextScope *exe_scope) const {
515   return (GetBitSize(exe_scope) + 7) / 8;
516 }
517 
518 size_t CompilerType::GetTypeBitAlign() const {
519   if (IsValid())
520     return m_type_system->GetTypeBitAlign(m_type);
521   return 0;
522 }
523 
524 lldb::Encoding CompilerType::GetEncoding(uint64_t &count) const {
525   if (!IsValid())
526     return lldb::eEncodingInvalid;
527 
528   return m_type_system->GetEncoding(m_type, count);
529 }
530 
531 lldb::Format CompilerType::GetFormat() const {
532   if (!IsValid())
533     return lldb::eFormatDefault;
534 
535   return m_type_system->GetFormat(m_type);
536 }
537 
538 uint32_t CompilerType::GetNumChildren(bool omit_empty_base_classes,
539                                       const ExecutionContext *exe_ctx) const {
540   if (!IsValid())
541     return 0;
542   return m_type_system->GetNumChildren(m_type, omit_empty_base_classes,
543                                        exe_ctx);
544 }
545 
546 lldb::BasicType CompilerType::GetBasicTypeEnumeration() const {
547   if (IsValid())
548     return m_type_system->GetBasicTypeEnumeration(m_type);
549   return eBasicTypeInvalid;
550 }
551 
552 void CompilerType::ForEachEnumerator(
553     std::function<bool(const CompilerType &integer_type,
554                        const ConstString &name,
555                        const llvm::APSInt &value)> const &callback) const {
556   if (IsValid())
557     return m_type_system->ForEachEnumerator(m_type, callback);
558 }
559 
560 uint32_t CompilerType::GetNumFields() const {
561   if (!IsValid())
562     return 0;
563   return m_type_system->GetNumFields(m_type);
564 }
565 
566 CompilerType CompilerType::GetFieldAtIndex(size_t idx, std::string &name,
567                                            uint64_t *bit_offset_ptr,
568                                            uint32_t *bitfield_bit_size_ptr,
569                                            bool *is_bitfield_ptr) const {
570   if (!IsValid())
571     return CompilerType();
572   return m_type_system->GetFieldAtIndex(m_type, idx, name, bit_offset_ptr,
573                                         bitfield_bit_size_ptr, is_bitfield_ptr);
574 }
575 
576 uint32_t CompilerType::GetNumDirectBaseClasses() const {
577   if (IsValid())
578     return m_type_system->GetNumDirectBaseClasses(m_type);
579   return 0;
580 }
581 
582 uint32_t CompilerType::GetNumVirtualBaseClasses() const {
583   if (IsValid())
584     return m_type_system->GetNumVirtualBaseClasses(m_type);
585   return 0;
586 }
587 
588 CompilerType
589 CompilerType::GetDirectBaseClassAtIndex(size_t idx,
590                                         uint32_t *bit_offset_ptr) const {
591   if (IsValid())
592     return m_type_system->GetDirectBaseClassAtIndex(m_type, idx,
593                                                     bit_offset_ptr);
594   return CompilerType();
595 }
596 
597 CompilerType
598 CompilerType::GetVirtualBaseClassAtIndex(size_t idx,
599                                          uint32_t *bit_offset_ptr) const {
600   if (IsValid())
601     return m_type_system->GetVirtualBaseClassAtIndex(m_type, idx,
602                                                      bit_offset_ptr);
603   return CompilerType();
604 }
605 
606 uint32_t CompilerType::GetIndexOfFieldWithName(
607     const char *name, CompilerType *field_compiler_type_ptr,
608     uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr,
609     bool *is_bitfield_ptr) const {
610   unsigned count = GetNumFields();
611   std::string field_name;
612   for (unsigned index = 0; index < count; index++) {
613     CompilerType field_compiler_type(
614         GetFieldAtIndex(index, field_name, bit_offset_ptr,
615                         bitfield_bit_size_ptr, is_bitfield_ptr));
616     if (strcmp(field_name.c_str(), name) == 0) {
617       if (field_compiler_type_ptr)
618         *field_compiler_type_ptr = field_compiler_type;
619       return index;
620     }
621   }
622   return UINT32_MAX;
623 }
624 
625 CompilerType CompilerType::GetChildCompilerTypeAtIndex(
626     ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
627     bool omit_empty_base_classes, bool ignore_array_bounds,
628     std::string &child_name, uint32_t &child_byte_size,
629     int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size,
630     uint32_t &child_bitfield_bit_offset, bool &child_is_base_class,
631     bool &child_is_deref_of_parent, ValueObject *valobj,
632     uint64_t &language_flags) const {
633   if (!IsValid())
634     return CompilerType();
635   return m_type_system->GetChildCompilerTypeAtIndex(
636       m_type, exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
637       ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
638       child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
639       child_is_deref_of_parent, valobj, language_flags);
640 }
641 
642 // Look for a child member (doesn't include base classes, but it does include
643 // their members) in the type hierarchy. Returns an index path into
644 // "clang_type" on how to reach the appropriate member.
645 //
646 //    class A
647 //    {
648 //    public:
649 //        int m_a;
650 //        int m_b;
651 //    };
652 //
653 //    class B
654 //    {
655 //    };
656 //
657 //    class C :
658 //        public B,
659 //        public A
660 //    {
661 //    };
662 //
663 // If we have a clang type that describes "class C", and we wanted to looked
664 // "m_b" in it:
665 //
666 // With omit_empty_base_classes == false we would get an integer array back
667 // with: { 1,  1 } The first index 1 is the child index for "class A" within
668 // class C The second index 1 is the child index for "m_b" within class A
669 //
670 // With omit_empty_base_classes == true we would get an integer array back
671 // with: { 0,  1 } The first index 0 is the child index for "class A" within
672 // class C (since class B doesn't have any members it doesn't count) The second
673 // index 1 is the child index for "m_b" within class A
674 
675 size_t CompilerType::GetIndexOfChildMemberWithName(
676     const char *name, bool omit_empty_base_classes,
677     std::vector<uint32_t> &child_indexes) const {
678   if (IsValid() && name && name[0]) {
679     return m_type_system->GetIndexOfChildMemberWithName(
680         m_type, name, omit_empty_base_classes, child_indexes);
681   }
682   return 0;
683 }
684 
685 size_t CompilerType::GetNumTemplateArguments() const {
686   if (IsValid()) {
687     return m_type_system->GetNumTemplateArguments(m_type);
688   }
689   return 0;
690 }
691 
692 TemplateArgumentKind CompilerType::GetTemplateArgumentKind(size_t idx) const {
693   if (IsValid())
694     return m_type_system->GetTemplateArgumentKind(m_type, idx);
695   return eTemplateArgumentKindNull;
696 }
697 
698 CompilerType CompilerType::GetTypeTemplateArgument(size_t idx) const {
699   if (IsValid()) {
700     return m_type_system->GetTypeTemplateArgument(m_type, idx);
701   }
702   return CompilerType();
703 }
704 
705 llvm::Optional<CompilerType::IntegralTemplateArgument>
706 CompilerType::GetIntegralTemplateArgument(size_t idx) const {
707   if (IsValid())
708     return m_type_system->GetIntegralTemplateArgument(m_type, idx);
709   return llvm::None;
710 }
711 
712 CompilerType CompilerType::GetTypeForFormatters() const {
713   if (IsValid())
714     return m_type_system->GetTypeForFormatters(m_type);
715   return CompilerType();
716 }
717 
718 LazyBool CompilerType::ShouldPrintAsOneLiner(ValueObject *valobj) const {
719   if (IsValid())
720     return m_type_system->ShouldPrintAsOneLiner(m_type, valobj);
721   return eLazyBoolCalculate;
722 }
723 
724 bool CompilerType::IsMeaninglessWithoutDynamicResolution() const {
725   if (IsValid())
726     return m_type_system->IsMeaninglessWithoutDynamicResolution(m_type);
727   return false;
728 }
729 
730 // Get the index of the child of "clang_type" whose name matches. This function
731 // doesn't descend into the children, but only looks one level deep and name
732 // matches can include base class names.
733 
734 uint32_t
735 CompilerType::GetIndexOfChildWithName(const char *name,
736                                       bool omit_empty_base_classes) const {
737   if (IsValid() && name && name[0]) {
738     return m_type_system->GetIndexOfChildWithName(m_type, name,
739                                                   omit_empty_base_classes);
740   }
741   return UINT32_MAX;
742 }
743 
744 size_t CompilerType::ConvertStringToFloatValue(const char *s, uint8_t *dst,
745                                                size_t dst_size) const {
746   if (IsValid())
747     return m_type_system->ConvertStringToFloatValue(m_type, s, dst, dst_size);
748   return 0;
749 }
750 
751 //----------------------------------------------------------------------
752 // Dumping types
753 //----------------------------------------------------------------------
754 #define DEPTH_INCREMENT 2
755 
756 void CompilerType::DumpValue(ExecutionContext *exe_ctx, Stream *s,
757                              lldb::Format format, const DataExtractor &data,
758                              lldb::offset_t data_byte_offset,
759                              size_t data_byte_size, uint32_t bitfield_bit_size,
760                              uint32_t bitfield_bit_offset, bool show_types,
761                              bool show_summary, bool verbose, uint32_t depth) {
762   if (!IsValid())
763     return;
764   m_type_system->DumpValue(m_type, exe_ctx, s, format, data, data_byte_offset,
765                            data_byte_size, bitfield_bit_size,
766                            bitfield_bit_offset, show_types, show_summary,
767                            verbose, depth);
768 }
769 
770 bool CompilerType::DumpTypeValue(Stream *s, lldb::Format format,
771                                  const DataExtractor &data,
772                                  lldb::offset_t byte_offset, size_t byte_size,
773                                  uint32_t bitfield_bit_size,
774                                  uint32_t bitfield_bit_offset,
775                                  ExecutionContextScope *exe_scope) {
776   if (!IsValid())
777     return false;
778   return m_type_system->DumpTypeValue(m_type, s, format, data, byte_offset,
779                                       byte_size, bitfield_bit_size,
780                                       bitfield_bit_offset, exe_scope);
781 }
782 
783 void CompilerType::DumpSummary(ExecutionContext *exe_ctx, Stream *s,
784                                const DataExtractor &data,
785                                lldb::offset_t data_byte_offset,
786                                size_t data_byte_size) {
787   if (IsValid())
788     m_type_system->DumpSummary(m_type, exe_ctx, s, data, data_byte_offset,
789                                data_byte_size);
790 }
791 
792 void CompilerType::DumpTypeDescription() const {
793   if (IsValid())
794     m_type_system->DumpTypeDescription(m_type);
795 }
796 
797 void CompilerType::DumpTypeDescription(Stream *s) const {
798   if (IsValid()) {
799     m_type_system->DumpTypeDescription(m_type, s);
800   }
801 }
802 
803 bool CompilerType::GetValueAsScalar(const lldb_private::DataExtractor &data,
804                                     lldb::offset_t data_byte_offset,
805                                     size_t data_byte_size,
806                                     Scalar &value) const {
807   if (!IsValid())
808     return false;
809 
810   if (IsAggregateType()) {
811     return false; // Aggregate types don't have scalar values
812   } else {
813     uint64_t count = 0;
814     lldb::Encoding encoding = GetEncoding(count);
815 
816     if (encoding == lldb::eEncodingInvalid || count != 1)
817       return false;
818 
819     const uint64_t byte_size = GetByteSize(nullptr);
820     lldb::offset_t offset = data_byte_offset;
821     switch (encoding) {
822     case lldb::eEncodingInvalid:
823       break;
824     case lldb::eEncodingVector:
825       break;
826     case lldb::eEncodingUint:
827       if (byte_size <= sizeof(unsigned long long)) {
828         uint64_t uval64 = data.GetMaxU64(&offset, byte_size);
829         if (byte_size <= sizeof(unsigned int)) {
830           value = (unsigned int)uval64;
831           return true;
832         } else if (byte_size <= sizeof(unsigned long)) {
833           value = (unsigned long)uval64;
834           return true;
835         } else if (byte_size <= sizeof(unsigned long long)) {
836           value = (unsigned long long)uval64;
837           return true;
838         } else
839           value.Clear();
840       }
841       break;
842 
843     case lldb::eEncodingSint:
844       if (byte_size <= sizeof(long long)) {
845         int64_t sval64 = data.GetMaxS64(&offset, byte_size);
846         if (byte_size <= sizeof(int)) {
847           value = (int)sval64;
848           return true;
849         } else if (byte_size <= sizeof(long)) {
850           value = (long)sval64;
851           return true;
852         } else if (byte_size <= sizeof(long long)) {
853           value = (long long)sval64;
854           return true;
855         } else
856           value.Clear();
857       }
858       break;
859 
860     case lldb::eEncodingIEEE754:
861       if (byte_size <= sizeof(long double)) {
862         uint32_t u32;
863         uint64_t u64;
864         if (byte_size == sizeof(float)) {
865           if (sizeof(float) == sizeof(uint32_t)) {
866             u32 = data.GetU32(&offset);
867             value = *((float *)&u32);
868             return true;
869           } else if (sizeof(float) == sizeof(uint64_t)) {
870             u64 = data.GetU64(&offset);
871             value = *((float *)&u64);
872             return true;
873           }
874         } else if (byte_size == sizeof(double)) {
875           if (sizeof(double) == sizeof(uint32_t)) {
876             u32 = data.GetU32(&offset);
877             value = *((double *)&u32);
878             return true;
879           } else if (sizeof(double) == sizeof(uint64_t)) {
880             u64 = data.GetU64(&offset);
881             value = *((double *)&u64);
882             return true;
883           }
884         } else if (byte_size == sizeof(long double)) {
885           if (sizeof(long double) == sizeof(uint32_t)) {
886             u32 = data.GetU32(&offset);
887             value = *((long double *)&u32);
888             return true;
889           } else if (sizeof(long double) == sizeof(uint64_t)) {
890             u64 = data.GetU64(&offset);
891             value = *((long double *)&u64);
892             return true;
893           }
894         }
895       }
896       break;
897     }
898   }
899   return false;
900 }
901 
902 bool CompilerType::SetValueFromScalar(const Scalar &value, Stream &strm) {
903   if (!IsValid())
904     return false;
905 
906   // Aggregate types don't have scalar values
907   if (!IsAggregateType()) {
908     strm.GetFlags().Set(Stream::eBinary);
909     uint64_t count = 0;
910     lldb::Encoding encoding = GetEncoding(count);
911 
912     if (encoding == lldb::eEncodingInvalid || count != 1)
913       return false;
914 
915     const uint64_t bit_width = GetBitSize(nullptr);
916     // This function doesn't currently handle non-byte aligned assignments
917     if ((bit_width % 8) != 0)
918       return false;
919 
920     const uint64_t byte_size = (bit_width + 7) / 8;
921     switch (encoding) {
922     case lldb::eEncodingInvalid:
923       break;
924     case lldb::eEncodingVector:
925       break;
926     case lldb::eEncodingUint:
927       switch (byte_size) {
928       case 1:
929         strm.PutHex8(value.UInt());
930         return true;
931       case 2:
932         strm.PutHex16(value.UInt());
933         return true;
934       case 4:
935         strm.PutHex32(value.UInt());
936         return true;
937       case 8:
938         strm.PutHex64(value.ULongLong());
939         return true;
940       default:
941         break;
942       }
943       break;
944 
945     case lldb::eEncodingSint:
946       switch (byte_size) {
947       case 1:
948         strm.PutHex8(value.SInt());
949         return true;
950       case 2:
951         strm.PutHex16(value.SInt());
952         return true;
953       case 4:
954         strm.PutHex32(value.SInt());
955         return true;
956       case 8:
957         strm.PutHex64(value.SLongLong());
958         return true;
959       default:
960         break;
961       }
962       break;
963 
964     case lldb::eEncodingIEEE754:
965       if (byte_size <= sizeof(long double)) {
966         if (byte_size == sizeof(float)) {
967           strm.PutFloat(value.Float());
968           return true;
969         } else if (byte_size == sizeof(double)) {
970           strm.PutDouble(value.Double());
971           return true;
972         } else if (byte_size == sizeof(long double)) {
973           strm.PutDouble(value.LongDouble());
974           return true;
975         }
976       }
977       break;
978     }
979   }
980   return false;
981 }
982 
983 bool CompilerType::ReadFromMemory(lldb_private::ExecutionContext *exe_ctx,
984                                   lldb::addr_t addr, AddressType address_type,
985                                   lldb_private::DataExtractor &data) {
986   if (!IsValid())
987     return false;
988 
989   // Can't convert a file address to anything valid without more context (which
990   // Module it came from)
991   if (address_type == eAddressTypeFile)
992     return false;
993 
994   if (!GetCompleteType())
995     return false;
996 
997   const uint64_t byte_size =
998       GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
999   if (data.GetByteSize() < byte_size) {
1000     lldb::DataBufferSP data_sp(new DataBufferHeap(byte_size, '\0'));
1001     data.SetData(data_sp);
1002   }
1003 
1004   uint8_t *dst = const_cast<uint8_t *>(data.PeekData(0, byte_size));
1005   if (dst != nullptr) {
1006     if (address_type == eAddressTypeHost) {
1007       if (addr == 0)
1008         return false;
1009       // The address is an address in this process, so just copy it
1010       memcpy(dst, reinterpret_cast<uint8_t *>(addr), byte_size);
1011       return true;
1012     } else {
1013       Process *process = nullptr;
1014       if (exe_ctx)
1015         process = exe_ctx->GetProcessPtr();
1016       if (process) {
1017         Status error;
1018         return process->ReadMemory(addr, dst, byte_size, error) == byte_size;
1019       }
1020     }
1021   }
1022   return false;
1023 }
1024 
1025 bool CompilerType::WriteToMemory(lldb_private::ExecutionContext *exe_ctx,
1026                                  lldb::addr_t addr, AddressType address_type,
1027                                  StreamString &new_value) {
1028   if (!IsValid())
1029     return false;
1030 
1031   // Can't convert a file address to anything valid without more context (which
1032   // Module it came from)
1033   if (address_type == eAddressTypeFile)
1034     return false;
1035 
1036   if (!GetCompleteType())
1037     return false;
1038 
1039   const uint64_t byte_size =
1040       GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
1041 
1042   if (byte_size > 0) {
1043     if (address_type == eAddressTypeHost) {
1044       // The address is an address in this process, so just copy it
1045       memcpy((void *)addr, new_value.GetData(), byte_size);
1046       return true;
1047     } else {
1048       Process *process = nullptr;
1049       if (exe_ctx)
1050         process = exe_ctx->GetProcessPtr();
1051       if (process) {
1052         Status error;
1053         return process->WriteMemory(addr, new_value.GetData(), byte_size,
1054                                     error) == byte_size;
1055       }
1056     }
1057   }
1058   return false;
1059 }
1060 
1061 bool lldb_private::operator==(const lldb_private::CompilerType &lhs,
1062                               const lldb_private::CompilerType &rhs) {
1063   return lhs.GetTypeSystem() == rhs.GetTypeSystem() &&
1064          lhs.GetOpaqueQualType() == rhs.GetOpaqueQualType();
1065 }
1066 
1067 bool lldb_private::operator!=(const lldb_private::CompilerType &lhs,
1068                               const lldb_private::CompilerType &rhs) {
1069   return !(lhs == rhs);
1070 }
1071