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/StreamFile.h"
14 #include "lldb/Symbol/ClangASTContext.h"
15 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
16 #include "lldb/Symbol/Type.h"
17 #include "lldb/Target/ExecutionContext.h"
18 #include "lldb/Target/Process.h"
19 #include "lldb/Utility/ConstString.h"
20 #include "lldb/Utility/DataBufferHeap.h"
21 #include "lldb/Utility/DataExtractor.h"
22 #include "lldb/Utility/Scalar.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
CompilerType(TypeSystem * type_system,lldb::opaque_compiler_type_t type)32 CompilerType::CompilerType(TypeSystem *type_system,
33 lldb::opaque_compiler_type_t type)
34 : m_type(type), m_type_system(type_system) {}
35
CompilerType(clang::ASTContext * ast,clang::QualType qual_type)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
~CompilerType()45 CompilerType::~CompilerType() {}
46
47 //----------------------------------------------------------------------
48 // Tests
49 //----------------------------------------------------------------------
50
IsAggregateType() const51 bool CompilerType::IsAggregateType() const {
52 if (IsValid())
53 return m_type_system->IsAggregateType(m_type);
54 return false;
55 }
56
IsAnonymousType() const57 bool CompilerType::IsAnonymousType() const {
58 if (IsValid())
59 return m_type_system->IsAnonymousType(m_type);
60 return false;
61 }
62
IsArrayType(CompilerType * element_type_ptr,uint64_t * size,bool * is_incomplete) const63 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
IsVectorType(CompilerType * element_type,uint64_t * size) const78 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
IsRuntimeGeneratedType() const85 bool CompilerType::IsRuntimeGeneratedType() const {
86 if (IsValid())
87 return m_type_system->IsRuntimeGeneratedType(m_type);
88 return false;
89 }
90
IsCharType() const91 bool CompilerType::IsCharType() const {
92 if (IsValid())
93 return m_type_system->IsCharType(m_type);
94 return false;
95 }
96
IsCompleteType() const97 bool CompilerType::IsCompleteType() const {
98 if (IsValid())
99 return m_type_system->IsCompleteType(m_type);
100 return false;
101 }
102
IsConst() const103 bool CompilerType::IsConst() const {
104 if (IsValid())
105 return m_type_system->IsConst(m_type);
106 return false;
107 }
108
IsCStringType(uint32_t & length) const109 bool CompilerType::IsCStringType(uint32_t &length) const {
110 if (IsValid())
111 return m_type_system->IsCStringType(m_type, length);
112 return false;
113 }
114
IsFunctionType(bool * is_variadic_ptr) const115 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
IsHomogeneousAggregate(CompilerType * base_type_ptr) const123 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
GetNumberOfFunctionArguments() const129 size_t CompilerType::GetNumberOfFunctionArguments() const {
130 if (IsValid())
131 return m_type_system->GetNumberOfFunctionArguments(m_type);
132 return 0;
133 }
134
135 CompilerType
GetFunctionArgumentAtIndex(const size_t index) const136 CompilerType::GetFunctionArgumentAtIndex(const size_t index) const {
137 if (IsValid())
138 return m_type_system->GetFunctionArgumentAtIndex(m_type, index);
139 return CompilerType();
140 }
141
IsFunctionPointerType() const142 bool CompilerType::IsFunctionPointerType() const {
143 if (IsValid())
144 return m_type_system->IsFunctionPointerType(m_type);
145 return false;
146 }
147
IsBlockPointerType(CompilerType * function_pointer_type_ptr) const148 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
IsIntegerType(bool & is_signed) const155 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
IsEnumerationType(bool & is_signed) const161 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
IsIntegerOrEnumerationType(bool & is_signed) const167 bool CompilerType::IsIntegerOrEnumerationType(bool &is_signed) const {
168 return IsIntegerType(is_signed) || IsEnumerationType(is_signed);
169 }
170
IsPointerType(CompilerType * pointee_type) const171 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
IsPointerOrReferenceType(CompilerType * pointee_type) const180 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
IsReferenceType(CompilerType * pointee_type,bool * is_rvalue) const189 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
ShouldTreatScalarValueAsAddress() const199 bool CompilerType::ShouldTreatScalarValueAsAddress() const {
200 if (IsValid())
201 return m_type_system->ShouldTreatScalarValueAsAddress(m_type);
202 return false;
203 }
204
IsFloatingPointType(uint32_t & count,bool & is_complex) const205 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
IsDefined() const215 bool CompilerType::IsDefined() const {
216 if (IsValid())
217 return m_type_system->IsDefined(m_type);
218 return true;
219 }
220
IsPolymorphicClass() const221 bool CompilerType::IsPolymorphicClass() const {
222 if (IsValid()) {
223 return m_type_system->IsPolymorphicClass(m_type);
224 }
225 return false;
226 }
227
IsPossibleDynamicType(CompilerType * dynamic_pointee_type,bool check_cplusplus,bool check_objc) const228 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
IsScalarType() const237 bool CompilerType::IsScalarType() const {
238 if (!IsValid())
239 return false;
240
241 return m_type_system->IsScalarType(m_type);
242 }
243
IsTypedefType() const244 bool CompilerType::IsTypedefType() const {
245 if (!IsValid())
246 return false;
247 return m_type_system->IsTypedefType(m_type);
248 }
249
IsVoidType() const250 bool CompilerType::IsVoidType() const {
251 if (!IsValid())
252 return false;
253 return m_type_system->IsVoidType(m_type);
254 }
255
IsPointerToScalarType() const256 bool CompilerType::IsPointerToScalarType() const {
257 if (!IsValid())
258 return false;
259
260 return IsPointerType() && GetPointeeType().IsScalarType();
261 }
262
IsArrayOfScalarType() const263 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
IsBeingDefined() const270 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
GetCompleteType() const280 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 //----------------------------------------------------------------------
GetPointerByteSize() const289 size_t CompilerType::GetPointerByteSize() const {
290 if (m_type_system)
291 return m_type_system->GetPointerByteSize();
292 return 0;
293 }
294
GetConstQualifiedTypeName() const295 ConstString CompilerType::GetConstQualifiedTypeName() const {
296 return GetConstTypeName();
297 }
298
GetConstTypeName() const299 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
GetTypeName() const308 ConstString CompilerType::GetTypeName() const {
309 if (IsValid()) {
310 return m_type_system->GetTypeName(m_type);
311 }
312 return ConstString("<invalid>");
313 }
314
GetDisplayTypeName() const315 ConstString CompilerType::GetDisplayTypeName() const { return GetTypeName(); }
316
GetTypeInfo(CompilerType * pointee_or_element_compiler_type) const317 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
GetMinimumLanguage()325 lldb::LanguageType CompilerType::GetMinimumLanguage() {
326 if (!IsValid())
327 return lldb::eLanguageTypeC;
328
329 return m_type_system->GetMinimumLanguage(m_type);
330 }
331
GetTypeClass() const332 lldb::TypeClass CompilerType::GetTypeClass() const {
333 if (!IsValid())
334 return lldb::eTypeClassInvalid;
335
336 return m_type_system->GetTypeClass(m_type);
337 }
338
SetCompilerType(TypeSystem * type_system,lldb::opaque_compiler_type_t type)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
SetCompilerType(clang::ASTContext * ast,clang::QualType qual_type)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
GetTypeQualifiers() const351 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
GetArrayElementType(uint64_t * stride) const361 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
GetArrayType(uint64_t size) const368 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
GetCanonicalType() const375 CompilerType CompilerType::GetCanonicalType() const {
376 if (IsValid())
377 return m_type_system->GetCanonicalType(m_type);
378 return CompilerType();
379 }
380
GetFullyUnqualifiedType() const381 CompilerType CompilerType::GetFullyUnqualifiedType() const {
382 if (IsValid())
383 return m_type_system->GetFullyUnqualifiedType(m_type);
384 return CompilerType();
385 }
386
GetFunctionArgumentCount() const387 int CompilerType::GetFunctionArgumentCount() const {
388 if (IsValid()) {
389 return m_type_system->GetFunctionArgumentCount(m_type);
390 }
391 return -1;
392 }
393
GetFunctionArgumentTypeAtIndex(size_t idx) const394 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
GetFunctionReturnType() const401 CompilerType CompilerType::GetFunctionReturnType() const {
402 if (IsValid()) {
403 return m_type_system->GetFunctionReturnType(m_type);
404 }
405 return CompilerType();
406 }
407
GetNumMemberFunctions() const408 size_t CompilerType::GetNumMemberFunctions() const {
409 if (IsValid()) {
410 return m_type_system->GetNumMemberFunctions(m_type);
411 }
412 return 0;
413 }
414
GetMemberFunctionAtIndex(size_t idx)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
GetNonReferenceType() const422 CompilerType CompilerType::GetNonReferenceType() const {
423 if (IsValid())
424 return m_type_system->GetNonReferenceType(m_type);
425 return CompilerType();
426 }
427
GetPointeeType() const428 CompilerType CompilerType::GetPointeeType() const {
429 if (IsValid()) {
430 return m_type_system->GetPointeeType(m_type);
431 }
432 return CompilerType();
433 }
434
GetPointerType() const435 CompilerType CompilerType::GetPointerType() const {
436 if (IsValid()) {
437 return m_type_system->GetPointerType(m_type);
438 }
439 return CompilerType();
440 }
441
GetLValueReferenceType() const442 CompilerType CompilerType::GetLValueReferenceType() const {
443 if (IsValid())
444 return m_type_system->GetLValueReferenceType(m_type);
445 else
446 return CompilerType();
447 }
448
GetRValueReferenceType() const449 CompilerType CompilerType::GetRValueReferenceType() const {
450 if (IsValid())
451 return m_type_system->GetRValueReferenceType(m_type);
452 else
453 return CompilerType();
454 }
455
AddConstModifier() const456 CompilerType CompilerType::AddConstModifier() const {
457 if (IsValid())
458 return m_type_system->AddConstModifier(m_type);
459 else
460 return CompilerType();
461 }
462
AddVolatileModifier() const463 CompilerType CompilerType::AddVolatileModifier() const {
464 if (IsValid())
465 return m_type_system->AddVolatileModifier(m_type);
466 else
467 return CompilerType();
468 }
469
AddRestrictModifier() const470 CompilerType CompilerType::AddRestrictModifier() const {
471 if (IsValid())
472 return m_type_system->AddRestrictModifier(m_type);
473 else
474 return CompilerType();
475 }
476
477 CompilerType
CreateTypedef(const char * name,const CompilerDeclContext & decl_ctx) const478 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
GetTypedefedType() const486 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
GetBasicTypeFromAST(lldb::BasicType basic_type) const498 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 llvm::Optional<uint64_t>
GetBitSize(ExecutionContextScope * exe_scope) const508 CompilerType::GetBitSize(ExecutionContextScope *exe_scope) const {
509 if (IsValid())
510 return m_type_system->GetBitSize(m_type, exe_scope);
511 return {};
512 }
513
514 llvm::Optional<uint64_t>
GetByteSize(ExecutionContextScope * exe_scope) const515 CompilerType::GetByteSize(ExecutionContextScope *exe_scope) const {
516 if (llvm::Optional<uint64_t> bit_size = GetBitSize(exe_scope))
517 return (*bit_size + 7) / 8;
518 return {};
519 }
520
GetTypeBitAlign() const521 size_t CompilerType::GetTypeBitAlign() const {
522 if (IsValid())
523 return m_type_system->GetTypeBitAlign(m_type);
524 return 0;
525 }
526
GetEncoding(uint64_t & count) const527 lldb::Encoding CompilerType::GetEncoding(uint64_t &count) const {
528 if (!IsValid())
529 return lldb::eEncodingInvalid;
530
531 return m_type_system->GetEncoding(m_type, count);
532 }
533
GetFormat() const534 lldb::Format CompilerType::GetFormat() const {
535 if (!IsValid())
536 return lldb::eFormatDefault;
537
538 return m_type_system->GetFormat(m_type);
539 }
540
GetNumChildren(bool omit_empty_base_classes,const ExecutionContext * exe_ctx) const541 uint32_t CompilerType::GetNumChildren(bool omit_empty_base_classes,
542 const ExecutionContext *exe_ctx) const {
543 if (!IsValid())
544 return 0;
545 return m_type_system->GetNumChildren(m_type, omit_empty_base_classes,
546 exe_ctx);
547 }
548
GetBasicTypeEnumeration() const549 lldb::BasicType CompilerType::GetBasicTypeEnumeration() const {
550 if (IsValid())
551 return m_type_system->GetBasicTypeEnumeration(m_type);
552 return eBasicTypeInvalid;
553 }
554
ForEachEnumerator(std::function<bool (const CompilerType & integer_type,const ConstString & name,const llvm::APSInt & value)> const & callback) const555 void CompilerType::ForEachEnumerator(
556 std::function<bool(const CompilerType &integer_type,
557 const ConstString &name,
558 const llvm::APSInt &value)> const &callback) const {
559 if (IsValid())
560 return m_type_system->ForEachEnumerator(m_type, callback);
561 }
562
GetNumFields() const563 uint32_t CompilerType::GetNumFields() const {
564 if (!IsValid())
565 return 0;
566 return m_type_system->GetNumFields(m_type);
567 }
568
GetFieldAtIndex(size_t idx,std::string & name,uint64_t * bit_offset_ptr,uint32_t * bitfield_bit_size_ptr,bool * is_bitfield_ptr) const569 CompilerType CompilerType::GetFieldAtIndex(size_t idx, std::string &name,
570 uint64_t *bit_offset_ptr,
571 uint32_t *bitfield_bit_size_ptr,
572 bool *is_bitfield_ptr) const {
573 if (!IsValid())
574 return CompilerType();
575 return m_type_system->GetFieldAtIndex(m_type, idx, name, bit_offset_ptr,
576 bitfield_bit_size_ptr, is_bitfield_ptr);
577 }
578
GetNumDirectBaseClasses() const579 uint32_t CompilerType::GetNumDirectBaseClasses() const {
580 if (IsValid())
581 return m_type_system->GetNumDirectBaseClasses(m_type);
582 return 0;
583 }
584
GetNumVirtualBaseClasses() const585 uint32_t CompilerType::GetNumVirtualBaseClasses() const {
586 if (IsValid())
587 return m_type_system->GetNumVirtualBaseClasses(m_type);
588 return 0;
589 }
590
591 CompilerType
GetDirectBaseClassAtIndex(size_t idx,uint32_t * bit_offset_ptr) const592 CompilerType::GetDirectBaseClassAtIndex(size_t idx,
593 uint32_t *bit_offset_ptr) const {
594 if (IsValid())
595 return m_type_system->GetDirectBaseClassAtIndex(m_type, idx,
596 bit_offset_ptr);
597 return CompilerType();
598 }
599
600 CompilerType
GetVirtualBaseClassAtIndex(size_t idx,uint32_t * bit_offset_ptr) const601 CompilerType::GetVirtualBaseClassAtIndex(size_t idx,
602 uint32_t *bit_offset_ptr) const {
603 if (IsValid())
604 return m_type_system->GetVirtualBaseClassAtIndex(m_type, idx,
605 bit_offset_ptr);
606 return CompilerType();
607 }
608
GetIndexOfFieldWithName(const char * name,CompilerType * field_compiler_type_ptr,uint64_t * bit_offset_ptr,uint32_t * bitfield_bit_size_ptr,bool * is_bitfield_ptr) const609 uint32_t CompilerType::GetIndexOfFieldWithName(
610 const char *name, CompilerType *field_compiler_type_ptr,
611 uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr,
612 bool *is_bitfield_ptr) const {
613 unsigned count = GetNumFields();
614 std::string field_name;
615 for (unsigned index = 0; index < count; index++) {
616 CompilerType field_compiler_type(
617 GetFieldAtIndex(index, field_name, bit_offset_ptr,
618 bitfield_bit_size_ptr, is_bitfield_ptr));
619 if (strcmp(field_name.c_str(), name) == 0) {
620 if (field_compiler_type_ptr)
621 *field_compiler_type_ptr = field_compiler_type;
622 return index;
623 }
624 }
625 return UINT32_MAX;
626 }
627
GetChildCompilerTypeAtIndex(ExecutionContext * exe_ctx,size_t idx,bool transparent_pointers,bool omit_empty_base_classes,bool ignore_array_bounds,std::string & child_name,uint32_t & child_byte_size,int32_t & child_byte_offset,uint32_t & child_bitfield_bit_size,uint32_t & child_bitfield_bit_offset,bool & child_is_base_class,bool & child_is_deref_of_parent,ValueObject * valobj,uint64_t & language_flags) const628 CompilerType CompilerType::GetChildCompilerTypeAtIndex(
629 ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
630 bool omit_empty_base_classes, bool ignore_array_bounds,
631 std::string &child_name, uint32_t &child_byte_size,
632 int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size,
633 uint32_t &child_bitfield_bit_offset, bool &child_is_base_class,
634 bool &child_is_deref_of_parent, ValueObject *valobj,
635 uint64_t &language_flags) const {
636 if (!IsValid())
637 return CompilerType();
638 return m_type_system->GetChildCompilerTypeAtIndex(
639 m_type, exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
640 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
641 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
642 child_is_deref_of_parent, valobj, language_flags);
643 }
644
645 // Look for a child member (doesn't include base classes, but it does include
646 // their members) in the type hierarchy. Returns an index path into
647 // "clang_type" on how to reach the appropriate member.
648 //
649 // class A
650 // {
651 // public:
652 // int m_a;
653 // int m_b;
654 // };
655 //
656 // class B
657 // {
658 // };
659 //
660 // class C :
661 // public B,
662 // public A
663 // {
664 // };
665 //
666 // If we have a clang type that describes "class C", and we wanted to looked
667 // "m_b" in it:
668 //
669 // With omit_empty_base_classes == false we would get an integer array back
670 // with: { 1, 1 } The first index 1 is the child index for "class A" within
671 // class C The second index 1 is the child index for "m_b" within class A
672 //
673 // With omit_empty_base_classes == true we would get an integer array back
674 // with: { 0, 1 } The first index 0 is the child index for "class A" within
675 // class C (since class B doesn't have any members it doesn't count) The second
676 // index 1 is the child index for "m_b" within class A
677
GetIndexOfChildMemberWithName(const char * name,bool omit_empty_base_classes,std::vector<uint32_t> & child_indexes) const678 size_t CompilerType::GetIndexOfChildMemberWithName(
679 const char *name, bool omit_empty_base_classes,
680 std::vector<uint32_t> &child_indexes) const {
681 if (IsValid() && name && name[0]) {
682 return m_type_system->GetIndexOfChildMemberWithName(
683 m_type, name, omit_empty_base_classes, child_indexes);
684 }
685 return 0;
686 }
687
GetNumTemplateArguments() const688 size_t CompilerType::GetNumTemplateArguments() const {
689 if (IsValid()) {
690 return m_type_system->GetNumTemplateArguments(m_type);
691 }
692 return 0;
693 }
694
GetTemplateArgumentKind(size_t idx) const695 TemplateArgumentKind CompilerType::GetTemplateArgumentKind(size_t idx) const {
696 if (IsValid())
697 return m_type_system->GetTemplateArgumentKind(m_type, idx);
698 return eTemplateArgumentKindNull;
699 }
700
GetTypeTemplateArgument(size_t idx) const701 CompilerType CompilerType::GetTypeTemplateArgument(size_t idx) const {
702 if (IsValid()) {
703 return m_type_system->GetTypeTemplateArgument(m_type, idx);
704 }
705 return CompilerType();
706 }
707
708 llvm::Optional<CompilerType::IntegralTemplateArgument>
GetIntegralTemplateArgument(size_t idx) const709 CompilerType::GetIntegralTemplateArgument(size_t idx) const {
710 if (IsValid())
711 return m_type_system->GetIntegralTemplateArgument(m_type, idx);
712 return llvm::None;
713 }
714
GetTypeForFormatters() const715 CompilerType CompilerType::GetTypeForFormatters() const {
716 if (IsValid())
717 return m_type_system->GetTypeForFormatters(m_type);
718 return CompilerType();
719 }
720
ShouldPrintAsOneLiner(ValueObject * valobj) const721 LazyBool CompilerType::ShouldPrintAsOneLiner(ValueObject *valobj) const {
722 if (IsValid())
723 return m_type_system->ShouldPrintAsOneLiner(m_type, valobj);
724 return eLazyBoolCalculate;
725 }
726
IsMeaninglessWithoutDynamicResolution() const727 bool CompilerType::IsMeaninglessWithoutDynamicResolution() const {
728 if (IsValid())
729 return m_type_system->IsMeaninglessWithoutDynamicResolution(m_type);
730 return false;
731 }
732
733 // Get the index of the child of "clang_type" whose name matches. This function
734 // doesn't descend into the children, but only looks one level deep and name
735 // matches can include base class names.
736
737 uint32_t
GetIndexOfChildWithName(const char * name,bool omit_empty_base_classes) const738 CompilerType::GetIndexOfChildWithName(const char *name,
739 bool omit_empty_base_classes) const {
740 if (IsValid() && name && name[0]) {
741 return m_type_system->GetIndexOfChildWithName(m_type, name,
742 omit_empty_base_classes);
743 }
744 return UINT32_MAX;
745 }
746
ConvertStringToFloatValue(const char * s,uint8_t * dst,size_t dst_size) const747 size_t CompilerType::ConvertStringToFloatValue(const char *s, uint8_t *dst,
748 size_t dst_size) const {
749 if (IsValid())
750 return m_type_system->ConvertStringToFloatValue(m_type, s, dst, dst_size);
751 return 0;
752 }
753
754 //----------------------------------------------------------------------
755 // Dumping types
756 //----------------------------------------------------------------------
757 #define DEPTH_INCREMENT 2
758
DumpValue(ExecutionContext * exe_ctx,Stream * s,lldb::Format format,const DataExtractor & data,lldb::offset_t data_byte_offset,size_t data_byte_size,uint32_t bitfield_bit_size,uint32_t bitfield_bit_offset,bool show_types,bool show_summary,bool verbose,uint32_t depth)759 void CompilerType::DumpValue(ExecutionContext *exe_ctx, Stream *s,
760 lldb::Format format, const DataExtractor &data,
761 lldb::offset_t data_byte_offset,
762 size_t data_byte_size, uint32_t bitfield_bit_size,
763 uint32_t bitfield_bit_offset, bool show_types,
764 bool show_summary, bool verbose, uint32_t depth) {
765 if (!IsValid())
766 return;
767 m_type_system->DumpValue(m_type, exe_ctx, s, format, data, data_byte_offset,
768 data_byte_size, bitfield_bit_size,
769 bitfield_bit_offset, show_types, show_summary,
770 verbose, depth);
771 }
772
DumpTypeValue(Stream * s,lldb::Format format,const DataExtractor & data,lldb::offset_t byte_offset,size_t byte_size,uint32_t bitfield_bit_size,uint32_t bitfield_bit_offset,ExecutionContextScope * exe_scope)773 bool CompilerType::DumpTypeValue(Stream *s, lldb::Format format,
774 const DataExtractor &data,
775 lldb::offset_t byte_offset, size_t byte_size,
776 uint32_t bitfield_bit_size,
777 uint32_t bitfield_bit_offset,
778 ExecutionContextScope *exe_scope) {
779 if (!IsValid())
780 return false;
781 return m_type_system->DumpTypeValue(m_type, s, format, data, byte_offset,
782 byte_size, bitfield_bit_size,
783 bitfield_bit_offset, exe_scope);
784 }
785
DumpSummary(ExecutionContext * exe_ctx,Stream * s,const DataExtractor & data,lldb::offset_t data_byte_offset,size_t data_byte_size)786 void CompilerType::DumpSummary(ExecutionContext *exe_ctx, Stream *s,
787 const DataExtractor &data,
788 lldb::offset_t data_byte_offset,
789 size_t data_byte_size) {
790 if (IsValid())
791 m_type_system->DumpSummary(m_type, exe_ctx, s, data, data_byte_offset,
792 data_byte_size);
793 }
794
DumpTypeDescription() const795 void CompilerType::DumpTypeDescription() const {
796 if (IsValid())
797 m_type_system->DumpTypeDescription(m_type);
798 }
799
DumpTypeDescription(Stream * s) const800 void CompilerType::DumpTypeDescription(Stream *s) const {
801 if (IsValid()) {
802 m_type_system->DumpTypeDescription(m_type, s);
803 }
804 }
805
GetValueAsScalar(const lldb_private::DataExtractor & data,lldb::offset_t data_byte_offset,size_t data_byte_size,Scalar & value) const806 bool CompilerType::GetValueAsScalar(const lldb_private::DataExtractor &data,
807 lldb::offset_t data_byte_offset,
808 size_t data_byte_size,
809 Scalar &value) const {
810 if (!IsValid())
811 return false;
812
813 if (IsAggregateType()) {
814 return false; // Aggregate types don't have scalar values
815 } else {
816 uint64_t count = 0;
817 lldb::Encoding encoding = GetEncoding(count);
818
819 if (encoding == lldb::eEncodingInvalid || count != 1)
820 return false;
821
822 llvm::Optional<uint64_t> byte_size = GetByteSize(nullptr);
823 if (!byte_size)
824 return false;
825 lldb::offset_t offset = data_byte_offset;
826 switch (encoding) {
827 case lldb::eEncodingInvalid:
828 break;
829 case lldb::eEncodingVector:
830 break;
831 case lldb::eEncodingUint:
832 if (*byte_size <= sizeof(unsigned long long)) {
833 uint64_t uval64 = data.GetMaxU64(&offset, *byte_size);
834 if (*byte_size <= sizeof(unsigned int)) {
835 value = (unsigned int)uval64;
836 return true;
837 } else if (*byte_size <= sizeof(unsigned long)) {
838 value = (unsigned long)uval64;
839 return true;
840 } else if (*byte_size <= sizeof(unsigned long long)) {
841 value = (unsigned long long)uval64;
842 return true;
843 } else
844 value.Clear();
845 }
846 break;
847
848 case lldb::eEncodingSint:
849 if (*byte_size <= sizeof(long long)) {
850 int64_t sval64 = data.GetMaxS64(&offset, *byte_size);
851 if (*byte_size <= sizeof(int)) {
852 value = (int)sval64;
853 return true;
854 } else if (*byte_size <= sizeof(long)) {
855 value = (long)sval64;
856 return true;
857 } else if (*byte_size <= sizeof(long long)) {
858 value = (long long)sval64;
859 return true;
860 } else
861 value.Clear();
862 }
863 break;
864
865 case lldb::eEncodingIEEE754:
866 if (*byte_size <= sizeof(long double)) {
867 uint32_t u32;
868 uint64_t u64;
869 if (*byte_size == sizeof(float)) {
870 if (sizeof(float) == sizeof(uint32_t)) {
871 u32 = data.GetU32(&offset);
872 value = *((float *)&u32);
873 return true;
874 } else if (sizeof(float) == sizeof(uint64_t)) {
875 u64 = data.GetU64(&offset);
876 value = *((float *)&u64);
877 return true;
878 }
879 } else if (*byte_size == sizeof(double)) {
880 if (sizeof(double) == sizeof(uint32_t)) {
881 u32 = data.GetU32(&offset);
882 value = *((double *)&u32);
883 return true;
884 } else if (sizeof(double) == sizeof(uint64_t)) {
885 u64 = data.GetU64(&offset);
886 value = *((double *)&u64);
887 return true;
888 }
889 } else if (*byte_size == sizeof(long double)) {
890 if (sizeof(long double) == sizeof(uint32_t)) {
891 u32 = data.GetU32(&offset);
892 value = *((long double *)&u32);
893 return true;
894 } else if (sizeof(long double) == sizeof(uint64_t)) {
895 u64 = data.GetU64(&offset);
896 value = *((long double *)&u64);
897 return true;
898 }
899 }
900 }
901 break;
902 }
903 }
904 return false;
905 }
906
SetValueFromScalar(const Scalar & value,Stream & strm)907 bool CompilerType::SetValueFromScalar(const Scalar &value, Stream &strm) {
908 if (!IsValid())
909 return false;
910
911 // Aggregate types don't have scalar values
912 if (!IsAggregateType()) {
913 strm.GetFlags().Set(Stream::eBinary);
914 uint64_t count = 0;
915 lldb::Encoding encoding = GetEncoding(count);
916
917 if (encoding == lldb::eEncodingInvalid || count != 1)
918 return false;
919
920 llvm::Optional<uint64_t> bit_width = GetBitSize(nullptr);
921 if (!bit_width)
922 return false;
923
924 // This function doesn't currently handle non-byte aligned assignments
925 if ((*bit_width % 8) != 0)
926 return false;
927
928 const uint64_t byte_size = (*bit_width + 7) / 8;
929 switch (encoding) {
930 case lldb::eEncodingInvalid:
931 break;
932 case lldb::eEncodingVector:
933 break;
934 case lldb::eEncodingUint:
935 switch (byte_size) {
936 case 1:
937 strm.PutHex8(value.UInt());
938 return true;
939 case 2:
940 strm.PutHex16(value.UInt());
941 return true;
942 case 4:
943 strm.PutHex32(value.UInt());
944 return true;
945 case 8:
946 strm.PutHex64(value.ULongLong());
947 return true;
948 default:
949 break;
950 }
951 break;
952
953 case lldb::eEncodingSint:
954 switch (byte_size) {
955 case 1:
956 strm.PutHex8(value.SInt());
957 return true;
958 case 2:
959 strm.PutHex16(value.SInt());
960 return true;
961 case 4:
962 strm.PutHex32(value.SInt());
963 return true;
964 case 8:
965 strm.PutHex64(value.SLongLong());
966 return true;
967 default:
968 break;
969 }
970 break;
971
972 case lldb::eEncodingIEEE754:
973 if (byte_size <= sizeof(long double)) {
974 if (byte_size == sizeof(float)) {
975 strm.PutFloat(value.Float());
976 return true;
977 } else if (byte_size == sizeof(double)) {
978 strm.PutDouble(value.Double());
979 return true;
980 } else if (byte_size == sizeof(long double)) {
981 strm.PutDouble(value.LongDouble());
982 return true;
983 }
984 }
985 break;
986 }
987 }
988 return false;
989 }
990
ReadFromMemory(lldb_private::ExecutionContext * exe_ctx,lldb::addr_t addr,AddressType address_type,lldb_private::DataExtractor & data)991 bool CompilerType::ReadFromMemory(lldb_private::ExecutionContext *exe_ctx,
992 lldb::addr_t addr, AddressType address_type,
993 lldb_private::DataExtractor &data) {
994 if (!IsValid())
995 return false;
996
997 // Can't convert a file address to anything valid without more context (which
998 // Module it came from)
999 if (address_type == eAddressTypeFile)
1000 return false;
1001
1002 if (!GetCompleteType())
1003 return false;
1004
1005 auto byte_size =
1006 GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
1007 if (!byte_size)
1008 return false;
1009
1010 if (data.GetByteSize() < *byte_size) {
1011 lldb::DataBufferSP data_sp(new DataBufferHeap(*byte_size, '\0'));
1012 data.SetData(data_sp);
1013 }
1014
1015 uint8_t *dst = const_cast<uint8_t *>(data.PeekData(0, *byte_size));
1016 if (dst != nullptr) {
1017 if (address_type == eAddressTypeHost) {
1018 if (addr == 0)
1019 return false;
1020 // The address is an address in this process, so just copy it
1021 memcpy(dst, reinterpret_cast<uint8_t *>(addr), *byte_size);
1022 return true;
1023 } else {
1024 Process *process = nullptr;
1025 if (exe_ctx)
1026 process = exe_ctx->GetProcessPtr();
1027 if (process) {
1028 Status error;
1029 return process->ReadMemory(addr, dst, *byte_size, error) == *byte_size;
1030 }
1031 }
1032 }
1033 return false;
1034 }
1035
WriteToMemory(lldb_private::ExecutionContext * exe_ctx,lldb::addr_t addr,AddressType address_type,StreamString & new_value)1036 bool CompilerType::WriteToMemory(lldb_private::ExecutionContext *exe_ctx,
1037 lldb::addr_t addr, AddressType address_type,
1038 StreamString &new_value) {
1039 if (!IsValid())
1040 return false;
1041
1042 // Can't convert a file address to anything valid without more context (which
1043 // Module it came from)
1044 if (address_type == eAddressTypeFile)
1045 return false;
1046
1047 if (!GetCompleteType())
1048 return false;
1049
1050 auto byte_size =
1051 GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
1052 if (!byte_size)
1053 return false;
1054
1055 if (*byte_size > 0) {
1056 if (address_type == eAddressTypeHost) {
1057 // The address is an address in this process, so just copy it
1058 memcpy((void *)addr, new_value.GetData(), *byte_size);
1059 return true;
1060 } else {
1061 Process *process = nullptr;
1062 if (exe_ctx)
1063 process = exe_ctx->GetProcessPtr();
1064 if (process) {
1065 Status error;
1066 return process->WriteMemory(addr, new_value.GetData(), *byte_size,
1067 error) == *byte_size;
1068 }
1069 }
1070 }
1071 return false;
1072 }
1073
operator ==(const lldb_private::CompilerType & lhs,const lldb_private::CompilerType & rhs)1074 bool lldb_private::operator==(const lldb_private::CompilerType &lhs,
1075 const lldb_private::CompilerType &rhs) {
1076 return lhs.GetTypeSystem() == rhs.GetTypeSystem() &&
1077 lhs.GetOpaqueQualType() == rhs.GetOpaqueQualType();
1078 }
1079
operator !=(const lldb_private::CompilerType & lhs,const lldb_private::CompilerType & rhs)1080 bool lldb_private::operator!=(const lldb_private::CompilerType &lhs,
1081 const lldb_private::CompilerType &rhs) {
1082 return !(lhs == rhs);
1083 }
1084