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