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