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