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#include "lldb/Core/Debugger.h"
11#include "lldb/Symbol/Type.h"
13#include "lldb/Target/Process.h"
18#include "lldb/Utility/Log.h"
19#include "lldb/Utility/Scalar.h"
20#include "lldb/Utility/Stream.h"
23#include "llvm/Support/ErrorExtras.h"
24
25#include <iterator>
26#include <mutex>
27#include <optional>
28
29using namespace lldb;
30using namespace lldb_private;
31
32// Tests
33
35 if (IsValid())
36 if (auto type_system_sp = GetTypeSystem())
37 return type_system_sp->IsAggregateType(m_type);
38 return false;
39}
40
42 if (IsValid())
43 if (auto type_system_sp = GetTypeSystem())
44 return type_system_sp->IsAnonymousType(m_type);
45 return false;
46}
47
49 if (IsValid())
50 if (auto type_system_sp = GetTypeSystem())
51 return type_system_sp->IsScopedEnumerationType(m_type);
52 return false;
53}
54
55bool CompilerType::IsArrayType(CompilerType *element_type_ptr, uint64_t *size,
56 bool *is_incomplete) const {
57 if (IsValid())
58 if (auto type_system_sp = GetTypeSystem())
59 return type_system_sp->IsArrayType(m_type, element_type_ptr, size,
60 is_incomplete);
61
62 if (element_type_ptr)
63 element_type_ptr->Clear();
64 if (size)
65 *size = 0;
66 if (is_incomplete)
67 *is_incomplete = false;
68 return false;
69}
70
72 uint64_t *size) const {
73 if (IsValid())
74 if (auto type_system_sp = GetTypeSystem())
75 return type_system_sp->IsVectorType(m_type, element_type, size);
76 return false;
77}
78
80 if (IsValid())
81 if (auto type_system_sp = GetTypeSystem())
82 return type_system_sp->IsRuntimeGeneratedType(m_type);
83 return false;
84}
85
87 if (IsValid())
88 if (auto type_system_sp = GetTypeSystem())
89 return type_system_sp->IsCharType(m_type);
90 return false;
91}
92
94 if (IsValid())
95 if (auto type_system_sp = GetTypeSystem())
96 return type_system_sp->IsCompleteType(m_type);
97 return false;
98}
99
101 if (IsValid())
102 if (auto type_system_sp = GetTypeSystem())
103 return type_system_sp->IsForcefullyCompleted(m_type);
104 return false;
105}
106
108 if (IsValid())
109 if (auto type_system_sp = GetTypeSystem())
110 return type_system_sp->IsConst(m_type);
111 return false;
112}
113
115 if (IsValid())
116 if (auto type_system_sp = GetTypeSystem())
117 return type_system_sp->GetPtrAuthKey(m_type);
118 return 0;
119}
120
122 if (IsValid())
123 if (auto type_system_sp = GetTypeSystem())
124 return type_system_sp->GetPtrAuthDiscriminator(m_type);
125 return 0;
126}
127
129 if (IsValid())
130 if (auto type_system_sp = GetTypeSystem())
131 return type_system_sp->GetPtrAuthAddressDiversity(m_type);
132 return false;
133}
134
136 if (IsValid())
137 if (auto type_system_sp = GetTypeSystem())
138 return type_system_sp->IsFunctionType(m_type);
139 return false;
140}
141
142// Used to detect "Homogeneous Floating-point Aggregates"
143uint32_t
145 if (IsValid())
146 if (auto type_system_sp = GetTypeSystem())
147 return type_system_sp->IsHomogeneousAggregate(m_type, base_type_ptr);
148 return 0;
149}
150
152 if (IsValid())
153 if (auto type_system_sp = GetTypeSystem())
154 return type_system_sp->GetNumberOfFunctionArguments(m_type);
155 return 0;
156}
157
160 if (IsValid())
161 if (auto type_system_sp = GetTypeSystem())
162 return type_system_sp->GetFunctionArgumentAtIndex(m_type, index);
163 return CompilerType();
164}
165
167 if (IsValid())
168 if (auto type_system_sp = GetTypeSystem())
169 return type_system_sp->IsFunctionPointerType(m_type);
170 return false;
171}
172
174 if (IsValid())
175 if (auto type_system_sp = GetTypeSystem())
176 return type_system_sp->IsMemberFunctionPointerType(m_type);
177 return false;
178}
179
181 if (IsValid())
182 if (auto type_system_sp = GetTypeSystem())
183 return type_system_sp->IsMemberDataPointerType(m_type);
184 return false;
185}
186
188 CompilerType *function_pointer_type_ptr) const {
189 if (IsValid())
190 if (auto type_system_sp = GetTypeSystem())
191 return type_system_sp->IsBlockPointerType(m_type, function_pointer_type_ptr);
192 return false;
193}
194
195bool CompilerType::IsIntegerType(bool &is_signed) const {
196 if (IsValid())
197 if (auto type_system_sp = GetTypeSystem())
198 return type_system_sp->IsIntegerType(m_type, is_signed);
199 return false;
200}
201
202bool CompilerType::IsEnumerationType(bool &is_signed) const {
203 if (IsValid())
204 if (auto type_system_sp = GetTypeSystem())
205 return type_system_sp->IsEnumerationType(m_type, is_signed);
206 return false;
207}
208
209bool CompilerType::IsIntegerOrEnumerationType(bool &is_signed) const {
210 return IsIntegerType(is_signed) || IsEnumerationType(is_signed);
211}
212
213bool CompilerType::IsPointerType(CompilerType *pointee_type) const {
214 if (IsValid()) {
215 if (auto type_system_sp = GetTypeSystem())
216 return type_system_sp->IsPointerType(m_type, pointee_type);
217 }
218 if (pointee_type)
219 pointee_type->Clear();
220 return false;
221}
222
224 if (IsValid()) {
225 if (auto type_system_sp = GetTypeSystem())
226 return type_system_sp->IsPointerOrReferenceType(m_type, pointee_type);
227 }
228 if (pointee_type)
229 pointee_type->Clear();
230 return false;
231}
232
234 bool *is_rvalue) const {
235 if (IsValid()) {
236 if (auto type_system_sp = GetTypeSystem())
237 return type_system_sp->IsReferenceType(m_type, pointee_type, is_rvalue);
238 }
239 if (pointee_type)
240 pointee_type->Clear();
241 return false;
242}
243
245 if (IsValid())
246 if (auto type_system_sp = GetTypeSystem())
247 return type_system_sp->ShouldTreatScalarValueAsAddress(m_type);
248 return false;
249}
250
252 return GetTypeClass() & eTypeClassComplexFloat ||
253 GetTypeClass() & eTypeClassComplexInteger;
254}
255
257 if (IsValid())
258 if (auto type_system_sp = GetTypeSystem())
259 return type_system_sp->IsFloatingPointType(m_type);
260
261 return false;
262}
263
267
269 if (IsValid())
270 if (auto type_system_sp = GetTypeSystem())
271 return type_system_sp->IsDefined(m_type);
272 return true;
273}
274
276 if (IsValid()) {
277 if (auto type_system_sp = GetTypeSystem())
278 return type_system_sp->IsPolymorphicClass(m_type);
279 }
280 return false;
281}
282
284 bool check_cplusplus,
285 bool check_objc) const {
286 if (IsValid())
287 if (auto type_system_sp = GetTypeSystem())
288 return type_system_sp->IsPossibleDynamicType(m_type, dynamic_pointee_type,
289 check_cplusplus, check_objc);
290 return false;
291}
292
294 if (IsValid())
295 if (auto type_system_sp = GetTypeSystem())
296 return type_system_sp->IsScalarType(m_type);
297 return false;
298}
299
301 if (IsValid())
302 if (auto type_system_sp = GetTypeSystem())
303 return type_system_sp->IsTemplateType(m_type);
304 return false;
305}
306
308 if (IsValid())
309 if (auto type_system_sp = GetTypeSystem())
310 return type_system_sp->IsTypedefType(m_type);
311 return false;
312}
313
315 if (IsValid())
316 if (auto type_system_sp = GetTypeSystem())
317 return type_system_sp->IsVoidType(m_type);
318 return false;
319}
320
322 if (IsValid())
323 if (auto type_system_sp = GetTypeSystem())
324 return type_system_sp->HasPointerAuthQualifier(m_type);
325 return false;
326}
327
328std::optional<uint32_t> CompilerType::GetTargetAddressSpace() const {
329 if (IsValid())
330 if (auto type_system_sp = GetTypeSystem())
331 return type_system_sp->GetTargetAddressSpace(m_type);
332 return std::nullopt;
333}
334
336 if (IsValid())
337 if (auto type_system_sp = GetTypeSystem())
338 return type_system_sp->IsBeingDefined(m_type);
339 return false;
340}
341
343 bool is_signed = false; // May be reset by the call below.
344 return IsIntegerType(is_signed);
345}
346
348 bool is_signed = false; // May be reset by the call below.
349 return IsEnumerationType(is_signed);
350}
351
355
359
361 return GetTypeInfo() & lldb::eTypeIsSigned;
362}
363
367
371
373 if (IsValid())
374 return GetEnumerationIntegerType().GetTypeInfo() & lldb::eTypeIsSigned;
375
376 return false;
377}
378
382
384 if (IsValid())
385 if (auto type_system_sp = GetTypeSystem())
386 return type_system_sp->IsPromotableIntegerType(m_type);
387 return false;
388}
389
391 if (!IsValid())
392 return false;
393
394 return IsPointerType() &&
396}
397
399 if (!IsValid())
400 return false;
401
402 return GetCanonicalType().GetTypeClass() &
403 (lldb::eTypeClassClass | lldb::eTypeClassStruct |
404 lldb::eTypeClassUnion);
405}
406
408 CompilerType *virtual_base,
409 bool carry_virtual) const {
410 if (CompareTypes(target_base))
411 return carry_virtual;
412
413 if (!carry_virtual) {
414 uint32_t num_virtual_bases = GetNumVirtualBaseClasses();
415 for (uint32_t i = 0; i < num_virtual_bases; ++i) {
416 uint32_t bit_offset;
417 auto base = GetVirtualBaseClassAtIndex(i, &bit_offset);
418 if (base.IsVirtualBase(target_base, virtual_base,
419 /*carry_virtual*/ true)) {
420 if (virtual_base)
421 *virtual_base = base;
422
423 return true;
424 }
425 }
426 }
427
428 uint32_t num_direct_bases = GetNumDirectBaseClasses();
429 for (uint32_t i = 0; i < num_direct_bases; ++i) {
430 uint32_t bit_offset;
431 auto base = GetDirectBaseClassAtIndex(i, &bit_offset);
432 if (base.IsVirtualBase(target_base, virtual_base, carry_virtual))
433 return true;
434 }
435
436 return false;
437}
438
443
447
449 auto name = GetTypeName();
450 auto canonical_name = GetCanonicalType().GetTypeName();
451 if (name.IsEmpty() || canonical_name.IsEmpty())
452 return "''"; // Should not happen, unless the input is broken somehow.
453
454 if (name == canonical_name)
455 return llvm::formatv("'{0}'", name);
456
457 return llvm::formatv("'{0}' (canonically referred to as '{1}')", name,
458 canonical_name);
459}
460
462 if (*this == rhs)
463 return true;
464
466 const ConstString rhs_name = rhs.GetFullyUnqualifiedType().GetTypeName();
467 return name == rhs_name;
468}
469
471 switch (GetTypeClass()) {
472 case lldb::eTypeClassClass:
473 return "class";
474 case lldb::eTypeClassEnumeration:
475 return "enum";
476 case lldb::eTypeClassStruct:
477 return "struct";
478 case lldb::eTypeClassUnion:
479 return "union";
480 default:
481 return "unknown";
482 }
483 llvm_unreachable("All cases are covered by code above.");
484}
485
487 uint32_t ret = 0;
488 uint32_t num_direct_bases = GetNumDirectBaseClasses();
489
490 for (uint32_t i = 0; i < num_direct_bases; ++i) {
491 uint32_t bit_offset;
492 CompilerType base_type = GetDirectBaseClassAtIndex(i, &bit_offset);
493 if (base_type.GetNumFields() > 0 ||
494 base_type.GetNumberOfNonEmptyBaseClasses() > 0)
495 ret += 1;
496 }
497 return ret;
498}
499
500// Type Completion
501
503 if (IsValid())
504 if (auto type_system_sp = GetTypeSystem())
505 return type_system_sp->GetCompleteType(m_type);
506 return false;
507}
508
509// AST related queries
511 if (auto type_system_sp = GetTypeSystem())
512 return type_system_sp->GetPointerByteSize();
513 return 0;
514}
515
517 if (IsValid()) {
518 if (auto type_system_sp = GetTypeSystem())
519 return type_system_sp->GetTypeName(m_type, BaseOnly);
520 }
521 return ConstString("<invalid>");
522}
523
525 if (IsValid())
526 if (auto type_system_sp = GetTypeSystem())
527 return type_system_sp->GetDisplayTypeName(m_type);
528 return ConstString("<invalid>");
529}
530
532 if (IsValid()) {
533 if (auto type_system_sp = GetTypeSystem())
534 return type_system_sp->GetMangledTypeName(m_type);
535 }
536 return ConstString("<invalid>");
537}
538
540 CompilerType *pointee_or_element_compiler_type) const {
541 if (IsValid())
542 if (auto type_system_sp = GetTypeSystem())
543 return type_system_sp->GetTypeInfo(m_type,
544 pointee_or_element_compiler_type);
545 return 0;
546}
547
549 if (IsValid())
550 if (auto type_system_sp = GetTypeSystem())
551 return type_system_sp->GetMinimumLanguage(m_type);
553}
554
555lldb::TypeClass CompilerType::GetTypeClass() const {
556 if (IsValid())
557 if (auto type_system_sp = GetTypeSystem())
558 return type_system_sp->GetTypeClass(m_type);
559 return lldb::eTypeClassInvalid;
560}
561
564 m_type_system = type_system;
565 m_type = type;
566}
567
573
575 if (IsValid())
576 if (auto type_system_sp = GetTypeSystem())
577 return type_system_sp->GetTypeQualifiers(m_type);
578 return 0;
579}
580
581// Creating related types
582
585 if (IsValid()) {
586 if (auto type_system_sp = GetTypeSystem())
587 return type_system_sp->GetArrayElementType(m_type, exe_scope);
588 }
589 return CompilerType();
590}
591
593 if (IsValid()) {
594 if (auto type_system_sp = GetTypeSystem())
595 return type_system_sp->GetArrayType(m_type, size);
596 }
597 return CompilerType();
598}
599
601 if (IsValid())
602 if (auto type_system_sp = GetTypeSystem())
603 return type_system_sp->GetCanonicalType(m_type);
604 return CompilerType();
605}
606
608 if (IsValid())
609 if (auto type_system_sp = GetTypeSystem())
610 return type_system_sp->GetFullyUnqualifiedType(m_type);
611 return CompilerType();
612}
613
615 if (IsValid())
616 if (auto type_system_sp = GetTypeSystem())
617 return type_system_sp->GetEnumerationIntegerType(m_type);
618 return CompilerType();
619}
620
622 if (IsValid()) {
623 if (auto type_system_sp = GetTypeSystem())
624 return type_system_sp->GetFunctionArgumentCount(m_type);
625 }
626 return -1;
627}
628
630 if (IsValid()) {
631 if (auto type_system_sp = GetTypeSystem())
632 return type_system_sp->GetFunctionArgumentTypeAtIndex(m_type, idx);
633 }
634 return CompilerType();
635}
636
638 if (IsValid()) {
639 if (auto type_system_sp = GetTypeSystem())
640 return type_system_sp->GetFunctionReturnType(m_type);
641 }
642 return CompilerType();
643}
644
646 if (IsValid()) {
647 if (auto type_system_sp = GetTypeSystem())
648 return type_system_sp->GetNumMemberFunctions(m_type);
649 }
650 return 0;
651}
652
654 if (IsValid()) {
655 if (auto type_system_sp = GetTypeSystem())
656 return type_system_sp->GetMemberFunctionAtIndex(m_type, idx);
657 }
658 return TypeMemberFunctionImpl();
659}
660
662 if (IsValid())
663 if (auto type_system_sp = GetTypeSystem())
664 return type_system_sp->GetNonReferenceType(m_type);
665 return CompilerType();
666}
667
669 if (IsValid()) {
670 if (auto type_system_sp = GetTypeSystem())
671 return type_system_sp->GetPointeeType(m_type);
672 }
673 return CompilerType();
674}
675
677 if (IsValid()) {
678 if (auto type_system_sp = GetTypeSystem())
679 return type_system_sp->GetPointerType(m_type);
680 }
681 return CompilerType();
682}
683
685 if (IsValid())
686 if (auto type_system_sp = GetTypeSystem())
687 return type_system_sp->AddPtrAuthModifier(m_type, payload);
688 return CompilerType();
689}
690
692CompilerType::AddAddressSpaceModifier(uint32_t address_space) const {
693 if (IsValid())
694 if (auto type_system_sp = GetTypeSystem())
695 return type_system_sp->AddAddressSpaceModifier(m_type, address_space);
696 return CompilerType();
697}
698
700 if (IsValid())
701 if (auto type_system_sp = GetTypeSystem())
702 return type_system_sp->GetLValueReferenceType(m_type);
703 return CompilerType();
704}
705
707 if (IsValid())
708 if (auto type_system_sp = GetTypeSystem())
709 return type_system_sp->GetRValueReferenceType(m_type);
710 return CompilerType();
711}
712
714 if (IsValid())
715 if (auto type_system_sp = GetTypeSystem())
716 return type_system_sp->GetAtomicType(m_type);
717 return CompilerType();
718}
719
721 if (IsValid())
722 if (auto type_system_sp = GetTypeSystem())
723 return type_system_sp->AddConstModifier(m_type);
724 return CompilerType();
725}
726
728 if (IsValid())
729 if (auto type_system_sp = GetTypeSystem())
730 return type_system_sp->AddVolatileModifier(m_type);
731 return CompilerType();
732}
733
735 if (IsValid())
736 if (auto type_system_sp = GetTypeSystem())
737 return type_system_sp->AddRestrictModifier(m_type);
738 return CompilerType();
739}
740
742 const CompilerDeclContext &decl_ctx,
743 uint32_t payload) const {
744 if (IsValid())
745 if (auto type_system_sp = GetTypeSystem())
746 return type_system_sp->CreateTypedef(m_type, name, decl_ctx, payload);
747 return CompilerType();
748}
749
751 if (IsValid())
752 if (auto type_system_sp = GetTypeSystem())
753 return type_system_sp->GetTypedefedType(m_type);
754 return CompilerType();
755}
756
757// Create related types using the current type's AST
758
761 if (IsValid())
762 if (auto type_system_sp = GetTypeSystem())
763 return type_system_sp->GetBasicTypeFromAST(basic_type);
764 return CompilerType();
765}
766// Exploring the type
767
768llvm::Expected<uint64_t>
770 if (IsValid())
771 if (auto type_system_sp = GetTypeSystem())
772 return type_system_sp->GetBitSize(m_type, exe_scope);
773 return llvm::createStringError("invalid type: cannot determine size");
774}
775
776llvm::Expected<uint64_t>
778 auto bit_size_or_err = GetBitSize(exe_scope);
779 if (!bit_size_or_err)
780 return bit_size_or_err.takeError();
781 return (*bit_size_or_err + 7) / 8;
782}
783
784std::optional<uint64_t> CompilerType::GetByteStride() const {
785 if (IsValid())
786 if (auto type_system_sp = GetTypeSystem())
787 return type_system_sp->GetByteStride(m_type);
788 return {};
789}
790
791std::optional<size_t>
793 if (IsValid())
794 if (auto type_system_sp = GetTypeSystem())
795 return type_system_sp->GetTypeBitAlign(m_type, exe_scope);
796 return {};
797}
798
800 if (IsValid())
801 if (auto type_system_sp = GetTypeSystem())
802 return type_system_sp->GetEncoding(m_type);
804}
805
807 if (IsValid())
808 if (auto type_system_sp = GetTypeSystem())
809 return type_system_sp->GetFormat(m_type);
811}
812
813llvm::Expected<uint32_t>
814CompilerType::GetNumChildren(bool omit_empty_base_classes,
815 const ExecutionContext *exe_ctx) const {
816 if (IsValid())
817 if (auto type_system_sp = GetTypeSystem())
818 return type_system_sp->GetNumChildren(m_type, omit_empty_base_classes,
819 exe_ctx);
820 return llvm::createStringError("invalid type");
821}
822
824 if (IsValid())
825 if (auto type_system_sp = GetTypeSystem())
826 return type_system_sp->GetBasicTypeEnumeration(m_type);
827 return eBasicTypeInvalid;
828}
829
831 std::function<bool(const CompilerType &integer_type,
832 ConstString name,
833 const llvm::APSInt &value)> const &callback) const {
834 if (IsValid())
835 if (auto type_system_sp = GetTypeSystem())
836 return type_system_sp->ForEachEnumerator(m_type, callback);
837}
838
840 if (IsValid())
841 if (auto type_system_sp = GetTypeSystem())
842 return type_system_sp->GetNumFields(m_type);
843 return 0;
844}
845
846CompilerType CompilerType::GetFieldAtIndex(size_t idx, std::string &name,
847 uint64_t *bit_offset_ptr,
848 uint32_t *bitfield_bit_size_ptr,
849 bool *is_bitfield_ptr) const {
850 if (IsValid())
851 if (auto type_system_sp = GetTypeSystem())
852 return type_system_sp->GetFieldAtIndex(m_type, idx, name, bit_offset_ptr,
853 bitfield_bit_size_ptr, is_bitfield_ptr);
854 return CompilerType();
855}
856
858 if (IsValid())
859 if (auto type_system_sp = GetTypeSystem())
860 return type_system_sp->GetNumDirectBaseClasses(m_type);
861 return 0;
862}
863
865 if (IsValid())
866 if (auto type_system_sp = GetTypeSystem())
867 return type_system_sp->GetNumVirtualBaseClasses(m_type);
868 return 0;
869}
870
873 uint32_t *bit_offset_ptr) const {
874 if (IsValid())
875 if (auto type_system_sp = GetTypeSystem())
876 return type_system_sp->GetDirectBaseClassAtIndex(m_type, idx,
877 bit_offset_ptr);
878 return CompilerType();
879}
880
883 uint32_t *bit_offset_ptr) const {
884 if (IsValid())
885 if (auto type_system_sp = GetTypeSystem())
886 return type_system_sp->GetVirtualBaseClassAtIndex(m_type, idx,
887 bit_offset_ptr);
888 return CompilerType();
889}
890
892 if (IsValid())
894 return CompilerDecl();
895}
896
897llvm::Expected<CompilerType> CompilerType::GetDereferencedType(
898 ExecutionContext *exe_ctx, std::string &deref_name,
899 uint32_t &deref_byte_size, int32_t &deref_byte_offset, ValueObject *valobj,
900 uint64_t &language_flags) const {
901 if (IsValid())
902 if (auto type_system_sp = GetTypeSystem())
903 return type_system_sp->GetDereferencedType(
904 m_type, exe_ctx, deref_name, deref_byte_size, deref_byte_offset,
905 valobj, language_flags);
906 return CompilerType();
907}
908
909llvm::Expected<ChildTypeInfo> CompilerType::GetChildAtIndex(
910 ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
911 bool omit_empty_base_classes, bool ignore_array_bounds,
912 ValueObject *valobj) const {
913 if (IsValid())
914 if (auto type_system_sp = GetTypeSystem())
915 return type_system_sp->GetChildAtIndex(
916 m_type, exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
917 ignore_array_bounds, valobj);
918 return ChildTypeInfo();
919}
920
921// Look for a child member (doesn't include base classes, but it does include
922// their members) in the type hierarchy. Returns an index path into
923// "clang_type" on how to reach the appropriate member.
924//
925// class A
926// {
927// public:
928// int m_a;
929// int m_b;
930// };
931//
932// class B
933// {
934// };
935//
936// class C :
937// public B,
938// public A
939// {
940// };
941//
942// If we have a clang type that describes "class C", and we wanted to looked
943// "m_b" in it:
944//
945// With omit_empty_base_classes == false we would get an integer array back
946// with: { 1, 1 } The first index 1 is the child index for "class A" within
947// class C The second index 1 is the child index for "m_b" within class A
948//
949// With omit_empty_base_classes == true we would get an integer array back
950// with: { 0, 1 } The first index 0 is the child index for "class A" within
951// class C (since class B doesn't have any members it doesn't count) The second
952// index 1 is the child index for "m_b" within class A
953
955 llvm::StringRef name, bool omit_empty_base_classes,
956 std::vector<uint32_t> &child_indexes) const {
957 if (IsValid() && !name.empty()) {
958 if (auto type_system_sp = GetTypeSystem())
959 return type_system_sp->GetIndexOfChildMemberWithName(
960 m_type, name, omit_empty_base_classes, child_indexes);
961 }
962 return 0;
963}
964
966CompilerType::GetDirectNestedTypeWithName(llvm::StringRef name) const {
967 if (IsValid() && !name.empty()) {
968 if (auto type_system_sp = GetTypeSystem())
969 return type_system_sp->GetDirectNestedTypeWithName(m_type, name);
970 }
971 return CompilerType();
972}
973
974size_t CompilerType::GetNumTemplateArguments(bool expand_pack) const {
975 if (IsValid()) {
976 if (auto type_system_sp = GetTypeSystem())
977 return type_system_sp->GetNumTemplateArguments(m_type, expand_pack);
978 }
979 return 0;
980}
981
983CompilerType::GetTemplateArgumentKind(size_t idx, bool expand_pack) const {
984 if (IsValid())
985 if (auto type_system_sp = GetTypeSystem())
986 return type_system_sp->GetTemplateArgumentKind(m_type, idx, expand_pack);
988}
989
991 bool expand_pack) const {
992 if (IsValid()) {
993 if (auto type_system_sp = GetTypeSystem())
994 return type_system_sp->GetTypeTemplateArgument(m_type, idx, expand_pack);
995 }
996 return CompilerType();
997}
998
999std::optional<CompilerType::IntegralTemplateArgument>
1000CompilerType::GetIntegralTemplateArgument(size_t idx, bool expand_pack) const {
1001 if (IsValid())
1002 if (auto type_system_sp = GetTypeSystem())
1003 return type_system_sp->GetIntegralTemplateArgument(m_type, idx, expand_pack);
1004 return std::nullopt;
1005}
1006
1008 if (IsValid())
1009 if (auto type_system_sp = GetTypeSystem())
1010 return type_system_sp->GetTypeForFormatters(m_type);
1011 return CompilerType();
1012}
1013
1015 if (IsValid())
1016 if (auto type_system_sp = GetTypeSystem())
1017 return type_system_sp->GetPromotedIntegerType(m_type);
1018 return CompilerType();
1019}
1020
1022 if (IsValid())
1023 if (auto type_system_sp = GetTypeSystem())
1024 return type_system_sp->ShouldPrintAsOneLiner(m_type, valobj);
1025 return eLazyBoolCalculate;
1026}
1027
1029 if (IsValid())
1030 if (auto type_system_sp = GetTypeSystem())
1031 return type_system_sp->IsMeaninglessWithoutDynamicResolution(m_type);
1032 return false;
1033}
1034
1035// Get the index of the child of "clang_type" whose name matches. This function
1036// doesn't descend into the children, but only looks one level deep and name
1037// matches can include base class names.
1038
1039llvm::Expected<uint32_t>
1041 bool omit_empty_base_classes) const {
1042 if (IsValid() && !name.empty()) {
1043 if (auto type_system_sp = GetTypeSystem())
1044 return type_system_sp->GetIndexOfChildWithName(m_type, name,
1045 omit_empty_base_classes);
1046 }
1047 return llvm::createStringErrorV("type has no child named '{0}'", name);
1048}
1049
1050// Dumping types
1051
1053 const DataExtractor &data,
1054 lldb::offset_t byte_offset, size_t byte_size,
1055 uint32_t bitfield_bit_size,
1056 uint32_t bitfield_bit_offset,
1057 ExecutionContextScope *exe_scope) {
1058 if (IsValid())
1059 if (auto type_system_sp = GetTypeSystem())
1060 return type_system_sp->DumpTypeValue(
1061 m_type, *s, format, data, byte_offset, byte_size, bitfield_bit_size,
1062 bitfield_bit_offset, exe_scope);
1063 return false;
1064}
1065
1067 if (IsValid())
1068 if (auto type_system_sp = GetTypeSystem())
1069 type_system_sp->DumpTypeDescription(m_type, level);
1070}
1071
1073 lldb::DescriptionLevel level) const {
1074 if (IsValid())
1075 if (auto type_system_sp = GetTypeSystem())
1076 type_system_sp->DumpTypeDescription(m_type, *s, level);
1077}
1078
1079#ifndef NDEBUG
1080LLVM_DUMP_METHOD void CompilerType::dump() const {
1081 if (IsValid())
1082 if (auto type_system_sp = GetTypeSystem())
1083 return type_system_sp->dump(m_type);
1084 llvm::errs() << "<invalid>\n";
1085}
1086#endif
1087
1089 lldb::offset_t data_byte_offset,
1090 size_t data_byte_size, Scalar &value,
1091 ExecutionContextScope *exe_scope) const {
1092 if (!IsValid())
1093 return false;
1094
1095 if (IsAggregateType()) {
1096 return false; // Aggregate types don't have scalar values
1097 } else {
1098 // FIXME: check that type is scalar instead of checking encoding?
1099 lldb::Encoding encoding = GetEncoding();
1100
1101 if (encoding == lldb::eEncodingInvalid || (GetTypeInfo() & eTypeIsComplex))
1102 return false;
1103
1104 auto byte_size_or_err = GetByteSize(exe_scope);
1105 if (!byte_size_or_err) {
1107 GetLog(LLDBLog::Types), byte_size_or_err.takeError(),
1108 "Cannot get value as scalar: Cannot determine type size: {0}");
1109 return false;
1110 }
1111 uint64_t byte_size = *byte_size_or_err;
1112
1113 // A bit or byte size of 0 is not a bug, but it doesn't make sense to read a
1114 // scalar of zero size.
1115 if (byte_size == 0)
1116 return false;
1117
1118 lldb::offset_t offset = data_byte_offset;
1119 switch (encoding) {
1121 break;
1123 break;
1125 if (byte_size <= sizeof(unsigned long long)) {
1126 uint64_t uval64 = data.GetMaxU64(&offset, byte_size);
1127 if (byte_size <= sizeof(unsigned int)) {
1128 value = (unsigned int)uval64;
1129 return true;
1130 } else if (byte_size <= sizeof(unsigned long)) {
1131 value = (unsigned long)uval64;
1132 return true;
1133 } else if (byte_size <= sizeof(unsigned long long)) {
1134 value = (unsigned long long)uval64;
1135 return true;
1136 } else
1137 value.Clear();
1138 }
1139 break;
1140
1142 if (byte_size <= sizeof(long long)) {
1143 int64_t sval64 = data.GetMaxS64(&offset, byte_size);
1144 if (byte_size <= sizeof(int)) {
1145 value = (int)sval64;
1146 return true;
1147 } else if (byte_size <= sizeof(long)) {
1148 value = (long)sval64;
1149 return true;
1150 } else if (byte_size <= sizeof(long long)) {
1151 value = (long long)sval64;
1152 return true;
1153 } else
1154 value.Clear();
1155 }
1156 break;
1157
1159 if (byte_size <= sizeof(long double)) {
1160 uint32_t u32;
1161 uint64_t u64;
1162 if (byte_size == sizeof(float)) {
1163 if (sizeof(float) == sizeof(uint32_t)) {
1164 u32 = data.GetU32(&offset);
1165 value = *((float *)&u32);
1166 return true;
1167 } else if (sizeof(float) == sizeof(uint64_t)) {
1168 u64 = data.GetU64(&offset);
1169 value = *((float *)&u64);
1170 return true;
1171 }
1172 } else if (byte_size == sizeof(double)) {
1173 if (sizeof(double) == sizeof(uint32_t)) {
1174 u32 = data.GetU32(&offset);
1175 value = *((double *)&u32);
1176 return true;
1177 } else if (sizeof(double) == sizeof(uint64_t)) {
1178 u64 = data.GetU64(&offset);
1179 value = *((double *)&u64);
1180 return true;
1181 }
1182 } else if (byte_size == sizeof(long double)) {
1183 if (sizeof(long double) == sizeof(uint32_t)) {
1184 u32 = data.GetU32(&offset);
1185 value = *((long double *)&u32);
1186 return true;
1187 } else if (sizeof(long double) == sizeof(uint64_t)) {
1188 u64 = data.GetU64(&offset);
1189 value = *((long double *)&u64);
1190 return true;
1191 }
1192 }
1193 }
1194 break;
1195 }
1196 }
1197 return false;
1198}
1199
1202 : m_type_system(type_system.GetSharedPointer()), m_type(type) {
1203 assert(Verify() && "verification failed");
1204}
1205
1208 : m_type_system(type_system), m_type(type) {
1209 assert(Verify() && "verification failed");
1210}
1211
1212#ifndef NDEBUG
1214 if (!IsValid())
1215 return true;
1216 if (auto type_system_sp = GetTypeSystem())
1217 return type_system_sp->Verify(m_type);
1218 return true;
1219}
1220#endif
1221
1225
1227 const CompilerType::TypeSystemSPWrapper &other) const {
1228 if (!m_typesystem_sp && !other.m_typesystem_sp)
1229 return true;
1230 if (m_typesystem_sp && other.m_typesystem_sp)
1231 return m_typesystem_sp.get() == other.m_typesystem_sp.get();
1232 return false;
1233}
1234
1239
1241 const lldb_private::CompilerType &rhs) {
1242 return lhs.GetTypeSystem() == rhs.GetTypeSystem() &&
1243 lhs.GetOpaqueQualType() == rhs.GetOpaqueQualType();
1244}
1245
1247 const lldb_private::CompilerType &rhs) {
1248 return !(lhs == rhs);
1249}
#define LLDB_LOG_ERRORV(log, error,...)
Definition Log.h:421
Represents a generic declaration context in a program.
Represents a generic declaration such as a function declaration.
This is a minimal wrapper of a TypeSystem shared pointer as returned by CompilerType which conventien...
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.
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 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)
CompilerType AddConstModifier() const
Return a new CompilerType adds a const modifier to this type if this type is valid and the type syste...
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.
llvm::Expected< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
CompilerDecl GetStaticFieldWithName(llvm::StringRef name) const
lldb::TypeClass GetTypeClass() const
lldb::opaque_compiler_type_t GetOpaqueQualType() const
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
CompilerType GetDirectNestedTypeWithName(llvm::StringRef name) const
bool GetPtrAuthAddressDiversity() const
uint32_t GetNumVirtualBaseClasses() const
lldb::Encoding GetEncoding() 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.
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
Checks if the type is eligible for integral promotion.
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
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.
CompilerType AddAddressSpaceModifier(uint32_t address_space) const
Return a new CompilerType that qualifies this type with the given target address space if this type i...
llvm::Expected< CompilerType > GetDereferencedType(ExecutionContext *exe_ctx, std::string &deref_name, uint32_t &deref_byte_size, int32_t &deref_byte_offset, ValueObject *valobj, uint64_t &language_flags) const
lldb::Format GetFormat() const
CompilerType GetFullyUnqualifiedType() const
ConstString GetMangledTypeName() const
unsigned GetTypeQualifiers() const
CompilerType GetDirectBaseClassAtIndex(size_t idx, uint32_t *bit_offset_ptr) const
std::optional< size_t > GetTypeBitAlign(ExecutionContextScope *exe_scope) 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
bool IsFloatingPointType() const
Returns true for floating point types (including complex floats).
uint32_t GetTypeInfo(CompilerType *pointee_or_element_compiler_type=nullptr) const
std::optional< uint32_t > GetTargetAddressSpace() 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 ...
llvm::Expected< uint32_t > GetIndexOfChildWithName(llvm::StringRef name, bool omit_empty_base_classes) const
Lookup a child given a name.
unsigned GetPtrAuthDiscriminator() const
bool IsRealFloatingPointType() const
Returns true for non-complex float types.
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
bool CompareTypes(CompilerType rhs) const
llvm::Expected< uint64_t > GetBitSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bits.
bool IsEnumerationType() const
This is used when you don't care about the signedness of the enum.
unsigned GetPtrAuthKey() const
CompilerType GetPromotedIntegerType() const
If the type is promotable, returns the type promoted to a larger integer type according to the type s...
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
llvm::Expected< ChildTypeInfo > GetChildAtIndex(ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers, bool omit_empty_base_classes, bool ignore_array_bounds, ValueObject *valobj) const
Get the child at idx, such as a field, a base class or a pointee.
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.
std::optional< uint64_t > GetByteStride() const
Return the stride of the type in bytes.
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
CompilerType(lldb::TypeSystemWP type_system, lldb::opaque_compiler_type_t type)
Creates a CompilerType with the given TypeSystem and opaque compiler type.
bool IsPointerType(CompilerType *pointee_type=nullptr) const
CompilerType AddPtrAuthModifier(uint32_t payload) const
Return a new CompilerType adds a ptrauth modifier from the given 32-bit opaque payload to this type i...
A uniqued constant string class.
Definition ConstString.h:40
An data extractor class.
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:72
virtual CompilerDecl GetStaticFieldWithName(lldb::opaque_compiler_type_t type, llvm::StringRef name)
Definition TypeSystem.h:390
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
bool operator!=(const Address &lhs, const Address &rhs)
Definition Address.cpp:1011
bool operator==(const Address &lhs, const Address &rhs)
Definition Address.cpp:1005
void * opaque_compiler_type_t
Definition lldb-types.h:91
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
BasicType
Basic types enumeration for the public API SBType::GetBasicType().
Format
Display format definitions.
uint64_t offset_t
Definition lldb-types.h:86
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
@ eEncodingSint
signed integer
std::weak_ptr< lldb_private::TypeSystem > TypeSystemWP
A child of a type, as returned by CompilerType::GetChildAtIndex.