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