Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / source / Symbol / Type.cpp
blob54eeace93b964875b4e2d2a7eeaf3e614a6468f8
1 //===-- Type.cpp ----------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include <cstdio>
10 #include <optional>
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"
35 using namespace lldb;
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.
44 if (ctx == ctx_end)
45 return false;
46 if (*ctx != pat) {
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;
52 });
53 continue;
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)
57 return false;
58 // The name is ignored for AnyModule, but not for AnyType.
59 if (pat.kind != CompilerContextKind::AnyModule && ctx->name != pat.name)
60 return false;
62 ++ctx;
64 return true;
67 void CompilerContext::Dump(Stream &s) const {
68 switch (kind) {
69 default:
70 s << "Invalid";
71 break;
72 case CompilerContextKind::TranslationUnit:
73 s << "TranslationUnit";
74 break;
75 case CompilerContextKind::Module:
76 s << "Module";
77 break;
78 case CompilerContextKind::Namespace:
79 s << "Namespace";
80 break;
81 case CompilerContextKind::Class:
82 s << "Class";
83 break;
84 case CompilerContextKind::Struct:
85 s << "Structure";
86 break;
87 case CompilerContextKind::Union:
88 s << "Union";
89 break;
90 case CompilerContextKind::Function:
91 s << "Function";
92 break;
93 case CompilerContextKind::Variable:
94 s << "Variable";
95 break;
96 case CompilerContextKind::Enum:
97 s << "Enumeration";
98 break;
99 case CompilerContextKind::Typedef:
100 s << "Typedef";
101 break;
102 case CompilerContextKind::AnyModule:
103 s << "AnyModule";
104 break;
105 case CompilerContextKind::AnyType:
106 s << "AnyType";
107 break;
109 s << "(" << name << ")";
112 class TypeAppendVisitor {
113 public:
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)));
118 return true;
121 private:
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() {
136 if (!m_type_sp) {
137 Type *resolved_type = m_symbol_file.ResolveTypeUID(GetID());
138 if (resolved_type)
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) {
156 if (byte_size) {
157 m_byte_size = *byte_size;
158 m_byte_size_has_value = true;
159 } else {
160 m_byte_size = 0;
161 m_byte_size_has_value = false;
165 Type::Type()
166 : std::enable_shared_from_this<Type>(), UserID(0), m_name("<INVALID TYPE>"),
167 m_payload(0) {
168 m_byte_size = 0;
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
177 if (show_name) {
178 ConstString type_name = GetName();
179 if (type_name) {
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);
197 *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:
202 break;
203 case eEncodingIsUID:
204 s->PutCString(" (unresolved type)");
205 break;
206 case eEncodingIsConstUID:
207 s->PutCString(" (unresolved const type)");
208 break;
209 case eEncodingIsRestrictUID:
210 s->PutCString(" (unresolved restrict type)");
211 break;
212 case eEncodingIsVolatileUID:
213 s->PutCString(" (unresolved volatile type)");
214 break;
215 case eEncodingIsAtomicUID:
216 s->PutCString(" (unresolved atomic type)");
217 break;
218 case eEncodingIsTypedefUID:
219 s->PutCString(" (unresolved typedef)");
220 break;
221 case eEncodingIsPointerUID:
222 s->PutCString(" (unresolved pointer)");
223 break;
224 case eEncodingIsLValueReferenceUID:
225 s->PutCString(" (unresolved L value reference)");
226 break;
227 case eEncodingIsRValueReferenceUID:
228 s->PutCString(" (unresolved R value reference)");
229 break;
230 case eEncodingIsSyntheticUID:
231 s->PutCString(" (synthetic type)");
232 break;
237 void Type::Dump(Stream *s, bool show_context, lldb::DescriptionLevel level) {
238 s->Printf("%p: ", static_cast<void *>(this));
239 s->Indent();
240 *s << "Type" << static_cast<const UserID &>(*this) << ' ';
241 if (m_name)
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);
250 s->PutCString(" )");
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:
263 break;
264 case eEncodingIsUID:
265 s->PutCString(" (unresolved type)");
266 break;
267 case eEncodingIsConstUID:
268 s->PutCString(" (unresolved const type)");
269 break;
270 case eEncodingIsRestrictUID:
271 s->PutCString(" (unresolved restrict type)");
272 break;
273 case eEncodingIsVolatileUID:
274 s->PutCString(" (unresolved volatile type)");
275 break;
276 case eEncodingIsAtomicUID:
277 s->PutCString(" (unresolved atomic type)");
278 break;
279 case eEncodingIsTypedefUID:
280 s->PutCString(" (unresolved typedef)");
281 break;
282 case eEncodingIsPointerUID:
283 s->PutCString(" (unresolved pointer)");
284 break;
285 case eEncodingIsLValueReferenceUID:
286 s->PutCString(" (unresolved L value reference)");
287 break;
288 case eEncodingIsRValueReferenceUID:
289 s->PutCString(" (unresolved R value reference)");
290 break;
291 case eEncodingIsSyntheticUID:
292 s->PutCString(" (synthetic type)");
293 break;
298 // if (m_access)
299 // s->Printf(", access = %u", m_access);
300 s->EOL();
303 ConstString Type::GetName() {
304 if (!m_name)
305 m_name = GetForwardCompilerType().GetTypeName();
306 return m_name;
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:
328 break;
329 case eEncodingIsUID:
330 case eEncodingIsConstUID:
331 case eEncodingIsRestrictUID:
332 case eEncodingIsVolatileUID:
333 case eEncodingIsAtomicUID:
334 case eEncodingIsTypedefUID: {
335 Type *encoding_type = GetEncodingType();
336 if (encoding_type)
337 if (std::optional<uint64_t> size =
338 encoding_type->GetByteSize(exe_scope)) {
339 m_byte_size = *size;
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)) {
346 m_byte_size = *size;
347 m_byte_size_has_value = true;
348 return static_cast<uint64_t>(m_byte_size);
350 } break;
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);
361 } break;
363 return {};
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;
380 if (IsTypedef()) {
381 Type *typedef_type = m_symbol_file->ResolveTypeUID(m_encoding_uid);
382 if (typedef_type)
383 type_sp = typedef_type->shared_from_this();
385 return type_sp;
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)
400 return false;
403 const uint64_t byte_size =
404 GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr)
405 .value_or(0);
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
415 if (addr == 0)
416 return false;
417 memcpy(dst, reinterpret_cast<uint8_t *>(addr), byte_size);
418 return true;
419 } else {
420 if (exe_ctx) {
421 Process *process = exe_ctx->GetProcessPtr();
422 if (process) {
423 Status error;
424 return exe_ctx->GetProcessPtr()->ReadMemory(addr, dst, byte_size,
425 error) == byte_size;
430 return false;
433 bool Type::WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t addr,
434 AddressType address_type, DataExtractor &data) {
435 return false;
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();
445 if (encoding_type) {
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;
455 } break;
457 case eEncodingIsConstUID:
458 m_compiler_type =
459 encoding_type->GetForwardCompilerType().AddConstModifier();
460 break;
462 case eEncodingIsRestrictUID:
463 m_compiler_type =
464 encoding_type->GetForwardCompilerType().AddRestrictModifier();
465 break;
467 case eEncodingIsVolatileUID:
468 m_compiler_type =
469 encoding_type->GetForwardCompilerType().AddVolatileModifier();
470 break;
472 case eEncodingIsAtomicUID:
473 m_compiler_type =
474 encoding_type->GetForwardCompilerType().GetAtomicType();
475 break;
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);
481 m_name.Clear();
482 break;
484 case eEncodingIsPointerUID:
485 m_compiler_type =
486 encoding_type->GetForwardCompilerType().GetPointerType();
487 break;
489 case eEncodingIsLValueReferenceUID:
490 m_compiler_type =
491 encoding_type->GetForwardCompilerType().GetLValueReferenceType();
492 break;
494 case eEncodingIsRValueReferenceUID:
495 m_compiler_type =
496 encoding_type->GetForwardCompilerType().GetRValueReferenceType();
497 break;
499 default:
500 llvm_unreachable("Unhandled encoding_data_type.");
502 } else {
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()) {
507 LLDB_LOG_ERROR(
508 GetLog(LLDBLog::Symbols), std::move(err),
509 "Unable to construct void type from TypeSystemClang: {0}");
510 } else {
511 CompilerType void_compiler_type;
512 auto ts = *type_system_or_err;
513 if (ts)
514 void_compiler_type = ts->GetBasicTypeFromAST(eBasicTypeVoid);
515 switch (m_encoding_uid_type) {
516 case eEncodingIsUID:
517 m_compiler_type = void_compiler_type;
518 break;
520 case eEncodingIsConstUID:
521 m_compiler_type = void_compiler_type.AddConstModifier();
522 break;
524 case eEncodingIsRestrictUID:
525 m_compiler_type = void_compiler_type.AddRestrictModifier();
526 break;
528 case eEncodingIsVolatileUID:
529 m_compiler_type = void_compiler_type.AddVolatileModifier();
530 break;
532 case eEncodingIsAtomicUID:
533 m_compiler_type = void_compiler_type.GetAtomicType();
534 break;
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);
540 break;
542 case eEncodingIsPointerUID:
543 m_compiler_type = void_compiler_type.GetPointerType();
544 break;
546 case eEncodingIsLValueReferenceUID:
547 m_compiler_type = void_compiler_type.GetLValueReferenceType();
548 break;
550 case eEncodingIsRValueReferenceUID:
551 m_compiler_type = void_compiler_type.GetRValueReferenceType();
552 break;
554 default:
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
576 // definition.
577 m_symbol_file->CompleteType(m_compiler_type);
582 // If we have an encoding type, then we need to make sure it is resolved
583 // appropriately.
584 if (m_encoding_uid != LLDB_INVALID_UID) {
585 if (encoding_type == nullptr)
586 encoding_type = GetEncodingType();
587 if (encoding_type) {
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;
597 break;
598 default:
599 break;
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);
611 if (encoding_type)
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;
641 if (name.empty())
642 return false;
644 basename = name;
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)
658 return false;
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() == '<')
669 template_depth++;
670 else if (template_arg.front() == '>')
671 template_depth--;
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;
679 } else {
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());
687 return true;
689 return false;
692 ModuleSP Type::GetModule() {
693 if (m_symbol_file)
694 return m_symbol_file->GetObjectFile()->GetModule();
695 return ModuleSP();
698 ModuleSP Type::GetExeModule() {
699 if (m_compiler_type) {
700 auto ts = m_compiler_type.GetTypeSystem();
701 if (!ts)
702 return {};
703 SymbolFile *symbol_file = ts->GetSymbolFile();
704 if (symbol_file)
705 return symbol_file->GetObjectFile()->GetModule();
707 return {};
710 TypeAndOrName::TypeAndOrName(TypeSP &in_type_sp) {
711 if (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)
725 return false;
726 if (m_type_name != other.m_type_name)
727 return false;
728 return true;
731 bool TypeAndOrName::operator!=(const TypeAndOrName &other) const {
732 return !(*this == other);
735 ConstString TypeAndOrName::GetName() const {
736 if (m_type_name)
737 return m_type_name;
738 if (m_compiler_type)
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) {
756 if (type_sp) {
757 m_compiler_type = type_sp->GetForwardCompilerType();
758 m_type_name = type_sp->GetName();
759 } else
760 Clear();
763 void TypeAndOrName::SetCompilerType(CompilerType compiler_type) {
764 m_compiler_type = compiler_type;
765 if (m_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() {
774 m_type_name.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() {
786 SetType(type_sp);
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) {
806 if (type_sp) {
807 m_static_type = type_sp->GetForwardCompilerType();
808 m_exe_module_wp = type_sp->GetExeModule();
809 m_module_wp = type_sp->GetModule();
810 } else {
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) {
823 SetType(type_sp);
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();
853 if (!module_sp) {
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
864 return false;
867 // We either successfully locked the module, or didn't have one to begin with
868 return true;
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
882 ModuleSP module_sp;
883 if (CheckModule(module_sp))
884 return m_static_type.IsValid() || m_dynamic_type.IsValid();
885 return false;
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))
899 return module_sp;
900 return nullptr;
903 ConstString TypeImpl::GetName() const {
904 ModuleSP module_sp;
905 if (CheckModule(module_sp)) {
906 if (m_dynamic_type)
907 return m_dynamic_type.GetTypeName();
908 return m_static_type.GetTypeName();
910 return ConstString();
913 ConstString TypeImpl::GetDisplayTypeName() const {
914 ModuleSP module_sp;
915 if (CheckModule(module_sp)) {
916 if (m_dynamic_type)
917 return m_dynamic_type.GetDisplayTypeName();
918 return m_static_type.GetDisplayTypeName();
920 return ConstString();
923 TypeImpl TypeImpl::GetPointerType() const {
924 ModuleSP module_sp;
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());
932 return TypeImpl();
935 TypeImpl TypeImpl::GetPointeeType() const {
936 ModuleSP module_sp;
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());
944 return TypeImpl();
947 TypeImpl TypeImpl::GetReferenceType() const {
948 ModuleSP module_sp;
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());
956 return TypeImpl();
959 TypeImpl TypeImpl::GetTypedefedType() const {
960 ModuleSP module_sp;
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());
968 return TypeImpl();
971 TypeImpl TypeImpl::GetDereferencedType() const {
972 ModuleSP module_sp;
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());
980 return TypeImpl();
983 TypeImpl TypeImpl::GetUnqualifiedType() const {
984 ModuleSP module_sp;
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());
992 return TypeImpl();
995 TypeImpl TypeImpl::GetCanonicalType() const {
996 ModuleSP module_sp;
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());
1004 return TypeImpl();
1007 CompilerType TypeImpl::GetCompilerType(bool prefer_dynamic) {
1008 ModuleSP module_sp;
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) {
1020 ModuleSP module_sp;
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();
1028 return {};
1031 bool TypeImpl::GetDescription(lldb_private::Stream &strm,
1032 lldb::DescriptionLevel description_level) {
1033 ModuleSP module_sp;
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);
1041 } else {
1042 strm.PutCString("Invalid TypeImpl module for type has been deleted\n");
1044 return true;
1047 CompilerType TypeImpl::FindDirectNestedType(llvm::StringRef name) {
1048 if (name.empty())
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 {
1077 return m_kind;
1080 bool TypeMemberFunctionImpl::GetDescription(Stream &stream) {
1081 switch (m_kind) {
1082 case lldb::eMemberFunctionKindUnknown:
1083 return false;
1084 case lldb::eMemberFunctionKindConstructor:
1085 stream.Printf("constructor for %s",
1086 m_type.GetTypeName().AsCString("<unknown>"));
1087 break;
1088 case lldb::eMemberFunctionKindDestructor:
1089 stream.Printf("destructor for %s",
1090 m_type.GetTypeName().AsCString("<unknown>"));
1091 break;
1092 case lldb::eMemberFunctionKindInstanceMethod:
1093 stream.Printf("instance method %s of type %s", m_name.AsCString(),
1094 m_decl.GetDeclContext().GetName().AsCString());
1095 break;
1096 case lldb::eMemberFunctionKindStaticMethod:
1097 stream.Printf("static method %s of type %s", m_name.AsCString(),
1098 m_decl.GetDeclContext().GetName().AsCString());
1099 break;
1101 return true;
1104 CompilerType TypeMemberFunctionImpl::GetReturnType() const {
1105 if (m_type)
1106 return m_type.GetFunctionReturnType();
1107 return m_decl.GetFunctionReturnType();
1110 size_t TypeMemberFunctionImpl::GetNumArguments() const {
1111 if (m_type)
1112 return m_type.GetNumberOfFunctionArguments();
1113 else
1114 return m_decl.GetNumFunctionArguments();
1117 CompilerType TypeMemberFunctionImpl::GetArgumentAtIndex(size_t idx) const {
1118 if (m_type)
1119 return m_type.GetFunctionArgumentAtIndex(idx);
1120 else
1121 return m_decl.GetFunctionArgumentType(idx);
1124 TypeEnumMemberImpl::TypeEnumMemberImpl(const lldb::TypeImplSP &integer_type_sp,
1125 ConstString name,
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)