LLDB mainline
CompilerType.cpp
Go to the documentation of this file.
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
10
11#include "lldb/Core/Debugger.h"
12#include "lldb/Symbol/Type.h"
14#include "lldb/Target/Process.h"
18#include "lldb/Utility/Scalar.h"
19#include "lldb/Utility/Stream.h"
21
22#include <iterator>
23#include <mutex>
24#include <optional>
25
26using namespace lldb;
27using namespace lldb_private;
28
29// Tests
30
32 if (IsValid())
33 if (auto type_system_sp = GetTypeSystem())
34 return type_system_sp->IsAggregateType(m_type);
35 return false;
36}
37
39 if (IsValid())
40 if (auto type_system_sp = GetTypeSystem())
41 return type_system_sp->IsAnonymousType(m_type);
42 return false;
43}
44
46 if (IsValid())
47 if (auto type_system_sp = GetTypeSystem())
48 return type_system_sp->IsScopedEnumerationType(m_type);
49 return false;
50}
51
52bool 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);
58
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;
66}
67
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;
74}
75
77 if (IsValid())
78 if (auto type_system_sp = GetTypeSystem())
79 return type_system_sp->IsRuntimeGeneratedType(m_type);
80 return false;
81}
82
84 if (IsValid())
85 if (auto type_system_sp = GetTypeSystem())
86 return type_system_sp->IsCharType(m_type);
87 return false;
88}
89
91 if (IsValid())
92 if (auto type_system_sp = GetTypeSystem())
93 return type_system_sp->IsCompleteType(m_type);
94 return false;
95}
96
98 if (IsValid())
99 if (auto type_system_sp = GetTypeSystem())
100 return type_system_sp->IsForcefullyCompleted(m_type);
101 return false;
102}
103
105 if (IsValid())
106 if (auto type_system_sp = GetTypeSystem())
107 return type_system_sp->IsConst(m_type);
108 return false;
109}
110
112 if (IsValid())
113 if (auto type_system_sp = GetTypeSystem())
114 return type_system_sp->IsFunctionType(m_type);
115 return false;
116}
117
118// Used to detect "Homogeneous Floating-point Aggregates"
119uint32_t
121 if (IsValid())
122 if (auto type_system_sp = GetTypeSystem())
123 return type_system_sp->IsHomogeneousAggregate(m_type, base_type_ptr);
124 return 0;
125}
126
128 if (IsValid())
129 if (auto type_system_sp = GetTypeSystem())
130 return type_system_sp->GetNumberOfFunctionArguments(m_type);
131 return 0;
132}
133
136 if (IsValid())
137 if (auto type_system_sp = GetTypeSystem())
138 return type_system_sp->GetFunctionArgumentAtIndex(m_type, index);
139 return CompilerType();
140}
141
143 if (IsValid())
144 if (auto type_system_sp = GetTypeSystem())
145 return type_system_sp->IsFunctionPointerType(m_type);
146 return false;
147}
148
150 if (IsValid())
151 if (auto type_system_sp = GetTypeSystem())
152 return type_system_sp->IsMemberFunctionPointerType(m_type);
153 return false;
154}
155
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;
162}
163
164bool 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;
169}
170
171bool 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;
176}
177
178bool CompilerType::IsIntegerOrEnumerationType(bool &is_signed) const {
179 return IsIntegerType(is_signed) || IsEnumerationType(is_signed);
180}
181
182bool 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);
186 }
187 if (pointee_type)
188 pointee_type->Clear();
189 return false;
190}
191
193 if (IsValid()) {
194 if (auto type_system_sp = GetTypeSystem())
195 return type_system_sp->IsPointerOrReferenceType(m_type, pointee_type);
196 }
197 if (pointee_type)
198 pointee_type->Clear();
199 return false;
200}
201
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);
207 }
208 if (pointee_type)
209 pointee_type->Clear();
210 return false;
211}
212
214 if (IsValid())
215 if (auto type_system_sp = GetTypeSystem())
216 return type_system_sp->ShouldTreatScalarValueAsAddress(m_type);
217 return false;
218}
219
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);
225 }
226 count = 0;
227 is_complex = false;
228 return false;
229}
230
232 if (IsValid())
233 if (auto type_system_sp = GetTypeSystem())
234 return type_system_sp->IsDefined(m_type);
235 return true;
236}
237
239 if (IsValid()) {
240 if (auto type_system_sp = GetTypeSystem())
241 return type_system_sp->IsPolymorphicClass(m_type);
242 }
243 return false;
244}
245
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;
254}
255
257 if (IsValid())
258 if (auto type_system_sp = GetTypeSystem())
259 return type_system_sp->IsScalarType(m_type);
260 return false;
261}
262
264 if (IsValid())
265 if (auto type_system_sp = GetTypeSystem())
266 return type_system_sp->IsTemplateType(m_type);
267 return false;
268}
269
271 if (IsValid())
272 if (auto type_system_sp = GetTypeSystem())
273 return type_system_sp->IsTypedefType(m_type);
274 return false;
275}
276
278 if (IsValid())
279 if (auto type_system_sp = GetTypeSystem())
280 return type_system_sp->IsVoidType(m_type);
281 return false;
282}
283
285 if (!IsValid())
286 return false;
287
289}
290
292 CompilerType element_type;
293 if (IsArrayType(&element_type))
294 return element_type.IsScalarType();
295 return false;
296}
297
299 if (IsValid())
300 if (auto type_system_sp = GetTypeSystem())
301 return type_system_sp->IsBeingDefined(m_type);
302 return false;
303}
304
306 bool is_signed = false; // May be reset by the call below.
307 return IsIntegerType(is_signed);
308}
309
311 uint32_t count = 0;
312 bool is_complex = false;
313 return IsFloatingPointType(count, is_complex);
314}
315
317 bool is_signed = false; // May be reset by the call below.
318 return IsEnumerationType(is_signed);
319}
320
323}
324
327}
328
330 return GetTypeInfo() & lldb::eTypeIsSigned;
331}
332
336}
337
340}
341
343 if (IsValid())
344 return GetEnumerationIntegerType().GetTypeInfo() & lldb::eTypeIsSigned;
345
346 return false;
347}
348
351}
352
354 // Unscoped enums are always considered as promotable, even if their
355 // underlying type does not need to be promoted (e.g. "int").
357 return true;
358
371 return true;
372
373 default:
374 return false;
375 }
376
377 llvm_unreachable("All cases handled above.");
378}
379
381 if (!IsValid())
382 return false;
383
384 return IsPointerType() &&
386}
387
389 if (!IsValid())
390 return false;
391
392 return GetCanonicalType().GetTypeClass() &
393 (lldb::eTypeClassClass | lldb::eTypeClassStruct |
394 lldb::eTypeClassUnion);
395}
396
398 CompilerType *virtual_base,
399 bool carry_virtual) const {
400 if (CompareTypes(target_base))
401 return carry_virtual;
402
403 if (!carry_virtual) {
404 uint32_t num_virtual_bases = GetNumVirtualBaseClasses();
405 for (uint32_t i = 0; i < num_virtual_bases; ++i) {
406 uint32_t bit_offset;
407 auto base = GetVirtualBaseClassAtIndex(i, &bit_offset);
408 if (base.IsVirtualBase(target_base, virtual_base,
409 /*carry_virtual*/ true)) {
410 if (virtual_base)
411 *virtual_base = base;
412
413 return true;
414 }
415 }
416 }
417
418 uint32_t num_direct_bases = GetNumDirectBaseClasses();
419 for (uint32_t i = 0; i < num_direct_bases; ++i) {
420 uint32_t bit_offset;
421 auto base = GetDirectBaseClassAtIndex(i, &bit_offset);
422 if (base.IsVirtualBase(target_base, virtual_base, carry_virtual))
423 return true;
424 }
425
426 return false;
427}
428
432}
433
437}
438
440 auto name = GetTypeName();
441 auto canonical_name = GetCanonicalType().GetTypeName();
442 if (name.IsEmpty() || canonical_name.IsEmpty())
443 return "''"; // Should not happen, unless the input is broken somehow.
444
445 if (name == canonical_name)
446 return llvm::formatv("'{0}'", name);
447
448 return llvm::formatv("'{0}' (canonically referred to as '{1}')", name,
449 canonical_name);
450}
451
453 if (*this == rhs)
454 return true;
455
457 const ConstString rhs_name = rhs.GetFullyUnqualifiedType().GetTypeName();
458 return name == rhs_name;
459}
460
462 switch (GetTypeClass()) {
463 case lldb::eTypeClassClass:
464 return "class";
465 case lldb::eTypeClassEnumeration:
466 return "enum";
467 case lldb::eTypeClassStruct:
468 return "struct";
469 case lldb::eTypeClassUnion:
470 return "union";
471 default:
472 return "unknown";
473 }
474 llvm_unreachable("All cases are covered by code above.");
475}
476
478 uint32_t ret = 0;
479 uint32_t num_direct_bases = GetNumDirectBaseClasses();
480
481 for (uint32_t i = 0; i < num_direct_bases; ++i) {
482 uint32_t bit_offset;
483 CompilerType base_type = GetDirectBaseClassAtIndex(i, &bit_offset);
484 if (base_type.GetNumFields() > 0 ||
485 base_type.GetNumberOfNonEmptyBaseClasses() > 0)
486 ret += 1;
487 }
488 return ret;
489}
490
491// Type Completion
492
494 if (IsValid())
495 if (auto type_system_sp = GetTypeSystem())
496 return type_system_sp->GetCompleteType(m_type);
497 return false;
498}
499
500// AST related queries
502 if (auto type_system_sp = GetTypeSystem())
503 return type_system_sp->GetPointerByteSize();
504 return 0;
505}
506
508 if (IsValid()) {
509 if (auto type_system_sp = GetTypeSystem())
510 return type_system_sp->GetTypeName(m_type, BaseOnly);
511 }
512 return ConstString("<invalid>");
513}
514
516 if (IsValid())
517 if (auto type_system_sp = GetTypeSystem())
518 return type_system_sp->GetDisplayTypeName(m_type);
519 return ConstString("<invalid>");
520}
521
523 CompilerType *pointee_or_element_compiler_type) const {
524 if (IsValid())
525 if (auto type_system_sp = GetTypeSystem())
526 return type_system_sp->GetTypeInfo(m_type,
527 pointee_or_element_compiler_type);
528 return 0;
529}
530
532 if (IsValid())
533 if (auto type_system_sp = GetTypeSystem())
534 return type_system_sp->GetMinimumLanguage(m_type);
536}
537
538lldb::TypeClass CompilerType::GetTypeClass() const {
539 if (IsValid())
540 if (auto type_system_sp = GetTypeSystem())
541 return type_system_sp->GetTypeClass(m_type);
542 return lldb::eTypeClassInvalid;
543}
544
547 m_type_system = type_system;
548 m_type = type;
549}
550
553 m_type_system = type_system.GetSharedPointer();
554 m_type = type;
555}
556
558 if (IsValid())
559 if (auto type_system_sp = GetTypeSystem())
560 return type_system_sp->GetTypeQualifiers(m_type);
561 return 0;
562}
563
564// Creating related types
565
568 if (IsValid()) {
569 if (auto type_system_sp = GetTypeSystem())
570 return type_system_sp->GetArrayElementType(m_type, exe_scope);
571 }
572 return CompilerType();
573}
574
576 if (IsValid()) {
577 if (auto type_system_sp = GetTypeSystem())
578 return type_system_sp->GetArrayType(m_type, size);
579 }
580 return CompilerType();
581}
582
584 if (IsValid())
585 if (auto type_system_sp = GetTypeSystem())
586 return type_system_sp->GetCanonicalType(m_type);
587 return CompilerType();
588}
589
591 if (IsValid())
592 if (auto type_system_sp = GetTypeSystem())
593 return type_system_sp->GetFullyUnqualifiedType(m_type);
594 return CompilerType();
595}
596
598 if (IsValid())
599 if (auto type_system_sp = GetTypeSystem())
600 return type_system_sp->GetEnumerationIntegerType(m_type);
601 return CompilerType();
602}
603
605 if (IsValid()) {
606 if (auto type_system_sp = GetTypeSystem())
607 return type_system_sp->GetFunctionArgumentCount(m_type);
608 }
609 return -1;
610}
611
613 if (IsValid()) {
614 if (auto type_system_sp = GetTypeSystem())
615 return type_system_sp->GetFunctionArgumentTypeAtIndex(m_type, idx);
616 }
617 return CompilerType();
618}
619
621 if (IsValid()) {
622 if (auto type_system_sp = GetTypeSystem())
623 return type_system_sp->GetFunctionReturnType(m_type);
624 }
625 return CompilerType();
626}
627
629 if (IsValid()) {
630 if (auto type_system_sp = GetTypeSystem())
631 return type_system_sp->GetNumMemberFunctions(m_type);
632 }
633 return 0;
634}
635
637 if (IsValid()) {
638 if (auto type_system_sp = GetTypeSystem())
639 return type_system_sp->GetMemberFunctionAtIndex(m_type, idx);
640 }
641 return TypeMemberFunctionImpl();
642}
643
645 if (IsValid())
646 if (auto type_system_sp = GetTypeSystem())
647 return type_system_sp->GetNonReferenceType(m_type);
648 return CompilerType();
649}
650
652 if (IsValid()) {
653 if (auto type_system_sp = GetTypeSystem())
654 return type_system_sp->GetPointeeType(m_type);
655 }
656 return CompilerType();
657}
658
660 if (IsValid()) {
661 if (auto type_system_sp = GetTypeSystem())
662 return type_system_sp->GetPointerType(m_type);
663 }
664 return CompilerType();
665}
666
668 if (IsValid())
669 if (auto type_system_sp = GetTypeSystem())
670 return type_system_sp->GetLValueReferenceType(m_type);
671 return CompilerType();
672}
673
675 if (IsValid())
676 if (auto type_system_sp = GetTypeSystem())
677 return type_system_sp->GetRValueReferenceType(m_type);
678 return CompilerType();
679}
680
682 if (IsValid())
683 if (auto type_system_sp = GetTypeSystem())
684 return type_system_sp->GetAtomicType(m_type);
685 return CompilerType();
686}
687
689 if (IsValid())
690 if (auto type_system_sp = GetTypeSystem())
691 return type_system_sp->AddConstModifier(m_type);
692 return CompilerType();
693}
694
696 if (IsValid())
697 if (auto type_system_sp = GetTypeSystem())
698 return type_system_sp->AddVolatileModifier(m_type);
699 return CompilerType();
700}
701
703 if (IsValid())
704 if (auto type_system_sp = GetTypeSystem())
705 return type_system_sp->AddRestrictModifier(m_type);
706 return CompilerType();
707}
708
710 const CompilerDeclContext &decl_ctx,
711 uint32_t payload) const {
712 if (IsValid())
713 if (auto type_system_sp = GetTypeSystem())
714 return type_system_sp->CreateTypedef(m_type, name, decl_ctx, payload);
715 return CompilerType();
716}
717
719 if (IsValid())
720 if (auto type_system_sp = GetTypeSystem())
721 return type_system_sp->GetTypedefedType(m_type);
722 return CompilerType();
723}
724
725// Create related types using the current type's AST
726
729 if (IsValid())
730 if (auto type_system_sp = GetTypeSystem())
731 return type_system_sp->GetBasicTypeFromAST(basic_type);
732 return CompilerType();
733}
734// Exploring the type
735
736std::optional<uint64_t>
738 if (IsValid())
739 if (auto type_system_sp = GetTypeSystem())
740 return type_system_sp->GetBitSize(m_type, exe_scope);
741 return {};
742}
743
744std::optional<uint64_t>
746 if (std::optional<uint64_t> bit_size = GetBitSize(exe_scope))
747 return (*bit_size + 7) / 8;
748 return {};
749}
750
751std::optional<size_t>
753 if (IsValid())
754 if (auto type_system_sp = GetTypeSystem())
755 return type_system_sp->GetTypeBitAlign(m_type, exe_scope);
756 return {};
757}
758
760 if (IsValid())
761 if (auto type_system_sp = GetTypeSystem())
762 return type_system_sp->GetEncoding(m_type, count);
764}
765
767 if (IsValid())
768 if (auto type_system_sp = GetTypeSystem())
769 return type_system_sp->GetFormat(m_type);
771}
772
773llvm::Expected<uint32_t>
774CompilerType::GetNumChildren(bool omit_empty_base_classes,
775 const ExecutionContext *exe_ctx) const {
776 if (IsValid())
777 if (auto type_system_sp = GetTypeSystem())
778 return type_system_sp->GetNumChildren(m_type, omit_empty_base_classes,
779 exe_ctx);
780 return llvm::make_error<llvm::StringError>("invalid type",
781 llvm::inconvertibleErrorCode());
782}
783
785 if (IsValid())
786 if (auto type_system_sp = GetTypeSystem())
787 return type_system_sp->GetBasicTypeEnumeration(m_type);
788 return eBasicTypeInvalid;
789}
790
792 std::function<bool(const CompilerType &integer_type,
793 ConstString name,
794 const llvm::APSInt &value)> const &callback) const {
795 if (IsValid())
796 if (auto type_system_sp = GetTypeSystem())
797 return type_system_sp->ForEachEnumerator(m_type, callback);
798}
799
801 if (IsValid())
802 if (auto type_system_sp = GetTypeSystem())
803 return type_system_sp->GetNumFields(m_type);
804 return 0;
805}
806
807CompilerType CompilerType::GetFieldAtIndex(size_t idx, std::string &name,
808 uint64_t *bit_offset_ptr,
809 uint32_t *bitfield_bit_size_ptr,
810 bool *is_bitfield_ptr) const {
811 if (IsValid())
812 if (auto type_system_sp = GetTypeSystem())
813 return type_system_sp->GetFieldAtIndex(m_type, idx, name, bit_offset_ptr,
814 bitfield_bit_size_ptr, is_bitfield_ptr);
815 return CompilerType();
816}
817
819 if (IsValid())
820 if (auto type_system_sp = GetTypeSystem())
821 return type_system_sp->GetNumDirectBaseClasses(m_type);
822 return 0;
823}
824
826 if (IsValid())
827 if (auto type_system_sp = GetTypeSystem())
828 return type_system_sp->GetNumVirtualBaseClasses(m_type);
829 return 0;
830}
831
834 uint32_t *bit_offset_ptr) const {
835 if (IsValid())
836 if (auto type_system_sp = GetTypeSystem())
837 return type_system_sp->GetDirectBaseClassAtIndex(m_type, idx,
838 bit_offset_ptr);
839 return CompilerType();
840}
841
844 uint32_t *bit_offset_ptr) const {
845 if (IsValid())
846 if (auto type_system_sp = GetTypeSystem())
847 return type_system_sp->GetVirtualBaseClassAtIndex(m_type, idx,
848 bit_offset_ptr);
849 return CompilerType();
850}
851
853 const char *name, CompilerType *field_compiler_type_ptr,
854 uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr,
855 bool *is_bitfield_ptr) const {
856 unsigned count = GetNumFields();
857 std::string field_name;
858 for (unsigned index = 0; index < count; index++) {
859 CompilerType field_compiler_type(
860 GetFieldAtIndex(index, field_name, bit_offset_ptr,
861 bitfield_bit_size_ptr, is_bitfield_ptr));
862 if (strcmp(field_name.c_str(), name) == 0) {
863 if (field_compiler_type_ptr)
864 *field_compiler_type_ptr = field_compiler_type;
865 return index;
866 }
867 }
868 return UINT32_MAX;
869}
870
872 ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
873 bool omit_empty_base_classes, bool ignore_array_bounds,
874 std::string &child_name, uint32_t &child_byte_size,
875 int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size,
876 uint32_t &child_bitfield_bit_offset, bool &child_is_base_class,
877 bool &child_is_deref_of_parent, ValueObject *valobj,
878 uint64_t &language_flags) const {
879 if (IsValid())
880 if (auto type_system_sp = GetTypeSystem())
881 return type_system_sp->GetChildCompilerTypeAtIndex(
882 m_type, exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
883 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
884 child_bitfield_bit_size, child_bitfield_bit_offset,
885 child_is_base_class, child_is_deref_of_parent, valobj,
886 language_flags);
887 return CompilerType();
888}
889
890// Look for a child member (doesn't include base classes, but it does include
891// their members) in the type hierarchy. Returns an index path into
892// "clang_type" on how to reach the appropriate member.
893//
894// class A
895// {
896// public:
897// int m_a;
898// int m_b;
899// };
900//
901// class B
902// {
903// };
904//
905// class C :
906// public B,
907// public A
908// {
909// };
910//
911// If we have a clang type that describes "class C", and we wanted to looked
912// "m_b" in it:
913//
914// With omit_empty_base_classes == false we would get an integer array back
915// with: { 1, 1 } The first index 1 is the child index for "class A" within
916// class C The second index 1 is the child index for "m_b" within class A
917//
918// With omit_empty_base_classes == true we would get an integer array back
919// with: { 0, 1 } The first index 0 is the child index for "class A" within
920// class C (since class B doesn't have any members it doesn't count) The second
921// index 1 is the child index for "m_b" within class A
922
924 llvm::StringRef name, bool omit_empty_base_classes,
925 std::vector<uint32_t> &child_indexes) const {
926 if (IsValid() && !name.empty()) {
927 if (auto type_system_sp = GetTypeSystem())
928 return type_system_sp->GetIndexOfChildMemberWithName(
929 m_type, name, omit_empty_base_classes, child_indexes);
930 }
931 return 0;
932}
933
934size_t CompilerType::GetNumTemplateArguments(bool expand_pack) const {
935 if (IsValid()) {
936 if (auto type_system_sp = GetTypeSystem())
937 return type_system_sp->GetNumTemplateArguments(m_type, expand_pack);
938 }
939 return 0;
940}
941
943CompilerType::GetTemplateArgumentKind(size_t idx, bool expand_pack) const {
944 if (IsValid())
945 if (auto type_system_sp = GetTypeSystem())
946 return type_system_sp->GetTemplateArgumentKind(m_type, idx, expand_pack);
948}
949
951 bool expand_pack) const {
952 if (IsValid()) {
953 if (auto type_system_sp = GetTypeSystem())
954 return type_system_sp->GetTypeTemplateArgument(m_type, idx, expand_pack);
955 }
956 return CompilerType();
957}
958
959std::optional<CompilerType::IntegralTemplateArgument>
960CompilerType::GetIntegralTemplateArgument(size_t idx, bool expand_pack) const {
961 if (IsValid())
962 if (auto type_system_sp = GetTypeSystem())
963 return type_system_sp->GetIntegralTemplateArgument(m_type, idx, expand_pack);
964 return std::nullopt;
965}
966
968 if (IsValid())
969 if (auto type_system_sp = GetTypeSystem())
970 return type_system_sp->GetTypeForFormatters(m_type);
971 return CompilerType();
972}
973
975 if (IsValid())
976 if (auto type_system_sp = GetTypeSystem())
977 return type_system_sp->ShouldPrintAsOneLiner(m_type, valobj);
978 return eLazyBoolCalculate;
979}
980
982 if (IsValid())
983 if (auto type_system_sp = GetTypeSystem())
984 return type_system_sp->IsMeaninglessWithoutDynamicResolution(m_type);
985 return false;
986}
987
988// Get the index of the child of "clang_type" whose name matches. This function
989// doesn't descend into the children, but only looks one level deep and name
990// matches can include base class names.
991
992uint32_t
994 bool omit_empty_base_classes) const {
995 if (IsValid() && !name.empty()) {
996 if (auto type_system_sp = GetTypeSystem())
997 return type_system_sp->GetIndexOfChildWithName(m_type, name,
998 omit_empty_base_classes);
999 }
1000 return UINT32_MAX;
1001}
1002
1003// Dumping types
1004
1006 const DataExtractor &data,
1007 lldb::offset_t byte_offset, size_t byte_size,
1008 uint32_t bitfield_bit_size,
1009 uint32_t bitfield_bit_offset,
1010 ExecutionContextScope *exe_scope) {
1011 if (IsValid())
1012 if (auto type_system_sp = GetTypeSystem())
1013 return type_system_sp->DumpTypeValue(
1014 m_type, *s, format, data, byte_offset, byte_size, bitfield_bit_size,
1015 bitfield_bit_offset, exe_scope);
1016 return false;
1017}
1018
1020 if (IsValid())
1021 if (auto type_system_sp = GetTypeSystem())
1022 type_system_sp->DumpTypeDescription(m_type, level);
1023}
1024
1026 lldb::DescriptionLevel level) const {
1027 if (IsValid())
1028 if (auto type_system_sp = GetTypeSystem())
1029 type_system_sp->DumpTypeDescription(m_type, *s, level);
1030}
1031
1032#ifndef NDEBUG
1033LLVM_DUMP_METHOD void CompilerType::dump() const {
1034 if (IsValid())
1035 if (auto type_system_sp = GetTypeSystem())
1036 return type_system_sp->dump(m_type);
1037 llvm::errs() << "<invalid>\n";
1038}
1039#endif
1040
1042 lldb::offset_t data_byte_offset,
1043 size_t data_byte_size, Scalar &value,
1044 ExecutionContextScope *exe_scope) const {
1045 if (!IsValid())
1046 return false;
1047
1048 if (IsAggregateType()) {
1049 return false; // Aggregate types don't have scalar values
1050 } else {
1051 uint64_t count = 0;
1052 lldb::Encoding encoding = GetEncoding(count);
1053
1054 if (encoding == lldb::eEncodingInvalid || count != 1)
1055 return false;
1056
1057 std::optional<uint64_t> byte_size = GetByteSize(exe_scope);
1058 if (!byte_size)
1059 return false;
1060 lldb::offset_t offset = data_byte_offset;
1061 switch (encoding) {
1063 break;
1065 break;
1067 if (*byte_size <= sizeof(unsigned long long)) {
1068 uint64_t uval64 = data.GetMaxU64(&offset, *byte_size);
1069 if (*byte_size <= sizeof(unsigned int)) {
1070 value = (unsigned int)uval64;
1071 return true;
1072 } else if (*byte_size <= sizeof(unsigned long)) {
1073 value = (unsigned long)uval64;
1074 return true;
1075 } else if (*byte_size <= sizeof(unsigned long long)) {
1076 value = (unsigned long long)uval64;
1077 return true;
1078 } else
1079 value.Clear();
1080 }
1081 break;
1082
1084 if (*byte_size <= sizeof(long long)) {
1085 int64_t sval64 = data.GetMaxS64(&offset, *byte_size);
1086 if (*byte_size <= sizeof(int)) {
1087 value = (int)sval64;
1088 return true;
1089 } else if (*byte_size <= sizeof(long)) {
1090 value = (long)sval64;
1091 return true;
1092 } else if (*byte_size <= sizeof(long long)) {
1093 value = (long long)sval64;
1094 return true;
1095 } else
1096 value.Clear();
1097 }
1098 break;
1099
1101 if (*byte_size <= sizeof(long double)) {
1102 uint32_t u32;
1103 uint64_t u64;
1104 if (*byte_size == sizeof(float)) {
1105 if (sizeof(float) == sizeof(uint32_t)) {
1106 u32 = data.GetU32(&offset);
1107 value = *((float *)&u32);
1108 return true;
1109 } else if (sizeof(float) == sizeof(uint64_t)) {
1110 u64 = data.GetU64(&offset);
1111 value = *((float *)&u64);
1112 return true;
1113 }
1114 } else if (*byte_size == sizeof(double)) {
1115 if (sizeof(double) == sizeof(uint32_t)) {
1116 u32 = data.GetU32(&offset);
1117 value = *((double *)&u32);
1118 return true;
1119 } else if (sizeof(double) == sizeof(uint64_t)) {
1120 u64 = data.GetU64(&offset);
1121 value = *((double *)&u64);
1122 return true;
1123 }
1124 } else if (*byte_size == sizeof(long double)) {
1125 if (sizeof(long double) == sizeof(uint32_t)) {
1126 u32 = data.GetU32(&offset);
1127 value = *((long double *)&u32);
1128 return true;
1129 } else if (sizeof(long double) == sizeof(uint64_t)) {
1130 u64 = data.GetU64(&offset);
1131 value = *((long double *)&u64);
1132 return true;
1133 }
1134 }
1135 }
1136 break;
1137 }
1138 }
1139 return false;
1140}
1141
1144 : m_type_system(type_system.GetSharedPointer()), m_type(type) {
1145 assert(Verify() && "verification failed");
1146}
1147
1150 : m_type_system(type_system), m_type(type) {
1151 assert(Verify() && "verification failed");
1152}
1153
1154#ifndef NDEBUG
1156 if (!IsValid())
1157 return true;
1158 if (auto type_system_sp = GetTypeSystem())
1159 return type_system_sp->Verify(m_type);
1160 return true;
1161}
1162#endif
1163
1165 return {m_type_system.lock()};
1166}
1167
1169 const CompilerType::TypeSystemSPWrapper &other) const {
1170 if (!m_typesystem_sp && !other.m_typesystem_sp)
1171 return true;
1172 if (m_typesystem_sp && other.m_typesystem_sp)
1173 return m_typesystem_sp.get() == other.m_typesystem_sp.get();
1174 return false;
1175}
1176
1178 assert(m_typesystem_sp);
1179 return m_typesystem_sp.get();
1180}
1181
1183 const lldb_private::CompilerType &rhs) {
1184 return lhs.GetTypeSystem() == rhs.GetTypeSystem() &&
1185 lhs.GetOpaqueQualType() == rhs.GetOpaqueQualType();
1186}
1187
1189 const lldb_private::CompilerType &rhs) {
1190 return !(lhs == rhs);
1191}
Represents a generic declaration context in a program.
This is a minimal wrapper of a TypeSystem shared pointer as returned by CompilerType which conventien...
Definition: CompilerType.h:49
lldb::TypeSystemSP GetSharedPointer() const
Definition: CompilerType.h:85
bool operator==(const TypeSystemSPWrapper &other) const
TypeSystem * operator->() const
Only to be used in a one-off situations like if (typesystem && typesystem->method()) Do not store thi...
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
uint32_t GetNumberOfNonEmptyBaseClasses()
Go through the base classes and count non-empty ones.
CompilerType GetTypeForFormatters() const
CompilerType GetTypeTemplateArgument(size_t idx, bool expand_pack=false) const
lldb::LanguageType GetMinimumLanguage()
bool GetValueAsScalar(const DataExtractor &data, lldb::offset_t data_offset, size_t data_byte_size, Scalar &value, ExecutionContextScope *exe_scope) const
bool Verify() const
If the type is valid, ask the TypeSystem to verify the integrity of the type to catch CompilerTypes t...
lldb::BasicType GetBasicTypeEnumeration() const
std::optional< IntegralTemplateArgument > GetIntegralTemplateArgument(size_t idx, bool expand_pack=false) const
Returns the value of the template argument and its type.
TypeSystemSPWrapper GetTypeSystem() const
Accessors.
CompilerType GetArrayType(uint64_t size) const
CompilerType 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) const
CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const
Create related types using the current type's AST.
bool IsPossibleDynamicType(CompilerType *target_type, bool check_cplusplus, bool check_objc) const
void SetCompilerType(lldb::TypeSystemWP type_system, lldb::opaque_compiler_type_t type)
std::optional< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
CompilerType AddConstModifier() const
Return a new CompilerType adds a const modifier to this type if this type is valid and the type syste...
lldb::Encoding GetEncoding(uint64_t &count) const
bool IsArrayType(CompilerType *element_type=nullptr, uint64_t *size=nullptr, bool *is_incomplete=nullptr) const
ConstString GetDisplayTypeName() const
CompilerType GetVirtualBaseClassAtIndex(size_t idx, uint32_t *bit_offset_ptr) const
size_t GetNumMemberFunctions() const
CompilerType GetFunctionArgumentTypeAtIndex(size_t idx) const
uint32_t IsHomogeneousAggregate(CompilerType *base_type_ptr) const
CompilerType GetFieldAtIndex(size_t idx, std::string &name, uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) const
CompilerType GetRValueReferenceType() const
Return a new CompilerType that is a R value reference to this type if this type is valid and the type...
bool IsIntegerOrUnscopedEnumerationType() const
size_t GetIndexOfChildMemberWithName(llvm::StringRef name, bool omit_empty_base_classes, std::vector< uint32_t > &child_indexes) const
Lookup a child member given a name.
bool IsScalarOrUnscopedEnumerationType() const
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
bool IsContextuallyConvertibleToBool() const
This may only be defined in TypeSystemClang.
lldb::TypeClass GetTypeClass() const
lldb::opaque_compiler_type_t GetOpaqueQualType() const
Definition: CompilerType.h:281
size_t GetNumTemplateArguments(bool expand_pack=false) const
Return the number of template arguments the type has.
CompilerType AddVolatileModifier() const
Return a new CompilerType adds a volatile modifier to this type if this type is valid and the type sy...
bool IsVirtualBase(CompilerType target_base, CompilerType *virtual_base, bool carry_virtual=false) const
Checks whether target_base is a virtual base of type (direct or indirect).
CompilerType AddRestrictModifier() const
Return a new CompilerType adds a restrict modifier to this type if this type is valid and the type sy...
lldb::TypeSystemWP m_type_system
Definition: CompilerType.h:521
uint32_t GetNumVirtualBaseClasses() const
size_t GetPointerByteSize() const
AST related queries.
CompilerType GetFunctionArgumentAtIndex(const size_t index) const
void ForEachEnumerator(std::function< bool(const CompilerType &integer_type, ConstString name, const llvm::APSInt &value)> const &callback) const
If this type is an enumeration, iterate through all of its enumerators using a callback.
bool IsFloatingPointType(uint32_t &count, bool &is_complex) const
LLVM_DUMP_METHOD void dump() const
Dumping types.
CompilerType GetLValueReferenceType() const
Return a new CompilerType that is a L value reference to this type if this type is valid and the type...
uint32_t GetNumFields() const
bool IsPromotableIntegerType() const
size_t GetNumberOfFunctionArguments() const
bool IsIntegerOrEnumerationType(bool &is_signed) const
bool IsScopedEnumerationType() const
bool ShouldTreatScalarValueAsAddress() const
CompilerType GetNonReferenceType() const
If this type is a reference to a type (L value or R value reference), return a new type with the refe...
uint32_t GetNumDirectBaseClasses() const
bool IsMeaninglessWithoutDynamicResolution() const
bool IsBlockPointerType(CompilerType *function_pointer_type_ptr=nullptr) const
ConstString GetTypeName(bool BaseOnly=false) const
uint32_t GetIndexOfChildWithName(llvm::StringRef name, bool omit_empty_base_classes) const
Lookup a child given a name.
bool IsEnumerationIntegerTypeSigned() const
CompilerType GetTypedefedType() const
If the current object represents a typedef type, get the underlying type.
bool IsReferenceType(CompilerType *pointee_type=nullptr, bool *is_rvalue=nullptr) const
bool DumpTypeValue(Stream *s, lldb::Format format, const DataExtractor &data, lldb::offset_t data_offset, size_t data_byte_size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, ExecutionContextScope *exe_scope)
CompilerType GetArrayElementType(ExecutionContextScope *exe_scope) const
Creating related types.
bool IsInteger() const
This is used when you don't care about the signedness of the integer.
lldb::Format GetFormat() const
CompilerType GetFullyUnqualifiedType() const
unsigned GetTypeQualifiers() const
CompilerType GetDirectBaseClassAtIndex(size_t idx, uint32_t *bit_offset_ptr) const
std::optional< size_t > GetTypeBitAlign(ExecutionContextScope *exe_scope) const
bool IsFunctionPointerType() const
CompilerType GetPointeeType() const
If this type is a pointer type, return the type that the pointer points to, else return an invalid ty...
bool IsIntegerType(bool &is_signed) const
bool GetCompleteType() const
Type Completion.
bool IsUnscopedEnumerationType() const
uint32_t GetTypeInfo(CompilerType *pointee_or_element_compiler_type=nullptr) const
int GetFunctionArgumentCount() const
Returns -1 if this isn't a function of if the function doesn't have a prototype Returns a value >= 0 ...
std::optional< uint64_t > GetBitSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bits.
uint32_t GetIndexOfFieldWithName(const char *name, CompilerType *field_compiler_type=nullptr, uint64_t *bit_offset_ptr=nullptr, uint32_t *bitfield_bit_size_ptr=nullptr, bool *is_bitfield_ptr=nullptr) const
llvm::Expected< uint32_t > GetNumChildren(bool omit_empty_base_classes, const ExecutionContext *exe_ctx) const
CompilerType GetEnumerationIntegerType() const
lldb::opaque_compiler_type_t m_type
Definition: CompilerType.h:522
bool CompareTypes(CompilerType rhs) const
bool IsForcefullyCompleted() const
bool IsEnumerationType() const
This is used when you don't care about the signedness of the enum.
CompilerType CreateTypedef(const char *name, const CompilerDeclContext &decl_ctx, uint32_t payload) const
Create a typedef to this type using "name" as the name of the typedef this type is valid and the type...
CompilerType GetFunctionReturnType() const
bool IsVectorType(CompilerType *element_type=nullptr, uint64_t *size=nullptr) const
bool IsMemberFunctionPointerType() const
CompilerType GetAtomicType() const
Return a new CompilerType that is the atomic type of this type.
CompilerType GetCanonicalType() const
void DumpTypeDescription(lldb::DescriptionLevel level=lldb::eDescriptionLevelFull) const
Dump to stdout.
bool IsRuntimeGeneratedType() const
lldb::TemplateArgumentKind GetTemplateArgumentKind(size_t idx, bool expand_pack=false) const
LazyBool ShouldPrintAsOneLiner(ValueObject *valobj) const
TypeMemberFunctionImpl GetMemberFunctionAtIndex(size_t idx)
bool IsPointerOrReferenceType(CompilerType *pointee_type=nullptr) const
bool IsPointerToScalarType() const
bool IsPointerType(CompilerType *pointee_type=nullptr) const
A uniqued constant string class.
Definition: ConstString.h:40
An data extractor class.
Definition: DataExtractor.h:48
int64_t GetMaxS64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an signed integer of size byte_size from *offset_ptr.
uint64_t GetU64(lldb::offset_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an unsigned integer of size byte_size from *offset_ptr.
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
Interface for representing a type system.
Definition: TypeSystem.h:67
#define UINT32_MAX
Definition: lldb-defines.h:19
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
bool operator!=(const Address &lhs, const Address &rhs)
Definition: Address.cpp:1028
bool operator==(const Address &lhs, const Address &rhs)
Definition: Address.cpp:1022
Definition: SBAddress.h:15
void * opaque_compiler_type_t
Definition: lldb-types.h:87
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
BasicType
Basic types enumeration for the public API SBType::GetBasicType().
@ eBasicTypeUnsignedShort
@ eBasicTypeSignedChar
@ eBasicTypeNullPtr
@ eBasicTypeUnsignedWChar
@ eBasicTypeInvalid
@ eBasicTypeSignedWChar
@ eBasicTypeChar16
@ eBasicTypeUnsignedChar
@ eBasicTypeChar32
Format
Display format definitions.
uint64_t offset_t
Definition: lldb-types.h:83
LanguageType
Programming language type.
@ eLanguageTypeC
Non-standardized C, such as K&R.
@ eTemplateArgumentKindNull
Encoding
Register encoding definitions.
@ eEncodingIEEE754
float
@ eEncodingVector
vector registers
@ eEncodingUint
unsigned integer
@ eEncodingInvalid
@ eEncodingSint
signed integer
std::weak_ptr< lldb_private::TypeSystem > TypeSystemWP
Definition: lldb-forward.h:459