1 //===-- Type.cpp ----------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
12 #include "lldb/Core/Module.h"
13 #include "lldb/Utility/DataBufferHeap.h"
14 #include "lldb/Utility/DataExtractor.h"
15 #include "lldb/Utility/LLDBLog.h"
16 #include "lldb/Utility/Log.h"
17 #include "lldb/Utility/Scalar.h"
18 #include "lldb/Utility/StreamString.h"
20 #include "lldb/Symbol/CompilerType.h"
21 #include "lldb/Symbol/ObjectFile.h"
22 #include "lldb/Symbol/SymbolContextScope.h"
23 #include "lldb/Symbol/SymbolFile.h"
24 #include "lldb/Symbol/SymbolVendor.h"
25 #include "lldb/Symbol/Type.h"
26 #include "lldb/Symbol/TypeList.h"
27 #include "lldb/Symbol/TypeSystem.h"
29 #include "lldb/Target/ExecutionContext.h"
30 #include "lldb/Target/Process.h"
31 #include "lldb/Target/Target.h"
33 #include "llvm/ADT/StringRef.h"
36 using namespace lldb_private
;
38 bool lldb_private::contextMatches(llvm::ArrayRef
<CompilerContext
> context_chain
,
39 llvm::ArrayRef
<CompilerContext
> pattern
) {
40 auto ctx
= context_chain
.begin();
41 auto ctx_end
= context_chain
.end();
42 for (const CompilerContext
&pat
: pattern
) {
43 // Early exit if the pattern is too long.
47 // Skip any number of module matches.
48 if (pat
.kind
== CompilerContextKind::AnyModule
) {
49 // Greedily match 0..n modules.
50 ctx
= std::find_if(ctx
, ctx_end
, [](const CompilerContext
&ctx
) {
51 return ctx
.kind
!= CompilerContextKind::Module
;
55 // See if there is a kind mismatch; they should have 1 bit in common.
56 if (((uint16_t)ctx
->kind
& (uint16_t)pat
.kind
) == 0)
58 // The name is ignored for AnyModule, but not for AnyType.
59 if (pat
.kind
!= CompilerContextKind::AnyModule
&& ctx
->name
!= pat
.name
)
67 void CompilerContext::Dump(Stream
&s
) const {
72 case CompilerContextKind::TranslationUnit
:
73 s
<< "TranslationUnit";
75 case CompilerContextKind::Module
:
78 case CompilerContextKind::Namespace
:
81 case CompilerContextKind::Class
:
84 case CompilerContextKind::Struct
:
87 case CompilerContextKind::Union
:
90 case CompilerContextKind::Function
:
93 case CompilerContextKind::Variable
:
96 case CompilerContextKind::Enum
:
99 case CompilerContextKind::Typedef
:
102 case CompilerContextKind::AnyModule
:
105 case CompilerContextKind::AnyType
:
109 s
<< "(" << name
<< ")";
112 class TypeAppendVisitor
{
114 TypeAppendVisitor(TypeListImpl
&type_list
) : m_type_list(type_list
) {}
116 bool operator()(const lldb::TypeSP
&type
) {
117 m_type_list
.Append(TypeImplSP(new TypeImpl(type
)));
122 TypeListImpl
&m_type_list
;
125 void TypeListImpl::Append(const lldb_private::TypeList
&type_list
) {
126 TypeAppendVisitor
cb(*this);
127 type_list
.ForEach(cb
);
130 SymbolFileType::SymbolFileType(SymbolFile
&symbol_file
,
131 const lldb::TypeSP
&type_sp
)
132 : UserID(type_sp
? type_sp
->GetID() : LLDB_INVALID_UID
),
133 m_symbol_file(symbol_file
), m_type_sp(type_sp
) {}
135 Type
*SymbolFileType::GetType() {
137 Type
*resolved_type
= m_symbol_file
.ResolveTypeUID(GetID());
139 m_type_sp
= resolved_type
->shared_from_this();
141 return m_type_sp
.get();
144 Type::Type(lldb::user_id_t uid
, SymbolFile
*symbol_file
, ConstString name
,
145 std::optional
<uint64_t> byte_size
, SymbolContextScope
*context
,
146 user_id_t encoding_uid
, EncodingDataType encoding_uid_type
,
147 const Declaration
&decl
, const CompilerType
&compiler_type
,
148 ResolveState compiler_type_resolve_state
, uint32_t opaque_payload
)
149 : std::enable_shared_from_this
<Type
>(), UserID(uid
), m_name(name
),
150 m_symbol_file(symbol_file
), m_context(context
),
151 m_encoding_uid(encoding_uid
), m_encoding_uid_type(encoding_uid_type
),
152 m_decl(decl
), m_compiler_type(compiler_type
),
153 m_compiler_type_resolve_state(compiler_type
? compiler_type_resolve_state
154 : ResolveState::Unresolved
),
155 m_payload(opaque_payload
) {
157 m_byte_size
= *byte_size
;
158 m_byte_size_has_value
= true;
161 m_byte_size_has_value
= false;
166 : std::enable_shared_from_this
<Type
>(), UserID(0), m_name("<INVALID TYPE>"),
169 m_byte_size_has_value
= false;
172 void Type::GetDescription(Stream
*s
, lldb::DescriptionLevel level
,
173 bool show_name
, ExecutionContextScope
*exe_scope
) {
174 *s
<< "id = " << (const UserID
&)*this;
176 // Call the name accessor to make sure we resolve the type name
178 ConstString type_name
= GetName();
180 *s
<< ", name = \"" << type_name
<< '"';
181 ConstString
qualified_type_name(GetQualifiedName());
182 if (qualified_type_name
!= type_name
) {
183 *s
<< ", qualified = \"" << qualified_type_name
<< '"';
188 // Call the get byte size accessor so we resolve our byte size
189 if (GetByteSize(exe_scope
))
190 s
->Printf(", byte-size = %" PRIu64
, m_byte_size
);
191 bool show_fullpaths
= (level
== lldb::eDescriptionLevelVerbose
);
192 m_decl
.Dump(s
, show_fullpaths
);
194 if (m_compiler_type
.IsValid()) {
195 *s
<< ", compiler_type = \"";
196 GetForwardCompilerType().DumpTypeDescription(s
);
198 } else if (m_encoding_uid
!= LLDB_INVALID_UID
) {
199 s
->Printf(", type_uid = 0x%8.8" PRIx64
, m_encoding_uid
);
200 switch (m_encoding_uid_type
) {
201 case eEncodingInvalid
:
204 s
->PutCString(" (unresolved type)");
206 case eEncodingIsConstUID
:
207 s
->PutCString(" (unresolved const type)");
209 case eEncodingIsRestrictUID
:
210 s
->PutCString(" (unresolved restrict type)");
212 case eEncodingIsVolatileUID
:
213 s
->PutCString(" (unresolved volatile type)");
215 case eEncodingIsAtomicUID
:
216 s
->PutCString(" (unresolved atomic type)");
218 case eEncodingIsTypedefUID
:
219 s
->PutCString(" (unresolved typedef)");
221 case eEncodingIsPointerUID
:
222 s
->PutCString(" (unresolved pointer)");
224 case eEncodingIsLValueReferenceUID
:
225 s
->PutCString(" (unresolved L value reference)");
227 case eEncodingIsRValueReferenceUID
:
228 s
->PutCString(" (unresolved R value reference)");
230 case eEncodingIsSyntheticUID
:
231 s
->PutCString(" (synthetic type)");
237 void Type::Dump(Stream
*s
, bool show_context
, lldb::DescriptionLevel level
) {
238 s
->Printf("%p: ", static_cast<void *>(this));
240 *s
<< "Type" << static_cast<const UserID
&>(*this) << ' ';
242 *s
<< ", name = \"" << m_name
<< "\"";
244 if (m_byte_size_has_value
)
245 s
->Printf(", size = %" PRIu64
, m_byte_size
);
247 if (show_context
&& m_context
!= nullptr) {
248 s
->PutCString(", context = ( ");
249 m_context
->DumpSymbolContext(s
);
253 bool show_fullpaths
= false;
254 m_decl
.Dump(s
, show_fullpaths
);
256 if (m_compiler_type
.IsValid()) {
257 *s
<< ", compiler_type = " << m_compiler_type
.GetOpaqueQualType() << ' ';
258 GetForwardCompilerType().DumpTypeDescription(s
, level
);
259 } else if (m_encoding_uid
!= LLDB_INVALID_UID
) {
260 s
->Format(", type_data = {0:x-16}", m_encoding_uid
);
261 switch (m_encoding_uid_type
) {
262 case eEncodingInvalid
:
265 s
->PutCString(" (unresolved type)");
267 case eEncodingIsConstUID
:
268 s
->PutCString(" (unresolved const type)");
270 case eEncodingIsRestrictUID
:
271 s
->PutCString(" (unresolved restrict type)");
273 case eEncodingIsVolatileUID
:
274 s
->PutCString(" (unresolved volatile type)");
276 case eEncodingIsAtomicUID
:
277 s
->PutCString(" (unresolved atomic type)");
279 case eEncodingIsTypedefUID
:
280 s
->PutCString(" (unresolved typedef)");
282 case eEncodingIsPointerUID
:
283 s
->PutCString(" (unresolved pointer)");
285 case eEncodingIsLValueReferenceUID
:
286 s
->PutCString(" (unresolved L value reference)");
288 case eEncodingIsRValueReferenceUID
:
289 s
->PutCString(" (unresolved R value reference)");
291 case eEncodingIsSyntheticUID
:
292 s
->PutCString(" (synthetic type)");
299 // s->Printf(", access = %u", m_access);
303 ConstString
Type::GetName() {
305 m_name
= GetForwardCompilerType().GetTypeName();
309 ConstString
Type::GetBaseName() {
310 return GetForwardCompilerType().GetTypeName(/*BaseOnly*/ true);
313 void Type::DumpTypeName(Stream
*s
) { GetName().Dump(s
, "<invalid-type-name>"); }
315 Type
*Type::GetEncodingType() {
316 if (m_encoding_type
== nullptr && m_encoding_uid
!= LLDB_INVALID_UID
)
317 m_encoding_type
= m_symbol_file
->ResolveTypeUID(m_encoding_uid
);
318 return m_encoding_type
;
321 std::optional
<uint64_t> Type::GetByteSize(ExecutionContextScope
*exe_scope
) {
322 if (m_byte_size_has_value
)
323 return static_cast<uint64_t>(m_byte_size
);
325 switch (m_encoding_uid_type
) {
326 case eEncodingInvalid
:
327 case eEncodingIsSyntheticUID
:
330 case eEncodingIsConstUID
:
331 case eEncodingIsRestrictUID
:
332 case eEncodingIsVolatileUID
:
333 case eEncodingIsAtomicUID
:
334 case eEncodingIsTypedefUID
: {
335 Type
*encoding_type
= GetEncodingType();
337 if (std::optional
<uint64_t> size
=
338 encoding_type
->GetByteSize(exe_scope
)) {
340 m_byte_size_has_value
= true;
341 return static_cast<uint64_t>(m_byte_size
);
344 if (std::optional
<uint64_t> size
=
345 GetLayoutCompilerType().GetByteSize(exe_scope
)) {
347 m_byte_size_has_value
= true;
348 return static_cast<uint64_t>(m_byte_size
);
352 // If we are a pointer or reference, then this is just a pointer size;
353 case eEncodingIsPointerUID
:
354 case eEncodingIsLValueReferenceUID
:
355 case eEncodingIsRValueReferenceUID
: {
356 if (ArchSpec arch
= m_symbol_file
->GetObjectFile()->GetArchitecture()) {
357 m_byte_size
= arch
.GetAddressByteSize();
358 m_byte_size_has_value
= true;
359 return static_cast<uint64_t>(m_byte_size
);
366 uint32_t Type::GetNumChildren(bool omit_empty_base_classes
) {
367 return GetForwardCompilerType().GetNumChildren(omit_empty_base_classes
, nullptr);
370 bool Type::IsAggregateType() {
371 return GetForwardCompilerType().IsAggregateType();
374 bool Type::IsTemplateType() {
375 return GetForwardCompilerType().IsTemplateType();
378 lldb::TypeSP
Type::GetTypedefType() {
379 lldb::TypeSP type_sp
;
381 Type
*typedef_type
= m_symbol_file
->ResolveTypeUID(m_encoding_uid
);
383 type_sp
= typedef_type
->shared_from_this();
388 lldb::Format
Type::GetFormat() { return GetForwardCompilerType().GetFormat(); }
390 lldb::Encoding
Type::GetEncoding(uint64_t &count
) {
391 // Make sure we resolve our type if it already hasn't been.
392 return GetForwardCompilerType().GetEncoding(count
);
395 bool Type::ReadFromMemory(ExecutionContext
*exe_ctx
, lldb::addr_t addr
,
396 AddressType address_type
, DataExtractor
&data
) {
397 if (address_type
== eAddressTypeFile
) {
398 // Can't convert a file address to anything valid without more context
399 // (which Module it came from)
403 const uint64_t byte_size
=
404 GetByteSize(exe_ctx
? exe_ctx
->GetBestExecutionContextScope() : nullptr)
406 if (data
.GetByteSize() < byte_size
) {
407 lldb::DataBufferSP
data_sp(new DataBufferHeap(byte_size
, '\0'));
408 data
.SetData(data_sp
);
411 uint8_t *dst
= const_cast<uint8_t *>(data
.PeekData(0, byte_size
));
412 if (dst
!= nullptr) {
413 if (address_type
== eAddressTypeHost
) {
414 // The address is an address in this process, so just copy it
417 memcpy(dst
, reinterpret_cast<uint8_t *>(addr
), byte_size
);
421 Process
*process
= exe_ctx
->GetProcessPtr();
424 return exe_ctx
->GetProcessPtr()->ReadMemory(addr
, dst
, byte_size
,
433 bool Type::WriteToMemory(ExecutionContext
*exe_ctx
, lldb::addr_t addr
,
434 AddressType address_type
, DataExtractor
&data
) {
438 const Declaration
&Type::GetDeclaration() const { return m_decl
; }
440 bool Type::ResolveCompilerType(ResolveState compiler_type_resolve_state
) {
441 // TODO: This needs to consider the correct type system to use.
442 Type
*encoding_type
= nullptr;
443 if (!m_compiler_type
.IsValid()) {
444 encoding_type
= GetEncodingType();
446 switch (m_encoding_uid_type
) {
447 case eEncodingIsUID
: {
448 CompilerType encoding_compiler_type
=
449 encoding_type
->GetForwardCompilerType();
450 if (encoding_compiler_type
.IsValid()) {
451 m_compiler_type
= encoding_compiler_type
;
452 m_compiler_type_resolve_state
=
453 encoding_type
->m_compiler_type_resolve_state
;
457 case eEncodingIsConstUID
:
459 encoding_type
->GetForwardCompilerType().AddConstModifier();
462 case eEncodingIsRestrictUID
:
464 encoding_type
->GetForwardCompilerType().AddRestrictModifier();
467 case eEncodingIsVolatileUID
:
469 encoding_type
->GetForwardCompilerType().AddVolatileModifier();
472 case eEncodingIsAtomicUID
:
474 encoding_type
->GetForwardCompilerType().GetAtomicType();
477 case eEncodingIsTypedefUID
:
478 m_compiler_type
= encoding_type
->GetForwardCompilerType().CreateTypedef(
479 m_name
.AsCString("__lldb_invalid_typedef_name"),
480 GetSymbolFile()->GetDeclContextContainingUID(GetID()), m_payload
);
484 case eEncodingIsPointerUID
:
486 encoding_type
->GetForwardCompilerType().GetPointerType();
489 case eEncodingIsLValueReferenceUID
:
491 encoding_type
->GetForwardCompilerType().GetLValueReferenceType();
494 case eEncodingIsRValueReferenceUID
:
496 encoding_type
->GetForwardCompilerType().GetRValueReferenceType();
500 llvm_unreachable("Unhandled encoding_data_type.");
503 // We have no encoding type, return void?
504 auto type_system_or_err
=
505 m_symbol_file
->GetTypeSystemForLanguage(eLanguageTypeC
);
506 if (auto err
= type_system_or_err
.takeError()) {
508 GetLog(LLDBLog::Symbols
), std::move(err
),
509 "Unable to construct void type from TypeSystemClang: {0}");
511 CompilerType void_compiler_type
;
512 auto ts
= *type_system_or_err
;
514 void_compiler_type
= ts
->GetBasicTypeFromAST(eBasicTypeVoid
);
515 switch (m_encoding_uid_type
) {
517 m_compiler_type
= void_compiler_type
;
520 case eEncodingIsConstUID
:
521 m_compiler_type
= void_compiler_type
.AddConstModifier();
524 case eEncodingIsRestrictUID
:
525 m_compiler_type
= void_compiler_type
.AddRestrictModifier();
528 case eEncodingIsVolatileUID
:
529 m_compiler_type
= void_compiler_type
.AddVolatileModifier();
532 case eEncodingIsAtomicUID
:
533 m_compiler_type
= void_compiler_type
.GetAtomicType();
536 case eEncodingIsTypedefUID
:
537 m_compiler_type
= void_compiler_type
.CreateTypedef(
538 m_name
.AsCString("__lldb_invalid_typedef_name"),
539 GetSymbolFile()->GetDeclContextContainingUID(GetID()), m_payload
);
542 case eEncodingIsPointerUID
:
543 m_compiler_type
= void_compiler_type
.GetPointerType();
546 case eEncodingIsLValueReferenceUID
:
547 m_compiler_type
= void_compiler_type
.GetLValueReferenceType();
550 case eEncodingIsRValueReferenceUID
:
551 m_compiler_type
= void_compiler_type
.GetRValueReferenceType();
555 llvm_unreachable("Unhandled encoding_data_type.");
560 // When we have a EncodingUID, our "m_flags.compiler_type_resolve_state" is
561 // set to eResolveStateUnresolved so we need to update it to say that we
562 // now have a forward declaration since that is what we created above.
563 if (m_compiler_type
.IsValid())
564 m_compiler_type_resolve_state
= ResolveState::Forward
;
567 // Check if we have a forward reference to a class/struct/union/enum?
568 if (compiler_type_resolve_state
== ResolveState::Layout
||
569 compiler_type_resolve_state
== ResolveState::Full
) {
570 // Check if we have a forward reference to a class/struct/union/enum?
571 if (m_compiler_type
.IsValid() &&
572 m_compiler_type_resolve_state
< compiler_type_resolve_state
) {
573 m_compiler_type_resolve_state
= ResolveState::Full
;
574 if (!m_compiler_type
.IsDefined()) {
575 // We have a forward declaration, we need to resolve it to a complete
577 m_symbol_file
->CompleteType(m_compiler_type
);
582 // If we have an encoding type, then we need to make sure it is resolved
584 if (m_encoding_uid
!= LLDB_INVALID_UID
) {
585 if (encoding_type
== nullptr)
586 encoding_type
= GetEncodingType();
588 ResolveState encoding_compiler_type_resolve_state
=
589 compiler_type_resolve_state
;
591 if (compiler_type_resolve_state
== ResolveState::Layout
) {
592 switch (m_encoding_uid_type
) {
593 case eEncodingIsPointerUID
:
594 case eEncodingIsLValueReferenceUID
:
595 case eEncodingIsRValueReferenceUID
:
596 encoding_compiler_type_resolve_state
= ResolveState::Forward
;
602 encoding_type
->ResolveCompilerType(encoding_compiler_type_resolve_state
);
605 return m_compiler_type
.IsValid();
607 uint32_t Type::GetEncodingMask() {
608 uint32_t encoding_mask
= 1u << m_encoding_uid_type
;
609 Type
*encoding_type
= GetEncodingType();
610 assert(encoding_type
!= this);
612 encoding_mask
|= encoding_type
->GetEncodingMask();
613 return encoding_mask
;
616 CompilerType
Type::GetFullCompilerType() {
617 ResolveCompilerType(ResolveState::Full
);
618 return m_compiler_type
;
621 CompilerType
Type::GetLayoutCompilerType() {
622 ResolveCompilerType(ResolveState::Layout
);
623 return m_compiler_type
;
626 CompilerType
Type::GetForwardCompilerType() {
627 ResolveCompilerType(ResolveState::Forward
);
628 return m_compiler_type
;
631 ConstString
Type::GetQualifiedName() {
632 return GetForwardCompilerType().GetTypeName();
635 bool Type::GetTypeScopeAndBasename(llvm::StringRef name
,
636 llvm::StringRef
&scope
,
637 llvm::StringRef
&basename
,
638 TypeClass
&type_class
) {
639 type_class
= eTypeClassAny
;
645 if (basename
.consume_front("struct "))
646 type_class
= eTypeClassStruct
;
647 else if (basename
.consume_front("class "))
648 type_class
= eTypeClassClass
;
649 else if (basename
.consume_front("union "))
650 type_class
= eTypeClassUnion
;
651 else if (basename
.consume_front("enum "))
652 type_class
= eTypeClassEnumeration
;
653 else if (basename
.consume_front("typedef "))
654 type_class
= eTypeClassTypedef
;
656 size_t namespace_separator
= basename
.find("::");
657 if (namespace_separator
== llvm::StringRef::npos
)
660 size_t template_begin
= basename
.find('<');
661 while (namespace_separator
!= llvm::StringRef::npos
) {
662 if (template_begin
!= llvm::StringRef::npos
&&
663 namespace_separator
> template_begin
) {
664 size_t template_depth
= 1;
665 llvm::StringRef template_arg
=
666 basename
.drop_front(template_begin
+ 1);
667 while (template_depth
> 0 && !template_arg
.empty()) {
668 if (template_arg
.front() == '<')
670 else if (template_arg
.front() == '>')
672 template_arg
= template_arg
.drop_front(1);
674 if (template_depth
!= 0)
675 return false; // We have an invalid type name. Bail out.
676 if (template_arg
.empty())
677 break; // The template ends at the end of the full name.
678 basename
= template_arg
;
680 basename
= basename
.drop_front(namespace_separator
+ 2);
682 template_begin
= basename
.find('<');
683 namespace_separator
= basename
.find("::");
685 if (basename
.size() < name
.size()) {
686 scope
= name
.take_front(name
.size() - basename
.size());
692 ModuleSP
Type::GetModule() {
694 return m_symbol_file
->GetObjectFile()->GetModule();
698 ModuleSP
Type::GetExeModule() {
699 if (m_compiler_type
) {
700 auto ts
= m_compiler_type
.GetTypeSystem();
703 SymbolFile
*symbol_file
= ts
->GetSymbolFile();
705 return symbol_file
->GetObjectFile()->GetModule();
710 TypeAndOrName::TypeAndOrName(TypeSP
&in_type_sp
) {
712 m_compiler_type
= in_type_sp
->GetForwardCompilerType();
713 m_type_name
= in_type_sp
->GetName();
717 TypeAndOrName::TypeAndOrName(const char *in_type_str
)
718 : m_type_name(in_type_str
) {}
720 TypeAndOrName::TypeAndOrName(ConstString
&in_type_const_string
)
721 : m_type_name(in_type_const_string
) {}
723 bool TypeAndOrName::operator==(const TypeAndOrName
&other
) const {
724 if (m_compiler_type
!= other
.m_compiler_type
)
726 if (m_type_name
!= other
.m_type_name
)
731 bool TypeAndOrName::operator!=(const TypeAndOrName
&other
) const {
732 return !(*this == other
);
735 ConstString
TypeAndOrName::GetName() const {
739 return m_compiler_type
.GetTypeName();
740 return ConstString("<invalid>");
743 void TypeAndOrName::SetName(ConstString type_name
) {
744 m_type_name
= type_name
;
747 void TypeAndOrName::SetName(const char *type_name_cstr
) {
748 m_type_name
.SetCString(type_name_cstr
);
751 void TypeAndOrName::SetName(llvm::StringRef type_name
) {
752 m_type_name
.SetString(type_name
);
755 void TypeAndOrName::SetTypeSP(lldb::TypeSP type_sp
) {
757 m_compiler_type
= type_sp
->GetForwardCompilerType();
758 m_type_name
= type_sp
->GetName();
763 void TypeAndOrName::SetCompilerType(CompilerType compiler_type
) {
764 m_compiler_type
= compiler_type
;
766 m_type_name
= m_compiler_type
.GetTypeName();
769 bool TypeAndOrName::IsEmpty() const {
770 return !((bool)m_type_name
|| (bool)m_compiler_type
);
773 void TypeAndOrName::Clear() {
775 m_compiler_type
.Clear();
778 bool TypeAndOrName::HasName() const { return (bool)m_type_name
; }
780 bool TypeAndOrName::HasCompilerType() const {
781 return m_compiler_type
.IsValid();
784 TypeImpl::TypeImpl(const lldb::TypeSP
&type_sp
)
785 : m_module_wp(), m_static_type(), m_dynamic_type() {
789 TypeImpl::TypeImpl(const CompilerType
&compiler_type
)
790 : m_module_wp(), m_static_type(), m_dynamic_type() {
791 SetType(compiler_type
);
794 TypeImpl::TypeImpl(const lldb::TypeSP
&type_sp
, const CompilerType
&dynamic
)
795 : m_module_wp(), m_static_type(), m_dynamic_type(dynamic
) {
796 SetType(type_sp
, dynamic
);
799 TypeImpl::TypeImpl(const CompilerType
&static_type
,
800 const CompilerType
&dynamic_type
)
801 : m_module_wp(), m_static_type(), m_dynamic_type() {
802 SetType(static_type
, dynamic_type
);
805 void TypeImpl::SetType(const lldb::TypeSP
&type_sp
) {
807 m_static_type
= type_sp
->GetForwardCompilerType();
808 m_exe_module_wp
= type_sp
->GetExeModule();
809 m_module_wp
= type_sp
->GetModule();
811 m_static_type
.Clear();
812 m_module_wp
= lldb::ModuleWP();
816 void TypeImpl::SetType(const CompilerType
&compiler_type
) {
817 m_module_wp
= lldb::ModuleWP();
818 m_static_type
= 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
= compiler_type
;
831 m_dynamic_type
= dynamic
;
834 bool TypeImpl::CheckModule(lldb::ModuleSP
&module_sp
) const {
835 return CheckModuleCommon(m_module_wp
, module_sp
);
838 bool TypeImpl::CheckExeModule(lldb::ModuleSP
&module_sp
) const {
839 return CheckModuleCommon(m_exe_module_wp
, module_sp
);
842 bool TypeImpl::CheckModuleCommon(const lldb::ModuleWP
&input_module_wp
,
843 lldb::ModuleSP
&module_sp
) const {
844 // Check if we have a module for this type. If we do and the shared pointer
845 // is can be successfully initialized with m_module_wp, return true. Else
846 // return false if we didn't have a module, or if we had a module and it has
847 // been deleted. Any functions doing anything with a TypeSP in this TypeImpl
848 // class should call this function and only do anything with the ivars if
849 // this function returns true. If we have a module, the "module_sp" will be
850 // filled in with a strong reference to the module so that the module will at
851 // least stay around long enough for the type query to succeed.
852 module_sp
= input_module_wp
.lock();
854 lldb::ModuleWP empty_module_wp
;
855 // If either call to "std::weak_ptr::owner_before(...) value returns true,
856 // this indicates that m_module_wp once contained (possibly still does) a
857 // reference to a valid shared pointer. This helps us know if we had a
858 // valid reference to a section which is now invalid because the module it
859 // was in was deleted
860 if (empty_module_wp
.owner_before(input_module_wp
) ||
861 input_module_wp
.owner_before(empty_module_wp
)) {
862 // input_module_wp had a valid reference to a module, but all strong
863 // references have been released and the module has been deleted
867 // We either successfully locked the module, or didn't have one to begin with
871 bool TypeImpl::operator==(const TypeImpl
&rhs
) const {
872 return m_static_type
== rhs
.m_static_type
&&
873 m_dynamic_type
== rhs
.m_dynamic_type
;
876 bool TypeImpl::operator!=(const TypeImpl
&rhs
) const {
877 return !(*this == rhs
);
880 bool TypeImpl::IsValid() const {
881 // just a name is not valid
883 if (CheckModule(module_sp
))
884 return m_static_type
.IsValid() || m_dynamic_type
.IsValid();
888 TypeImpl::operator bool() const { return IsValid(); }
890 void TypeImpl::Clear() {
891 m_module_wp
= lldb::ModuleWP();
892 m_static_type
.Clear();
893 m_dynamic_type
.Clear();
896 ModuleSP
TypeImpl::GetModule() const {
897 lldb::ModuleSP module_sp
;
898 if (CheckExeModule(module_sp
))
903 ConstString
TypeImpl::GetName() const {
905 if (CheckModule(module_sp
)) {
907 return m_dynamic_type
.GetTypeName();
908 return m_static_type
.GetTypeName();
910 return ConstString();
913 ConstString
TypeImpl::GetDisplayTypeName() const {
915 if (CheckModule(module_sp
)) {
917 return m_dynamic_type
.GetDisplayTypeName();
918 return m_static_type
.GetDisplayTypeName();
920 return ConstString();
923 TypeImpl
TypeImpl::GetPointerType() const {
925 if (CheckModule(module_sp
)) {
926 if (m_dynamic_type
.IsValid()) {
927 return TypeImpl(m_static_type
.GetPointerType(),
928 m_dynamic_type
.GetPointerType());
930 return TypeImpl(m_static_type
.GetPointerType());
935 TypeImpl
TypeImpl::GetPointeeType() const {
937 if (CheckModule(module_sp
)) {
938 if (m_dynamic_type
.IsValid()) {
939 return TypeImpl(m_static_type
.GetPointeeType(),
940 m_dynamic_type
.GetPointeeType());
942 return TypeImpl(m_static_type
.GetPointeeType());
947 TypeImpl
TypeImpl::GetReferenceType() const {
949 if (CheckModule(module_sp
)) {
950 if (m_dynamic_type
.IsValid()) {
951 return TypeImpl(m_static_type
.GetLValueReferenceType(),
952 m_dynamic_type
.GetLValueReferenceType());
954 return TypeImpl(m_static_type
.GetLValueReferenceType());
959 TypeImpl
TypeImpl::GetTypedefedType() const {
961 if (CheckModule(module_sp
)) {
962 if (m_dynamic_type
.IsValid()) {
963 return TypeImpl(m_static_type
.GetTypedefedType(),
964 m_dynamic_type
.GetTypedefedType());
966 return TypeImpl(m_static_type
.GetTypedefedType());
971 TypeImpl
TypeImpl::GetDereferencedType() const {
973 if (CheckModule(module_sp
)) {
974 if (m_dynamic_type
.IsValid()) {
975 return TypeImpl(m_static_type
.GetNonReferenceType(),
976 m_dynamic_type
.GetNonReferenceType());
978 return TypeImpl(m_static_type
.GetNonReferenceType());
983 TypeImpl
TypeImpl::GetUnqualifiedType() const {
985 if (CheckModule(module_sp
)) {
986 if (m_dynamic_type
.IsValid()) {
987 return TypeImpl(m_static_type
.GetFullyUnqualifiedType(),
988 m_dynamic_type
.GetFullyUnqualifiedType());
990 return TypeImpl(m_static_type
.GetFullyUnqualifiedType());
995 TypeImpl
TypeImpl::GetCanonicalType() const {
997 if (CheckModule(module_sp
)) {
998 if (m_dynamic_type
.IsValid()) {
999 return TypeImpl(m_static_type
.GetCanonicalType(),
1000 m_dynamic_type
.GetCanonicalType());
1002 return TypeImpl(m_static_type
.GetCanonicalType());
1007 CompilerType
TypeImpl::GetCompilerType(bool prefer_dynamic
) {
1009 if (CheckModule(module_sp
)) {
1010 if (prefer_dynamic
) {
1011 if (m_dynamic_type
.IsValid())
1012 return m_dynamic_type
;
1014 return m_static_type
;
1016 return CompilerType();
1019 CompilerType::TypeSystemSPWrapper
TypeImpl::GetTypeSystem(bool prefer_dynamic
) {
1021 if (CheckModule(module_sp
)) {
1022 if (prefer_dynamic
) {
1023 if (m_dynamic_type
.IsValid())
1024 return m_dynamic_type
.GetTypeSystem();
1026 return m_static_type
.GetTypeSystem();
1031 bool TypeImpl::GetDescription(lldb_private::Stream
&strm
,
1032 lldb::DescriptionLevel description_level
) {
1034 if (CheckModule(module_sp
)) {
1035 if (m_dynamic_type
.IsValid()) {
1036 strm
.Printf("Dynamic:\n");
1037 m_dynamic_type
.DumpTypeDescription(&strm
);
1038 strm
.Printf("\nStatic:\n");
1040 m_static_type
.DumpTypeDescription(&strm
);
1042 strm
.PutCString("Invalid TypeImpl module for type has been deleted\n");
1047 CompilerType
TypeImpl::FindDirectNestedType(llvm::StringRef name
) {
1049 return CompilerType();
1050 auto type_system
= GetTypeSystem(/*prefer_dynamic*/ false);
1051 auto *symbol_file
= type_system
->GetSymbolFile();
1052 auto decl_context
= type_system
->GetCompilerDeclContextForType(m_static_type
);
1053 if (!decl_context
.IsValid())
1054 return CompilerType();
1055 llvm::DenseSet
<lldb_private::SymbolFile
*> searched_symbol_files
;
1056 TypeMap search_result
;
1057 symbol_file
->FindTypes(ConstString(name
), decl_context
, /*max_matches*/ 1,
1058 searched_symbol_files
, search_result
);
1059 if (search_result
.Empty())
1060 return CompilerType();
1061 return search_result
.GetTypeAtIndex(0)->GetFullCompilerType();
1064 bool TypeMemberFunctionImpl::IsValid() {
1065 return m_type
.IsValid() && m_kind
!= lldb::eMemberFunctionKindUnknown
;
1068 ConstString
TypeMemberFunctionImpl::GetName() const { return m_name
; }
1070 ConstString
TypeMemberFunctionImpl::GetMangledName() const {
1071 return m_decl
.GetMangledName();
1074 CompilerType
TypeMemberFunctionImpl::GetType() const { return m_type
; }
1076 lldb::MemberFunctionKind
TypeMemberFunctionImpl::GetKind() const {
1080 bool TypeMemberFunctionImpl::GetDescription(Stream
&stream
) {
1082 case lldb::eMemberFunctionKindUnknown
:
1084 case lldb::eMemberFunctionKindConstructor
:
1085 stream
.Printf("constructor for %s",
1086 m_type
.GetTypeName().AsCString("<unknown>"));
1088 case lldb::eMemberFunctionKindDestructor
:
1089 stream
.Printf("destructor for %s",
1090 m_type
.GetTypeName().AsCString("<unknown>"));
1092 case lldb::eMemberFunctionKindInstanceMethod
:
1093 stream
.Printf("instance method %s of type %s", m_name
.AsCString(),
1094 m_decl
.GetDeclContext().GetName().AsCString());
1096 case lldb::eMemberFunctionKindStaticMethod
:
1097 stream
.Printf("static method %s of type %s", m_name
.AsCString(),
1098 m_decl
.GetDeclContext().GetName().AsCString());
1104 CompilerType
TypeMemberFunctionImpl::GetReturnType() const {
1106 return m_type
.GetFunctionReturnType();
1107 return m_decl
.GetFunctionReturnType();
1110 size_t TypeMemberFunctionImpl::GetNumArguments() const {
1112 return m_type
.GetNumberOfFunctionArguments();
1114 return m_decl
.GetNumFunctionArguments();
1117 CompilerType
TypeMemberFunctionImpl::GetArgumentAtIndex(size_t idx
) const {
1119 return m_type
.GetFunctionArgumentAtIndex(idx
);
1121 return m_decl
.GetFunctionArgumentType(idx
);
1124 TypeEnumMemberImpl::TypeEnumMemberImpl(const lldb::TypeImplSP
&integer_type_sp
,
1126 const llvm::APSInt
&value
)
1127 : m_integer_type_sp(integer_type_sp
), m_name(name
), m_value(value
),
1128 m_valid((bool)name
&& (bool)integer_type_sp
)