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