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/Scalar.h"
14 #include "lldb/Core/StreamFile.h"
15 #include "lldb/Symbol/ClangASTContext.h"
16 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
17 #include "lldb/Symbol/Type.h"
18 #include "lldb/Target/ExecutionContext.h"
19 #include "lldb/Target/Process.h"
20 #include "lldb/Utility/ConstString.h"
21 #include "lldb/Utility/DataBufferHeap.h"
22 #include "lldb/Utility/DataExtractor.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) const {
539   if (!IsValid())
540     return 0;
541   return m_type_system->GetNumChildren(m_type, omit_empty_base_classes);
542 }
543 
544 lldb::BasicType CompilerType::GetBasicTypeEnumeration() const {
545   if (IsValid())
546     return m_type_system->GetBasicTypeEnumeration(m_type);
547   return eBasicTypeInvalid;
548 }
549 
550 void CompilerType::ForEachEnumerator(
551     std::function<bool(const CompilerType &integer_type,
552                        const ConstString &name,
553                        const llvm::APSInt &value)> const &callback) const {
554   if (IsValid())
555     return m_type_system->ForEachEnumerator(m_type, callback);
556 }
557 
558 uint32_t CompilerType::GetNumFields() const {
559   if (!IsValid())
560     return 0;
561   return m_type_system->GetNumFields(m_type);
562 }
563 
564 CompilerType CompilerType::GetFieldAtIndex(size_t idx, std::string &name,
565                                            uint64_t *bit_offset_ptr,
566                                            uint32_t *bitfield_bit_size_ptr,
567                                            bool *is_bitfield_ptr) const {
568   if (!IsValid())
569     return CompilerType();
570   return m_type_system->GetFieldAtIndex(m_type, idx, name, bit_offset_ptr,
571                                         bitfield_bit_size_ptr, is_bitfield_ptr);
572 }
573 
574 uint32_t CompilerType::GetNumDirectBaseClasses() const {
575   if (IsValid())
576     return m_type_system->GetNumDirectBaseClasses(m_type);
577   return 0;
578 }
579 
580 uint32_t CompilerType::GetNumVirtualBaseClasses() const {
581   if (IsValid())
582     return m_type_system->GetNumVirtualBaseClasses(m_type);
583   return 0;
584 }
585 
586 CompilerType
587 CompilerType::GetDirectBaseClassAtIndex(size_t idx,
588                                         uint32_t *bit_offset_ptr) const {
589   if (IsValid())
590     return m_type_system->GetDirectBaseClassAtIndex(m_type, idx,
591                                                     bit_offset_ptr);
592   return CompilerType();
593 }
594 
595 CompilerType
596 CompilerType::GetVirtualBaseClassAtIndex(size_t idx,
597                                          uint32_t *bit_offset_ptr) const {
598   if (IsValid())
599     return m_type_system->GetVirtualBaseClassAtIndex(m_type, idx,
600                                                      bit_offset_ptr);
601   return CompilerType();
602 }
603 
604 uint32_t CompilerType::GetIndexOfFieldWithName(
605     const char *name, CompilerType *field_compiler_type_ptr,
606     uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr,
607     bool *is_bitfield_ptr) const {
608   unsigned count = GetNumFields();
609   std::string field_name;
610   for (unsigned index = 0; index < count; index++) {
611     CompilerType field_compiler_type(
612         GetFieldAtIndex(index, field_name, bit_offset_ptr,
613                         bitfield_bit_size_ptr, is_bitfield_ptr));
614     if (strcmp(field_name.c_str(), name) == 0) {
615       if (field_compiler_type_ptr)
616         *field_compiler_type_ptr = field_compiler_type;
617       return index;
618     }
619   }
620   return UINT32_MAX;
621 }
622 
623 CompilerType CompilerType::GetChildCompilerTypeAtIndex(
624     ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
625     bool omit_empty_base_classes, bool ignore_array_bounds,
626     std::string &child_name, uint32_t &child_byte_size,
627     int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size,
628     uint32_t &child_bitfield_bit_offset, bool &child_is_base_class,
629     bool &child_is_deref_of_parent, ValueObject *valobj,
630     uint64_t &language_flags) const {
631   if (!IsValid())
632     return CompilerType();
633   return m_type_system->GetChildCompilerTypeAtIndex(
634       m_type, exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
635       ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
636       child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
637       child_is_deref_of_parent, valobj, language_flags);
638 }
639 
640 // Look for a child member (doesn't include base classes, but it does include
641 // their members) in the type hierarchy. Returns an index path into "clang_type"
642 // on how to reach the appropriate member.
643 //
644 //    class A
645 //    {
646 //    public:
647 //        int m_a;
648 //        int m_b;
649 //    };
650 //
651 //    class B
652 //    {
653 //    };
654 //
655 //    class C :
656 //        public B,
657 //        public A
658 //    {
659 //    };
660 //
661 // If we have a clang type that describes "class C", and we wanted to looked
662 // "m_b" in it:
663 //
664 // With omit_empty_base_classes == false we would get an integer array back
665 // with:
666 // { 1,  1 }
667 // The first index 1 is the child index for "class A" within class C
668 // 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 with:
671 // { 0,  1 }
672 // The first index 0 is the child index for "class A" within class C (since
673 // class B doesn't have any members it doesn't count)
674 // The second index 1 is the child index for "m_b" within class A
675 
676 size_t CompilerType::GetIndexOfChildMemberWithName(
677     const char *name, bool omit_empty_base_classes,
678     std::vector<uint32_t> &child_indexes) const {
679   if (IsValid() && name && name[0]) {
680     return m_type_system->GetIndexOfChildMemberWithName(
681         m_type, name, omit_empty_base_classes, child_indexes);
682   }
683   return 0;
684 }
685 
686 size_t CompilerType::GetNumTemplateArguments() const {
687   if (IsValid()) {
688     return m_type_system->GetNumTemplateArguments(m_type);
689   }
690   return 0;
691 }
692 
693 TemplateArgumentKind CompilerType::GetTemplateArgumentKind(size_t idx) const {
694   if (IsValid())
695     return m_type_system->GetTemplateArgumentKind(m_type, idx);
696   return eTemplateArgumentKindNull;
697 }
698 
699 CompilerType CompilerType::GetTypeTemplateArgument(size_t idx) const {
700   if (IsValid()) {
701     return m_type_system->GetTypeTemplateArgument(m_type, idx);
702   }
703   return CompilerType();
704 }
705 
706 std::pair<llvm::APSInt, CompilerType>
707 CompilerType::GetIntegralTemplateArgument(size_t idx) const
708 {
709   if (IsValid())
710     return m_type_system->GetIntegralTemplateArgument(m_type, idx);
711   return {llvm::APSInt(0), CompilerType()};
712 }
713 
714 CompilerType CompilerType::GetTypeForFormatters() const {
715   if (IsValid())
716     return m_type_system->GetTypeForFormatters(m_type);
717   return CompilerType();
718 }
719 
720 LazyBool CompilerType::ShouldPrintAsOneLiner(ValueObject *valobj) const {
721   if (IsValid())
722     return m_type_system->ShouldPrintAsOneLiner(m_type, valobj);
723   return eLazyBoolCalculate;
724 }
725 
726 bool CompilerType::IsMeaninglessWithoutDynamicResolution() const {
727   if (IsValid())
728     return m_type_system->IsMeaninglessWithoutDynamicResolution(m_type);
729   return false;
730 }
731 
732 // Get the index of the child of "clang_type" whose name matches. This function
733 // doesn't descend into the children, but only looks one level deep and name
734 // matches can include base class names.
735 
736 uint32_t
737 CompilerType::GetIndexOfChildWithName(const char *name,
738                                       bool omit_empty_base_classes) const {
739   if (IsValid() && name && name[0]) {
740     return m_type_system->GetIndexOfChildWithName(m_type, name,
741                                                   omit_empty_base_classes);
742   }
743   return UINT32_MAX;
744 }
745 
746 size_t CompilerType::ConvertStringToFloatValue(const char *s, uint8_t *dst,
747                                                size_t dst_size) const {
748   if (IsValid())
749     return m_type_system->ConvertStringToFloatValue(m_type, s, dst, dst_size);
750   return 0;
751 }
752 
753 //----------------------------------------------------------------------
754 // Dumping types
755 //----------------------------------------------------------------------
756 #define DEPTH_INCREMENT 2
757 
758 void CompilerType::DumpValue(ExecutionContext *exe_ctx, Stream *s,
759                              lldb::Format format, const DataExtractor &data,
760                              lldb::offset_t data_byte_offset,
761                              size_t data_byte_size, uint32_t bitfield_bit_size,
762                              uint32_t bitfield_bit_offset, bool show_types,
763                              bool show_summary, bool verbose, uint32_t depth) {
764   if (!IsValid())
765     return;
766   m_type_system->DumpValue(m_type, exe_ctx, s, format, data, data_byte_offset,
767                            data_byte_size, bitfield_bit_size,
768                            bitfield_bit_offset, show_types, show_summary,
769                            verbose, depth);
770 }
771 
772 bool CompilerType::DumpTypeValue(Stream *s, lldb::Format format,
773                                  const DataExtractor &data,
774                                  lldb::offset_t byte_offset, size_t byte_size,
775                                  uint32_t bitfield_bit_size,
776                                  uint32_t bitfield_bit_offset,
777                                  ExecutionContextScope *exe_scope) {
778   if (!IsValid())
779     return false;
780   return m_type_system->DumpTypeValue(m_type, s, format, data, byte_offset,
781                                       byte_size, bitfield_bit_size,
782                                       bitfield_bit_offset, exe_scope);
783 }
784 
785 void CompilerType::DumpSummary(ExecutionContext *exe_ctx, Stream *s,
786                                const DataExtractor &data,
787                                lldb::offset_t data_byte_offset,
788                                size_t data_byte_size) {
789   if (IsValid())
790     m_type_system->DumpSummary(m_type, exe_ctx, s, data, data_byte_offset,
791                                data_byte_size);
792 }
793 
794 void CompilerType::DumpTypeDescription() const {
795   if (IsValid())
796     m_type_system->DumpTypeDescription(m_type);
797 }
798 
799 void CompilerType::DumpTypeDescription(Stream *s) const {
800   if (IsValid()) {
801     m_type_system->DumpTypeDescription(m_type, s);
802   }
803 }
804 
805 bool CompilerType::GetValueAsScalar(const lldb_private::DataExtractor &data,
806                                     lldb::offset_t data_byte_offset,
807                                     size_t data_byte_size,
808                                     Scalar &value) const {
809   if (!IsValid())
810     return false;
811 
812   if (IsAggregateType()) {
813     return false; // Aggregate types don't have scalar values
814   } else {
815     uint64_t count = 0;
816     lldb::Encoding encoding = GetEncoding(count);
817 
818     if (encoding == lldb::eEncodingInvalid || count != 1)
819       return false;
820 
821     const uint64_t byte_size = GetByteSize(nullptr);
822     lldb::offset_t offset = data_byte_offset;
823     switch (encoding) {
824     case lldb::eEncodingInvalid:
825       break;
826     case lldb::eEncodingVector:
827       break;
828     case lldb::eEncodingUint:
829       if (byte_size <= sizeof(unsigned long long)) {
830         uint64_t uval64 = data.GetMaxU64(&offset, byte_size);
831         if (byte_size <= sizeof(unsigned int)) {
832           value = (unsigned int)uval64;
833           return true;
834         } else if (byte_size <= sizeof(unsigned long)) {
835           value = (unsigned long)uval64;
836           return true;
837         } else if (byte_size <= sizeof(unsigned long long)) {
838           value = (unsigned long long)uval64;
839           return true;
840         } else
841           value.Clear();
842       }
843       break;
844 
845     case lldb::eEncodingSint:
846       if (byte_size <= sizeof(long long)) {
847         int64_t sval64 = data.GetMaxS64(&offset, byte_size);
848         if (byte_size <= sizeof(int)) {
849           value = (int)sval64;
850           return true;
851         } else if (byte_size <= sizeof(long)) {
852           value = (long)sval64;
853           return true;
854         } else if (byte_size <= sizeof(long long)) {
855           value = (long long)sval64;
856           return true;
857         } else
858           value.Clear();
859       }
860       break;
861 
862     case lldb::eEncodingIEEE754:
863       if (byte_size <= sizeof(long double)) {
864         uint32_t u32;
865         uint64_t u64;
866         if (byte_size == sizeof(float)) {
867           if (sizeof(float) == sizeof(uint32_t)) {
868             u32 = data.GetU32(&offset);
869             value = *((float *)&u32);
870             return true;
871           } else if (sizeof(float) == sizeof(uint64_t)) {
872             u64 = data.GetU64(&offset);
873             value = *((float *)&u64);
874             return true;
875           }
876         } else if (byte_size == sizeof(double)) {
877           if (sizeof(double) == sizeof(uint32_t)) {
878             u32 = data.GetU32(&offset);
879             value = *((double *)&u32);
880             return true;
881           } else if (sizeof(double) == sizeof(uint64_t)) {
882             u64 = data.GetU64(&offset);
883             value = *((double *)&u64);
884             return true;
885           }
886         } else if (byte_size == sizeof(long double)) {
887           if (sizeof(long double) == sizeof(uint32_t)) {
888             u32 = data.GetU32(&offset);
889             value = *((long double *)&u32);
890             return true;
891           } else if (sizeof(long double) == sizeof(uint64_t)) {
892             u64 = data.GetU64(&offset);
893             value = *((long double *)&u64);
894             return true;
895           }
896         }
897       }
898       break;
899     }
900   }
901   return false;
902 }
903 
904 bool CompilerType::SetValueFromScalar(const Scalar &value, Stream &strm) {
905   if (!IsValid())
906     return false;
907 
908   // Aggregate types don't have scalar values
909   if (!IsAggregateType()) {
910     strm.GetFlags().Set(Stream::eBinary);
911     uint64_t count = 0;
912     lldb::Encoding encoding = GetEncoding(count);
913 
914     if (encoding == lldb::eEncodingInvalid || count != 1)
915       return false;
916 
917     const uint64_t bit_width = GetBitSize(nullptr);
918     // This function doesn't currently handle non-byte aligned assignments
919     if ((bit_width % 8) != 0)
920       return false;
921 
922     const uint64_t byte_size = (bit_width + 7) / 8;
923     switch (encoding) {
924     case lldb::eEncodingInvalid:
925       break;
926     case lldb::eEncodingVector:
927       break;
928     case lldb::eEncodingUint:
929       switch (byte_size) {
930       case 1:
931         strm.PutHex8(value.UInt());
932         return true;
933       case 2:
934         strm.PutHex16(value.UInt());
935         return true;
936       case 4:
937         strm.PutHex32(value.UInt());
938         return true;
939       case 8:
940         strm.PutHex64(value.ULongLong());
941         return true;
942       default:
943         break;
944       }
945       break;
946 
947     case lldb::eEncodingSint:
948       switch (byte_size) {
949       case 1:
950         strm.PutHex8(value.SInt());
951         return true;
952       case 2:
953         strm.PutHex16(value.SInt());
954         return true;
955       case 4:
956         strm.PutHex32(value.SInt());
957         return true;
958       case 8:
959         strm.PutHex64(value.SLongLong());
960         return true;
961       default:
962         break;
963       }
964       break;
965 
966     case lldb::eEncodingIEEE754:
967       if (byte_size <= sizeof(long double)) {
968         if (byte_size == sizeof(float)) {
969           strm.PutFloat(value.Float());
970           return true;
971         } else if (byte_size == sizeof(double)) {
972           strm.PutDouble(value.Double());
973           return true;
974         } else if (byte_size == sizeof(long double)) {
975           strm.PutDouble(value.LongDouble());
976           return true;
977         }
978       }
979       break;
980     }
981   }
982   return false;
983 }
984 
985 bool CompilerType::ReadFromMemory(lldb_private::ExecutionContext *exe_ctx,
986                                   lldb::addr_t addr, AddressType address_type,
987                                   lldb_private::DataExtractor &data) {
988   if (!IsValid())
989     return false;
990 
991   // Can't convert a file address to anything valid without more
992   // context (which Module it came from)
993   if (address_type == eAddressTypeFile)
994     return false;
995 
996   if (!GetCompleteType())
997     return false;
998 
999   const uint64_t byte_size =
1000       GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
1001   if (data.GetByteSize() < byte_size) {
1002     lldb::DataBufferSP data_sp(new DataBufferHeap(byte_size, '\0'));
1003     data.SetData(data_sp);
1004   }
1005 
1006   uint8_t *dst = const_cast<uint8_t *>(data.PeekData(0, byte_size));
1007   if (dst != nullptr) {
1008     if (address_type == eAddressTypeHost) {
1009       if (addr == 0)
1010         return false;
1011       // The address is an address in this process, so just copy it
1012       memcpy(dst, reinterpret_cast<uint8_t *>(addr), byte_size);
1013       return true;
1014     } else {
1015       Process *process = nullptr;
1016       if (exe_ctx)
1017         process = exe_ctx->GetProcessPtr();
1018       if (process) {
1019         Status error;
1020         return process->ReadMemory(addr, dst, byte_size, error) == byte_size;
1021       }
1022     }
1023   }
1024   return false;
1025 }
1026 
1027 bool CompilerType::WriteToMemory(lldb_private::ExecutionContext *exe_ctx,
1028                                  lldb::addr_t addr, AddressType address_type,
1029                                  StreamString &new_value) {
1030   if (!IsValid())
1031     return false;
1032 
1033   // Can't convert a file address to anything valid without more
1034   // context (which Module it came from)
1035   if (address_type == eAddressTypeFile)
1036     return false;
1037 
1038   if (!GetCompleteType())
1039     return false;
1040 
1041   const uint64_t byte_size =
1042       GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
1043 
1044   if (byte_size > 0) {
1045     if (address_type == eAddressTypeHost) {
1046       // The address is an address in this process, so just copy it
1047       memcpy((void *)addr, new_value.GetData(), byte_size);
1048       return true;
1049     } else {
1050       Process *process = nullptr;
1051       if (exe_ctx)
1052         process = exe_ctx->GetProcessPtr();
1053       if (process) {
1054         Status error;
1055         return process->WriteMemory(addr, new_value.GetData(), byte_size,
1056                                     error) == byte_size;
1057       }
1058     }
1059   }
1060   return false;
1061 }
1062 
1063 // clang::CXXRecordDecl *
1064 // CompilerType::GetAsCXXRecordDecl (lldb::opaque_compiler_type_t
1065 // opaque_compiler_qual_type)
1066 //{
1067 //    if (opaque_compiler_qual_type)
1068 //        return
1069 //        clang::QualType::getFromOpaquePtr(opaque_compiler_qual_type)->getAsCXXRecordDecl();
1070 //    return NULL;
1071 //}
1072 
1073 bool lldb_private::operator==(const lldb_private::CompilerType &lhs,
1074                               const lldb_private::CompilerType &rhs) {
1075   return lhs.GetTypeSystem() == rhs.GetTypeSystem() &&
1076          lhs.GetOpaqueQualType() == rhs.GetOpaqueQualType();
1077 }
1078 
1079 bool lldb_private::operator!=(const lldb_private::CompilerType &lhs,
1080                               const lldb_private::CompilerType &rhs) {
1081   return lhs.GetTypeSystem() != rhs.GetTypeSystem() ||
1082          lhs.GetOpaqueQualType() != rhs.GetOpaqueQualType();
1083 }
1084