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 context
391 // (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, reinterpret_cast<uint8_t *>(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 so we need to update it to say that we
537 // now have a forward declaration since that is what we created above.
538 if (m_compiler_type.IsValid())
539 m_flags.compiler_type_resolve_state = eResolveStateForward;
542 // Check if we have a forward reference to a class/struct/union/enum?
543 if (compiler_type_resolve_state == eResolveStateLayout ||
544 compiler_type_resolve_state == eResolveStateFull) {
545 // Check if we have a forward reference to a class/struct/union/enum?
546 if (m_compiler_type.IsValid() &&
547 m_flags.compiler_type_resolve_state < compiler_type_resolve_state) {
548 m_flags.compiler_type_resolve_state = eResolveStateFull;
549 if (!m_compiler_type.IsDefined()) {
550 // We have a forward declaration, we need to resolve it to a complete
552 m_symbol_file->CompleteType(m_compiler_type);
557 // If we have an encoding type, then we need to make sure it is resolved
559 if (m_encoding_uid != LLDB_INVALID_UID) {
560 if (encoding_type == nullptr)
561 encoding_type = GetEncodingType();
563 ResolveState encoding_compiler_type_resolve_state =
564 compiler_type_resolve_state;
566 if (compiler_type_resolve_state == eResolveStateLayout) {
567 switch (m_encoding_uid_type) {
568 case eEncodingIsPointerUID:
569 case eEncodingIsLValueReferenceUID:
570 case eEncodingIsRValueReferenceUID:
571 encoding_compiler_type_resolve_state = eResolveStateForward;
577 encoding_type->ResolveClangType(encoding_compiler_type_resolve_state);
580 return m_compiler_type.IsValid();
582 uint32_t Type::GetEncodingMask() {
583 uint32_t encoding_mask = 1u << m_encoding_uid_type;
584 Type *encoding_type = GetEncodingType();
585 assert(encoding_type != this);
587 encoding_mask |= encoding_type->GetEncodingMask();
588 return encoding_mask;
591 CompilerType Type::GetFullCompilerType() {
592 ResolveClangType(eResolveStateFull);
593 return m_compiler_type;
596 CompilerType Type::GetLayoutCompilerType() {
597 ResolveClangType(eResolveStateLayout);
598 return m_compiler_type;
601 CompilerType Type::GetForwardCompilerType() {
602 ResolveClangType(eResolveStateForward);
603 return m_compiler_type;
606 int Type::Compare(const Type &a, const Type &b) {
607 // Just compare the UID values for now...
608 lldb::user_id_t a_uid = a.GetID();
609 lldb::user_id_t b_uid = b.GetID();
617 ConstString Type::GetQualifiedName() {
618 return GetForwardCompilerType().GetConstTypeName();
621 bool Type::GetTypeScopeAndBasename(const llvm::StringRef& name,
622 llvm::StringRef &scope,
623 llvm::StringRef &basename,
624 TypeClass &type_class) {
625 type_class = eTypeClassAny;
631 if (basename.consume_front("struct "))
632 type_class = eTypeClassStruct;
633 else if (basename.consume_front("class "))
634 type_class = eTypeClassClass;
635 else if (basename.consume_front("union "))
636 type_class = eTypeClassUnion;
637 else if (basename.consume_front("enum "))
638 type_class = eTypeClassEnumeration;
639 else if (basename.consume_front("typedef "))
640 type_class = eTypeClassTypedef;
642 size_t namespace_separator = basename.find("::");
643 if (namespace_separator == llvm::StringRef::npos)
646 size_t template_begin = basename.find('<');
647 while (namespace_separator != llvm::StringRef::npos) {
648 if (template_begin != llvm::StringRef::npos &&
649 namespace_separator > template_begin) {
650 size_t template_depth = 1;
651 llvm::StringRef template_arg =
652 basename.drop_front(template_begin + 1);
653 while (template_depth > 0 && !template_arg.empty()) {
654 if (template_arg.front() == '<')
656 else if (template_arg.front() == '>')
658 template_arg = template_arg.drop_front(1);
660 if (template_depth != 0)
661 return false; // We have an invalid type name. Bail out.
662 if (template_arg.empty())
663 break; // The template ends at the end of the full name.
664 basename = template_arg;
666 basename = basename.drop_front(namespace_separator + 2);
668 template_begin = basename.find('<');
669 namespace_separator = basename.find("::");
671 if (basename.size() < name.size()) {
672 scope = name.take_front(name.size() - basename.size());
678 ModuleSP Type::GetModule() {
680 return m_symbol_file->GetObjectFile()->GetModule();
684 TypeAndOrName::TypeAndOrName() : m_type_pair(), m_type_name() {}
686 TypeAndOrName::TypeAndOrName(TypeSP &in_type_sp) : m_type_pair(in_type_sp) {
688 m_type_name = in_type_sp->GetName();
691 TypeAndOrName::TypeAndOrName(const char *in_type_str)
692 : m_type_name(in_type_str) {}
694 TypeAndOrName::TypeAndOrName(const TypeAndOrName &rhs)
695 : m_type_pair(rhs.m_type_pair), m_type_name(rhs.m_type_name) {}
697 TypeAndOrName::TypeAndOrName(ConstString &in_type_const_string)
698 : m_type_name(in_type_const_string) {}
700 TypeAndOrName &TypeAndOrName::operator=(const TypeAndOrName &rhs) {
702 m_type_name = rhs.m_type_name;
703 m_type_pair = rhs.m_type_pair;
708 bool TypeAndOrName::operator==(const TypeAndOrName &other) const {
709 if (m_type_pair != other.m_type_pair)
711 if (m_type_name != other.m_type_name)
716 bool TypeAndOrName::operator!=(const TypeAndOrName &other) const {
717 if (m_type_pair != other.m_type_pair)
719 if (m_type_name != other.m_type_name)
724 ConstString TypeAndOrName::GetName() const {
728 return m_type_pair.GetName();
729 return ConstString("<invalid>");
732 void TypeAndOrName::SetName(const ConstString &type_name) {
733 m_type_name = type_name;
736 void TypeAndOrName::SetName(const char *type_name_cstr) {
737 m_type_name.SetCString(type_name_cstr);
740 void TypeAndOrName::SetTypeSP(lldb::TypeSP type_sp) {
741 m_type_pair.SetType(type_sp);
743 m_type_name = m_type_pair.GetName();
746 void TypeAndOrName::SetCompilerType(CompilerType compiler_type) {
747 m_type_pair.SetType(compiler_type);
749 m_type_name = m_type_pair.GetName();
752 bool TypeAndOrName::IsEmpty() const {
753 if ((bool)m_type_name || (bool)m_type_pair)
759 void TypeAndOrName::Clear() {
764 bool TypeAndOrName::HasName() const { return (bool)m_type_name; }
766 bool TypeAndOrName::HasTypeSP() const {
767 return m_type_pair.GetTypeSP().get() != nullptr;
770 bool TypeAndOrName::HasCompilerType() const {
771 return m_type_pair.GetCompilerType().IsValid();
774 TypeImpl::TypeImpl() : m_module_wp(), m_static_type(), m_dynamic_type() {}
776 TypeImpl::TypeImpl(const TypeImpl &rhs)
777 : m_module_wp(rhs.m_module_wp), m_static_type(rhs.m_static_type),
778 m_dynamic_type(rhs.m_dynamic_type) {}
780 TypeImpl::TypeImpl(const lldb::TypeSP &type_sp)
781 : m_module_wp(), m_static_type(), m_dynamic_type() {
785 TypeImpl::TypeImpl(const CompilerType &compiler_type)
786 : m_module_wp(), m_static_type(), m_dynamic_type() {
787 SetType(compiler_type);
790 TypeImpl::TypeImpl(const lldb::TypeSP &type_sp, const CompilerType &dynamic)
791 : m_module_wp(), m_static_type(type_sp), m_dynamic_type(dynamic) {
792 SetType(type_sp, dynamic);
795 TypeImpl::TypeImpl(const CompilerType &static_type,
796 const CompilerType &dynamic_type)
797 : m_module_wp(), m_static_type(), m_dynamic_type() {
798 SetType(static_type, dynamic_type);
801 TypeImpl::TypeImpl(const TypePair &pair, const CompilerType &dynamic)
802 : m_module_wp(), m_static_type(), m_dynamic_type() {
803 SetType(pair, dynamic);
806 void TypeImpl::SetType(const lldb::TypeSP &type_sp) {
807 m_static_type.SetType(type_sp);
809 m_module_wp = type_sp->GetModule();
811 m_module_wp = lldb::ModuleWP();
814 void TypeImpl::SetType(const CompilerType &compiler_type) {
815 m_module_wp = lldb::ModuleWP();
816 m_static_type.SetType(compiler_type);
819 void TypeImpl::SetType(const lldb::TypeSP &type_sp,
820 const CompilerType &dynamic) {
822 m_dynamic_type = dynamic;
825 void TypeImpl::SetType(const CompilerType &compiler_type,
826 const CompilerType &dynamic) {
827 m_module_wp = lldb::ModuleWP();
828 m_static_type.SetType(compiler_type);
829 m_dynamic_type = dynamic;
832 void TypeImpl::SetType(const TypePair &pair, const CompilerType &dynamic) {
833 m_module_wp = pair.GetModule();
834 m_static_type = pair;
835 m_dynamic_type = dynamic;
838 TypeImpl &TypeImpl::operator=(const TypeImpl &rhs) {
840 m_module_wp = rhs.m_module_wp;
841 m_static_type = rhs.m_static_type;
842 m_dynamic_type = rhs.m_dynamic_type;
847 bool TypeImpl::CheckModule(lldb::ModuleSP &module_sp) const {
848 // Check if we have a module for this type. If we do and the shared pointer
849 // is can be successfully initialized with m_module_wp, return true. Else
850 // return false if we didn't have a module, or if we had a module and it has
851 // been deleted. Any functions doing anything with a TypeSP in this TypeImpl
852 // class should call this function and only do anything with the ivars if
853 // this function returns true. If we have a module, the "module_sp" will be
854 // filled in with a strong reference to the module so that the module will at
855 // least stay around long enough for the type query to succeed.
856 module_sp = m_module_wp.lock();
858 lldb::ModuleWP empty_module_wp;
859 // If either call to "std::weak_ptr::owner_before(...) value returns true,
860 // this indicates that m_module_wp once contained (possibly still does) a
861 // reference to a valid shared pointer. This helps us know if we had a
862 // valid reference to a section which is now invalid because the module it
863 // was in was deleted
864 if (empty_module_wp.owner_before(m_module_wp) ||
865 m_module_wp.owner_before(empty_module_wp)) {
866 // m_module_wp had a valid reference to a module, but all strong
867 // references have been released and the module has been deleted
871 // We either successfully locked the module, or didn't have one to begin with
875 bool TypeImpl::operator==(const TypeImpl &rhs) const {
876 return m_static_type == rhs.m_static_type &&
877 m_dynamic_type == rhs.m_dynamic_type;
880 bool TypeImpl::operator!=(const TypeImpl &rhs) const {
881 return m_static_type != rhs.m_static_type ||
882 m_dynamic_type != rhs.m_dynamic_type;
885 bool TypeImpl::IsValid() const {
886 // just a name is not valid
888 if (CheckModule(module_sp))
889 return m_static_type.IsValid() || m_dynamic_type.IsValid();
893 TypeImpl::operator bool() const { return IsValid(); }
895 void TypeImpl::Clear() {
896 m_module_wp = lldb::ModuleWP();
897 m_static_type.Clear();
898 m_dynamic_type.Clear();
901 ConstString TypeImpl::GetName() const {
903 if (CheckModule(module_sp)) {
905 return m_dynamic_type.GetTypeName();
906 return m_static_type.GetName();
908 return ConstString();
911 ConstString TypeImpl::GetDisplayTypeName() const {
913 if (CheckModule(module_sp)) {
915 return m_dynamic_type.GetDisplayTypeName();
916 return m_static_type.GetDisplayTypeName();
918 return ConstString();
921 TypeImpl TypeImpl::GetPointerType() const {
923 if (CheckModule(module_sp)) {
924 if (m_dynamic_type.IsValid()) {
925 return TypeImpl(m_static_type.GetPointerType(),
926 m_dynamic_type.GetPointerType());
928 return TypeImpl(m_static_type.GetPointerType());
933 TypeImpl TypeImpl::GetPointeeType() const {
935 if (CheckModule(module_sp)) {
936 if (m_dynamic_type.IsValid()) {
937 return TypeImpl(m_static_type.GetPointeeType(),
938 m_dynamic_type.GetPointeeType());
940 return TypeImpl(m_static_type.GetPointeeType());
945 TypeImpl TypeImpl::GetReferenceType() const {
947 if (CheckModule(module_sp)) {
948 if (m_dynamic_type.IsValid()) {
949 return TypeImpl(m_static_type.GetReferenceType(),
950 m_dynamic_type.GetLValueReferenceType());
952 return TypeImpl(m_static_type.GetReferenceType());
957 TypeImpl TypeImpl::GetTypedefedType() const {
959 if (CheckModule(module_sp)) {
960 if (m_dynamic_type.IsValid()) {
961 return TypeImpl(m_static_type.GetTypedefedType(),
962 m_dynamic_type.GetTypedefedType());
964 return TypeImpl(m_static_type.GetTypedefedType());
969 TypeImpl TypeImpl::GetDereferencedType() const {
971 if (CheckModule(module_sp)) {
972 if (m_dynamic_type.IsValid()) {
973 return TypeImpl(m_static_type.GetDereferencedType(),
974 m_dynamic_type.GetNonReferenceType());
976 return TypeImpl(m_static_type.GetDereferencedType());
981 TypeImpl TypeImpl::GetUnqualifiedType() const {
983 if (CheckModule(module_sp)) {
984 if (m_dynamic_type.IsValid()) {
985 return TypeImpl(m_static_type.GetUnqualifiedType(),
986 m_dynamic_type.GetFullyUnqualifiedType());
988 return TypeImpl(m_static_type.GetUnqualifiedType());
993 TypeImpl TypeImpl::GetCanonicalType() const {
995 if (CheckModule(module_sp)) {
996 if (m_dynamic_type.IsValid()) {
997 return TypeImpl(m_static_type.GetCanonicalType(),
998 m_dynamic_type.GetCanonicalType());
1000 return TypeImpl(m_static_type.GetCanonicalType());
1005 CompilerType TypeImpl::GetCompilerType(bool prefer_dynamic) {
1007 if (CheckModule(module_sp)) {
1008 if (prefer_dynamic) {
1009 if (m_dynamic_type.IsValid())
1010 return m_dynamic_type;
1012 return m_static_type.GetCompilerType();
1014 return CompilerType();
1017 TypeSystem *TypeImpl::GetTypeSystem(bool prefer_dynamic) {
1019 if (CheckModule(module_sp)) {
1020 if (prefer_dynamic) {
1021 if (m_dynamic_type.IsValid())
1022 return m_dynamic_type.GetTypeSystem();
1024 return m_static_type.GetCompilerType().GetTypeSystem();
1029 bool TypeImpl::GetDescription(lldb_private::Stream &strm,
1030 lldb::DescriptionLevel description_level) {
1032 if (CheckModule(module_sp)) {
1033 if (m_dynamic_type.IsValid()) {
1034 strm.Printf("Dynamic:\n");
1035 m_dynamic_type.DumpTypeDescription(&strm);
1036 strm.Printf("\nStatic:\n");
1038 m_static_type.GetCompilerType().DumpTypeDescription(&strm);
1040 strm.PutCString("Invalid TypeImpl module for type has been deleted\n");
1045 bool TypeMemberFunctionImpl::IsValid() {
1046 return m_type.IsValid() && m_kind != lldb::eMemberFunctionKindUnknown;
1049 ConstString TypeMemberFunctionImpl::GetName() const { return m_name; }
1051 ConstString TypeMemberFunctionImpl::GetMangledName() const {
1052 return m_decl.GetMangledName();
1055 CompilerType TypeMemberFunctionImpl::GetType() const { return m_type; }
1057 lldb::MemberFunctionKind TypeMemberFunctionImpl::GetKind() const {
1061 bool TypeMemberFunctionImpl::GetDescription(Stream &stream) {
1063 case lldb::eMemberFunctionKindUnknown:
1065 case lldb::eMemberFunctionKindConstructor:
1066 stream.Printf("constructor for %s",
1067 m_type.GetTypeName().AsCString("<unknown>"));
1069 case lldb::eMemberFunctionKindDestructor:
1070 stream.Printf("destructor for %s",
1071 m_type.GetTypeName().AsCString("<unknown>"));
1073 case lldb::eMemberFunctionKindInstanceMethod:
1074 stream.Printf("instance method %s of type %s", m_name.AsCString(),
1075 m_decl.GetDeclContext().GetName().AsCString());
1077 case lldb::eMemberFunctionKindStaticMethod:
1078 stream.Printf("static method %s of type %s", m_name.AsCString(),
1079 m_decl.GetDeclContext().GetName().AsCString());
1085 CompilerType TypeMemberFunctionImpl::GetReturnType() const {
1087 return m_type.GetFunctionReturnType();
1088 return m_decl.GetFunctionReturnType();
1091 size_t TypeMemberFunctionImpl::GetNumArguments() const {
1093 return m_type.GetNumberOfFunctionArguments();
1095 return m_decl.GetNumFunctionArguments();
1098 CompilerType TypeMemberFunctionImpl::GetArgumentAtIndex(size_t idx) const {
1100 return m_type.GetFunctionArgumentAtIndex(idx);
1102 return m_decl.GetFunctionArgumentType(idx);
1105 TypeEnumMemberImpl::TypeEnumMemberImpl(const lldb::TypeImplSP &integer_type_sp,
1106 const ConstString &name,
1107 const llvm::APSInt &value)
1108 : m_integer_type_sp(integer_type_sp), m_name(name), m_value(value),
1109 m_valid((bool)name && (bool)integer_type_sp)