1 //===-- CompilerType.cpp --------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "lldb/Symbol/CompilerType.h"
10
11 #include "lldb/Core/Debugger.h"
12 #include "lldb/Core/StreamFile.h"
13 #include "lldb/Symbol/Type.h"
14 #include "lldb/Target/ExecutionContext.h"
15 #include "lldb/Target/Process.h"
16 #include "lldb/Utility/ConstString.h"
17 #include "lldb/Utility/DataBufferHeap.h"
18 #include "lldb/Utility/DataExtractor.h"
19 #include "lldb/Utility/Scalar.h"
20 #include "lldb/Utility/Stream.h"
21 #include "lldb/Utility/StreamString.h"
22
23 #include <iterator>
24 #include <mutex>
25
26 using namespace lldb;
27 using namespace lldb_private;
28
29 // Tests
30
IsAggregateType() const31 bool CompilerType::IsAggregateType() const {
32 if (IsValid())
33 return m_type_system->IsAggregateType(m_type);
34 return false;
35 }
36
IsAnonymousType() const37 bool CompilerType::IsAnonymousType() const {
38 if (IsValid())
39 return m_type_system->IsAnonymousType(m_type);
40 return false;
41 }
42
IsScopedEnumerationType() const43 bool CompilerType::IsScopedEnumerationType() const {
44 if (IsValid())
45 return m_type_system->IsScopedEnumerationType(m_type);
46 return false;
47 }
48
IsArrayType(CompilerType * element_type_ptr,uint64_t * size,bool * is_incomplete) const49 bool CompilerType::IsArrayType(CompilerType *element_type_ptr, uint64_t *size,
50 bool *is_incomplete) const {
51 if (IsValid())
52 return m_type_system->IsArrayType(m_type, element_type_ptr, size,
53 is_incomplete);
54
55 if (element_type_ptr)
56 element_type_ptr->Clear();
57 if (size)
58 *size = 0;
59 if (is_incomplete)
60 *is_incomplete = false;
61 return false;
62 }
63
IsVectorType(CompilerType * element_type,uint64_t * size) const64 bool CompilerType::IsVectorType(CompilerType *element_type,
65 uint64_t *size) const {
66 if (IsValid())
67 return m_type_system->IsVectorType(m_type, element_type, size);
68 return false;
69 }
70
IsRuntimeGeneratedType() const71 bool CompilerType::IsRuntimeGeneratedType() const {
72 if (IsValid())
73 return m_type_system->IsRuntimeGeneratedType(m_type);
74 return false;
75 }
76
IsCharType() const77 bool CompilerType::IsCharType() const {
78 if (IsValid())
79 return m_type_system->IsCharType(m_type);
80 return false;
81 }
82
IsCompleteType() const83 bool CompilerType::IsCompleteType() const {
84 if (IsValid())
85 return m_type_system->IsCompleteType(m_type);
86 return false;
87 }
88
IsConst() const89 bool CompilerType::IsConst() const {
90 if (IsValid())
91 return m_type_system->IsConst(m_type);
92 return false;
93 }
94
IsCStringType(uint32_t & length) const95 bool CompilerType::IsCStringType(uint32_t &length) const {
96 if (IsValid())
97 return m_type_system->IsCStringType(m_type, length);
98 return false;
99 }
100
IsFunctionType() const101 bool CompilerType::IsFunctionType() const {
102 if (IsValid())
103 return m_type_system->IsFunctionType(m_type);
104 return false;
105 }
106
107 // Used to detect "Homogeneous Floating-point Aggregates"
108 uint32_t
IsHomogeneousAggregate(CompilerType * base_type_ptr) const109 CompilerType::IsHomogeneousAggregate(CompilerType *base_type_ptr) const {
110 if (IsValid())
111 return m_type_system->IsHomogeneousAggregate(m_type, base_type_ptr);
112 return 0;
113 }
114
GetNumberOfFunctionArguments() const115 size_t CompilerType::GetNumberOfFunctionArguments() const {
116 if (IsValid())
117 return m_type_system->GetNumberOfFunctionArguments(m_type);
118 return 0;
119 }
120
121 CompilerType
GetFunctionArgumentAtIndex(const size_t index) const122 CompilerType::GetFunctionArgumentAtIndex(const size_t index) const {
123 if (IsValid())
124 return m_type_system->GetFunctionArgumentAtIndex(m_type, index);
125 return CompilerType();
126 }
127
IsFunctionPointerType() const128 bool CompilerType::IsFunctionPointerType() const {
129 if (IsValid())
130 return m_type_system->IsFunctionPointerType(m_type);
131 return false;
132 }
133
IsBlockPointerType(CompilerType * function_pointer_type_ptr) const134 bool CompilerType::IsBlockPointerType(
135 CompilerType *function_pointer_type_ptr) const {
136 if (IsValid())
137 return m_type_system->IsBlockPointerType(m_type, function_pointer_type_ptr);
138 return false;
139 }
140
IsIntegerType(bool & is_signed) const141 bool CompilerType::IsIntegerType(bool &is_signed) const {
142 if (IsValid())
143 return m_type_system->IsIntegerType(m_type, is_signed);
144 return false;
145 }
146
IsEnumerationType(bool & is_signed) const147 bool CompilerType::IsEnumerationType(bool &is_signed) const {
148 if (IsValid())
149 return m_type_system->IsEnumerationType(m_type, is_signed);
150 return false;
151 }
152
IsIntegerOrEnumerationType(bool & is_signed) const153 bool CompilerType::IsIntegerOrEnumerationType(bool &is_signed) const {
154 return IsIntegerType(is_signed) || IsEnumerationType(is_signed);
155 }
156
IsPointerType(CompilerType * pointee_type) const157 bool CompilerType::IsPointerType(CompilerType *pointee_type) const {
158 if (IsValid()) {
159 return m_type_system->IsPointerType(m_type, pointee_type);
160 }
161 if (pointee_type)
162 pointee_type->Clear();
163 return false;
164 }
165
IsPointerOrReferenceType(CompilerType * pointee_type) const166 bool CompilerType::IsPointerOrReferenceType(CompilerType *pointee_type) const {
167 if (IsValid()) {
168 return m_type_system->IsPointerOrReferenceType(m_type, pointee_type);
169 }
170 if (pointee_type)
171 pointee_type->Clear();
172 return false;
173 }
174
IsReferenceType(CompilerType * pointee_type,bool * is_rvalue) const175 bool CompilerType::IsReferenceType(CompilerType *pointee_type,
176 bool *is_rvalue) const {
177 if (IsValid()) {
178 return m_type_system->IsReferenceType(m_type, pointee_type, is_rvalue);
179 }
180 if (pointee_type)
181 pointee_type->Clear();
182 return false;
183 }
184
ShouldTreatScalarValueAsAddress() const185 bool CompilerType::ShouldTreatScalarValueAsAddress() const {
186 if (IsValid())
187 return m_type_system->ShouldTreatScalarValueAsAddress(m_type);
188 return false;
189 }
190
IsFloatingPointType(uint32_t & count,bool & is_complex) const191 bool CompilerType::IsFloatingPointType(uint32_t &count,
192 bool &is_complex) const {
193 if (IsValid()) {
194 return m_type_system->IsFloatingPointType(m_type, count, is_complex);
195 }
196 count = 0;
197 is_complex = false;
198 return false;
199 }
200
IsDefined() const201 bool CompilerType::IsDefined() const {
202 if (IsValid())
203 return m_type_system->IsDefined(m_type);
204 return true;
205 }
206
IsPolymorphicClass() const207 bool CompilerType::IsPolymorphicClass() const {
208 if (IsValid()) {
209 return m_type_system->IsPolymorphicClass(m_type);
210 }
211 return false;
212 }
213
IsPossibleDynamicType(CompilerType * dynamic_pointee_type,bool check_cplusplus,bool check_objc) const214 bool CompilerType::IsPossibleDynamicType(CompilerType *dynamic_pointee_type,
215 bool check_cplusplus,
216 bool check_objc) const {
217 if (IsValid())
218 return m_type_system->IsPossibleDynamicType(m_type, dynamic_pointee_type,
219 check_cplusplus, check_objc);
220 return false;
221 }
222
IsScalarType() const223 bool CompilerType::IsScalarType() const {
224 if (!IsValid())
225 return false;
226
227 return m_type_system->IsScalarType(m_type);
228 }
229
IsTypedefType() const230 bool CompilerType::IsTypedefType() const {
231 if (!IsValid())
232 return false;
233 return m_type_system->IsTypedefType(m_type);
234 }
235
IsVoidType() const236 bool CompilerType::IsVoidType() const {
237 if (!IsValid())
238 return false;
239 return m_type_system->IsVoidType(m_type);
240 }
241
IsPointerToScalarType() const242 bool CompilerType::IsPointerToScalarType() const {
243 if (!IsValid())
244 return false;
245
246 return IsPointerType() && GetPointeeType().IsScalarType();
247 }
248
IsArrayOfScalarType() const249 bool CompilerType::IsArrayOfScalarType() const {
250 CompilerType element_type;
251 if (IsArrayType(&element_type))
252 return element_type.IsScalarType();
253 return false;
254 }
255
IsBeingDefined() const256 bool CompilerType::IsBeingDefined() const {
257 if (!IsValid())
258 return false;
259 return m_type_system->IsBeingDefined(m_type);
260 }
261
262 // Type Completion
263
GetCompleteType() const264 bool CompilerType::GetCompleteType() const {
265 if (!IsValid())
266 return false;
267 return m_type_system->GetCompleteType(m_type);
268 }
269
270 // AST related queries
GetPointerByteSize() const271 size_t CompilerType::GetPointerByteSize() const {
272 if (m_type_system)
273 return m_type_system->GetPointerByteSize();
274 return 0;
275 }
276
GetTypeName() const277 ConstString CompilerType::GetTypeName() const {
278 if (IsValid()) {
279 return m_type_system->GetTypeName(m_type);
280 }
281 return ConstString("<invalid>");
282 }
283
GetDisplayTypeName() const284 ConstString CompilerType::GetDisplayTypeName() const {
285 if (IsValid())
286 return m_type_system->GetDisplayTypeName(m_type);
287 return ConstString("<invalid>");
288 }
289
GetTypeInfo(CompilerType * pointee_or_element_compiler_type) const290 uint32_t CompilerType::GetTypeInfo(
291 CompilerType *pointee_or_element_compiler_type) const {
292 if (!IsValid())
293 return 0;
294
295 return m_type_system->GetTypeInfo(m_type, pointee_or_element_compiler_type);
296 }
297
GetMinimumLanguage()298 lldb::LanguageType CompilerType::GetMinimumLanguage() {
299 if (!IsValid())
300 return lldb::eLanguageTypeC;
301
302 return m_type_system->GetMinimumLanguage(m_type);
303 }
304
GetTypeClass() const305 lldb::TypeClass CompilerType::GetTypeClass() const {
306 if (!IsValid())
307 return lldb::eTypeClassInvalid;
308
309 return m_type_system->GetTypeClass(m_type);
310 }
311
SetCompilerType(TypeSystem * type_system,lldb::opaque_compiler_type_t type)312 void CompilerType::SetCompilerType(TypeSystem *type_system,
313 lldb::opaque_compiler_type_t type) {
314 m_type_system = type_system;
315 m_type = type;
316 }
317
GetTypeQualifiers() const318 unsigned CompilerType::GetTypeQualifiers() const {
319 if (IsValid())
320 return m_type_system->GetTypeQualifiers(m_type);
321 return 0;
322 }
323
324 // Creating related types
325
326 CompilerType
GetArrayElementType(ExecutionContextScope * exe_scope) const327 CompilerType::GetArrayElementType(ExecutionContextScope *exe_scope) const {
328 if (IsValid()) {
329 return m_type_system->GetArrayElementType(m_type, exe_scope);
330 }
331 return CompilerType();
332 }
333
GetArrayType(uint64_t size) const334 CompilerType CompilerType::GetArrayType(uint64_t size) const {
335 if (IsValid()) {
336 return m_type_system->GetArrayType(m_type, size);
337 }
338 return CompilerType();
339 }
340
GetCanonicalType() const341 CompilerType CompilerType::GetCanonicalType() const {
342 if (IsValid())
343 return m_type_system->GetCanonicalType(m_type);
344 return CompilerType();
345 }
346
GetFullyUnqualifiedType() const347 CompilerType CompilerType::GetFullyUnqualifiedType() const {
348 if (IsValid())
349 return m_type_system->GetFullyUnqualifiedType(m_type);
350 return CompilerType();
351 }
352
GetEnumerationIntegerType() const353 CompilerType CompilerType::GetEnumerationIntegerType() const {
354 if (IsValid())
355 return m_type_system->GetEnumerationIntegerType(m_type);
356 return CompilerType();
357 }
358
GetFunctionArgumentCount() const359 int CompilerType::GetFunctionArgumentCount() const {
360 if (IsValid()) {
361 return m_type_system->GetFunctionArgumentCount(m_type);
362 }
363 return -1;
364 }
365
GetFunctionArgumentTypeAtIndex(size_t idx) const366 CompilerType CompilerType::GetFunctionArgumentTypeAtIndex(size_t idx) const {
367 if (IsValid()) {
368 return m_type_system->GetFunctionArgumentTypeAtIndex(m_type, idx);
369 }
370 return CompilerType();
371 }
372
GetFunctionReturnType() const373 CompilerType CompilerType::GetFunctionReturnType() const {
374 if (IsValid()) {
375 return m_type_system->GetFunctionReturnType(m_type);
376 }
377 return CompilerType();
378 }
379
GetNumMemberFunctions() const380 size_t CompilerType::GetNumMemberFunctions() const {
381 if (IsValid()) {
382 return m_type_system->GetNumMemberFunctions(m_type);
383 }
384 return 0;
385 }
386
GetMemberFunctionAtIndex(size_t idx)387 TypeMemberFunctionImpl CompilerType::GetMemberFunctionAtIndex(size_t idx) {
388 if (IsValid()) {
389 return m_type_system->GetMemberFunctionAtIndex(m_type, idx);
390 }
391 return TypeMemberFunctionImpl();
392 }
393
GetNonReferenceType() const394 CompilerType CompilerType::GetNonReferenceType() const {
395 if (IsValid())
396 return m_type_system->GetNonReferenceType(m_type);
397 return CompilerType();
398 }
399
GetPointeeType() const400 CompilerType CompilerType::GetPointeeType() const {
401 if (IsValid()) {
402 return m_type_system->GetPointeeType(m_type);
403 }
404 return CompilerType();
405 }
406
GetPointerType() const407 CompilerType CompilerType::GetPointerType() const {
408 if (IsValid()) {
409 return m_type_system->GetPointerType(m_type);
410 }
411 return CompilerType();
412 }
413
GetLValueReferenceType() const414 CompilerType CompilerType::GetLValueReferenceType() const {
415 if (IsValid())
416 return m_type_system->GetLValueReferenceType(m_type);
417 else
418 return CompilerType();
419 }
420
GetRValueReferenceType() const421 CompilerType CompilerType::GetRValueReferenceType() const {
422 if (IsValid())
423 return m_type_system->GetRValueReferenceType(m_type);
424 else
425 return CompilerType();
426 }
427
GetAtomicType() const428 CompilerType CompilerType::GetAtomicType() const {
429 if (IsValid())
430 return m_type_system->GetAtomicType(m_type);
431 return CompilerType();
432 }
433
AddConstModifier() const434 CompilerType CompilerType::AddConstModifier() const {
435 if (IsValid())
436 return m_type_system->AddConstModifier(m_type);
437 else
438 return CompilerType();
439 }
440
AddVolatileModifier() const441 CompilerType CompilerType::AddVolatileModifier() const {
442 if (IsValid())
443 return m_type_system->AddVolatileModifier(m_type);
444 else
445 return CompilerType();
446 }
447
AddRestrictModifier() const448 CompilerType CompilerType::AddRestrictModifier() const {
449 if (IsValid())
450 return m_type_system->AddRestrictModifier(m_type);
451 else
452 return CompilerType();
453 }
454
CreateTypedef(const char * name,const CompilerDeclContext & decl_ctx,uint32_t payload) const455 CompilerType CompilerType::CreateTypedef(const char *name,
456 const CompilerDeclContext &decl_ctx,
457 uint32_t payload) const {
458 if (IsValid())
459 return m_type_system->CreateTypedef(m_type, name, decl_ctx, payload);
460 else
461 return CompilerType();
462 }
463
GetTypedefedType() const464 CompilerType CompilerType::GetTypedefedType() const {
465 if (IsValid())
466 return m_type_system->GetTypedefedType(m_type);
467 else
468 return CompilerType();
469 }
470
471 // Create related types using the current type's AST
472
473 CompilerType
GetBasicTypeFromAST(lldb::BasicType basic_type) const474 CompilerType::GetBasicTypeFromAST(lldb::BasicType basic_type) const {
475 if (IsValid())
476 return m_type_system->GetBasicTypeFromAST(basic_type);
477 return CompilerType();
478 }
479 // Exploring the type
480
481 llvm::Optional<uint64_t>
GetBitSize(ExecutionContextScope * exe_scope) const482 CompilerType::GetBitSize(ExecutionContextScope *exe_scope) const {
483 if (IsValid())
484 return m_type_system->GetBitSize(m_type, exe_scope);
485 return {};
486 }
487
488 llvm::Optional<uint64_t>
GetByteSize(ExecutionContextScope * exe_scope) const489 CompilerType::GetByteSize(ExecutionContextScope *exe_scope) const {
490 if (llvm::Optional<uint64_t> bit_size = GetBitSize(exe_scope))
491 return (*bit_size + 7) / 8;
492 return {};
493 }
494
GetTypeBitAlign(ExecutionContextScope * exe_scope) const495 llvm::Optional<size_t> CompilerType::GetTypeBitAlign(ExecutionContextScope *exe_scope) const {
496 if (IsValid())
497 return m_type_system->GetTypeBitAlign(m_type, exe_scope);
498 return {};
499 }
500
GetEncoding(uint64_t & count) const501 lldb::Encoding CompilerType::GetEncoding(uint64_t &count) const {
502 if (!IsValid())
503 return lldb::eEncodingInvalid;
504
505 return m_type_system->GetEncoding(m_type, count);
506 }
507
GetFormat() const508 lldb::Format CompilerType::GetFormat() const {
509 if (!IsValid())
510 return lldb::eFormatDefault;
511
512 return m_type_system->GetFormat(m_type);
513 }
514
GetNumChildren(bool omit_empty_base_classes,const ExecutionContext * exe_ctx) const515 uint32_t CompilerType::GetNumChildren(bool omit_empty_base_classes,
516 const ExecutionContext *exe_ctx) const {
517 if (!IsValid())
518 return 0;
519 return m_type_system->GetNumChildren(m_type, omit_empty_base_classes,
520 exe_ctx);
521 }
522
GetBasicTypeEnumeration() const523 lldb::BasicType CompilerType::GetBasicTypeEnumeration() const {
524 if (IsValid())
525 return m_type_system->GetBasicTypeEnumeration(m_type);
526 return eBasicTypeInvalid;
527 }
528
ForEachEnumerator(std::function<bool (const CompilerType & integer_type,ConstString name,const llvm::APSInt & value)> const & callback) const529 void CompilerType::ForEachEnumerator(
530 std::function<bool(const CompilerType &integer_type,
531 ConstString name,
532 const llvm::APSInt &value)> const &callback) const {
533 if (IsValid())
534 return m_type_system->ForEachEnumerator(m_type, callback);
535 }
536
GetNumFields() const537 uint32_t CompilerType::GetNumFields() const {
538 if (!IsValid())
539 return 0;
540 return m_type_system->GetNumFields(m_type);
541 }
542
GetFieldAtIndex(size_t idx,std::string & name,uint64_t * bit_offset_ptr,uint32_t * bitfield_bit_size_ptr,bool * is_bitfield_ptr) const543 CompilerType CompilerType::GetFieldAtIndex(size_t idx, std::string &name,
544 uint64_t *bit_offset_ptr,
545 uint32_t *bitfield_bit_size_ptr,
546 bool *is_bitfield_ptr) const {
547 if (!IsValid())
548 return CompilerType();
549 return m_type_system->GetFieldAtIndex(m_type, idx, name, bit_offset_ptr,
550 bitfield_bit_size_ptr, is_bitfield_ptr);
551 }
552
GetNumDirectBaseClasses() const553 uint32_t CompilerType::GetNumDirectBaseClasses() const {
554 if (IsValid())
555 return m_type_system->GetNumDirectBaseClasses(m_type);
556 return 0;
557 }
558
GetNumVirtualBaseClasses() const559 uint32_t CompilerType::GetNumVirtualBaseClasses() const {
560 if (IsValid())
561 return m_type_system->GetNumVirtualBaseClasses(m_type);
562 return 0;
563 }
564
565 CompilerType
GetDirectBaseClassAtIndex(size_t idx,uint32_t * bit_offset_ptr) const566 CompilerType::GetDirectBaseClassAtIndex(size_t idx,
567 uint32_t *bit_offset_ptr) const {
568 if (IsValid())
569 return m_type_system->GetDirectBaseClassAtIndex(m_type, idx,
570 bit_offset_ptr);
571 return CompilerType();
572 }
573
574 CompilerType
GetVirtualBaseClassAtIndex(size_t idx,uint32_t * bit_offset_ptr) const575 CompilerType::GetVirtualBaseClassAtIndex(size_t idx,
576 uint32_t *bit_offset_ptr) const {
577 if (IsValid())
578 return m_type_system->GetVirtualBaseClassAtIndex(m_type, idx,
579 bit_offset_ptr);
580 return CompilerType();
581 }
582
GetIndexOfFieldWithName(const char * name,CompilerType * field_compiler_type_ptr,uint64_t * bit_offset_ptr,uint32_t * bitfield_bit_size_ptr,bool * is_bitfield_ptr) const583 uint32_t CompilerType::GetIndexOfFieldWithName(
584 const char *name, CompilerType *field_compiler_type_ptr,
585 uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr,
586 bool *is_bitfield_ptr) const {
587 unsigned count = GetNumFields();
588 std::string field_name;
589 for (unsigned index = 0; index < count; index++) {
590 CompilerType field_compiler_type(
591 GetFieldAtIndex(index, field_name, bit_offset_ptr,
592 bitfield_bit_size_ptr, is_bitfield_ptr));
593 if (strcmp(field_name.c_str(), name) == 0) {
594 if (field_compiler_type_ptr)
595 *field_compiler_type_ptr = field_compiler_type;
596 return index;
597 }
598 }
599 return UINT32_MAX;
600 }
601
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) const602 CompilerType CompilerType::GetChildCompilerTypeAtIndex(
603 ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
604 bool omit_empty_base_classes, bool ignore_array_bounds,
605 std::string &child_name, uint32_t &child_byte_size,
606 int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size,
607 uint32_t &child_bitfield_bit_offset, bool &child_is_base_class,
608 bool &child_is_deref_of_parent, ValueObject *valobj,
609 uint64_t &language_flags) const {
610 if (!IsValid())
611 return CompilerType();
612 return m_type_system->GetChildCompilerTypeAtIndex(
613 m_type, exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
614 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
615 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
616 child_is_deref_of_parent, valobj, language_flags);
617 }
618
619 // Look for a child member (doesn't include base classes, but it does include
620 // their members) in the type hierarchy. Returns an index path into
621 // "clang_type" on how to reach the appropriate member.
622 //
623 // class A
624 // {
625 // public:
626 // int m_a;
627 // int m_b;
628 // };
629 //
630 // class B
631 // {
632 // };
633 //
634 // class C :
635 // public B,
636 // public A
637 // {
638 // };
639 //
640 // If we have a clang type that describes "class C", and we wanted to looked
641 // "m_b" in it:
642 //
643 // With omit_empty_base_classes == false we would get an integer array back
644 // with: { 1, 1 } The first index 1 is the child index for "class A" within
645 // class C The second index 1 is the child index for "m_b" within class A
646 //
647 // With omit_empty_base_classes == true we would get an integer array back
648 // with: { 0, 1 } The first index 0 is the child index for "class A" within
649 // class C (since class B doesn't have any members it doesn't count) The second
650 // index 1 is the child index for "m_b" within class A
651
GetIndexOfChildMemberWithName(const char * name,bool omit_empty_base_classes,std::vector<uint32_t> & child_indexes) const652 size_t CompilerType::GetIndexOfChildMemberWithName(
653 const char *name, bool omit_empty_base_classes,
654 std::vector<uint32_t> &child_indexes) const {
655 if (IsValid() && name && name[0]) {
656 return m_type_system->GetIndexOfChildMemberWithName(
657 m_type, name, omit_empty_base_classes, child_indexes);
658 }
659 return 0;
660 }
661
GetNumTemplateArguments(bool expand_pack) const662 size_t CompilerType::GetNumTemplateArguments(bool expand_pack) const {
663 if (IsValid()) {
664 return m_type_system->GetNumTemplateArguments(m_type, expand_pack);
665 }
666 return 0;
667 }
668
669 TemplateArgumentKind
GetTemplateArgumentKind(size_t idx,bool expand_pack) const670 CompilerType::GetTemplateArgumentKind(size_t idx, bool expand_pack) const {
671 if (IsValid())
672 return m_type_system->GetTemplateArgumentKind(m_type, idx, expand_pack);
673 return eTemplateArgumentKindNull;
674 }
675
GetTypeTemplateArgument(size_t idx,bool expand_pack) const676 CompilerType CompilerType::GetTypeTemplateArgument(size_t idx,
677 bool expand_pack) const {
678 if (IsValid()) {
679 return m_type_system->GetTypeTemplateArgument(m_type, idx, expand_pack);
680 }
681 return CompilerType();
682 }
683
684 llvm::Optional<CompilerType::IntegralTemplateArgument>
GetIntegralTemplateArgument(size_t idx,bool expand_pack) const685 CompilerType::GetIntegralTemplateArgument(size_t idx, bool expand_pack) const {
686 if (IsValid())
687 return m_type_system->GetIntegralTemplateArgument(m_type, idx, expand_pack);
688 return llvm::None;
689 }
690
GetTypeForFormatters() const691 CompilerType CompilerType::GetTypeForFormatters() const {
692 if (IsValid())
693 return m_type_system->GetTypeForFormatters(m_type);
694 return CompilerType();
695 }
696
ShouldPrintAsOneLiner(ValueObject * valobj) const697 LazyBool CompilerType::ShouldPrintAsOneLiner(ValueObject *valobj) const {
698 if (IsValid())
699 return m_type_system->ShouldPrintAsOneLiner(m_type, valobj);
700 return eLazyBoolCalculate;
701 }
702
IsMeaninglessWithoutDynamicResolution() const703 bool CompilerType::IsMeaninglessWithoutDynamicResolution() const {
704 if (IsValid())
705 return m_type_system->IsMeaninglessWithoutDynamicResolution(m_type);
706 return false;
707 }
708
709 // Get the index of the child of "clang_type" whose name matches. This function
710 // doesn't descend into the children, but only looks one level deep and name
711 // matches can include base class names.
712
713 uint32_t
GetIndexOfChildWithName(const char * name,bool omit_empty_base_classes) const714 CompilerType::GetIndexOfChildWithName(const char *name,
715 bool omit_empty_base_classes) const {
716 if (IsValid() && name && name[0]) {
717 return m_type_system->GetIndexOfChildWithName(m_type, name,
718 omit_empty_base_classes);
719 }
720 return UINT32_MAX;
721 }
722
723 // Dumping types
724
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)725 void CompilerType::DumpValue(ExecutionContext *exe_ctx, Stream *s,
726 lldb::Format format, const DataExtractor &data,
727 lldb::offset_t data_byte_offset,
728 size_t data_byte_size, uint32_t bitfield_bit_size,
729 uint32_t bitfield_bit_offset, bool show_types,
730 bool show_summary, bool verbose, uint32_t depth) {
731 if (!IsValid())
732 return;
733 m_type_system->DumpValue(m_type, exe_ctx, s, format, data, data_byte_offset,
734 data_byte_size, bitfield_bit_size,
735 bitfield_bit_offset, show_types, show_summary,
736 verbose, depth);
737 }
738
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)739 bool CompilerType::DumpTypeValue(Stream *s, lldb::Format format,
740 const DataExtractor &data,
741 lldb::offset_t byte_offset, size_t byte_size,
742 uint32_t bitfield_bit_size,
743 uint32_t bitfield_bit_offset,
744 ExecutionContextScope *exe_scope) {
745 if (!IsValid())
746 return false;
747 return m_type_system->DumpTypeValue(m_type, s, format, data, byte_offset,
748 byte_size, bitfield_bit_size,
749 bitfield_bit_offset, exe_scope);
750 }
751
DumpSummary(ExecutionContext * exe_ctx,Stream * s,const DataExtractor & data,lldb::offset_t data_byte_offset,size_t data_byte_size)752 void CompilerType::DumpSummary(ExecutionContext *exe_ctx, Stream *s,
753 const DataExtractor &data,
754 lldb::offset_t data_byte_offset,
755 size_t data_byte_size) {
756 if (IsValid())
757 m_type_system->DumpSummary(m_type, exe_ctx, s, data, data_byte_offset,
758 data_byte_size);
759 }
760
DumpTypeDescription(lldb::DescriptionLevel level) const761 void CompilerType::DumpTypeDescription(lldb::DescriptionLevel level) const {
762 if (IsValid())
763 m_type_system->DumpTypeDescription(m_type, level);
764 }
765
DumpTypeDescription(Stream * s,lldb::DescriptionLevel level) const766 void CompilerType::DumpTypeDescription(Stream *s,
767 lldb::DescriptionLevel level) const {
768 if (IsValid()) {
769 m_type_system->DumpTypeDescription(m_type, s, level);
770 }
771 }
772
773 #ifndef NDEBUG
dump() const774 LLVM_DUMP_METHOD void CompilerType::dump() const {
775 if (IsValid())
776 m_type_system->dump(m_type);
777 else
778 llvm::errs() << "<invalid>\n";
779 }
780 #endif
781
GetValueAsScalar(const lldb_private::DataExtractor & data,lldb::offset_t data_byte_offset,size_t data_byte_size,Scalar & value,ExecutionContextScope * exe_scope) const782 bool CompilerType::GetValueAsScalar(const lldb_private::DataExtractor &data,
783 lldb::offset_t data_byte_offset,
784 size_t data_byte_size, Scalar &value,
785 ExecutionContextScope *exe_scope) const {
786 if (!IsValid())
787 return false;
788
789 if (IsAggregateType()) {
790 return false; // Aggregate types don't have scalar values
791 } else {
792 uint64_t count = 0;
793 lldb::Encoding encoding = GetEncoding(count);
794
795 if (encoding == lldb::eEncodingInvalid || count != 1)
796 return false;
797
798 llvm::Optional<uint64_t> byte_size = GetByteSize(exe_scope);
799 if (!byte_size)
800 return false;
801 lldb::offset_t offset = data_byte_offset;
802 switch (encoding) {
803 case lldb::eEncodingInvalid:
804 break;
805 case lldb::eEncodingVector:
806 break;
807 case lldb::eEncodingUint:
808 if (*byte_size <= sizeof(unsigned long long)) {
809 uint64_t uval64 = data.GetMaxU64(&offset, *byte_size);
810 if (*byte_size <= sizeof(unsigned int)) {
811 value = (unsigned int)uval64;
812 return true;
813 } else if (*byte_size <= sizeof(unsigned long)) {
814 value = (unsigned long)uval64;
815 return true;
816 } else if (*byte_size <= sizeof(unsigned long long)) {
817 value = (unsigned long long)uval64;
818 return true;
819 } else
820 value.Clear();
821 }
822 break;
823
824 case lldb::eEncodingSint:
825 if (*byte_size <= sizeof(long long)) {
826 int64_t sval64 = data.GetMaxS64(&offset, *byte_size);
827 if (*byte_size <= sizeof(int)) {
828 value = (int)sval64;
829 return true;
830 } else if (*byte_size <= sizeof(long)) {
831 value = (long)sval64;
832 return true;
833 } else if (*byte_size <= sizeof(long long)) {
834 value = (long long)sval64;
835 return true;
836 } else
837 value.Clear();
838 }
839 break;
840
841 case lldb::eEncodingIEEE754:
842 if (*byte_size <= sizeof(long double)) {
843 uint32_t u32;
844 uint64_t u64;
845 if (*byte_size == sizeof(float)) {
846 if (sizeof(float) == sizeof(uint32_t)) {
847 u32 = data.GetU32(&offset);
848 value = *((float *)&u32);
849 return true;
850 } else if (sizeof(float) == sizeof(uint64_t)) {
851 u64 = data.GetU64(&offset);
852 value = *((float *)&u64);
853 return true;
854 }
855 } else if (*byte_size == sizeof(double)) {
856 if (sizeof(double) == sizeof(uint32_t)) {
857 u32 = data.GetU32(&offset);
858 value = *((double *)&u32);
859 return true;
860 } else if (sizeof(double) == sizeof(uint64_t)) {
861 u64 = data.GetU64(&offset);
862 value = *((double *)&u64);
863 return true;
864 }
865 } else if (*byte_size == sizeof(long double)) {
866 if (sizeof(long double) == sizeof(uint32_t)) {
867 u32 = data.GetU32(&offset);
868 value = *((long double *)&u32);
869 return true;
870 } else if (sizeof(long double) == sizeof(uint64_t)) {
871 u64 = data.GetU64(&offset);
872 value = *((long double *)&u64);
873 return true;
874 }
875 }
876 }
877 break;
878 }
879 }
880 return false;
881 }
882
883 #ifndef NDEBUG
Verify() const884 bool CompilerType::Verify() const {
885 return !IsValid() || m_type_system->Verify(m_type);
886 }
887 #endif
888
operator ==(const lldb_private::CompilerType & lhs,const lldb_private::CompilerType & rhs)889 bool lldb_private::operator==(const lldb_private::CompilerType &lhs,
890 const lldb_private::CompilerType &rhs) {
891 return lhs.GetTypeSystem() == rhs.GetTypeSystem() &&
892 lhs.GetOpaqueQualType() == rhs.GetOpaqueQualType();
893 }
894
operator !=(const lldb_private::CompilerType & lhs,const lldb_private::CompilerType & rhs)895 bool lldb_private::operator!=(const lldb_private::CompilerType &lhs,
896 const lldb_private::CompilerType &rhs) {
897 return !(lhs == rhs);
898 }
899