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 //===----------------------------------------------------------------------===//
14 #include "lldb/Core/Module.h"
15 #include "lldb/Utility/DataBufferHeap.h"
16 #include "lldb/Utility/DataExtractor.h"
17 #include "lldb/Utility/LLDBLog.h"
18 #include "lldb/Utility/Log.h"
19 #include "lldb/Utility/Scalar.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"
34 #include "lldb/lldb-enumerations.h"
35 #include "lldb/lldb-private-enumerations.h"
37 #include "llvm/ADT/StringRef.h"
40 using namespace lldb_private
;
42 llvm::raw_ostream
&lldb_private::operator<<(llvm::raw_ostream
&os
,
43 const CompilerContext
&rhs
) {
44 StreamString lldb_stream
;
45 rhs
.Dump(lldb_stream
);
46 return os
<< lldb_stream
.GetString();
49 static CompilerContextKind
ConvertTypeClass(lldb::TypeClass type_class
) {
50 if (type_class
== eTypeClassAny
)
51 return CompilerContextKind::AnyType
;
52 CompilerContextKind result
= {};
53 if (type_class
& (lldb::eTypeClassClass
| lldb::eTypeClassStruct
))
54 result
|= CompilerContextKind::ClassOrStruct
;
55 if (type_class
& lldb::eTypeClassUnion
)
56 result
|= CompilerContextKind::Union
;
57 if (type_class
& lldb::eTypeClassEnumeration
)
58 result
|= CompilerContextKind::Enum
;
59 if (type_class
& lldb::eTypeClassFunction
)
60 result
|= CompilerContextKind::Function
;
61 if (type_class
& lldb::eTypeClassTypedef
)
62 result
|= CompilerContextKind::Typedef
;
66 TypeQuery::TypeQuery(llvm::StringRef name
, TypeQueryOptions options
)
67 : m_options(options
) {
68 if (std::optional
<Type::ParsedName
> parsed_name
=
69 Type::GetTypeScopeAndBasename(name
)) {
70 llvm::ArrayRef scope
= parsed_name
->scope
;
72 if (scope
[0] == "::") {
73 m_options
|= e_exact_match
;
74 scope
= scope
.drop_front();
76 for (llvm::StringRef s
: scope
) {
78 {CompilerContextKind::AnyDeclContext
, ConstString(s
)});
81 m_context
.push_back({ConvertTypeClass(parsed_name
->type_class
),
82 ConstString(parsed_name
->basename
)});
84 m_context
.push_back({CompilerContextKind::AnyType
, ConstString(name
)});
88 TypeQuery::TypeQuery(const CompilerDeclContext
&decl_ctx
,
89 ConstString type_basename
, TypeQueryOptions options
)
90 : m_options(options
) {
91 // Always use an exact match if we are looking for a type in compiler context.
92 m_options
|= e_exact_match
;
93 m_context
= decl_ctx
.GetCompilerContext();
94 m_context
.push_back({CompilerContextKind::AnyType
, type_basename
});
98 const llvm::ArrayRef
<lldb_private::CompilerContext
> &context
,
99 TypeQueryOptions options
)
100 : m_context(context
), m_options(options
) {
101 // Always use an exact match if we are looking for a type in compiler context.
102 m_options
|= e_exact_match
;
105 TypeQuery::TypeQuery(const CompilerDecl
&decl
, TypeQueryOptions options
)
106 : m_options(options
) {
107 // Always for an exact match if we are looking for a type using a declaration.
108 m_options
|= e_exact_match
;
109 m_context
= decl
.GetCompilerContext();
112 ConstString
TypeQuery::GetTypeBasename() const {
113 if (m_context
.empty())
114 return ConstString();
115 return m_context
.back().name
;
118 void TypeQuery::AddLanguage(LanguageType language
) {
120 m_languages
= LanguageSet();
121 m_languages
->Insert(language
);
124 void TypeQuery::SetLanguages(LanguageSet languages
) {
125 m_languages
= std::move(languages
);
128 bool TypeQuery::ContextMatches(
129 llvm::ArrayRef
<CompilerContext
> context_chain
) const {
130 auto ctx
= context_chain
.rbegin(), ctx_end
= context_chain
.rend();
131 for (auto pat
= m_context
.rbegin(), pat_end
= m_context
.rend();
135 return false; // Pattern too long.
137 if (ctx
->kind
== CompilerContextKind::Namespace
&& ctx
->name
.IsEmpty()) {
138 // We're matching an anonymous namespace. These are optional, so we check
139 // if the pattern expects an anonymous namespace.
140 if (pat
->name
.IsEmpty() && (pat
->kind
& CompilerContextKind::Namespace
) ==
141 CompilerContextKind::Namespace
) {
142 // Match, advance both iterators.
145 // Otherwise, only advance the context to skip over the anonymous
146 // namespace, and try matching again.
151 // See if there is a kind mismatch; they should have 1 bit in common.
152 if ((ctx
->kind
& pat
->kind
) == CompilerContextKind())
155 if (ctx
->name
!= pat
->name
)
162 // Skip over any remaining module and anonymous namespace entries if we were
164 auto should_skip
= [this](const CompilerContext
&ctx
) {
165 if (ctx
.kind
== CompilerContextKind::Module
)
166 return GetIgnoreModules();
167 if (ctx
.kind
== CompilerContextKind::Namespace
&& ctx
.name
.IsEmpty())
168 return !GetStrictNamespaces();
171 ctx
= std::find_if_not(ctx
, ctx_end
, should_skip
);
173 // At this point, we have exhausted the pattern and we have a partial match at
174 // least. If that's all we're looking for, we're done.
175 if (!GetExactMatch())
178 // We have an exact match if we've exhausted the target context as well.
179 return ctx
== ctx_end
;
182 bool TypeQuery::LanguageMatches(lldb::LanguageType language
) const {
183 // If we have no language filterm language always matches.
184 if (!m_languages
.has_value())
186 return (*m_languages
)[language
];
189 bool TypeResults::AlreadySearched(lldb_private::SymbolFile
*sym_file
) {
190 return !m_searched_symbol_files
.insert(sym_file
).second
;
193 bool TypeResults::InsertUnique(const lldb::TypeSP
&type_sp
) {
195 return m_type_map
.InsertUnique(type_sp
);
199 bool TypeResults::Done(const TypeQuery
&query
) const {
200 if (query
.GetFindOne())
201 return !m_type_map
.Empty();
205 void CompilerContext::Dump(Stream
&s
) const {
210 case CompilerContextKind::TranslationUnit
:
211 s
<< "TranslationUnit";
213 case CompilerContextKind::Module
:
216 case CompilerContextKind::Namespace
:
219 case CompilerContextKind::ClassOrStruct
:
220 s
<< "ClassOrStruct";
222 case CompilerContextKind::Union
:
225 case CompilerContextKind::Function
:
228 case CompilerContextKind::Variable
:
231 case CompilerContextKind::Enum
:
234 case CompilerContextKind::Typedef
:
237 case CompilerContextKind::AnyType
:
241 s
<< "(" << name
<< ")";
244 class TypeAppendVisitor
{
246 TypeAppendVisitor(TypeListImpl
&type_list
) : m_type_list(type_list
) {}
248 bool operator()(const lldb::TypeSP
&type
) {
249 m_type_list
.Append(TypeImplSP(new TypeImpl(type
)));
254 TypeListImpl
&m_type_list
;
257 void TypeListImpl::Append(const lldb_private::TypeList
&type_list
) {
258 TypeAppendVisitor
cb(*this);
259 type_list
.ForEach(cb
);
262 SymbolFileType::SymbolFileType(SymbolFile
&symbol_file
,
263 const lldb::TypeSP
&type_sp
)
264 : UserID(type_sp
? type_sp
->GetID() : LLDB_INVALID_UID
),
265 m_symbol_file(symbol_file
), m_type_sp(type_sp
) {}
267 Type
*SymbolFileType::GetType() {
269 Type
*resolved_type
= m_symbol_file
.ResolveTypeUID(GetID());
271 m_type_sp
= resolved_type
->shared_from_this();
273 return m_type_sp
.get();
276 Type::Type(lldb::user_id_t uid
, SymbolFile
*symbol_file
, ConstString name
,
277 std::optional
<uint64_t> byte_size
, SymbolContextScope
*context
,
278 user_id_t encoding_uid
, EncodingDataType encoding_uid_type
,
279 const Declaration
&decl
, const CompilerType
&compiler_type
,
280 ResolveState compiler_type_resolve_state
, uint32_t opaque_payload
)
281 : std::enable_shared_from_this
<Type
>(), UserID(uid
), m_name(name
),
282 m_symbol_file(symbol_file
), m_context(context
),
283 m_encoding_uid(encoding_uid
), m_encoding_uid_type(encoding_uid_type
),
284 m_decl(decl
), m_compiler_type(compiler_type
),
285 m_compiler_type_resolve_state(compiler_type
? compiler_type_resolve_state
286 : ResolveState::Unresolved
),
287 m_payload(opaque_payload
) {
289 m_byte_size
= *byte_size
;
290 m_byte_size_has_value
= true;
293 m_byte_size_has_value
= false;
298 : std::enable_shared_from_this
<Type
>(), UserID(0), m_name("<INVALID TYPE>"),
301 m_byte_size_has_value
= false;
304 void Type::GetDescription(Stream
*s
, lldb::DescriptionLevel level
,
305 bool show_name
, ExecutionContextScope
*exe_scope
) {
306 *s
<< "id = " << (const UserID
&)*this;
308 // Call the name accessor to make sure we resolve the type name
310 ConstString type_name
= GetName();
312 *s
<< ", name = \"" << type_name
<< '"';
313 ConstString
qualified_type_name(GetQualifiedName());
314 if (qualified_type_name
!= type_name
) {
315 *s
<< ", qualified = \"" << qualified_type_name
<< '"';
320 // Call the get byte size accessor so we resolve our byte size
321 if (GetByteSize(exe_scope
))
322 s
->Printf(", byte-size = %" PRIu64
, m_byte_size
);
323 bool show_fullpaths
= (level
== lldb::eDescriptionLevelVerbose
);
324 m_decl
.Dump(s
, show_fullpaths
);
326 if (m_compiler_type
.IsValid()) {
327 *s
<< ", compiler_type = \"";
328 GetForwardCompilerType().DumpTypeDescription(s
);
330 } else if (m_encoding_uid
!= LLDB_INVALID_UID
) {
331 s
->Printf(", type_uid = 0x%8.8" PRIx64
, m_encoding_uid
);
332 switch (m_encoding_uid_type
) {
333 case eEncodingInvalid
:
336 s
->PutCString(" (unresolved type)");
338 case eEncodingIsConstUID
:
339 s
->PutCString(" (unresolved const type)");
341 case eEncodingIsRestrictUID
:
342 s
->PutCString(" (unresolved restrict type)");
344 case eEncodingIsVolatileUID
:
345 s
->PutCString(" (unresolved volatile type)");
347 case eEncodingIsAtomicUID
:
348 s
->PutCString(" (unresolved atomic type)");
350 case eEncodingIsTypedefUID
:
351 s
->PutCString(" (unresolved typedef)");
353 case eEncodingIsPointerUID
:
354 s
->PutCString(" (unresolved pointer)");
356 case eEncodingIsLValueReferenceUID
:
357 s
->PutCString(" (unresolved L value reference)");
359 case eEncodingIsRValueReferenceUID
:
360 s
->PutCString(" (unresolved R value reference)");
362 case eEncodingIsSyntheticUID
:
363 s
->PutCString(" (synthetic type)");
365 case eEncodingIsLLVMPtrAuthUID
:
366 s
->PutCString(" (ptrauth type)");
372 void Type::Dump(Stream
*s
, bool show_context
, lldb::DescriptionLevel level
) {
373 s
->Printf("%p: ", static_cast<void *>(this));
375 *s
<< "Type" << static_cast<const UserID
&>(*this) << ' ';
377 *s
<< ", name = \"" << m_name
<< "\"";
379 if (m_byte_size_has_value
)
380 s
->Printf(", size = %" PRIu64
, m_byte_size
);
382 if (show_context
&& m_context
!= nullptr) {
383 s
->PutCString(", context = ( ");
384 m_context
->DumpSymbolContext(s
);
388 bool show_fullpaths
= false;
389 m_decl
.Dump(s
, show_fullpaths
);
391 if (m_compiler_type
.IsValid()) {
392 *s
<< ", compiler_type = " << m_compiler_type
.GetOpaqueQualType() << ' ';
393 GetForwardCompilerType().DumpTypeDescription(s
, level
);
394 } else if (m_encoding_uid
!= LLDB_INVALID_UID
) {
395 s
->Format(", type_data = {0:x-16}", m_encoding_uid
);
396 switch (m_encoding_uid_type
) {
397 case eEncodingInvalid
:
400 s
->PutCString(" (unresolved type)");
402 case eEncodingIsConstUID
:
403 s
->PutCString(" (unresolved const type)");
405 case eEncodingIsRestrictUID
:
406 s
->PutCString(" (unresolved restrict type)");
408 case eEncodingIsVolatileUID
:
409 s
->PutCString(" (unresolved volatile type)");
411 case eEncodingIsAtomicUID
:
412 s
->PutCString(" (unresolved atomic type)");
414 case eEncodingIsTypedefUID
:
415 s
->PutCString(" (unresolved typedef)");
417 case eEncodingIsPointerUID
:
418 s
->PutCString(" (unresolved pointer)");
420 case eEncodingIsLValueReferenceUID
:
421 s
->PutCString(" (unresolved L value reference)");
423 case eEncodingIsRValueReferenceUID
:
424 s
->PutCString(" (unresolved R value reference)");
426 case eEncodingIsSyntheticUID
:
427 s
->PutCString(" (synthetic type)");
429 case eEncodingIsLLVMPtrAuthUID
:
430 s
->PutCString(" (ptrauth type)");
436 // s->Printf(", access = %u", m_access);
440 ConstString
Type::GetName() {
442 m_name
= GetForwardCompilerType().GetTypeName();
446 ConstString
Type::GetBaseName() {
447 return GetForwardCompilerType().GetTypeName(/*BaseOnly*/ true);
450 void Type::DumpTypeName(Stream
*s
) { GetName().Dump(s
, "<invalid-type-name>"); }
452 Type
*Type::GetEncodingType() {
453 if (m_encoding_type
== nullptr && m_encoding_uid
!= LLDB_INVALID_UID
)
454 m_encoding_type
= m_symbol_file
->ResolveTypeUID(m_encoding_uid
);
455 return m_encoding_type
;
458 std::optional
<uint64_t> Type::GetByteSize(ExecutionContextScope
*exe_scope
) {
459 if (m_byte_size_has_value
)
460 return static_cast<uint64_t>(m_byte_size
);
462 switch (m_encoding_uid_type
) {
463 case eEncodingInvalid
:
464 case eEncodingIsSyntheticUID
:
467 case eEncodingIsConstUID
:
468 case eEncodingIsRestrictUID
:
469 case eEncodingIsVolatileUID
:
470 case eEncodingIsAtomicUID
:
471 case eEncodingIsTypedefUID
: {
472 Type
*encoding_type
= GetEncodingType();
474 if (std::optional
<uint64_t> size
=
475 encoding_type
->GetByteSize(exe_scope
)) {
477 m_byte_size_has_value
= true;
478 return static_cast<uint64_t>(m_byte_size
);
481 if (std::optional
<uint64_t> size
=
482 GetLayoutCompilerType().GetByteSize(exe_scope
)) {
484 m_byte_size_has_value
= true;
485 return static_cast<uint64_t>(m_byte_size
);
489 // If we are a pointer or reference, then this is just a pointer size;
490 case eEncodingIsPointerUID
:
491 case eEncodingIsLValueReferenceUID
:
492 case eEncodingIsRValueReferenceUID
:
493 case eEncodingIsLLVMPtrAuthUID
: {
494 if (ArchSpec arch
= m_symbol_file
->GetObjectFile()->GetArchitecture()) {
495 m_byte_size
= arch
.GetAddressByteSize();
496 m_byte_size_has_value
= true;
497 return static_cast<uint64_t>(m_byte_size
);
504 llvm::Expected
<uint32_t> Type::GetNumChildren(bool omit_empty_base_classes
) {
505 return GetForwardCompilerType().GetNumChildren(omit_empty_base_classes
, nullptr);
508 bool Type::IsAggregateType() {
509 return GetForwardCompilerType().IsAggregateType();
512 bool Type::IsTemplateType() {
513 return GetForwardCompilerType().IsTemplateType();
516 lldb::TypeSP
Type::GetTypedefType() {
517 lldb::TypeSP type_sp
;
519 Type
*typedef_type
= m_symbol_file
->ResolveTypeUID(m_encoding_uid
);
521 type_sp
= typedef_type
->shared_from_this();
526 lldb::Format
Type::GetFormat() { return GetForwardCompilerType().GetFormat(); }
528 lldb::Encoding
Type::GetEncoding(uint64_t &count
) {
529 // Make sure we resolve our type if it already hasn't been.
530 return GetForwardCompilerType().GetEncoding(count
);
533 bool Type::ReadFromMemory(ExecutionContext
*exe_ctx
, lldb::addr_t addr
,
534 AddressType address_type
, DataExtractor
&data
) {
535 if (address_type
== eAddressTypeFile
) {
536 // Can't convert a file address to anything valid without more context
537 // (which Module it came from)
541 const uint64_t byte_size
=
542 GetByteSize(exe_ctx
? exe_ctx
->GetBestExecutionContextScope() : nullptr)
544 if (data
.GetByteSize() < byte_size
) {
545 lldb::DataBufferSP
data_sp(new DataBufferHeap(byte_size
, '\0'));
546 data
.SetData(data_sp
);
549 uint8_t *dst
= const_cast<uint8_t *>(data
.PeekData(0, byte_size
));
550 if (dst
!= nullptr) {
551 if (address_type
== eAddressTypeHost
) {
552 // The address is an address in this process, so just copy it
555 memcpy(dst
, reinterpret_cast<uint8_t *>(addr
), byte_size
);
559 Process
*process
= exe_ctx
->GetProcessPtr();
562 return exe_ctx
->GetProcessPtr()->ReadMemory(addr
, dst
, byte_size
,
571 bool Type::WriteToMemory(ExecutionContext
*exe_ctx
, lldb::addr_t addr
,
572 AddressType address_type
, DataExtractor
&data
) {
576 const Declaration
&Type::GetDeclaration() const { return m_decl
; }
578 bool Type::ResolveCompilerType(ResolveState compiler_type_resolve_state
) {
579 // TODO: This needs to consider the correct type system to use.
580 Type
*encoding_type
= nullptr;
581 if (!m_compiler_type
.IsValid()) {
582 encoding_type
= GetEncodingType();
584 switch (m_encoding_uid_type
) {
585 case eEncodingIsUID
: {
586 CompilerType encoding_compiler_type
=
587 encoding_type
->GetForwardCompilerType();
588 if (encoding_compiler_type
.IsValid()) {
589 m_compiler_type
= encoding_compiler_type
;
590 m_compiler_type_resolve_state
=
591 encoding_type
->m_compiler_type_resolve_state
;
595 case eEncodingIsConstUID
:
597 encoding_type
->GetForwardCompilerType().AddConstModifier();
600 case eEncodingIsRestrictUID
:
602 encoding_type
->GetForwardCompilerType().AddRestrictModifier();
605 case eEncodingIsVolatileUID
:
607 encoding_type
->GetForwardCompilerType().AddVolatileModifier();
610 case eEncodingIsAtomicUID
:
612 encoding_type
->GetForwardCompilerType().GetAtomicType();
615 case eEncodingIsTypedefUID
:
616 m_compiler_type
= encoding_type
->GetForwardCompilerType().CreateTypedef(
617 m_name
.AsCString("__lldb_invalid_typedef_name"),
618 GetSymbolFile()->GetDeclContextContainingUID(GetID()), m_payload
);
622 case eEncodingIsPointerUID
:
624 encoding_type
->GetForwardCompilerType().GetPointerType();
627 case eEncodingIsLValueReferenceUID
:
629 encoding_type
->GetForwardCompilerType().GetLValueReferenceType();
632 case eEncodingIsRValueReferenceUID
:
634 encoding_type
->GetForwardCompilerType().GetRValueReferenceType();
637 case eEncodingIsLLVMPtrAuthUID
:
639 encoding_type
->GetForwardCompilerType().AddPtrAuthModifier(
644 llvm_unreachable("Unhandled encoding_data_type.");
647 // We have no encoding type, return void?
648 auto type_system_or_err
=
649 m_symbol_file
->GetTypeSystemForLanguage(eLanguageTypeC
);
650 if (auto err
= type_system_or_err
.takeError()) {
652 GetLog(LLDBLog::Symbols
), std::move(err
),
653 "Unable to construct void type from TypeSystemClang: {0}");
655 CompilerType void_compiler_type
;
656 auto ts
= *type_system_or_err
;
658 void_compiler_type
= ts
->GetBasicTypeFromAST(eBasicTypeVoid
);
659 switch (m_encoding_uid_type
) {
661 m_compiler_type
= void_compiler_type
;
664 case eEncodingIsConstUID
:
665 m_compiler_type
= void_compiler_type
.AddConstModifier();
668 case eEncodingIsRestrictUID
:
669 m_compiler_type
= void_compiler_type
.AddRestrictModifier();
672 case eEncodingIsVolatileUID
:
673 m_compiler_type
= void_compiler_type
.AddVolatileModifier();
676 case eEncodingIsAtomicUID
:
677 m_compiler_type
= void_compiler_type
.GetAtomicType();
680 case eEncodingIsTypedefUID
:
681 m_compiler_type
= void_compiler_type
.CreateTypedef(
682 m_name
.AsCString("__lldb_invalid_typedef_name"),
683 GetSymbolFile()->GetDeclContextContainingUID(GetID()), m_payload
);
686 case eEncodingIsPointerUID
:
687 m_compiler_type
= void_compiler_type
.GetPointerType();
690 case eEncodingIsLValueReferenceUID
:
691 m_compiler_type
= void_compiler_type
.GetLValueReferenceType();
694 case eEncodingIsRValueReferenceUID
:
695 m_compiler_type
= void_compiler_type
.GetRValueReferenceType();
698 case eEncodingIsLLVMPtrAuthUID
:
699 llvm_unreachable("Cannot handle eEncodingIsLLVMPtrAuthUID without "
700 "valid encoding_type");
703 llvm_unreachable("Unhandled encoding_data_type.");
708 // When we have a EncodingUID, our "m_flags.compiler_type_resolve_state" is
709 // set to eResolveStateUnresolved so we need to update it to say that we
710 // now have a forward declaration since that is what we created above.
711 if (m_compiler_type
.IsValid())
712 m_compiler_type_resolve_state
= ResolveState::Forward
;
715 // Check if we have a forward reference to a class/struct/union/enum?
716 if (compiler_type_resolve_state
== ResolveState::Layout
||
717 compiler_type_resolve_state
== ResolveState::Full
) {
718 // Check if we have a forward reference to a class/struct/union/enum?
719 if (m_compiler_type
.IsValid() &&
720 m_compiler_type_resolve_state
< compiler_type_resolve_state
) {
721 m_compiler_type_resolve_state
= ResolveState::Full
;
722 if (!m_compiler_type
.IsDefined()) {
723 // We have a forward declaration, we need to resolve it to a complete
725 m_symbol_file
->CompleteType(m_compiler_type
);
730 // If we have an encoding type, then we need to make sure it is resolved
732 if (m_encoding_uid
!= LLDB_INVALID_UID
) {
733 if (encoding_type
== nullptr)
734 encoding_type
= GetEncodingType();
736 ResolveState encoding_compiler_type_resolve_state
=
737 compiler_type_resolve_state
;
739 if (compiler_type_resolve_state
== ResolveState::Layout
) {
740 switch (m_encoding_uid_type
) {
741 case eEncodingIsPointerUID
:
742 case eEncodingIsLValueReferenceUID
:
743 case eEncodingIsRValueReferenceUID
:
744 encoding_compiler_type_resolve_state
= ResolveState::Forward
;
750 encoding_type
->ResolveCompilerType(encoding_compiler_type_resolve_state
);
753 return m_compiler_type
.IsValid();
755 uint32_t Type::GetEncodingMask() {
756 uint32_t encoding_mask
= 1u << m_encoding_uid_type
;
757 Type
*encoding_type
= GetEncodingType();
758 assert(encoding_type
!= this);
760 encoding_mask
|= encoding_type
->GetEncodingMask();
761 return encoding_mask
;
764 CompilerType
Type::GetFullCompilerType() {
765 ResolveCompilerType(ResolveState::Full
);
766 return m_compiler_type
;
769 CompilerType
Type::GetLayoutCompilerType() {
770 ResolveCompilerType(ResolveState::Layout
);
771 return m_compiler_type
;
774 CompilerType
Type::GetForwardCompilerType() {
775 ResolveCompilerType(ResolveState::Forward
);
776 return m_compiler_type
;
779 ConstString
Type::GetQualifiedName() {
780 return GetForwardCompilerType().GetTypeName();
783 std::optional
<Type::ParsedName
>
784 Type::GetTypeScopeAndBasename(llvm::StringRef name
) {
790 if (name
.consume_front("struct "))
791 result
.type_class
= eTypeClassStruct
;
792 else if (name
.consume_front("class "))
793 result
.type_class
= eTypeClassClass
;
794 else if (name
.consume_front("union "))
795 result
.type_class
= eTypeClassUnion
;
796 else if (name
.consume_front("enum "))
797 result
.type_class
= eTypeClassEnumeration
;
798 else if (name
.consume_front("typedef "))
799 result
.type_class
= eTypeClassTypedef
;
801 if (name
.consume_front("::"))
802 result
.scope
.push_back("::");
804 bool prev_is_colon
= false;
805 size_t template_depth
= 0;
806 size_t name_begin
= 0;
807 for (const auto &pos
: llvm::enumerate(name
)) {
808 switch (pos
.value()) {
810 if (prev_is_colon
&& template_depth
== 0) {
811 llvm::StringRef scope_name
= name
.slice(name_begin
, pos
.index() - 1);
812 // The itanium demangler uses this string to represent anonymous
813 // namespaces. Convert it to a more language-agnostic form (which is
814 // also used in DWARF).
815 if (scope_name
== "(anonymous namespace)")
817 result
.scope
.push_back(scope_name
);
818 name_begin
= pos
.index() + 1;
825 if (template_depth
== 0)
826 return std::nullopt
; // Invalid name.
830 prev_is_colon
= pos
.value() == ':';
833 if (name_begin
< name
.size() && template_depth
== 0)
834 result
.basename
= name
.substr(name_begin
);
841 ModuleSP
Type::GetModule() {
843 return m_symbol_file
->GetObjectFile()->GetModule();
847 ModuleSP
Type::GetExeModule() {
848 if (m_compiler_type
) {
849 auto ts
= m_compiler_type
.GetTypeSystem();
852 SymbolFile
*symbol_file
= ts
->GetSymbolFile();
854 return symbol_file
->GetObjectFile()->GetModule();
859 TypeAndOrName::TypeAndOrName(TypeSP
&in_type_sp
) {
861 m_compiler_type
= in_type_sp
->GetForwardCompilerType();
862 m_type_name
= in_type_sp
->GetName();
866 TypeAndOrName::TypeAndOrName(const char *in_type_str
)
867 : m_type_name(in_type_str
) {}
869 TypeAndOrName::TypeAndOrName(ConstString
&in_type_const_string
)
870 : m_type_name(in_type_const_string
) {}
872 bool TypeAndOrName::operator==(const TypeAndOrName
&other
) const {
873 if (m_compiler_type
!= other
.m_compiler_type
)
875 if (m_type_name
!= other
.m_type_name
)
880 bool TypeAndOrName::operator!=(const TypeAndOrName
&other
) const {
881 return !(*this == other
);
884 ConstString
TypeAndOrName::GetName() const {
888 return m_compiler_type
.GetTypeName();
889 return ConstString("<invalid>");
892 void TypeAndOrName::SetName(ConstString type_name
) {
893 m_type_name
= type_name
;
896 void TypeAndOrName::SetName(const char *type_name_cstr
) {
897 m_type_name
.SetCString(type_name_cstr
);
900 void TypeAndOrName::SetName(llvm::StringRef type_name
) {
901 m_type_name
.SetString(type_name
);
904 void TypeAndOrName::SetTypeSP(lldb::TypeSP type_sp
) {
906 m_compiler_type
= type_sp
->GetForwardCompilerType();
907 m_type_name
= type_sp
->GetName();
912 void TypeAndOrName::SetCompilerType(CompilerType compiler_type
) {
913 m_compiler_type
= compiler_type
;
915 m_type_name
= m_compiler_type
.GetTypeName();
918 bool TypeAndOrName::IsEmpty() const {
919 return !((bool)m_type_name
|| (bool)m_compiler_type
);
922 void TypeAndOrName::Clear() {
924 m_compiler_type
.Clear();
927 bool TypeAndOrName::HasName() const { return (bool)m_type_name
; }
929 bool TypeAndOrName::HasCompilerType() const {
930 return m_compiler_type
.IsValid();
933 TypeImpl::TypeImpl(const lldb::TypeSP
&type_sp
)
934 : m_module_wp(), m_static_type(), m_dynamic_type() {
938 TypeImpl::TypeImpl(const CompilerType
&compiler_type
)
939 : m_module_wp(), m_static_type(), m_dynamic_type() {
940 SetType(compiler_type
);
943 TypeImpl::TypeImpl(const lldb::TypeSP
&type_sp
, const CompilerType
&dynamic
)
944 : m_module_wp(), m_static_type(), m_dynamic_type(dynamic
) {
945 SetType(type_sp
, dynamic
);
948 TypeImpl::TypeImpl(const CompilerType
&static_type
,
949 const CompilerType
&dynamic_type
)
950 : m_module_wp(), m_static_type(), m_dynamic_type() {
951 SetType(static_type
, dynamic_type
);
954 void TypeImpl::SetType(const lldb::TypeSP
&type_sp
) {
956 m_static_type
= type_sp
->GetForwardCompilerType();
957 m_exe_module_wp
= type_sp
->GetExeModule();
958 m_module_wp
= type_sp
->GetModule();
960 m_static_type
.Clear();
961 m_module_wp
= lldb::ModuleWP();
965 void TypeImpl::SetType(const CompilerType
&compiler_type
) {
966 m_module_wp
= lldb::ModuleWP();
967 m_static_type
= compiler_type
;
970 void TypeImpl::SetType(const lldb::TypeSP
&type_sp
,
971 const CompilerType
&dynamic
) {
973 m_dynamic_type
= dynamic
;
976 void TypeImpl::SetType(const CompilerType
&compiler_type
,
977 const CompilerType
&dynamic
) {
978 m_module_wp
= lldb::ModuleWP();
979 m_static_type
= compiler_type
;
980 m_dynamic_type
= dynamic
;
983 bool TypeImpl::CheckModule(lldb::ModuleSP
&module_sp
) const {
984 return CheckModuleCommon(m_module_wp
, module_sp
);
987 bool TypeImpl::CheckExeModule(lldb::ModuleSP
&module_sp
) const {
988 return CheckModuleCommon(m_exe_module_wp
, module_sp
);
991 bool TypeImpl::CheckModuleCommon(const lldb::ModuleWP
&input_module_wp
,
992 lldb::ModuleSP
&module_sp
) const {
993 // Check if we have a module for this type. If we do and the shared pointer
994 // is can be successfully initialized with m_module_wp, return true. Else
995 // return false if we didn't have a module, or if we had a module and it has
996 // been deleted. Any functions doing anything with a TypeSP in this TypeImpl
997 // class should call this function and only do anything with the ivars if
998 // this function returns true. If we have a module, the "module_sp" will be
999 // filled in with a strong reference to the module so that the module will at
1000 // least stay around long enough for the type query to succeed.
1001 module_sp
= input_module_wp
.lock();
1003 lldb::ModuleWP empty_module_wp
;
1004 // If either call to "std::weak_ptr::owner_before(...) value returns true,
1005 // this indicates that m_module_wp once contained (possibly still does) a
1006 // reference to a valid shared pointer. This helps us know if we had a
1007 // valid reference to a section which is now invalid because the module it
1008 // was in was deleted
1009 if (empty_module_wp
.owner_before(input_module_wp
) ||
1010 input_module_wp
.owner_before(empty_module_wp
)) {
1011 // input_module_wp had a valid reference to a module, but all strong
1012 // references have been released and the module has been deleted
1016 // We either successfully locked the module, or didn't have one to begin with
1020 bool TypeImpl::operator==(const TypeImpl
&rhs
) const {
1021 return m_static_type
== rhs
.m_static_type
&&
1022 m_dynamic_type
== rhs
.m_dynamic_type
;
1025 bool TypeImpl::operator!=(const TypeImpl
&rhs
) const {
1026 return !(*this == rhs
);
1029 bool TypeImpl::IsValid() const {
1030 // just a name is not valid
1032 if (CheckModule(module_sp
))
1033 return m_static_type
.IsValid() || m_dynamic_type
.IsValid();
1037 TypeImpl::operator bool() const { return IsValid(); }
1039 void TypeImpl::Clear() {
1040 m_module_wp
= lldb::ModuleWP();
1041 m_static_type
.Clear();
1042 m_dynamic_type
.Clear();
1045 ModuleSP
TypeImpl::GetModule() const {
1046 lldb::ModuleSP module_sp
;
1047 if (CheckExeModule(module_sp
))
1052 ConstString
TypeImpl::GetName() const {
1054 if (CheckModule(module_sp
)) {
1056 return m_dynamic_type
.GetTypeName();
1057 return m_static_type
.GetTypeName();
1059 return ConstString();
1062 ConstString
TypeImpl::GetDisplayTypeName() const {
1064 if (CheckModule(module_sp
)) {
1066 return m_dynamic_type
.GetDisplayTypeName();
1067 return m_static_type
.GetDisplayTypeName();
1069 return ConstString();
1072 TypeImpl
TypeImpl::GetPointerType() const {
1074 if (CheckModule(module_sp
)) {
1075 if (m_dynamic_type
.IsValid()) {
1076 return TypeImpl(m_static_type
.GetPointerType(),
1077 m_dynamic_type
.GetPointerType());
1079 return TypeImpl(m_static_type
.GetPointerType());
1084 TypeImpl
TypeImpl::GetPointeeType() const {
1086 if (CheckModule(module_sp
)) {
1087 if (m_dynamic_type
.IsValid()) {
1088 return TypeImpl(m_static_type
.GetPointeeType(),
1089 m_dynamic_type
.GetPointeeType());
1091 return TypeImpl(m_static_type
.GetPointeeType());
1096 TypeImpl
TypeImpl::GetReferenceType() const {
1098 if (CheckModule(module_sp
)) {
1099 if (m_dynamic_type
.IsValid()) {
1100 return TypeImpl(m_static_type
.GetLValueReferenceType(),
1101 m_dynamic_type
.GetLValueReferenceType());
1103 return TypeImpl(m_static_type
.GetLValueReferenceType());
1108 TypeImpl
TypeImpl::GetTypedefedType() const {
1110 if (CheckModule(module_sp
)) {
1111 if (m_dynamic_type
.IsValid()) {
1112 return TypeImpl(m_static_type
.GetTypedefedType(),
1113 m_dynamic_type
.GetTypedefedType());
1115 return TypeImpl(m_static_type
.GetTypedefedType());
1120 TypeImpl
TypeImpl::GetDereferencedType() const {
1122 if (CheckModule(module_sp
)) {
1123 if (m_dynamic_type
.IsValid()) {
1124 return TypeImpl(m_static_type
.GetNonReferenceType(),
1125 m_dynamic_type
.GetNonReferenceType());
1127 return TypeImpl(m_static_type
.GetNonReferenceType());
1132 TypeImpl
TypeImpl::GetUnqualifiedType() const {
1134 if (CheckModule(module_sp
)) {
1135 if (m_dynamic_type
.IsValid()) {
1136 return TypeImpl(m_static_type
.GetFullyUnqualifiedType(),
1137 m_dynamic_type
.GetFullyUnqualifiedType());
1139 return TypeImpl(m_static_type
.GetFullyUnqualifiedType());
1144 TypeImpl
TypeImpl::GetCanonicalType() const {
1146 if (CheckModule(module_sp
)) {
1147 if (m_dynamic_type
.IsValid()) {
1148 return TypeImpl(m_static_type
.GetCanonicalType(),
1149 m_dynamic_type
.GetCanonicalType());
1151 return TypeImpl(m_static_type
.GetCanonicalType());
1156 CompilerType
TypeImpl::GetCompilerType(bool prefer_dynamic
) {
1158 if (CheckModule(module_sp
)) {
1159 if (prefer_dynamic
) {
1160 if (m_dynamic_type
.IsValid())
1161 return m_dynamic_type
;
1163 return m_static_type
;
1165 return CompilerType();
1168 CompilerType::TypeSystemSPWrapper
TypeImpl::GetTypeSystem(bool prefer_dynamic
) {
1170 if (CheckModule(module_sp
)) {
1171 if (prefer_dynamic
) {
1172 if (m_dynamic_type
.IsValid())
1173 return m_dynamic_type
.GetTypeSystem();
1175 return m_static_type
.GetTypeSystem();
1180 bool TypeImpl::GetDescription(lldb_private::Stream
&strm
,
1181 lldb::DescriptionLevel description_level
) {
1183 if (CheckModule(module_sp
)) {
1184 if (m_dynamic_type
.IsValid()) {
1185 strm
.Printf("Dynamic:\n");
1186 m_dynamic_type
.DumpTypeDescription(&strm
);
1187 strm
.Printf("\nStatic:\n");
1189 m_static_type
.DumpTypeDescription(&strm
);
1191 strm
.PutCString("Invalid TypeImpl module for type has been deleted\n");
1196 CompilerType
TypeImpl::FindDirectNestedType(llvm::StringRef name
) {
1198 return CompilerType();
1199 return GetCompilerType(/*prefer_dynamic=*/false)
1200 .GetDirectNestedTypeWithName(name
);
1203 bool TypeMemberFunctionImpl::IsValid() {
1204 return m_type
.IsValid() && m_kind
!= lldb::eMemberFunctionKindUnknown
;
1207 ConstString
TypeMemberFunctionImpl::GetName() const { return m_name
; }
1209 ConstString
TypeMemberFunctionImpl::GetMangledName() const {
1210 return m_decl
.GetMangledName();
1213 CompilerType
TypeMemberFunctionImpl::GetType() const { return m_type
; }
1215 lldb::MemberFunctionKind
TypeMemberFunctionImpl::GetKind() const {
1219 bool TypeMemberFunctionImpl::GetDescription(Stream
&stream
) {
1221 case lldb::eMemberFunctionKindUnknown
:
1223 case lldb::eMemberFunctionKindConstructor
:
1224 stream
.Printf("constructor for %s",
1225 m_type
.GetTypeName().AsCString("<unknown>"));
1227 case lldb::eMemberFunctionKindDestructor
:
1228 stream
.Printf("destructor for %s",
1229 m_type
.GetTypeName().AsCString("<unknown>"));
1231 case lldb::eMemberFunctionKindInstanceMethod
:
1232 stream
.Printf("instance method %s of type %s", m_name
.AsCString(),
1233 m_decl
.GetDeclContext().GetName().AsCString());
1235 case lldb::eMemberFunctionKindStaticMethod
:
1236 stream
.Printf("static method %s of type %s", m_name
.AsCString(),
1237 m_decl
.GetDeclContext().GetName().AsCString());
1243 CompilerType
TypeMemberFunctionImpl::GetReturnType() const {
1245 return m_type
.GetFunctionReturnType();
1246 return m_decl
.GetFunctionReturnType();
1249 size_t TypeMemberFunctionImpl::GetNumArguments() const {
1251 return m_type
.GetNumberOfFunctionArguments();
1253 return m_decl
.GetNumFunctionArguments();
1256 CompilerType
TypeMemberFunctionImpl::GetArgumentAtIndex(size_t idx
) const {
1258 return m_type
.GetFunctionArgumentAtIndex(idx
);
1260 return m_decl
.GetFunctionArgumentType(idx
);
1263 TypeEnumMemberImpl::TypeEnumMemberImpl(const lldb::TypeImplSP
&integer_type_sp
,
1265 const llvm::APSInt
&value
)
1266 : m_integer_type_sp(integer_type_sp
), m_name(name
), m_value(value
),
1267 m_valid((bool)name
&& (bool)integer_type_sp
)