Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / source / Symbol / CompilerType.cpp
blob78cc8dad94a9c5f2062feadf9a2a4c0d8dcc2152
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 //===----------------------------------------------------------------------===//
9 #include "lldb/Symbol/CompilerType.h"
11 #include "lldb/Core/Debugger.h"
12 #include "lldb/Symbol/Type.h"
13 #include "lldb/Target/ExecutionContext.h"
14 #include "lldb/Target/Process.h"
15 #include "lldb/Utility/ConstString.h"
16 #include "lldb/Utility/DataBufferHeap.h"
17 #include "lldb/Utility/DataExtractor.h"
18 #include "lldb/Utility/Scalar.h"
19 #include "lldb/Utility/Stream.h"
20 #include "lldb/Utility/StreamString.h"
22 #include <iterator>
23 #include <mutex>
24 #include <optional>
26 using namespace lldb;
27 using namespace lldb_private;
29 // Tests
31 bool CompilerType::IsAggregateType() const {
32 if (IsValid())
33 if (auto type_system_sp = GetTypeSystem())
34 return type_system_sp->IsAggregateType(m_type);
35 return false;
38 bool CompilerType::IsAnonymousType() const {
39 if (IsValid())
40 if (auto type_system_sp = GetTypeSystem())
41 return type_system_sp->IsAnonymousType(m_type);
42 return false;
45 bool CompilerType::IsScopedEnumerationType() const {
46 if (IsValid())
47 if (auto type_system_sp = GetTypeSystem())
48 return type_system_sp->IsScopedEnumerationType(m_type);
49 return false;
52 bool CompilerType::IsArrayType(CompilerType *element_type_ptr, uint64_t *size,
53 bool *is_incomplete) const {
54 if (IsValid())
55 if (auto type_system_sp = GetTypeSystem())
56 return type_system_sp->IsArrayType(m_type, element_type_ptr, size,
57 is_incomplete);
59 if (element_type_ptr)
60 element_type_ptr->Clear();
61 if (size)
62 *size = 0;
63 if (is_incomplete)
64 *is_incomplete = false;
65 return false;
68 bool CompilerType::IsVectorType(CompilerType *element_type,
69 uint64_t *size) const {
70 if (IsValid())
71 if (auto type_system_sp = GetTypeSystem())
72 return type_system_sp->IsVectorType(m_type, element_type, size);
73 return false;
76 bool CompilerType::IsRuntimeGeneratedType() const {
77 if (IsValid())
78 if (auto type_system_sp = GetTypeSystem())
79 return type_system_sp->IsRuntimeGeneratedType(m_type);
80 return false;
83 bool CompilerType::IsCharType() const {
84 if (IsValid())
85 if (auto type_system_sp = GetTypeSystem())
86 return type_system_sp->IsCharType(m_type);
87 return false;
90 bool CompilerType::IsCompleteType() const {
91 if (IsValid())
92 if (auto type_system_sp = GetTypeSystem())
93 return type_system_sp->IsCompleteType(m_type);
94 return false;
97 bool CompilerType::IsForcefullyCompleted() const {
98 if (IsValid())
99 if (auto type_system_sp = GetTypeSystem())
100 return type_system_sp->IsForcefullyCompleted(m_type);
101 return false;
104 bool CompilerType::IsConst() const {
105 if (IsValid())
106 if (auto type_system_sp = GetTypeSystem())
107 return type_system_sp->IsConst(m_type);
108 return false;
111 bool CompilerType::IsFunctionType() const {
112 if (IsValid())
113 if (auto type_system_sp = GetTypeSystem())
114 return type_system_sp->IsFunctionType(m_type);
115 return false;
118 // Used to detect "Homogeneous Floating-point Aggregates"
119 uint32_t
120 CompilerType::IsHomogeneousAggregate(CompilerType *base_type_ptr) const {
121 if (IsValid())
122 if (auto type_system_sp = GetTypeSystem())
123 return type_system_sp->IsHomogeneousAggregate(m_type, base_type_ptr);
124 return 0;
127 size_t CompilerType::GetNumberOfFunctionArguments() const {
128 if (IsValid())
129 if (auto type_system_sp = GetTypeSystem())
130 return type_system_sp->GetNumberOfFunctionArguments(m_type);
131 return 0;
134 CompilerType
135 CompilerType::GetFunctionArgumentAtIndex(const size_t index) const {
136 if (IsValid())
137 if (auto type_system_sp = GetTypeSystem())
138 return type_system_sp->GetFunctionArgumentAtIndex(m_type, index);
139 return CompilerType();
142 bool CompilerType::IsFunctionPointerType() const {
143 if (IsValid())
144 if (auto type_system_sp = GetTypeSystem())
145 return type_system_sp->IsFunctionPointerType(m_type);
146 return false;
149 bool CompilerType::IsMemberFunctionPointerType() const {
150 if (IsValid())
151 if (auto type_system_sp = GetTypeSystem())
152 return type_system_sp->IsMemberFunctionPointerType(m_type);
153 return false;
156 bool CompilerType::IsBlockPointerType(
157 CompilerType *function_pointer_type_ptr) const {
158 if (IsValid())
159 if (auto type_system_sp = GetTypeSystem())
160 return type_system_sp->IsBlockPointerType(m_type, function_pointer_type_ptr);
161 return false;
164 bool CompilerType::IsIntegerType(bool &is_signed) const {
165 if (IsValid())
166 if (auto type_system_sp = GetTypeSystem())
167 return type_system_sp->IsIntegerType(m_type, is_signed);
168 return false;
171 bool CompilerType::IsEnumerationType(bool &is_signed) const {
172 if (IsValid())
173 if (auto type_system_sp = GetTypeSystem())
174 return type_system_sp->IsEnumerationType(m_type, is_signed);
175 return false;
178 bool CompilerType::IsIntegerOrEnumerationType(bool &is_signed) const {
179 return IsIntegerType(is_signed) || IsEnumerationType(is_signed);
182 bool CompilerType::IsPointerType(CompilerType *pointee_type) const {
183 if (IsValid()) {
184 if (auto type_system_sp = GetTypeSystem())
185 return type_system_sp->IsPointerType(m_type, pointee_type);
187 if (pointee_type)
188 pointee_type->Clear();
189 return false;
192 bool CompilerType::IsPointerOrReferenceType(CompilerType *pointee_type) const {
193 if (IsValid()) {
194 if (auto type_system_sp = GetTypeSystem())
195 return type_system_sp->IsPointerOrReferenceType(m_type, pointee_type);
197 if (pointee_type)
198 pointee_type->Clear();
199 return false;
202 bool CompilerType::IsReferenceType(CompilerType *pointee_type,
203 bool *is_rvalue) const {
204 if (IsValid()) {
205 if (auto type_system_sp = GetTypeSystem())
206 return type_system_sp->IsReferenceType(m_type, pointee_type, is_rvalue);
208 if (pointee_type)
209 pointee_type->Clear();
210 return false;
213 bool CompilerType::ShouldTreatScalarValueAsAddress() const {
214 if (IsValid())
215 if (auto type_system_sp = GetTypeSystem())
216 return type_system_sp->ShouldTreatScalarValueAsAddress(m_type);
217 return false;
220 bool CompilerType::IsFloatingPointType(uint32_t &count,
221 bool &is_complex) const {
222 if (IsValid()) {
223 if (auto type_system_sp = GetTypeSystem())
224 return type_system_sp->IsFloatingPointType(m_type, count, is_complex);
226 count = 0;
227 is_complex = false;
228 return false;
231 bool CompilerType::IsDefined() const {
232 if (IsValid())
233 if (auto type_system_sp = GetTypeSystem())
234 return type_system_sp->IsDefined(m_type);
235 return true;
238 bool CompilerType::IsPolymorphicClass() const {
239 if (IsValid()) {
240 if (auto type_system_sp = GetTypeSystem())
241 return type_system_sp->IsPolymorphicClass(m_type);
243 return false;
246 bool CompilerType::IsPossibleDynamicType(CompilerType *dynamic_pointee_type,
247 bool check_cplusplus,
248 bool check_objc) const {
249 if (IsValid())
250 if (auto type_system_sp = GetTypeSystem())
251 return type_system_sp->IsPossibleDynamicType(m_type, dynamic_pointee_type,
252 check_cplusplus, check_objc);
253 return false;
256 bool CompilerType::IsScalarType() const {
257 if (IsValid())
258 if (auto type_system_sp = GetTypeSystem())
259 return type_system_sp->IsScalarType(m_type);
260 return false;
263 bool CompilerType::IsTemplateType() const {
264 if (IsValid())
265 if (auto type_system_sp = GetTypeSystem())
266 return type_system_sp->IsTemplateType(m_type);
267 return false;
270 bool CompilerType::IsTypedefType() const {
271 if (IsValid())
272 if (auto type_system_sp = GetTypeSystem())
273 return type_system_sp->IsTypedefType(m_type);
274 return false;
277 bool CompilerType::IsVoidType() const {
278 if (IsValid())
279 if (auto type_system_sp = GetTypeSystem())
280 return type_system_sp->IsVoidType(m_type);
281 return false;
284 bool CompilerType::IsPointerToScalarType() const {
285 if (!IsValid())
286 return false;
288 return IsPointerType() && GetPointeeType().IsScalarType();
291 bool CompilerType::IsArrayOfScalarType() const {
292 CompilerType element_type;
293 if (IsArrayType(&element_type))
294 return element_type.IsScalarType();
295 return false;
298 bool CompilerType::IsBeingDefined() const {
299 if (IsValid())
300 if (auto type_system_sp = GetTypeSystem())
301 return type_system_sp->IsBeingDefined(m_type);
302 return false;
305 // Type Completion
307 bool CompilerType::GetCompleteType() const {
308 if (IsValid())
309 if (auto type_system_sp = GetTypeSystem())
310 return type_system_sp->GetCompleteType(m_type);
311 return false;
314 // AST related queries
315 size_t CompilerType::GetPointerByteSize() const {
316 if (auto type_system_sp = GetTypeSystem())
317 return type_system_sp->GetPointerByteSize();
318 return 0;
321 ConstString CompilerType::GetTypeName(bool BaseOnly) const {
322 if (IsValid()) {
323 if (auto type_system_sp = GetTypeSystem())
324 return type_system_sp->GetTypeName(m_type, BaseOnly);
326 return ConstString("<invalid>");
329 ConstString CompilerType::GetDisplayTypeName() const {
330 if (IsValid())
331 if (auto type_system_sp = GetTypeSystem())
332 return type_system_sp->GetDisplayTypeName(m_type);
333 return ConstString("<invalid>");
336 uint32_t CompilerType::GetTypeInfo(
337 CompilerType *pointee_or_element_compiler_type) const {
338 if (IsValid())
339 if (auto type_system_sp = GetTypeSystem())
340 return type_system_sp->GetTypeInfo(m_type,
341 pointee_or_element_compiler_type);
342 return 0;
345 lldb::LanguageType CompilerType::GetMinimumLanguage() {
346 if (IsValid())
347 if (auto type_system_sp = GetTypeSystem())
348 return type_system_sp->GetMinimumLanguage(m_type);
349 return lldb::eLanguageTypeC;
352 lldb::TypeClass CompilerType::GetTypeClass() const {
353 if (IsValid())
354 if (auto type_system_sp = GetTypeSystem())
355 return type_system_sp->GetTypeClass(m_type);
356 return lldb::eTypeClassInvalid;
359 void CompilerType::SetCompilerType(lldb::TypeSystemWP type_system,
360 lldb::opaque_compiler_type_t type) {
361 m_type_system = type_system;
362 m_type = type;
365 void CompilerType::SetCompilerType(CompilerType::TypeSystemSPWrapper type_system,
366 lldb::opaque_compiler_type_t type) {
367 m_type_system = type_system.GetSharedPointer();
368 m_type = type;
371 unsigned CompilerType::GetTypeQualifiers() const {
372 if (IsValid())
373 if (auto type_system_sp = GetTypeSystem())
374 return type_system_sp->GetTypeQualifiers(m_type);
375 return 0;
378 // Creating related types
380 CompilerType
381 CompilerType::GetArrayElementType(ExecutionContextScope *exe_scope) const {
382 if (IsValid()) {
383 if (auto type_system_sp = GetTypeSystem())
384 return type_system_sp->GetArrayElementType(m_type, exe_scope);
386 return CompilerType();
389 CompilerType CompilerType::GetArrayType(uint64_t size) const {
390 if (IsValid()) {
391 if (auto type_system_sp = GetTypeSystem())
392 return type_system_sp->GetArrayType(m_type, size);
394 return CompilerType();
397 CompilerType CompilerType::GetCanonicalType() const {
398 if (IsValid())
399 if (auto type_system_sp = GetTypeSystem())
400 return type_system_sp->GetCanonicalType(m_type);
401 return CompilerType();
404 CompilerType CompilerType::GetFullyUnqualifiedType() const {
405 if (IsValid())
406 if (auto type_system_sp = GetTypeSystem())
407 return type_system_sp->GetFullyUnqualifiedType(m_type);
408 return CompilerType();
411 CompilerType CompilerType::GetEnumerationIntegerType() const {
412 if (IsValid())
413 if (auto type_system_sp = GetTypeSystem())
414 return type_system_sp->GetEnumerationIntegerType(m_type);
415 return CompilerType();
418 int CompilerType::GetFunctionArgumentCount() const {
419 if (IsValid()) {
420 if (auto type_system_sp = GetTypeSystem())
421 return type_system_sp->GetFunctionArgumentCount(m_type);
423 return -1;
426 CompilerType CompilerType::GetFunctionArgumentTypeAtIndex(size_t idx) const {
427 if (IsValid()) {
428 if (auto type_system_sp = GetTypeSystem())
429 return type_system_sp->GetFunctionArgumentTypeAtIndex(m_type, idx);
431 return CompilerType();
434 CompilerType CompilerType::GetFunctionReturnType() const {
435 if (IsValid()) {
436 if (auto type_system_sp = GetTypeSystem())
437 return type_system_sp->GetFunctionReturnType(m_type);
439 return CompilerType();
442 size_t CompilerType::GetNumMemberFunctions() const {
443 if (IsValid()) {
444 if (auto type_system_sp = GetTypeSystem())
445 return type_system_sp->GetNumMemberFunctions(m_type);
447 return 0;
450 TypeMemberFunctionImpl CompilerType::GetMemberFunctionAtIndex(size_t idx) {
451 if (IsValid()) {
452 if (auto type_system_sp = GetTypeSystem())
453 return type_system_sp->GetMemberFunctionAtIndex(m_type, idx);
455 return TypeMemberFunctionImpl();
458 CompilerType CompilerType::GetNonReferenceType() const {
459 if (IsValid())
460 if (auto type_system_sp = GetTypeSystem())
461 return type_system_sp->GetNonReferenceType(m_type);
462 return CompilerType();
465 CompilerType CompilerType::GetPointeeType() const {
466 if (IsValid()) {
467 if (auto type_system_sp = GetTypeSystem())
468 return type_system_sp->GetPointeeType(m_type);
470 return CompilerType();
473 CompilerType CompilerType::GetPointerType() const {
474 if (IsValid()) {
475 if (auto type_system_sp = GetTypeSystem())
476 return type_system_sp->GetPointerType(m_type);
478 return CompilerType();
481 CompilerType CompilerType::GetLValueReferenceType() const {
482 if (IsValid())
483 if (auto type_system_sp = GetTypeSystem())
484 return type_system_sp->GetLValueReferenceType(m_type);
485 return CompilerType();
488 CompilerType CompilerType::GetRValueReferenceType() const {
489 if (IsValid())
490 if (auto type_system_sp = GetTypeSystem())
491 return type_system_sp->GetRValueReferenceType(m_type);
492 return CompilerType();
495 CompilerType CompilerType::GetAtomicType() const {
496 if (IsValid())
497 if (auto type_system_sp = GetTypeSystem())
498 return type_system_sp->GetAtomicType(m_type);
499 return CompilerType();
502 CompilerType CompilerType::AddConstModifier() const {
503 if (IsValid())
504 if (auto type_system_sp = GetTypeSystem())
505 return type_system_sp->AddConstModifier(m_type);
506 return CompilerType();
509 CompilerType CompilerType::AddVolatileModifier() const {
510 if (IsValid())
511 if (auto type_system_sp = GetTypeSystem())
512 return type_system_sp->AddVolatileModifier(m_type);
513 return CompilerType();
516 CompilerType CompilerType::AddRestrictModifier() const {
517 if (IsValid())
518 if (auto type_system_sp = GetTypeSystem())
519 return type_system_sp->AddRestrictModifier(m_type);
520 return CompilerType();
523 CompilerType CompilerType::CreateTypedef(const char *name,
524 const CompilerDeclContext &decl_ctx,
525 uint32_t payload) const {
526 if (IsValid())
527 if (auto type_system_sp = GetTypeSystem())
528 return type_system_sp->CreateTypedef(m_type, name, decl_ctx, payload);
529 return CompilerType();
532 CompilerType CompilerType::GetTypedefedType() const {
533 if (IsValid())
534 if (auto type_system_sp = GetTypeSystem())
535 return type_system_sp->GetTypedefedType(m_type);
536 return CompilerType();
539 // Create related types using the current type's AST
541 CompilerType
542 CompilerType::GetBasicTypeFromAST(lldb::BasicType basic_type) const {
543 if (IsValid())
544 if (auto type_system_sp = GetTypeSystem())
545 return type_system_sp->GetBasicTypeFromAST(basic_type);
546 return CompilerType();
548 // Exploring the type
550 std::optional<uint64_t>
551 CompilerType::GetBitSize(ExecutionContextScope *exe_scope) const {
552 if (IsValid())
553 if (auto type_system_sp = GetTypeSystem())
554 return type_system_sp->GetBitSize(m_type, exe_scope);
555 return {};
558 std::optional<uint64_t>
559 CompilerType::GetByteSize(ExecutionContextScope *exe_scope) const {
560 if (std::optional<uint64_t> bit_size = GetBitSize(exe_scope))
561 return (*bit_size + 7) / 8;
562 return {};
565 std::optional<size_t>
566 CompilerType::GetTypeBitAlign(ExecutionContextScope *exe_scope) const {
567 if (IsValid())
568 if (auto type_system_sp = GetTypeSystem())
569 return type_system_sp->GetTypeBitAlign(m_type, exe_scope);
570 return {};
573 lldb::Encoding CompilerType::GetEncoding(uint64_t &count) const {
574 if (IsValid())
575 if (auto type_system_sp = GetTypeSystem())
576 return type_system_sp->GetEncoding(m_type, count);
577 return lldb::eEncodingInvalid;
580 lldb::Format CompilerType::GetFormat() const {
581 if (IsValid())
582 if (auto type_system_sp = GetTypeSystem())
583 return type_system_sp->GetFormat(m_type);
584 return lldb::eFormatDefault;
587 uint32_t CompilerType::GetNumChildren(bool omit_empty_base_classes,
588 const ExecutionContext *exe_ctx) const {
589 if (IsValid())
590 if (auto type_system_sp = GetTypeSystem())
591 return type_system_sp->GetNumChildren(m_type, omit_empty_base_classes,
592 exe_ctx);
593 return 0;
596 lldb::BasicType CompilerType::GetBasicTypeEnumeration() const {
597 if (IsValid())
598 if (auto type_system_sp = GetTypeSystem())
599 return type_system_sp->GetBasicTypeEnumeration(m_type);
600 return eBasicTypeInvalid;
603 void CompilerType::ForEachEnumerator(
604 std::function<bool(const CompilerType &integer_type,
605 ConstString name,
606 const llvm::APSInt &value)> const &callback) const {
607 if (IsValid())
608 if (auto type_system_sp = GetTypeSystem())
609 return type_system_sp->ForEachEnumerator(m_type, callback);
612 uint32_t CompilerType::GetNumFields() const {
613 if (IsValid())
614 if (auto type_system_sp = GetTypeSystem())
615 return type_system_sp->GetNumFields(m_type);
616 return 0;
619 CompilerType CompilerType::GetFieldAtIndex(size_t idx, std::string &name,
620 uint64_t *bit_offset_ptr,
621 uint32_t *bitfield_bit_size_ptr,
622 bool *is_bitfield_ptr) const {
623 if (IsValid())
624 if (auto type_system_sp = GetTypeSystem())
625 return type_system_sp->GetFieldAtIndex(m_type, idx, name, bit_offset_ptr,
626 bitfield_bit_size_ptr, is_bitfield_ptr);
627 return CompilerType();
630 uint32_t CompilerType::GetNumDirectBaseClasses() const {
631 if (IsValid())
632 if (auto type_system_sp = GetTypeSystem())
633 return type_system_sp->GetNumDirectBaseClasses(m_type);
634 return 0;
637 uint32_t CompilerType::GetNumVirtualBaseClasses() const {
638 if (IsValid())
639 if (auto type_system_sp = GetTypeSystem())
640 return type_system_sp->GetNumVirtualBaseClasses(m_type);
641 return 0;
644 CompilerType
645 CompilerType::GetDirectBaseClassAtIndex(size_t idx,
646 uint32_t *bit_offset_ptr) const {
647 if (IsValid())
648 if (auto type_system_sp = GetTypeSystem())
649 return type_system_sp->GetDirectBaseClassAtIndex(m_type, idx,
650 bit_offset_ptr);
651 return CompilerType();
654 CompilerType
655 CompilerType::GetVirtualBaseClassAtIndex(size_t idx,
656 uint32_t *bit_offset_ptr) const {
657 if (IsValid())
658 if (auto type_system_sp = GetTypeSystem())
659 return type_system_sp->GetVirtualBaseClassAtIndex(m_type, idx,
660 bit_offset_ptr);
661 return CompilerType();
664 uint32_t CompilerType::GetIndexOfFieldWithName(
665 const char *name, CompilerType *field_compiler_type_ptr,
666 uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr,
667 bool *is_bitfield_ptr) const {
668 unsigned count = GetNumFields();
669 std::string field_name;
670 for (unsigned index = 0; index < count; index++) {
671 CompilerType field_compiler_type(
672 GetFieldAtIndex(index, field_name, bit_offset_ptr,
673 bitfield_bit_size_ptr, is_bitfield_ptr));
674 if (strcmp(field_name.c_str(), name) == 0) {
675 if (field_compiler_type_ptr)
676 *field_compiler_type_ptr = field_compiler_type;
677 return index;
680 return UINT32_MAX;
683 CompilerType CompilerType::GetChildCompilerTypeAtIndex(
684 ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
685 bool omit_empty_base_classes, bool ignore_array_bounds,
686 std::string &child_name, uint32_t &child_byte_size,
687 int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size,
688 uint32_t &child_bitfield_bit_offset, bool &child_is_base_class,
689 bool &child_is_deref_of_parent, ValueObject *valobj,
690 uint64_t &language_flags) const {
691 if (IsValid())
692 if (auto type_system_sp = GetTypeSystem())
693 return type_system_sp->GetChildCompilerTypeAtIndex(
694 m_type, exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
695 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
696 child_bitfield_bit_size, child_bitfield_bit_offset,
697 child_is_base_class, child_is_deref_of_parent, valobj,
698 language_flags);
699 return CompilerType();
702 // Look for a child member (doesn't include base classes, but it does include
703 // their members) in the type hierarchy. Returns an index path into
704 // "clang_type" on how to reach the appropriate member.
706 // class A
707 // {
708 // public:
709 // int m_a;
710 // int m_b;
711 // };
713 // class B
714 // {
715 // };
717 // class C :
718 // public B,
719 // public A
720 // {
721 // };
723 // If we have a clang type that describes "class C", and we wanted to looked
724 // "m_b" in it:
726 // With omit_empty_base_classes == false we would get an integer array back
727 // with: { 1, 1 } The first index 1 is the child index for "class A" within
728 // class C The second index 1 is the child index for "m_b" within class A
730 // With omit_empty_base_classes == true we would get an integer array back
731 // with: { 0, 1 } The first index 0 is the child index for "class A" within
732 // class C (since class B doesn't have any members it doesn't count) The second
733 // index 1 is the child index for "m_b" within class A
735 size_t CompilerType::GetIndexOfChildMemberWithName(
736 llvm::StringRef name, bool omit_empty_base_classes,
737 std::vector<uint32_t> &child_indexes) const {
738 if (IsValid() && !name.empty()) {
739 if (auto type_system_sp = GetTypeSystem())
740 return type_system_sp->GetIndexOfChildMemberWithName(
741 m_type, name, omit_empty_base_classes, child_indexes);
743 return 0;
746 size_t CompilerType::GetNumTemplateArguments(bool expand_pack) const {
747 if (IsValid()) {
748 if (auto type_system_sp = GetTypeSystem())
749 return type_system_sp->GetNumTemplateArguments(m_type, expand_pack);
751 return 0;
754 TemplateArgumentKind
755 CompilerType::GetTemplateArgumentKind(size_t idx, bool expand_pack) const {
756 if (IsValid())
757 if (auto type_system_sp = GetTypeSystem())
758 return type_system_sp->GetTemplateArgumentKind(m_type, idx, expand_pack);
759 return eTemplateArgumentKindNull;
762 CompilerType CompilerType::GetTypeTemplateArgument(size_t idx,
763 bool expand_pack) const {
764 if (IsValid()) {
765 if (auto type_system_sp = GetTypeSystem())
766 return type_system_sp->GetTypeTemplateArgument(m_type, idx, expand_pack);
768 return CompilerType();
771 std::optional<CompilerType::IntegralTemplateArgument>
772 CompilerType::GetIntegralTemplateArgument(size_t idx, bool expand_pack) const {
773 if (IsValid())
774 if (auto type_system_sp = GetTypeSystem())
775 return type_system_sp->GetIntegralTemplateArgument(m_type, idx, expand_pack);
776 return std::nullopt;
779 CompilerType CompilerType::GetTypeForFormatters() const {
780 if (IsValid())
781 if (auto type_system_sp = GetTypeSystem())
782 return type_system_sp->GetTypeForFormatters(m_type);
783 return CompilerType();
786 LazyBool CompilerType::ShouldPrintAsOneLiner(ValueObject *valobj) const {
787 if (IsValid())
788 if (auto type_system_sp = GetTypeSystem())
789 return type_system_sp->ShouldPrintAsOneLiner(m_type, valobj);
790 return eLazyBoolCalculate;
793 bool CompilerType::IsMeaninglessWithoutDynamicResolution() const {
794 if (IsValid())
795 if (auto type_system_sp = GetTypeSystem())
796 return type_system_sp->IsMeaninglessWithoutDynamicResolution(m_type);
797 return false;
800 // Get the index of the child of "clang_type" whose name matches. This function
801 // doesn't descend into the children, but only looks one level deep and name
802 // matches can include base class names.
804 uint32_t
805 CompilerType::GetIndexOfChildWithName(llvm::StringRef name,
806 bool omit_empty_base_classes) const {
807 if (IsValid() && !name.empty()) {
808 if (auto type_system_sp = GetTypeSystem())
809 return type_system_sp->GetIndexOfChildWithName(m_type, name,
810 omit_empty_base_classes);
812 return UINT32_MAX;
815 // Dumping types
817 bool CompilerType::DumpTypeValue(Stream *s, lldb::Format format,
818 const DataExtractor &data,
819 lldb::offset_t byte_offset, size_t byte_size,
820 uint32_t bitfield_bit_size,
821 uint32_t bitfield_bit_offset,
822 ExecutionContextScope *exe_scope) {
823 if (IsValid())
824 if (auto type_system_sp = GetTypeSystem())
825 return type_system_sp->DumpTypeValue(
826 m_type, *s, format, data, byte_offset, byte_size, bitfield_bit_size,
827 bitfield_bit_offset, exe_scope);
828 return false;
831 void CompilerType::DumpTypeDescription(lldb::DescriptionLevel level) const {
832 if (IsValid())
833 if (auto type_system_sp = GetTypeSystem())
834 type_system_sp->DumpTypeDescription(m_type, level);
837 void CompilerType::DumpTypeDescription(Stream *s,
838 lldb::DescriptionLevel level) const {
839 if (IsValid())
840 if (auto type_system_sp = GetTypeSystem())
841 type_system_sp->DumpTypeDescription(m_type, *s, level);
844 #ifndef NDEBUG
845 LLVM_DUMP_METHOD void CompilerType::dump() const {
846 if (IsValid())
847 if (auto type_system_sp = GetTypeSystem())
848 return type_system_sp->dump(m_type);
849 llvm::errs() << "<invalid>\n";
851 #endif
853 bool CompilerType::GetValueAsScalar(const lldb_private::DataExtractor &data,
854 lldb::offset_t data_byte_offset,
855 size_t data_byte_size, Scalar &value,
856 ExecutionContextScope *exe_scope) const {
857 if (!IsValid())
858 return false;
860 if (IsAggregateType()) {
861 return false; // Aggregate types don't have scalar values
862 } else {
863 uint64_t count = 0;
864 lldb::Encoding encoding = GetEncoding(count);
866 if (encoding == lldb::eEncodingInvalid || count != 1)
867 return false;
869 std::optional<uint64_t> byte_size = GetByteSize(exe_scope);
870 if (!byte_size)
871 return false;
872 lldb::offset_t offset = data_byte_offset;
873 switch (encoding) {
874 case lldb::eEncodingInvalid:
875 break;
876 case lldb::eEncodingVector:
877 break;
878 case lldb::eEncodingUint:
879 if (*byte_size <= sizeof(unsigned long long)) {
880 uint64_t uval64 = data.GetMaxU64(&offset, *byte_size);
881 if (*byte_size <= sizeof(unsigned int)) {
882 value = (unsigned int)uval64;
883 return true;
884 } else if (*byte_size <= sizeof(unsigned long)) {
885 value = (unsigned long)uval64;
886 return true;
887 } else if (*byte_size <= sizeof(unsigned long long)) {
888 value = (unsigned long long)uval64;
889 return true;
890 } else
891 value.Clear();
893 break;
895 case lldb::eEncodingSint:
896 if (*byte_size <= sizeof(long long)) {
897 int64_t sval64 = data.GetMaxS64(&offset, *byte_size);
898 if (*byte_size <= sizeof(int)) {
899 value = (int)sval64;
900 return true;
901 } else if (*byte_size <= sizeof(long)) {
902 value = (long)sval64;
903 return true;
904 } else if (*byte_size <= sizeof(long long)) {
905 value = (long long)sval64;
906 return true;
907 } else
908 value.Clear();
910 break;
912 case lldb::eEncodingIEEE754:
913 if (*byte_size <= sizeof(long double)) {
914 uint32_t u32;
915 uint64_t u64;
916 if (*byte_size == sizeof(float)) {
917 if (sizeof(float) == sizeof(uint32_t)) {
918 u32 = data.GetU32(&offset);
919 value = *((float *)&u32);
920 return true;
921 } else if (sizeof(float) == sizeof(uint64_t)) {
922 u64 = data.GetU64(&offset);
923 value = *((float *)&u64);
924 return true;
926 } else if (*byte_size == sizeof(double)) {
927 if (sizeof(double) == sizeof(uint32_t)) {
928 u32 = data.GetU32(&offset);
929 value = *((double *)&u32);
930 return true;
931 } else if (sizeof(double) == sizeof(uint64_t)) {
932 u64 = data.GetU64(&offset);
933 value = *((double *)&u64);
934 return true;
936 } else if (*byte_size == sizeof(long double)) {
937 if (sizeof(long double) == sizeof(uint32_t)) {
938 u32 = data.GetU32(&offset);
939 value = *((long double *)&u32);
940 return true;
941 } else if (sizeof(long double) == sizeof(uint64_t)) {
942 u64 = data.GetU64(&offset);
943 value = *((long double *)&u64);
944 return true;
948 break;
951 return false;
954 CompilerType::CompilerType(CompilerType::TypeSystemSPWrapper type_system,
955 lldb::opaque_compiler_type_t type)
956 : m_type_system(type_system.GetSharedPointer()), m_type(type) {
957 assert(Verify() && "verification failed");
960 CompilerType::CompilerType(lldb::TypeSystemWP type_system,
961 lldb::opaque_compiler_type_t type)
962 : m_type_system(type_system), m_type(type) {
963 assert(Verify() && "verification failed");
966 #ifndef NDEBUG
967 bool CompilerType::Verify() const {
968 if (!IsValid())
969 return true;
970 if (auto type_system_sp = GetTypeSystem())
971 return type_system_sp->Verify(m_type);
972 return true;
974 #endif
976 CompilerType::TypeSystemSPWrapper CompilerType::GetTypeSystem() const {
977 return {m_type_system.lock()};
980 bool CompilerType::TypeSystemSPWrapper::operator==(
981 const CompilerType::TypeSystemSPWrapper &other) const {
982 if (!m_typesystem_sp && !other.m_typesystem_sp)
983 return true;
984 if (m_typesystem_sp && other.m_typesystem_sp)
985 return m_typesystem_sp.get() == other.m_typesystem_sp.get();
986 return false;
989 TypeSystem *CompilerType::TypeSystemSPWrapper::operator->() const {
990 assert(m_typesystem_sp);
991 return m_typesystem_sp.get();
994 bool lldb_private::operator==(const lldb_private::CompilerType &lhs,
995 const lldb_private::CompilerType &rhs) {
996 return lhs.GetTypeSystem() == rhs.GetTypeSystem() &&
997 lhs.GetOpaqueQualType() == rhs.GetOpaqueQualType();
1000 bool lldb_private::operator!=(const lldb_private::CompilerType &lhs,
1001 const lldb_private::CompilerType &rhs) {
1002 return !(lhs == rhs);