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