1 //===-- Type.cpp ------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
14 // Other libraries and framework includes
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/Scalar.h"
18 #include "lldb/Utility/DataBufferHeap.h"
19 #include "lldb/Utility/DataExtractor.h"
20 #include "lldb/Utility/StreamString.h"
22 #include "lldb/Symbol/CompilerType.h"
23 #include "lldb/Symbol/ObjectFile.h"
24 #include "lldb/Symbol/SymbolContextScope.h"
25 #include "lldb/Symbol/SymbolFile.h"
26 #include "lldb/Symbol/SymbolVendor.h"
27 #include "lldb/Symbol/Type.h"
28 #include "lldb/Symbol/TypeList.h"
29 #include "lldb/Symbol/TypeSystem.h"
31 #include "lldb/Target/ExecutionContext.h"
32 #include "lldb/Target/Process.h"
33 #include "lldb/Target/Target.h"
35 #include "llvm/ADT/StringRef.h"
37 #include "clang/AST/Decl.h"
38 #include "clang/AST/DeclObjC.h"
41 using namespace lldb_private;
43 void CompilerContext::Dump() const {
45 case CompilerContextKind::Invalid:
48 case CompilerContextKind::TranslationUnit:
49 printf("TranslationUnit");
51 case CompilerContextKind::Module:
54 case CompilerContextKind::Namespace:
57 case CompilerContextKind::Class:
60 case CompilerContextKind::Structure:
63 case CompilerContextKind::Union:
66 case CompilerContextKind::Function:
69 case CompilerContextKind::Variable:
72 case CompilerContextKind::Enumeration:
73 printf("Enumeration");
75 case CompilerContextKind::Typedef:
79 printf("(\"%s\")\n", name.GetCString());
82 class TypeAppendVisitor {
84 TypeAppendVisitor(TypeListImpl &type_list) : m_type_list(type_list) {}
86 bool operator()(const lldb::TypeSP &type) {
87 m_type_list.Append(TypeImplSP(new TypeImpl(type)));
92 TypeListImpl &m_type_list;
95 void TypeListImpl::Append(const lldb_private::TypeList &type_list) {
96 TypeAppendVisitor cb(*this);
97 type_list.ForEach(cb);
100 SymbolFileType::SymbolFileType(SymbolFile &symbol_file,
101 const lldb::TypeSP &type_sp)
102 : UserID(type_sp ? type_sp->GetID() : LLDB_INVALID_UID),
103 m_symbol_file(symbol_file), m_type_sp(type_sp) {}
105 Type *SymbolFileType::GetType() {
107 Type *resolved_type = m_symbol_file.ResolveTypeUID(GetID());
109 m_type_sp = resolved_type->shared_from_this();
111 return m_type_sp.get();
114 Type::Type(lldb::user_id_t uid, SymbolFile *symbol_file,
115 const ConstString &name, uint64_t byte_size,
116 SymbolContextScope *context, user_id_t encoding_uid,
117 EncodingDataType encoding_uid_type, const Declaration &decl,
118 const CompilerType &compiler_type,
119 ResolveState compiler_type_resolve_state)
120 : std::enable_shared_from_this<Type>(), UserID(uid), m_name(name),
121 m_symbol_file(symbol_file), m_context(context), m_encoding_type(nullptr),
122 m_encoding_uid(encoding_uid), m_encoding_uid_type(encoding_uid_type),
123 m_byte_size(byte_size), m_decl(decl), m_compiler_type(compiler_type) {
124 m_flags.compiler_type_resolve_state =
125 (compiler_type ? compiler_type_resolve_state : eResolveStateUnresolved);
126 m_flags.is_complete_objc_class = false;
130 : std::enable_shared_from_this<Type>(), UserID(0), m_name("<INVALID TYPE>"),
131 m_symbol_file(nullptr), m_context(nullptr), m_encoding_type(nullptr),
132 m_encoding_uid(LLDB_INVALID_UID), m_encoding_uid_type(eEncodingInvalid),
133 m_byte_size(0), m_decl(), m_compiler_type() {
134 m_flags.compiler_type_resolve_state = eResolveStateUnresolved;
135 m_flags.is_complete_objc_class = false;
138 Type::Type(const Type &rhs)
139 : std::enable_shared_from_this<Type>(rhs), UserID(rhs), m_name(rhs.m_name),
140 m_symbol_file(rhs.m_symbol_file), m_context(rhs.m_context),
141 m_encoding_type(rhs.m_encoding_type), m_encoding_uid(rhs.m_encoding_uid),
142 m_encoding_uid_type(rhs.m_encoding_uid_type),
143 m_byte_size(rhs.m_byte_size), m_decl(rhs.m_decl),
144 m_compiler_type(rhs.m_compiler_type), m_flags(rhs.m_flags) {}
146 const Type &Type::operator=(const Type &rhs) {
152 void Type::GetDescription(Stream *s, lldb::DescriptionLevel level,
154 *s << "id = " << (const UserID &)*this;
156 // Call the name accessor to make sure we resolve the type name
158 const ConstString &type_name = GetName();
160 *s << ", name = \"" << type_name << '"';
161 ConstString qualified_type_name(GetQualifiedName());
162 if (qualified_type_name != type_name) {
163 *s << ", qualified = \"" << qualified_type_name << '"';
168 // Call the get byte size accesor so we resolve our byte size
170 s->Printf(", byte-size = %" PRIu64, m_byte_size);
171 bool show_fullpaths = (level == lldb::eDescriptionLevelVerbose);
172 m_decl.Dump(s, show_fullpaths);
174 if (m_compiler_type.IsValid()) {
175 *s << ", compiler_type = \"";
176 GetForwardCompilerType().DumpTypeDescription(s);
178 } else if (m_encoding_uid != LLDB_INVALID_UID) {
179 s->Printf(", type_uid = 0x%8.8" PRIx64, m_encoding_uid);
180 switch (m_encoding_uid_type) {
181 case eEncodingInvalid:
184 s->PutCString(" (unresolved type)");
186 case eEncodingIsConstUID:
187 s->PutCString(" (unresolved const type)");
189 case eEncodingIsRestrictUID:
190 s->PutCString(" (unresolved restrict type)");
192 case eEncodingIsVolatileUID:
193 s->PutCString(" (unresolved volatile type)");
195 case eEncodingIsTypedefUID:
196 s->PutCString(" (unresolved typedef)");
198 case eEncodingIsPointerUID:
199 s->PutCString(" (unresolved pointer)");
201 case eEncodingIsLValueReferenceUID:
202 s->PutCString(" (unresolved L value reference)");
204 case eEncodingIsRValueReferenceUID:
205 s->PutCString(" (unresolved R value reference)");
207 case eEncodingIsSyntheticUID:
208 s->PutCString(" (synthetic type)");
214 void Type::Dump(Stream *s, bool show_context) {
215 s->Printf("%p: ", static_cast<void *>(this));
217 *s << "Type" << static_cast<const UserID &>(*this) << ' ';
219 *s << ", name = \"" << m_name << "\"";
221 if (m_byte_size != 0)
222 s->Printf(", size = %" PRIu64, m_byte_size);
224 if (show_context && m_context != nullptr) {
225 s->PutCString(", context = ( ");
226 m_context->DumpSymbolContext(s);
230 bool show_fullpaths = false;
231 m_decl.Dump(s, show_fullpaths);
233 if (m_compiler_type.IsValid()) {
234 *s << ", compiler_type = " << m_compiler_type.GetOpaqueQualType() << ' ';
235 GetForwardCompilerType().DumpTypeDescription(s);
236 } else if (m_encoding_uid != LLDB_INVALID_UID) {
237 *s << ", type_data = " << (uint64_t)m_encoding_uid;
238 switch (m_encoding_uid_type) {
239 case eEncodingInvalid:
242 s->PutCString(" (unresolved type)");
244 case eEncodingIsConstUID:
245 s->PutCString(" (unresolved const type)");
247 case eEncodingIsRestrictUID:
248 s->PutCString(" (unresolved restrict type)");
250 case eEncodingIsVolatileUID:
251 s->PutCString(" (unresolved volatile type)");
253 case eEncodingIsTypedefUID:
254 s->PutCString(" (unresolved typedef)");
256 case eEncodingIsPointerUID:
257 s->PutCString(" (unresolved pointer)");
259 case eEncodingIsLValueReferenceUID:
260 s->PutCString(" (unresolved L value reference)");
262 case eEncodingIsRValueReferenceUID:
263 s->PutCString(" (unresolved R value reference)");
265 case eEncodingIsSyntheticUID:
266 s->PutCString(" (synthetic type)");
273 // s->Printf(", access = %u", m_access);
277 const ConstString &Type::GetName() {
279 m_name = GetForwardCompilerType().GetConstTypeName();
283 void Type::DumpTypeName(Stream *s) { GetName().Dump(s, "<invalid-type-name>"); }
285 void Type::DumpValue(ExecutionContext *exe_ctx, Stream *s,
286 const DataExtractor &data, uint32_t data_byte_offset,
287 bool show_types, bool show_summary, bool verbose,
288 lldb::Format format) {
289 if (ResolveClangType(eResolveStateForward)) {
293 s->Printf("Type{0x%8.8" PRIx64 "} ", GetID());
298 GetForwardCompilerType().DumpValue(
299 exe_ctx, s, format == lldb::eFormatDefault ? GetFormat() : format, data,
300 data_byte_offset, GetByteSize(),
301 0, // Bitfield bit size
302 0, // Bitfield bit offset
303 show_types, show_summary, verbose, 0);
307 Type *Type::GetEncodingType() {
308 if (m_encoding_type == nullptr && m_encoding_uid != LLDB_INVALID_UID)
309 m_encoding_type = m_symbol_file->ResolveTypeUID(m_encoding_uid);
310 return m_encoding_type;
313 uint64_t Type::GetByteSize() {
314 if (m_byte_size == 0) {
315 switch (m_encoding_uid_type) {
316 case eEncodingInvalid:
317 case eEncodingIsSyntheticUID:
320 case eEncodingIsConstUID:
321 case eEncodingIsRestrictUID:
322 case eEncodingIsVolatileUID:
323 case eEncodingIsTypedefUID: {
324 Type *encoding_type = GetEncodingType();
326 m_byte_size = encoding_type->GetByteSize();
327 if (m_byte_size == 0)
328 m_byte_size = GetLayoutCompilerType().GetByteSize(nullptr);
331 // If we are a pointer or reference, then this is just a pointer size;
332 case eEncodingIsPointerUID:
333 case eEncodingIsLValueReferenceUID:
334 case eEncodingIsRValueReferenceUID: {
336 if (m_symbol_file->GetObjectFile()->GetArchitecture(arch))
337 m_byte_size = arch.GetAddressByteSize();
344 uint32_t Type::GetNumChildren(bool omit_empty_base_classes) {
345 return GetForwardCompilerType().GetNumChildren(omit_empty_base_classes);
348 bool Type::IsAggregateType() {
349 return GetForwardCompilerType().IsAggregateType();
352 lldb::TypeSP Type::GetTypedefType() {
353 lldb::TypeSP type_sp;
355 Type *typedef_type = m_symbol_file->ResolveTypeUID(m_encoding_uid);
357 type_sp = typedef_type->shared_from_this();
362 lldb::Format Type::GetFormat() { return GetForwardCompilerType().GetFormat(); }
364 lldb::Encoding Type::GetEncoding(uint64_t &count) {
365 // Make sure we resolve our type if it already hasn't been.
366 return GetForwardCompilerType().GetEncoding(count);
369 bool Type::DumpValueInMemory(ExecutionContext *exe_ctx, Stream *s,
370 lldb::addr_t address, AddressType address_type,
371 bool show_types, bool show_summary, bool verbose) {
372 if (address != LLDB_INVALID_ADDRESS) {
374 Target *target = nullptr;
376 target = exe_ctx->GetTargetPtr();
378 data.SetByteOrder(target->GetArchitecture().GetByteOrder());
379 if (ReadFromMemory(exe_ctx, address, address_type, data)) {
380 DumpValue(exe_ctx, s, data, 0, show_types, show_summary, verbose);
387 bool Type::ReadFromMemory(ExecutionContext *exe_ctx, lldb::addr_t addr,
388 AddressType address_type, DataExtractor &data) {
389 if (address_type == eAddressTypeFile) {
390 // Can't convert a file address to anything valid without more
391 // context (which Module it came from)
395 const uint64_t byte_size = GetByteSize();
396 if (data.GetByteSize() < byte_size) {
397 lldb::DataBufferSP data_sp(new DataBufferHeap(byte_size, '\0'));
398 data.SetData(data_sp);
401 uint8_t *dst = const_cast<uint8_t *>(data.PeekData(0, byte_size));
402 if (dst != nullptr) {
403 if (address_type == eAddressTypeHost) {
404 // The address is an address in this process, so just copy it
407 memcpy(dst, (uint8_t *)nullptr + addr, byte_size);
411 Process *process = exe_ctx->GetProcessPtr();
414 return exe_ctx->GetProcessPtr()->ReadMemory(addr, dst, byte_size,
423 bool Type::WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t addr,
424 AddressType address_type, DataExtractor &data) {
428 TypeList *Type::GetTypeList() { return GetSymbolFile()->GetTypeList(); }
430 const Declaration &Type::GetDeclaration() const { return m_decl; }
432 bool Type::ResolveClangType(ResolveState compiler_type_resolve_state) {
433 // TODO: This needs to consider the correct type system to use.
434 Type *encoding_type = nullptr;
435 if (!m_compiler_type.IsValid()) {
436 encoding_type = GetEncodingType();
438 switch (m_encoding_uid_type) {
439 case eEncodingIsUID: {
440 CompilerType encoding_compiler_type =
441 encoding_type->GetForwardCompilerType();
442 if (encoding_compiler_type.IsValid()) {
443 m_compiler_type = encoding_compiler_type;
444 m_flags.compiler_type_resolve_state =
445 encoding_type->m_flags.compiler_type_resolve_state;
449 case eEncodingIsConstUID:
451 encoding_type->GetForwardCompilerType().AddConstModifier();
454 case eEncodingIsRestrictUID:
456 encoding_type->GetForwardCompilerType().AddRestrictModifier();
459 case eEncodingIsVolatileUID:
461 encoding_type->GetForwardCompilerType().AddVolatileModifier();
464 case eEncodingIsTypedefUID:
465 m_compiler_type = encoding_type->GetForwardCompilerType().CreateTypedef(
466 m_name.AsCString("__lldb_invalid_typedef_name"),
467 GetSymbolFile()->GetDeclContextContainingUID(GetID()));
471 case eEncodingIsPointerUID:
473 encoding_type->GetForwardCompilerType().GetPointerType();
476 case eEncodingIsLValueReferenceUID:
478 encoding_type->GetForwardCompilerType().GetLValueReferenceType();
481 case eEncodingIsRValueReferenceUID:
483 encoding_type->GetForwardCompilerType().GetRValueReferenceType();
487 llvm_unreachable("Unhandled encoding_data_type.");
490 // We have no encoding type, return void?
491 TypeSystem *type_system =
492 m_symbol_file->GetTypeSystemForLanguage(eLanguageTypeC);
493 CompilerType void_compiler_type =
494 type_system->GetBasicTypeFromAST(eBasicTypeVoid);
495 switch (m_encoding_uid_type) {
497 m_compiler_type = void_compiler_type;
500 case eEncodingIsConstUID:
501 m_compiler_type = void_compiler_type.AddConstModifier();
504 case eEncodingIsRestrictUID:
505 m_compiler_type = void_compiler_type.AddRestrictModifier();
508 case eEncodingIsVolatileUID:
509 m_compiler_type = void_compiler_type.AddVolatileModifier();
512 case eEncodingIsTypedefUID:
513 m_compiler_type = void_compiler_type.CreateTypedef(
514 m_name.AsCString("__lldb_invalid_typedef_name"),
515 GetSymbolFile()->GetDeclContextContainingUID(GetID()));
518 case eEncodingIsPointerUID:
519 m_compiler_type = void_compiler_type.GetPointerType();
522 case eEncodingIsLValueReferenceUID:
523 m_compiler_type = void_compiler_type.GetLValueReferenceType();
526 case eEncodingIsRValueReferenceUID:
527 m_compiler_type = void_compiler_type.GetRValueReferenceType();
531 llvm_unreachable("Unhandled encoding_data_type.");
535 // When we have a EncodingUID, our "m_flags.compiler_type_resolve_state" is
536 // set to eResolveStateUnresolved
537 // so we need to update it to say that we now have a forward declaration
538 // since that is what we created
540 if (m_compiler_type.IsValid())
541 m_flags.compiler_type_resolve_state = eResolveStateForward;
544 // Check if we have a forward reference to a class/struct/union/enum?
545 if (compiler_type_resolve_state == eResolveStateLayout ||
546 compiler_type_resolve_state == eResolveStateFull) {
547 // Check if we have a forward reference to a class/struct/union/enum?
548 if (m_compiler_type.IsValid() &&
549 m_flags.compiler_type_resolve_state < compiler_type_resolve_state) {
550 m_flags.compiler_type_resolve_state = eResolveStateFull;
551 if (!m_compiler_type.IsDefined()) {
552 // We have a forward declaration, we need to resolve it to a complete
554 m_symbol_file->CompleteType(m_compiler_type);
559 // If we have an encoding type, then we need to make sure it is
560 // resolved appropriately.
561 if (m_encoding_uid != LLDB_INVALID_UID) {
562 if (encoding_type == nullptr)
563 encoding_type = GetEncodingType();
565 ResolveState encoding_compiler_type_resolve_state =
566 compiler_type_resolve_state;
568 if (compiler_type_resolve_state == eResolveStateLayout) {
569 switch (m_encoding_uid_type) {
570 case eEncodingIsPointerUID:
571 case eEncodingIsLValueReferenceUID:
572 case eEncodingIsRValueReferenceUID:
573 encoding_compiler_type_resolve_state = eResolveStateForward;
579 encoding_type->ResolveClangType(encoding_compiler_type_resolve_state);
582 return m_compiler_type.IsValid();
584 uint32_t Type::GetEncodingMask() {
585 uint32_t encoding_mask = 1u << m_encoding_uid_type;
586 Type *encoding_type = GetEncodingType();
587 assert(encoding_type != this);
589 encoding_mask |= encoding_type->GetEncodingMask();
590 return encoding_mask;
593 CompilerType Type::GetFullCompilerType() {
594 ResolveClangType(eResolveStateFull);
595 return m_compiler_type;
598 CompilerType Type::GetLayoutCompilerType() {
599 ResolveClangType(eResolveStateLayout);
600 return m_compiler_type;
603 CompilerType Type::GetForwardCompilerType() {
604 ResolveClangType(eResolveStateForward);
605 return m_compiler_type;
608 int Type::Compare(const Type &a, const Type &b) {
609 // Just compare the UID values for now...
610 lldb::user_id_t a_uid = a.GetID();
611 lldb::user_id_t b_uid = b.GetID();
619 ConstString Type::GetQualifiedName() {
620 return GetForwardCompilerType().GetConstTypeName();
623 bool Type::GetTypeScopeAndBasename(const llvm::StringRef& name,
624 llvm::StringRef &scope,
625 llvm::StringRef &basename,
626 TypeClass &type_class) {
627 type_class = eTypeClassAny;
633 if (basename.consume_front("struct "))
634 type_class = eTypeClassStruct;
635 else if (basename.consume_front("class "))
636 type_class = eTypeClassClass;
637 else if (basename.consume_front("union "))
638 type_class = eTypeClassUnion;
639 else if (basename.consume_front("enum "))
640 type_class = eTypeClassEnumeration;
641 else if (basename.consume_front("typedef "))
642 type_class = eTypeClassTypedef;
644 size_t namespace_separator = basename.find("::");
645 if (namespace_separator == llvm::StringRef::npos)
648 size_t template_begin = basename.find('<');
649 while (namespace_separator != llvm::StringRef::npos) {
650 if (template_begin != llvm::StringRef::npos &&
651 namespace_separator > template_begin) {
652 size_t template_depth = 1;
653 llvm::StringRef template_arg =
654 basename.drop_front(template_begin + 1);
655 while (template_depth > 0 && !template_arg.empty()) {
656 if (template_arg.front() == '<')
658 else if (template_arg.front() == '>')
660 template_arg = template_arg.drop_front(1);
662 if (template_depth != 0)
663 return false; // We have an invalid type name. Bail out.
664 if (template_arg.empty())
665 break; // The template ends at the end of the full name.
666 basename = template_arg;
668 basename = basename.drop_front(namespace_separator + 2);
670 template_begin = basename.find('<');
671 namespace_separator = basename.find("::");
673 if (basename.size() < name.size()) {
674 scope = name.take_front(name.size() - basename.size());
680 ModuleSP Type::GetModule() {
682 return m_symbol_file->GetObjectFile()->GetModule();
686 TypeAndOrName::TypeAndOrName() : m_type_pair(), m_type_name() {}
688 TypeAndOrName::TypeAndOrName(TypeSP &in_type_sp) : m_type_pair(in_type_sp) {
690 m_type_name = in_type_sp->GetName();
693 TypeAndOrName::TypeAndOrName(const char *in_type_str)
694 : m_type_name(in_type_str) {}
696 TypeAndOrName::TypeAndOrName(const TypeAndOrName &rhs)
697 : m_type_pair(rhs.m_type_pair), m_type_name(rhs.m_type_name) {}
699 TypeAndOrName::TypeAndOrName(ConstString &in_type_const_string)
700 : m_type_name(in_type_const_string) {}
702 TypeAndOrName &TypeAndOrName::operator=(const TypeAndOrName &rhs) {
704 m_type_name = rhs.m_type_name;
705 m_type_pair = rhs.m_type_pair;
710 bool TypeAndOrName::operator==(const TypeAndOrName &other) const {
711 if (m_type_pair != other.m_type_pair)
713 if (m_type_name != other.m_type_name)
718 bool TypeAndOrName::operator!=(const TypeAndOrName &other) const {
719 if (m_type_pair != other.m_type_pair)
721 if (m_type_name != other.m_type_name)
726 ConstString TypeAndOrName::GetName() const {
730 return m_type_pair.GetName();
731 return ConstString("<invalid>");
734 void TypeAndOrName::SetName(const ConstString &type_name) {
735 m_type_name = type_name;
738 void TypeAndOrName::SetName(const char *type_name_cstr) {
739 m_type_name.SetCString(type_name_cstr);
742 void TypeAndOrName::SetTypeSP(lldb::TypeSP type_sp) {
743 m_type_pair.SetType(type_sp);
745 m_type_name = m_type_pair.GetName();
748 void TypeAndOrName::SetCompilerType(CompilerType compiler_type) {
749 m_type_pair.SetType(compiler_type);
751 m_type_name = m_type_pair.GetName();
754 bool TypeAndOrName::IsEmpty() const {
755 if ((bool)m_type_name || (bool)m_type_pair)
761 void TypeAndOrName::Clear() {
766 bool TypeAndOrName::HasName() const { return (bool)m_type_name; }
768 bool TypeAndOrName::HasTypeSP() const {
769 return m_type_pair.GetTypeSP().get() != nullptr;
772 bool TypeAndOrName::HasCompilerType() const {
773 return m_type_pair.GetCompilerType().IsValid();
776 TypeImpl::TypeImpl() : m_module_wp(), m_static_type(), m_dynamic_type() {}
778 TypeImpl::TypeImpl(const TypeImpl &rhs)
779 : m_module_wp(rhs.m_module_wp), m_static_type(rhs.m_static_type),
780 m_dynamic_type(rhs.m_dynamic_type) {}
782 TypeImpl::TypeImpl(const lldb::TypeSP &type_sp)
783 : m_module_wp(), m_static_type(), m_dynamic_type() {
787 TypeImpl::TypeImpl(const CompilerType &compiler_type)
788 : m_module_wp(), m_static_type(), m_dynamic_type() {
789 SetType(compiler_type);
792 TypeImpl::TypeImpl(const lldb::TypeSP &type_sp, const CompilerType &dynamic)
793 : m_module_wp(), m_static_type(type_sp), m_dynamic_type(dynamic) {
794 SetType(type_sp, dynamic);
797 TypeImpl::TypeImpl(const CompilerType &static_type,
798 const CompilerType &dynamic_type)
799 : m_module_wp(), m_static_type(), m_dynamic_type() {
800 SetType(static_type, dynamic_type);
803 TypeImpl::TypeImpl(const TypePair &pair, const CompilerType &dynamic)
804 : m_module_wp(), m_static_type(), m_dynamic_type() {
805 SetType(pair, dynamic);
808 void TypeImpl::SetType(const lldb::TypeSP &type_sp) {
809 m_static_type.SetType(type_sp);
811 m_module_wp = type_sp->GetModule();
813 m_module_wp = lldb::ModuleWP();
816 void TypeImpl::SetType(const CompilerType &compiler_type) {
817 m_module_wp = lldb::ModuleWP();
818 m_static_type.SetType(compiler_type);
821 void TypeImpl::SetType(const lldb::TypeSP &type_sp,
822 const CompilerType &dynamic) {
824 m_dynamic_type = dynamic;
827 void TypeImpl::SetType(const CompilerType &compiler_type,
828 const CompilerType &dynamic) {
829 m_module_wp = lldb::ModuleWP();
830 m_static_type.SetType(compiler_type);
831 m_dynamic_type = dynamic;
834 void TypeImpl::SetType(const TypePair &pair, const CompilerType &dynamic) {
835 m_module_wp = pair.GetModule();
836 m_static_type = pair;
837 m_dynamic_type = dynamic;
840 TypeImpl &TypeImpl::operator=(const TypeImpl &rhs) {
842 m_module_wp = rhs.m_module_wp;
843 m_static_type = rhs.m_static_type;
844 m_dynamic_type = rhs.m_dynamic_type;
849 bool TypeImpl::CheckModule(lldb::ModuleSP &module_sp) const {
850 // Check if we have a module for this type. If we do and the shared pointer is
851 // can be successfully initialized with m_module_wp, return true. Else return
853 // if we didn't have a module, or if we had a module and it has been deleted.
855 // functions doing anything with a TypeSP in this TypeImpl class should call
857 // function and only do anything with the ivars if this function returns true.
859 // we have a module, the "module_sp" will be filled in with a strong reference
861 // module so that the module will at least stay around long enough for the
864 module_sp = m_module_wp.lock();
866 lldb::ModuleWP empty_module_wp;
867 // If either call to "std::weak_ptr::owner_before(...) value returns true,
869 // indicates that m_module_wp once contained (possibly still does) a
871 // to a valid shared pointer. This helps us know if we had a valid reference
873 // a section which is now invalid because the module it was in was deleted
874 if (empty_module_wp.owner_before(m_module_wp) ||
875 m_module_wp.owner_before(empty_module_wp)) {
876 // m_module_wp had a valid reference to a module, but all strong
878 // have been released and the module has been deleted
882 // We either successfully locked the module, or didn't have one to begin with
886 bool TypeImpl::operator==(const TypeImpl &rhs) const {
887 return m_static_type == rhs.m_static_type &&
888 m_dynamic_type == rhs.m_dynamic_type;
891 bool TypeImpl::operator!=(const TypeImpl &rhs) const {
892 return m_static_type != rhs.m_static_type ||
893 m_dynamic_type != rhs.m_dynamic_type;
896 bool TypeImpl::IsValid() const {
897 // just a name is not valid
899 if (CheckModule(module_sp))
900 return m_static_type.IsValid() || m_dynamic_type.IsValid();
904 TypeImpl::operator bool() const { return IsValid(); }
906 void TypeImpl::Clear() {
907 m_module_wp = lldb::ModuleWP();
908 m_static_type.Clear();
909 m_dynamic_type.Clear();
912 ConstString TypeImpl::GetName() const {
914 if (CheckModule(module_sp)) {
916 return m_dynamic_type.GetTypeName();
917 return m_static_type.GetName();
919 return ConstString();
922 ConstString TypeImpl::GetDisplayTypeName() const {
924 if (CheckModule(module_sp)) {
926 return m_dynamic_type.GetDisplayTypeName();
927 return m_static_type.GetDisplayTypeName();
929 return ConstString();
932 TypeImpl TypeImpl::GetPointerType() const {
934 if (CheckModule(module_sp)) {
935 if (m_dynamic_type.IsValid()) {
936 return TypeImpl(m_static_type.GetPointerType(),
937 m_dynamic_type.GetPointerType());
939 return TypeImpl(m_static_type.GetPointerType());
944 TypeImpl TypeImpl::GetPointeeType() const {
946 if (CheckModule(module_sp)) {
947 if (m_dynamic_type.IsValid()) {
948 return TypeImpl(m_static_type.GetPointeeType(),
949 m_dynamic_type.GetPointeeType());
951 return TypeImpl(m_static_type.GetPointeeType());
956 TypeImpl TypeImpl::GetReferenceType() const {
958 if (CheckModule(module_sp)) {
959 if (m_dynamic_type.IsValid()) {
960 return TypeImpl(m_static_type.GetReferenceType(),
961 m_dynamic_type.GetLValueReferenceType());
963 return TypeImpl(m_static_type.GetReferenceType());
968 TypeImpl TypeImpl::GetTypedefedType() const {
970 if (CheckModule(module_sp)) {
971 if (m_dynamic_type.IsValid()) {
972 return TypeImpl(m_static_type.GetTypedefedType(),
973 m_dynamic_type.GetTypedefedType());
975 return TypeImpl(m_static_type.GetTypedefedType());
980 TypeImpl TypeImpl::GetDereferencedType() const {
982 if (CheckModule(module_sp)) {
983 if (m_dynamic_type.IsValid()) {
984 return TypeImpl(m_static_type.GetDereferencedType(),
985 m_dynamic_type.GetNonReferenceType());
987 return TypeImpl(m_static_type.GetDereferencedType());
992 TypeImpl TypeImpl::GetUnqualifiedType() const {
994 if (CheckModule(module_sp)) {
995 if (m_dynamic_type.IsValid()) {
996 return TypeImpl(m_static_type.GetUnqualifiedType(),
997 m_dynamic_type.GetFullyUnqualifiedType());
999 return TypeImpl(m_static_type.GetUnqualifiedType());
1004 TypeImpl TypeImpl::GetCanonicalType() const {
1006 if (CheckModule(module_sp)) {
1007 if (m_dynamic_type.IsValid()) {
1008 return TypeImpl(m_static_type.GetCanonicalType(),
1009 m_dynamic_type.GetCanonicalType());
1011 return TypeImpl(m_static_type.GetCanonicalType());
1016 CompilerType TypeImpl::GetCompilerType(bool prefer_dynamic) {
1018 if (CheckModule(module_sp)) {
1019 if (prefer_dynamic) {
1020 if (m_dynamic_type.IsValid())
1021 return m_dynamic_type;
1023 return m_static_type.GetCompilerType();
1025 return CompilerType();
1028 TypeSystem *TypeImpl::GetTypeSystem(bool prefer_dynamic) {
1030 if (CheckModule(module_sp)) {
1031 if (prefer_dynamic) {
1032 if (m_dynamic_type.IsValid())
1033 return m_dynamic_type.GetTypeSystem();
1035 return m_static_type.GetCompilerType().GetTypeSystem();
1040 bool TypeImpl::GetDescription(lldb_private::Stream &strm,
1041 lldb::DescriptionLevel description_level) {
1043 if (CheckModule(module_sp)) {
1044 if (m_dynamic_type.IsValid()) {
1045 strm.Printf("Dynamic:\n");
1046 m_dynamic_type.DumpTypeDescription(&strm);
1047 strm.Printf("\nStatic:\n");
1049 m_static_type.GetCompilerType().DumpTypeDescription(&strm);
1051 strm.PutCString("Invalid TypeImpl module for type has been deleted\n");
1056 bool TypeMemberFunctionImpl::IsValid() {
1057 return m_type.IsValid() && m_kind != lldb::eMemberFunctionKindUnknown;
1060 ConstString TypeMemberFunctionImpl::GetName() const { return m_name; }
1062 ConstString TypeMemberFunctionImpl::GetMangledName() const {
1063 return m_decl.GetMangledName();
1066 CompilerType TypeMemberFunctionImpl::GetType() const { return m_type; }
1068 lldb::MemberFunctionKind TypeMemberFunctionImpl::GetKind() const {
1072 bool TypeMemberFunctionImpl::GetDescription(Stream &stream) {
1074 case lldb::eMemberFunctionKindUnknown:
1076 case lldb::eMemberFunctionKindConstructor:
1077 stream.Printf("constructor for %s",
1078 m_type.GetTypeName().AsCString("<unknown>"));
1080 case lldb::eMemberFunctionKindDestructor:
1081 stream.Printf("destructor for %s",
1082 m_type.GetTypeName().AsCString("<unknown>"));
1084 case lldb::eMemberFunctionKindInstanceMethod:
1085 stream.Printf("instance method %s of type %s", m_name.AsCString(),
1086 m_decl.GetDeclContext().GetName().AsCString());
1088 case lldb::eMemberFunctionKindStaticMethod:
1089 stream.Printf("static method %s of type %s", m_name.AsCString(),
1090 m_decl.GetDeclContext().GetName().AsCString());
1096 CompilerType TypeMemberFunctionImpl::GetReturnType() const {
1098 return m_type.GetFunctionReturnType();
1099 return m_decl.GetFunctionReturnType();
1102 size_t TypeMemberFunctionImpl::GetNumArguments() const {
1104 return m_type.GetNumberOfFunctionArguments();
1106 return m_decl.GetNumFunctionArguments();
1109 CompilerType TypeMemberFunctionImpl::GetArgumentAtIndex(size_t idx) const {
1111 return m_type.GetFunctionArgumentAtIndex(idx);
1113 return m_decl.GetFunctionArgumentType(idx);
1116 TypeEnumMemberImpl::TypeEnumMemberImpl(const lldb::TypeImplSP &integer_type_sp,
1117 const ConstString &name,
1118 const llvm::APSInt &value)
1119 : m_integer_type_sp(integer_type_sp), m_name(name), m_value(value),
1120 m_valid((bool)name && (bool)integer_type_sp)