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