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 //===----------------------------------------------------------------------===// 8 9 #include <cstdio> 10 #include <optional> 11 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" 19 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" 28 29 #include "lldb/Target/ExecutionContext.h" 30 #include "lldb/Target/Process.h" 31 #include "lldb/Target/Target.h" 32 #include "lldb/lldb-enumerations.h" 33 34 #include "llvm/ADT/StringRef.h" 35 36 using namespace lldb; 37 using namespace lldb_private; 38 39 llvm::raw_ostream &lldb_private::operator<<(llvm::raw_ostream &os, 40 const CompilerContext &rhs) { 41 StreamString lldb_stream; 42 rhs.Dump(lldb_stream); 43 return os << lldb_stream.GetString(); 44 } 45 46 bool lldb_private::contextMatches(llvm::ArrayRef<CompilerContext> context_chain, 47 llvm::ArrayRef<CompilerContext> pattern) { 48 auto ctx = context_chain.begin(); 49 auto ctx_end = context_chain.end(); 50 for (const CompilerContext &pat : pattern) { 51 // Early exit if the pattern is too long. 52 if (ctx == ctx_end) 53 return false; 54 if (*ctx != pat) { 55 // Skip any number of module matches. 56 if (pat.kind == CompilerContextKind::AnyModule) { 57 // Greedily match 0..n modules. 58 ctx = std::find_if(ctx, ctx_end, [](const CompilerContext &ctx) { 59 return ctx.kind != CompilerContextKind::Module; 60 }); 61 continue; 62 } 63 // See if there is a kind mismatch; they should have 1 bit in common. 64 if (((uint16_t)ctx->kind & (uint16_t)pat.kind) == 0) 65 return false; 66 // The name is ignored for AnyModule, but not for AnyType. 67 if (pat.kind != CompilerContextKind::AnyModule && ctx->name != pat.name) 68 return false; 69 } 70 ++ctx; 71 } 72 return true; 73 } 74 75 static CompilerContextKind ConvertTypeClass(lldb::TypeClass type_class) { 76 if (type_class == eTypeClassAny) 77 return CompilerContextKind::AnyType; 78 CompilerContextKind result = {}; 79 if (type_class & (lldb::eTypeClassClass | lldb::eTypeClassStruct)) 80 result |= CompilerContextKind::ClassOrStruct; 81 if (type_class & lldb::eTypeClassUnion) 82 result |= CompilerContextKind::Union; 83 if (type_class & lldb::eTypeClassEnumeration) 84 result |= CompilerContextKind::Enum; 85 if (type_class & lldb::eTypeClassFunction) 86 result |= CompilerContextKind::Function; 87 if (type_class & lldb::eTypeClassTypedef) 88 result |= CompilerContextKind::Typedef; 89 return result; 90 } 91 92 TypeQuery::TypeQuery(llvm::StringRef name, TypeQueryOptions options) 93 : m_options(options) { 94 if (std::optional<Type::ParsedName> parsed_name = 95 Type::GetTypeScopeAndBasename(name)) { 96 llvm::ArrayRef scope = parsed_name->scope; 97 if (!scope.empty()) { 98 if (scope[0] == "::") { 99 m_options |= e_exact_match; 100 scope = scope.drop_front(); 101 } 102 for (llvm::StringRef s : scope) { 103 m_context.push_back( 104 {CompilerContextKind::AnyDeclContext, ConstString(s)}); 105 } 106 } 107 m_context.push_back({ConvertTypeClass(parsed_name->type_class), 108 ConstString(parsed_name->basename)}); 109 } else { 110 m_context.push_back({CompilerContextKind::AnyType, ConstString(name)}); 111 } 112 } 113 114 TypeQuery::TypeQuery(const CompilerDeclContext &decl_ctx, 115 ConstString type_basename, TypeQueryOptions options) 116 : m_options(options) { 117 // Always use an exact match if we are looking for a type in compiler context. 118 m_options |= e_exact_match; 119 m_context = decl_ctx.GetCompilerContext(); 120 m_context.push_back({CompilerContextKind::AnyType, type_basename}); 121 } 122 123 TypeQuery::TypeQuery( 124 const llvm::ArrayRef<lldb_private::CompilerContext> &context, 125 TypeQueryOptions options) 126 : m_context(context), m_options(options) { 127 // Always use an exact match if we are looking for a type in compiler context. 128 m_options |= e_exact_match; 129 } 130 131 TypeQuery::TypeQuery(const CompilerDecl &decl, TypeQueryOptions options) 132 : m_options(options) { 133 // Always for an exact match if we are looking for a type using a declaration. 134 m_options |= e_exact_match; 135 m_context = decl.GetCompilerContext(); 136 } 137 138 ConstString TypeQuery::GetTypeBasename() const { 139 if (m_context.empty()) 140 return ConstString(); 141 return m_context.back().name; 142 } 143 144 void TypeQuery::AddLanguage(LanguageType language) { 145 if (!m_languages) 146 m_languages = LanguageSet(); 147 m_languages->Insert(language); 148 } 149 150 void TypeQuery::SetLanguages(LanguageSet languages) { 151 m_languages = std::move(languages); 152 } 153 154 bool TypeQuery::ContextMatches( 155 llvm::ArrayRef<CompilerContext> context_chain) const { 156 if (GetExactMatch() || context_chain.size() == m_context.size()) 157 return ::contextMatches(context_chain, m_context); 158 159 // We don't have an exact match, we need to bottom m_context.size() items to 160 // match for a successful lookup. 161 if (context_chain.size() < m_context.size()) 162 return false; // Not enough items in context_chain to allow for a match. 163 164 size_t compare_count = context_chain.size() - m_context.size(); 165 return ::contextMatches( 166 llvm::ArrayRef<CompilerContext>(context_chain.data() + compare_count, 167 m_context.size()), 168 m_context); 169 } 170 171 bool TypeQuery::LanguageMatches(lldb::LanguageType language) const { 172 // If we have no language filterm language always matches. 173 if (!m_languages.has_value()) 174 return true; 175 return (*m_languages)[language]; 176 } 177 178 bool TypeResults::AlreadySearched(lldb_private::SymbolFile *sym_file) { 179 return !m_searched_symbol_files.insert(sym_file).second; 180 } 181 182 bool TypeResults::InsertUnique(const lldb::TypeSP &type_sp) { 183 if (type_sp) 184 return m_type_map.InsertUnique(type_sp); 185 return false; 186 } 187 188 bool TypeResults::Done(const TypeQuery &query) const { 189 if (query.GetFindOne()) 190 return !m_type_map.Empty(); 191 return false; 192 } 193 194 void CompilerContext::Dump(Stream &s) const { 195 switch (kind) { 196 default: 197 s << "Invalid"; 198 break; 199 case CompilerContextKind::TranslationUnit: 200 s << "TranslationUnit"; 201 break; 202 case CompilerContextKind::Module: 203 s << "Module"; 204 break; 205 case CompilerContextKind::Namespace: 206 s << "Namespace"; 207 break; 208 case CompilerContextKind::ClassOrStruct: 209 s << "ClassOrStruct"; 210 break; 211 case CompilerContextKind::Union: 212 s << "Union"; 213 break; 214 case CompilerContextKind::Function: 215 s << "Function"; 216 break; 217 case CompilerContextKind::Variable: 218 s << "Variable"; 219 break; 220 case CompilerContextKind::Enum: 221 s << "Enumeration"; 222 break; 223 case CompilerContextKind::Typedef: 224 s << "Typedef"; 225 break; 226 case CompilerContextKind::AnyModule: 227 s << "AnyModule"; 228 break; 229 case CompilerContextKind::AnyType: 230 s << "AnyType"; 231 break; 232 } 233 s << "(" << name << ")"; 234 } 235 236 class TypeAppendVisitor { 237 public: 238 TypeAppendVisitor(TypeListImpl &type_list) : m_type_list(type_list) {} 239 240 bool operator()(const lldb::TypeSP &type) { 241 m_type_list.Append(TypeImplSP(new TypeImpl(type))); 242 return true; 243 } 244 245 private: 246 TypeListImpl &m_type_list; 247 }; 248 249 void TypeListImpl::Append(const lldb_private::TypeList &type_list) { 250 TypeAppendVisitor cb(*this); 251 type_list.ForEach(cb); 252 } 253 254 SymbolFileType::SymbolFileType(SymbolFile &symbol_file, 255 const lldb::TypeSP &type_sp) 256 : UserID(type_sp ? type_sp->GetID() : LLDB_INVALID_UID), 257 m_symbol_file(symbol_file), m_type_sp(type_sp) {} 258 259 Type *SymbolFileType::GetType() { 260 if (!m_type_sp) { 261 Type *resolved_type = m_symbol_file.ResolveTypeUID(GetID()); 262 if (resolved_type) 263 m_type_sp = resolved_type->shared_from_this(); 264 } 265 return m_type_sp.get(); 266 } 267 268 Type::Type(lldb::user_id_t uid, SymbolFile *symbol_file, ConstString name, 269 std::optional<uint64_t> byte_size, SymbolContextScope *context, 270 user_id_t encoding_uid, EncodingDataType encoding_uid_type, 271 const Declaration &decl, const CompilerType &compiler_type, 272 ResolveState compiler_type_resolve_state, uint32_t opaque_payload) 273 : std::enable_shared_from_this<Type>(), UserID(uid), m_name(name), 274 m_symbol_file(symbol_file), m_context(context), 275 m_encoding_uid(encoding_uid), m_encoding_uid_type(encoding_uid_type), 276 m_decl(decl), m_compiler_type(compiler_type), 277 m_compiler_type_resolve_state(compiler_type ? compiler_type_resolve_state 278 : ResolveState::Unresolved), 279 m_payload(opaque_payload) { 280 if (byte_size) { 281 m_byte_size = *byte_size; 282 m_byte_size_has_value = true; 283 } else { 284 m_byte_size = 0; 285 m_byte_size_has_value = false; 286 } 287 } 288 289 Type::Type() 290 : std::enable_shared_from_this<Type>(), UserID(0), m_name("<INVALID TYPE>"), 291 m_payload(0) { 292 m_byte_size = 0; 293 m_byte_size_has_value = false; 294 } 295 296 void Type::GetDescription(Stream *s, lldb::DescriptionLevel level, 297 bool show_name, ExecutionContextScope *exe_scope) { 298 *s << "id = " << (const UserID &)*this; 299 300 // Call the name accessor to make sure we resolve the type name 301 if (show_name) { 302 ConstString type_name = GetName(); 303 if (type_name) { 304 *s << ", name = \"" << type_name << '"'; 305 ConstString qualified_type_name(GetQualifiedName()); 306 if (qualified_type_name != type_name) { 307 *s << ", qualified = \"" << qualified_type_name << '"'; 308 } 309 } 310 } 311 312 // Call the get byte size accessor so we resolve our byte size 313 if (GetByteSize(exe_scope)) 314 s->Printf(", byte-size = %" PRIu64, m_byte_size); 315 bool show_fullpaths = (level == lldb::eDescriptionLevelVerbose); 316 m_decl.Dump(s, show_fullpaths); 317 318 if (m_compiler_type.IsValid()) { 319 *s << ", compiler_type = \""; 320 GetForwardCompilerType().DumpTypeDescription(s); 321 *s << '"'; 322 } else if (m_encoding_uid != LLDB_INVALID_UID) { 323 s->Printf(", type_uid = 0x%8.8" PRIx64, m_encoding_uid); 324 switch (m_encoding_uid_type) { 325 case eEncodingInvalid: 326 break; 327 case eEncodingIsUID: 328 s->PutCString(" (unresolved type)"); 329 break; 330 case eEncodingIsConstUID: 331 s->PutCString(" (unresolved const type)"); 332 break; 333 case eEncodingIsRestrictUID: 334 s->PutCString(" (unresolved restrict type)"); 335 break; 336 case eEncodingIsVolatileUID: 337 s->PutCString(" (unresolved volatile type)"); 338 break; 339 case eEncodingIsAtomicUID: 340 s->PutCString(" (unresolved atomic type)"); 341 break; 342 case eEncodingIsTypedefUID: 343 s->PutCString(" (unresolved typedef)"); 344 break; 345 case eEncodingIsPointerUID: 346 s->PutCString(" (unresolved pointer)"); 347 break; 348 case eEncodingIsLValueReferenceUID: 349 s->PutCString(" (unresolved L value reference)"); 350 break; 351 case eEncodingIsRValueReferenceUID: 352 s->PutCString(" (unresolved R value reference)"); 353 break; 354 case eEncodingIsSyntheticUID: 355 s->PutCString(" (synthetic type)"); 356 break; 357 case eEncodingIsLLVMPtrAuthUID: 358 s->PutCString(" (ptrauth type)"); 359 break; 360 } 361 } 362 } 363 364 void Type::Dump(Stream *s, bool show_context, lldb::DescriptionLevel level) { 365 s->Printf("%p: ", static_cast<void *>(this)); 366 s->Indent(); 367 *s << "Type" << static_cast<const UserID &>(*this) << ' '; 368 if (m_name) 369 *s << ", name = \"" << m_name << "\""; 370 371 if (m_byte_size_has_value) 372 s->Printf(", size = %" PRIu64, m_byte_size); 373 374 if (show_context && m_context != nullptr) { 375 s->PutCString(", context = ( "); 376 m_context->DumpSymbolContext(s); 377 s->PutCString(" )"); 378 } 379 380 bool show_fullpaths = false; 381 m_decl.Dump(s, show_fullpaths); 382 383 if (m_compiler_type.IsValid()) { 384 *s << ", compiler_type = " << m_compiler_type.GetOpaqueQualType() << ' '; 385 GetForwardCompilerType().DumpTypeDescription(s, level); 386 } else if (m_encoding_uid != LLDB_INVALID_UID) { 387 s->Format(", type_data = {0:x-16}", m_encoding_uid); 388 switch (m_encoding_uid_type) { 389 case eEncodingInvalid: 390 break; 391 case eEncodingIsUID: 392 s->PutCString(" (unresolved type)"); 393 break; 394 case eEncodingIsConstUID: 395 s->PutCString(" (unresolved const type)"); 396 break; 397 case eEncodingIsRestrictUID: 398 s->PutCString(" (unresolved restrict type)"); 399 break; 400 case eEncodingIsVolatileUID: 401 s->PutCString(" (unresolved volatile type)"); 402 break; 403 case eEncodingIsAtomicUID: 404 s->PutCString(" (unresolved atomic type)"); 405 break; 406 case eEncodingIsTypedefUID: 407 s->PutCString(" (unresolved typedef)"); 408 break; 409 case eEncodingIsPointerUID: 410 s->PutCString(" (unresolved pointer)"); 411 break; 412 case eEncodingIsLValueReferenceUID: 413 s->PutCString(" (unresolved L value reference)"); 414 break; 415 case eEncodingIsRValueReferenceUID: 416 s->PutCString(" (unresolved R value reference)"); 417 break; 418 case eEncodingIsSyntheticUID: 419 s->PutCString(" (synthetic type)"); 420 break; 421 case eEncodingIsLLVMPtrAuthUID: 422 s->PutCString(" (ptrauth type)"); 423 } 424 } 425 426 // 427 // if (m_access) 428 // s->Printf(", access = %u", m_access); 429 s->EOL(); 430 } 431 432 ConstString Type::GetName() { 433 if (!m_name) 434 m_name = GetForwardCompilerType().GetTypeName(); 435 return m_name; 436 } 437 438 ConstString Type::GetBaseName() { 439 return GetForwardCompilerType().GetTypeName(/*BaseOnly*/ true); 440 } 441 442 void Type::DumpTypeName(Stream *s) { GetName().Dump(s, "<invalid-type-name>"); } 443 444 Type *Type::GetEncodingType() { 445 if (m_encoding_type == nullptr && m_encoding_uid != LLDB_INVALID_UID) 446 m_encoding_type = m_symbol_file->ResolveTypeUID(m_encoding_uid); 447 return m_encoding_type; 448 } 449 450 std::optional<uint64_t> Type::GetByteSize(ExecutionContextScope *exe_scope) { 451 if (m_byte_size_has_value) 452 return static_cast<uint64_t>(m_byte_size); 453 454 switch (m_encoding_uid_type) { 455 case eEncodingInvalid: 456 case eEncodingIsSyntheticUID: 457 break; 458 case eEncodingIsUID: 459 case eEncodingIsConstUID: 460 case eEncodingIsRestrictUID: 461 case eEncodingIsVolatileUID: 462 case eEncodingIsAtomicUID: 463 case eEncodingIsTypedefUID: { 464 Type *encoding_type = GetEncodingType(); 465 if (encoding_type) 466 if (std::optional<uint64_t> size = 467 encoding_type->GetByteSize(exe_scope)) { 468 m_byte_size = *size; 469 m_byte_size_has_value = true; 470 return static_cast<uint64_t>(m_byte_size); 471 } 472 473 if (std::optional<uint64_t> size = 474 GetLayoutCompilerType().GetByteSize(exe_scope)) { 475 m_byte_size = *size; 476 m_byte_size_has_value = true; 477 return static_cast<uint64_t>(m_byte_size); 478 } 479 } break; 480 481 // If we are a pointer or reference, then this is just a pointer size; 482 case eEncodingIsPointerUID: 483 case eEncodingIsLValueReferenceUID: 484 case eEncodingIsRValueReferenceUID: 485 case eEncodingIsLLVMPtrAuthUID: { 486 if (ArchSpec arch = m_symbol_file->GetObjectFile()->GetArchitecture()) { 487 m_byte_size = arch.GetAddressByteSize(); 488 m_byte_size_has_value = true; 489 return static_cast<uint64_t>(m_byte_size); 490 } 491 } break; 492 } 493 return {}; 494 } 495 496 llvm::Expected<uint32_t> Type::GetNumChildren(bool omit_empty_base_classes) { 497 return GetForwardCompilerType().GetNumChildren(omit_empty_base_classes, nullptr); 498 } 499 500 bool Type::IsAggregateType() { 501 return GetForwardCompilerType().IsAggregateType(); 502 } 503 504 bool Type::IsTemplateType() { 505 return GetForwardCompilerType().IsTemplateType(); 506 } 507 508 lldb::TypeSP Type::GetTypedefType() { 509 lldb::TypeSP type_sp; 510 if (IsTypedef()) { 511 Type *typedef_type = m_symbol_file->ResolveTypeUID(m_encoding_uid); 512 if (typedef_type) 513 type_sp = typedef_type->shared_from_this(); 514 } 515 return type_sp; 516 } 517 518 lldb::Format Type::GetFormat() { return GetForwardCompilerType().GetFormat(); } 519 520 lldb::Encoding Type::GetEncoding(uint64_t &count) { 521 // Make sure we resolve our type if it already hasn't been. 522 return GetForwardCompilerType().GetEncoding(count); 523 } 524 525 bool Type::ReadFromMemory(ExecutionContext *exe_ctx, lldb::addr_t addr, 526 AddressType address_type, DataExtractor &data) { 527 if (address_type == eAddressTypeFile) { 528 // Can't convert a file address to anything valid without more context 529 // (which Module it came from) 530 return false; 531 } 532 533 const uint64_t byte_size = 534 GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr) 535 .value_or(0); 536 if (data.GetByteSize() < byte_size) { 537 lldb::DataBufferSP data_sp(new DataBufferHeap(byte_size, '\0')); 538 data.SetData(data_sp); 539 } 540 541 uint8_t *dst = const_cast<uint8_t *>(data.PeekData(0, byte_size)); 542 if (dst != nullptr) { 543 if (address_type == eAddressTypeHost) { 544 // The address is an address in this process, so just copy it 545 if (addr == 0) 546 return false; 547 memcpy(dst, reinterpret_cast<uint8_t *>(addr), byte_size); 548 return true; 549 } else { 550 if (exe_ctx) { 551 Process *process = exe_ctx->GetProcessPtr(); 552 if (process) { 553 Status error; 554 return exe_ctx->GetProcessPtr()->ReadMemory(addr, dst, byte_size, 555 error) == byte_size; 556 } 557 } 558 } 559 } 560 return false; 561 } 562 563 bool Type::WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t addr, 564 AddressType address_type, DataExtractor &data) { 565 return false; 566 } 567 568 const Declaration &Type::GetDeclaration() const { return m_decl; } 569 570 bool Type::ResolveCompilerType(ResolveState compiler_type_resolve_state) { 571 // TODO: This needs to consider the correct type system to use. 572 Type *encoding_type = nullptr; 573 if (!m_compiler_type.IsValid()) { 574 encoding_type = GetEncodingType(); 575 if (encoding_type) { 576 switch (m_encoding_uid_type) { 577 case eEncodingIsUID: { 578 CompilerType encoding_compiler_type = 579 encoding_type->GetForwardCompilerType(); 580 if (encoding_compiler_type.IsValid()) { 581 m_compiler_type = encoding_compiler_type; 582 m_compiler_type_resolve_state = 583 encoding_type->m_compiler_type_resolve_state; 584 } 585 } break; 586 587 case eEncodingIsConstUID: 588 m_compiler_type = 589 encoding_type->GetForwardCompilerType().AddConstModifier(); 590 break; 591 592 case eEncodingIsRestrictUID: 593 m_compiler_type = 594 encoding_type->GetForwardCompilerType().AddRestrictModifier(); 595 break; 596 597 case eEncodingIsVolatileUID: 598 m_compiler_type = 599 encoding_type->GetForwardCompilerType().AddVolatileModifier(); 600 break; 601 602 case eEncodingIsAtomicUID: 603 m_compiler_type = 604 encoding_type->GetForwardCompilerType().GetAtomicType(); 605 break; 606 607 case eEncodingIsTypedefUID: 608 m_compiler_type = encoding_type->GetForwardCompilerType().CreateTypedef( 609 m_name.AsCString("__lldb_invalid_typedef_name"), 610 GetSymbolFile()->GetDeclContextContainingUID(GetID()), m_payload); 611 m_name.Clear(); 612 break; 613 614 case eEncodingIsPointerUID: 615 m_compiler_type = 616 encoding_type->GetForwardCompilerType().GetPointerType(); 617 break; 618 619 case eEncodingIsLValueReferenceUID: 620 m_compiler_type = 621 encoding_type->GetForwardCompilerType().GetLValueReferenceType(); 622 break; 623 624 case eEncodingIsRValueReferenceUID: 625 m_compiler_type = 626 encoding_type->GetForwardCompilerType().GetRValueReferenceType(); 627 break; 628 629 case eEncodingIsLLVMPtrAuthUID: 630 m_compiler_type = 631 encoding_type->GetForwardCompilerType().AddPtrAuthModifier( 632 m_payload); 633 break; 634 635 default: 636 llvm_unreachable("Unhandled encoding_data_type."); 637 } 638 } else { 639 // We have no encoding type, return void? 640 auto type_system_or_err = 641 m_symbol_file->GetTypeSystemForLanguage(eLanguageTypeC); 642 if (auto err = type_system_or_err.takeError()) { 643 LLDB_LOG_ERROR( 644 GetLog(LLDBLog::Symbols), std::move(err), 645 "Unable to construct void type from TypeSystemClang: {0}"); 646 } else { 647 CompilerType void_compiler_type; 648 auto ts = *type_system_or_err; 649 if (ts) 650 void_compiler_type = ts->GetBasicTypeFromAST(eBasicTypeVoid); 651 switch (m_encoding_uid_type) { 652 case eEncodingIsUID: 653 m_compiler_type = void_compiler_type; 654 break; 655 656 case eEncodingIsConstUID: 657 m_compiler_type = void_compiler_type.AddConstModifier(); 658 break; 659 660 case eEncodingIsRestrictUID: 661 m_compiler_type = void_compiler_type.AddRestrictModifier(); 662 break; 663 664 case eEncodingIsVolatileUID: 665 m_compiler_type = void_compiler_type.AddVolatileModifier(); 666 break; 667 668 case eEncodingIsAtomicUID: 669 m_compiler_type = void_compiler_type.GetAtomicType(); 670 break; 671 672 case eEncodingIsTypedefUID: 673 m_compiler_type = void_compiler_type.CreateTypedef( 674 m_name.AsCString("__lldb_invalid_typedef_name"), 675 GetSymbolFile()->GetDeclContextContainingUID(GetID()), m_payload); 676 break; 677 678 case eEncodingIsPointerUID: 679 m_compiler_type = void_compiler_type.GetPointerType(); 680 break; 681 682 case eEncodingIsLValueReferenceUID: 683 m_compiler_type = void_compiler_type.GetLValueReferenceType(); 684 break; 685 686 case eEncodingIsRValueReferenceUID: 687 m_compiler_type = void_compiler_type.GetRValueReferenceType(); 688 break; 689 690 case eEncodingIsLLVMPtrAuthUID: 691 llvm_unreachable("Cannot handle eEncodingIsLLVMPtrAuthUID without " 692 "valid encoding_type"); 693 694 default: 695 llvm_unreachable("Unhandled encoding_data_type."); 696 } 697 } 698 } 699 700 // When we have a EncodingUID, our "m_flags.compiler_type_resolve_state" is 701 // set to eResolveStateUnresolved so we need to update it to say that we 702 // now have a forward declaration since that is what we created above. 703 if (m_compiler_type.IsValid()) 704 m_compiler_type_resolve_state = ResolveState::Forward; 705 } 706 707 // Check if we have a forward reference to a class/struct/union/enum? 708 if (compiler_type_resolve_state == ResolveState::Layout || 709 compiler_type_resolve_state == ResolveState::Full) { 710 // Check if we have a forward reference to a class/struct/union/enum? 711 if (m_compiler_type.IsValid() && 712 m_compiler_type_resolve_state < compiler_type_resolve_state) { 713 m_compiler_type_resolve_state = ResolveState::Full; 714 if (!m_compiler_type.IsDefined()) { 715 // We have a forward declaration, we need to resolve it to a complete 716 // definition. 717 m_symbol_file->CompleteType(m_compiler_type); 718 } 719 } 720 } 721 722 // If we have an encoding type, then we need to make sure it is resolved 723 // appropriately. 724 if (m_encoding_uid != LLDB_INVALID_UID) { 725 if (encoding_type == nullptr) 726 encoding_type = GetEncodingType(); 727 if (encoding_type) { 728 ResolveState encoding_compiler_type_resolve_state = 729 compiler_type_resolve_state; 730 731 if (compiler_type_resolve_state == ResolveState::Layout) { 732 switch (m_encoding_uid_type) { 733 case eEncodingIsPointerUID: 734 case eEncodingIsLValueReferenceUID: 735 case eEncodingIsRValueReferenceUID: 736 encoding_compiler_type_resolve_state = ResolveState::Forward; 737 break; 738 default: 739 break; 740 } 741 } 742 encoding_type->ResolveCompilerType(encoding_compiler_type_resolve_state); 743 } 744 } 745 return m_compiler_type.IsValid(); 746 } 747 uint32_t Type::GetEncodingMask() { 748 uint32_t encoding_mask = 1u << m_encoding_uid_type; 749 Type *encoding_type = GetEncodingType(); 750 assert(encoding_type != this); 751 if (encoding_type) 752 encoding_mask |= encoding_type->GetEncodingMask(); 753 return encoding_mask; 754 } 755 756 CompilerType Type::GetFullCompilerType() { 757 ResolveCompilerType(ResolveState::Full); 758 return m_compiler_type; 759 } 760 761 CompilerType Type::GetLayoutCompilerType() { 762 ResolveCompilerType(ResolveState::Layout); 763 return m_compiler_type; 764 } 765 766 CompilerType Type::GetForwardCompilerType() { 767 ResolveCompilerType(ResolveState::Forward); 768 return m_compiler_type; 769 } 770 771 ConstString Type::GetQualifiedName() { 772 return GetForwardCompilerType().GetTypeName(); 773 } 774 775 std::optional<Type::ParsedName> 776 Type::GetTypeScopeAndBasename(llvm::StringRef name) { 777 ParsedName result; 778 779 if (name.empty()) 780 return std::nullopt; 781 782 if (name.consume_front("struct ")) 783 result.type_class = eTypeClassStruct; 784 else if (name.consume_front("class ")) 785 result.type_class = eTypeClassClass; 786 else if (name.consume_front("union ")) 787 result.type_class = eTypeClassUnion; 788 else if (name.consume_front("enum ")) 789 result.type_class = eTypeClassEnumeration; 790 else if (name.consume_front("typedef ")) 791 result.type_class = eTypeClassTypedef; 792 793 if (name.consume_front("::")) 794 result.scope.push_back("::"); 795 796 bool prev_is_colon = false; 797 size_t template_depth = 0; 798 size_t name_begin = 0; 799 for (const auto &pos : llvm::enumerate(name)) { 800 switch (pos.value()) { 801 case ':': 802 if (prev_is_colon && template_depth == 0) { 803 result.scope.push_back(name.slice(name_begin, pos.index() - 1)); 804 name_begin = pos.index() + 1; 805 } 806 break; 807 case '<': 808 ++template_depth; 809 break; 810 case '>': 811 if (template_depth == 0) 812 return std::nullopt; // Invalid name. 813 --template_depth; 814 break; 815 } 816 prev_is_colon = pos.value() == ':'; 817 } 818 819 if (name_begin < name.size() && template_depth == 0) 820 result.basename = name.substr(name_begin); 821 else 822 return std::nullopt; 823 824 return result; 825 } 826 827 ModuleSP Type::GetModule() { 828 if (m_symbol_file) 829 return m_symbol_file->GetObjectFile()->GetModule(); 830 return ModuleSP(); 831 } 832 833 ModuleSP Type::GetExeModule() { 834 if (m_compiler_type) { 835 auto ts = m_compiler_type.GetTypeSystem(); 836 if (!ts) 837 return {}; 838 SymbolFile *symbol_file = ts->GetSymbolFile(); 839 if (symbol_file) 840 return symbol_file->GetObjectFile()->GetModule(); 841 } 842 return {}; 843 } 844 845 TypeAndOrName::TypeAndOrName(TypeSP &in_type_sp) { 846 if (in_type_sp) { 847 m_compiler_type = in_type_sp->GetForwardCompilerType(); 848 m_type_name = in_type_sp->GetName(); 849 } 850 } 851 852 TypeAndOrName::TypeAndOrName(const char *in_type_str) 853 : m_type_name(in_type_str) {} 854 855 TypeAndOrName::TypeAndOrName(ConstString &in_type_const_string) 856 : m_type_name(in_type_const_string) {} 857 858 bool TypeAndOrName::operator==(const TypeAndOrName &other) const { 859 if (m_compiler_type != other.m_compiler_type) 860 return false; 861 if (m_type_name != other.m_type_name) 862 return false; 863 return true; 864 } 865 866 bool TypeAndOrName::operator!=(const TypeAndOrName &other) const { 867 return !(*this == other); 868 } 869 870 ConstString TypeAndOrName::GetName() const { 871 if (m_type_name) 872 return m_type_name; 873 if (m_compiler_type) 874 return m_compiler_type.GetTypeName(); 875 return ConstString("<invalid>"); 876 } 877 878 void TypeAndOrName::SetName(ConstString type_name) { 879 m_type_name = type_name; 880 } 881 882 void TypeAndOrName::SetName(const char *type_name_cstr) { 883 m_type_name.SetCString(type_name_cstr); 884 } 885 886 void TypeAndOrName::SetName(llvm::StringRef type_name) { 887 m_type_name.SetString(type_name); 888 } 889 890 void TypeAndOrName::SetTypeSP(lldb::TypeSP type_sp) { 891 if (type_sp) { 892 m_compiler_type = type_sp->GetForwardCompilerType(); 893 m_type_name = type_sp->GetName(); 894 } else 895 Clear(); 896 } 897 898 void TypeAndOrName::SetCompilerType(CompilerType compiler_type) { 899 m_compiler_type = compiler_type; 900 if (m_compiler_type) 901 m_type_name = m_compiler_type.GetTypeName(); 902 } 903 904 bool TypeAndOrName::IsEmpty() const { 905 return !((bool)m_type_name || (bool)m_compiler_type); 906 } 907 908 void TypeAndOrName::Clear() { 909 m_type_name.Clear(); 910 m_compiler_type.Clear(); 911 } 912 913 bool TypeAndOrName::HasName() const { return (bool)m_type_name; } 914 915 bool TypeAndOrName::HasCompilerType() const { 916 return m_compiler_type.IsValid(); 917 } 918 919 TypeImpl::TypeImpl(const lldb::TypeSP &type_sp) 920 : m_module_wp(), m_static_type(), m_dynamic_type() { 921 SetType(type_sp); 922 } 923 924 TypeImpl::TypeImpl(const CompilerType &compiler_type) 925 : m_module_wp(), m_static_type(), m_dynamic_type() { 926 SetType(compiler_type); 927 } 928 929 TypeImpl::TypeImpl(const lldb::TypeSP &type_sp, const CompilerType &dynamic) 930 : m_module_wp(), m_static_type(), m_dynamic_type(dynamic) { 931 SetType(type_sp, dynamic); 932 } 933 934 TypeImpl::TypeImpl(const CompilerType &static_type, 935 const CompilerType &dynamic_type) 936 : m_module_wp(), m_static_type(), m_dynamic_type() { 937 SetType(static_type, dynamic_type); 938 } 939 940 void TypeImpl::SetType(const lldb::TypeSP &type_sp) { 941 if (type_sp) { 942 m_static_type = type_sp->GetForwardCompilerType(); 943 m_exe_module_wp = type_sp->GetExeModule(); 944 m_module_wp = type_sp->GetModule(); 945 } else { 946 m_static_type.Clear(); 947 m_module_wp = lldb::ModuleWP(); 948 } 949 } 950 951 void TypeImpl::SetType(const CompilerType &compiler_type) { 952 m_module_wp = lldb::ModuleWP(); 953 m_static_type = compiler_type; 954 } 955 956 void TypeImpl::SetType(const lldb::TypeSP &type_sp, 957 const CompilerType &dynamic) { 958 SetType(type_sp); 959 m_dynamic_type = dynamic; 960 } 961 962 void TypeImpl::SetType(const CompilerType &compiler_type, 963 const CompilerType &dynamic) { 964 m_module_wp = lldb::ModuleWP(); 965 m_static_type = compiler_type; 966 m_dynamic_type = dynamic; 967 } 968 969 bool TypeImpl::CheckModule(lldb::ModuleSP &module_sp) const { 970 return CheckModuleCommon(m_module_wp, module_sp); 971 } 972 973 bool TypeImpl::CheckExeModule(lldb::ModuleSP &module_sp) const { 974 return CheckModuleCommon(m_exe_module_wp, module_sp); 975 } 976 977 bool TypeImpl::CheckModuleCommon(const lldb::ModuleWP &input_module_wp, 978 lldb::ModuleSP &module_sp) const { 979 // Check if we have a module for this type. If we do and the shared pointer 980 // is can be successfully initialized with m_module_wp, return true. Else 981 // return false if we didn't have a module, or if we had a module and it has 982 // been deleted. Any functions doing anything with a TypeSP in this TypeImpl 983 // class should call this function and only do anything with the ivars if 984 // this function returns true. If we have a module, the "module_sp" will be 985 // filled in with a strong reference to the module so that the module will at 986 // least stay around long enough for the type query to succeed. 987 module_sp = input_module_wp.lock(); 988 if (!module_sp) { 989 lldb::ModuleWP empty_module_wp; 990 // If either call to "std::weak_ptr::owner_before(...) value returns true, 991 // this indicates that m_module_wp once contained (possibly still does) a 992 // reference to a valid shared pointer. This helps us know if we had a 993 // valid reference to a section which is now invalid because the module it 994 // was in was deleted 995 if (empty_module_wp.owner_before(input_module_wp) || 996 input_module_wp.owner_before(empty_module_wp)) { 997 // input_module_wp had a valid reference to a module, but all strong 998 // references have been released and the module has been deleted 999 return false; 1000 } 1001 } 1002 // We either successfully locked the module, or didn't have one to begin with 1003 return true; 1004 } 1005 1006 bool TypeImpl::operator==(const TypeImpl &rhs) const { 1007 return m_static_type == rhs.m_static_type && 1008 m_dynamic_type == rhs.m_dynamic_type; 1009 } 1010 1011 bool TypeImpl::operator!=(const TypeImpl &rhs) const { 1012 return !(*this == rhs); 1013 } 1014 1015 bool TypeImpl::IsValid() const { 1016 // just a name is not valid 1017 ModuleSP module_sp; 1018 if (CheckModule(module_sp)) 1019 return m_static_type.IsValid() || m_dynamic_type.IsValid(); 1020 return false; 1021 } 1022 1023 TypeImpl::operator bool() const { return IsValid(); } 1024 1025 void TypeImpl::Clear() { 1026 m_module_wp = lldb::ModuleWP(); 1027 m_static_type.Clear(); 1028 m_dynamic_type.Clear(); 1029 } 1030 1031 ModuleSP TypeImpl::GetModule() const { 1032 lldb::ModuleSP module_sp; 1033 if (CheckExeModule(module_sp)) 1034 return module_sp; 1035 return nullptr; 1036 } 1037 1038 ConstString TypeImpl::GetName() const { 1039 ModuleSP module_sp; 1040 if (CheckModule(module_sp)) { 1041 if (m_dynamic_type) 1042 return m_dynamic_type.GetTypeName(); 1043 return m_static_type.GetTypeName(); 1044 } 1045 return ConstString(); 1046 } 1047 1048 ConstString TypeImpl::GetDisplayTypeName() const { 1049 ModuleSP module_sp; 1050 if (CheckModule(module_sp)) { 1051 if (m_dynamic_type) 1052 return m_dynamic_type.GetDisplayTypeName(); 1053 return m_static_type.GetDisplayTypeName(); 1054 } 1055 return ConstString(); 1056 } 1057 1058 TypeImpl TypeImpl::GetPointerType() const { 1059 ModuleSP module_sp; 1060 if (CheckModule(module_sp)) { 1061 if (m_dynamic_type.IsValid()) { 1062 return TypeImpl(m_static_type.GetPointerType(), 1063 m_dynamic_type.GetPointerType()); 1064 } 1065 return TypeImpl(m_static_type.GetPointerType()); 1066 } 1067 return TypeImpl(); 1068 } 1069 1070 TypeImpl TypeImpl::GetPointeeType() const { 1071 ModuleSP module_sp; 1072 if (CheckModule(module_sp)) { 1073 if (m_dynamic_type.IsValid()) { 1074 return TypeImpl(m_static_type.GetPointeeType(), 1075 m_dynamic_type.GetPointeeType()); 1076 } 1077 return TypeImpl(m_static_type.GetPointeeType()); 1078 } 1079 return TypeImpl(); 1080 } 1081 1082 TypeImpl TypeImpl::GetReferenceType() const { 1083 ModuleSP module_sp; 1084 if (CheckModule(module_sp)) { 1085 if (m_dynamic_type.IsValid()) { 1086 return TypeImpl(m_static_type.GetLValueReferenceType(), 1087 m_dynamic_type.GetLValueReferenceType()); 1088 } 1089 return TypeImpl(m_static_type.GetLValueReferenceType()); 1090 } 1091 return TypeImpl(); 1092 } 1093 1094 TypeImpl TypeImpl::GetTypedefedType() const { 1095 ModuleSP module_sp; 1096 if (CheckModule(module_sp)) { 1097 if (m_dynamic_type.IsValid()) { 1098 return TypeImpl(m_static_type.GetTypedefedType(), 1099 m_dynamic_type.GetTypedefedType()); 1100 } 1101 return TypeImpl(m_static_type.GetTypedefedType()); 1102 } 1103 return TypeImpl(); 1104 } 1105 1106 TypeImpl TypeImpl::GetDereferencedType() const { 1107 ModuleSP module_sp; 1108 if (CheckModule(module_sp)) { 1109 if (m_dynamic_type.IsValid()) { 1110 return TypeImpl(m_static_type.GetNonReferenceType(), 1111 m_dynamic_type.GetNonReferenceType()); 1112 } 1113 return TypeImpl(m_static_type.GetNonReferenceType()); 1114 } 1115 return TypeImpl(); 1116 } 1117 1118 TypeImpl TypeImpl::GetUnqualifiedType() const { 1119 ModuleSP module_sp; 1120 if (CheckModule(module_sp)) { 1121 if (m_dynamic_type.IsValid()) { 1122 return TypeImpl(m_static_type.GetFullyUnqualifiedType(), 1123 m_dynamic_type.GetFullyUnqualifiedType()); 1124 } 1125 return TypeImpl(m_static_type.GetFullyUnqualifiedType()); 1126 } 1127 return TypeImpl(); 1128 } 1129 1130 TypeImpl TypeImpl::GetCanonicalType() const { 1131 ModuleSP module_sp; 1132 if (CheckModule(module_sp)) { 1133 if (m_dynamic_type.IsValid()) { 1134 return TypeImpl(m_static_type.GetCanonicalType(), 1135 m_dynamic_type.GetCanonicalType()); 1136 } 1137 return TypeImpl(m_static_type.GetCanonicalType()); 1138 } 1139 return TypeImpl(); 1140 } 1141 1142 CompilerType TypeImpl::GetCompilerType(bool prefer_dynamic) { 1143 ModuleSP module_sp; 1144 if (CheckModule(module_sp)) { 1145 if (prefer_dynamic) { 1146 if (m_dynamic_type.IsValid()) 1147 return m_dynamic_type; 1148 } 1149 return m_static_type; 1150 } 1151 return CompilerType(); 1152 } 1153 1154 CompilerType::TypeSystemSPWrapper TypeImpl::GetTypeSystem(bool prefer_dynamic) { 1155 ModuleSP module_sp; 1156 if (CheckModule(module_sp)) { 1157 if (prefer_dynamic) { 1158 if (m_dynamic_type.IsValid()) 1159 return m_dynamic_type.GetTypeSystem(); 1160 } 1161 return m_static_type.GetTypeSystem(); 1162 } 1163 return {}; 1164 } 1165 1166 bool TypeImpl::GetDescription(lldb_private::Stream &strm, 1167 lldb::DescriptionLevel description_level) { 1168 ModuleSP module_sp; 1169 if (CheckModule(module_sp)) { 1170 if (m_dynamic_type.IsValid()) { 1171 strm.Printf("Dynamic:\n"); 1172 m_dynamic_type.DumpTypeDescription(&strm); 1173 strm.Printf("\nStatic:\n"); 1174 } 1175 m_static_type.DumpTypeDescription(&strm); 1176 } else { 1177 strm.PutCString("Invalid TypeImpl module for type has been deleted\n"); 1178 } 1179 return true; 1180 } 1181 1182 CompilerType TypeImpl::FindDirectNestedType(llvm::StringRef name) { 1183 if (name.empty()) 1184 return CompilerType(); 1185 return GetCompilerType(/*prefer_dynamic=*/false) 1186 .GetDirectNestedTypeWithName(name); 1187 } 1188 1189 bool TypeMemberFunctionImpl::IsValid() { 1190 return m_type.IsValid() && m_kind != lldb::eMemberFunctionKindUnknown; 1191 } 1192 1193 ConstString TypeMemberFunctionImpl::GetName() const { return m_name; } 1194 1195 ConstString TypeMemberFunctionImpl::GetMangledName() const { 1196 return m_decl.GetMangledName(); 1197 } 1198 1199 CompilerType TypeMemberFunctionImpl::GetType() const { return m_type; } 1200 1201 lldb::MemberFunctionKind TypeMemberFunctionImpl::GetKind() const { 1202 return m_kind; 1203 } 1204 1205 bool TypeMemberFunctionImpl::GetDescription(Stream &stream) { 1206 switch (m_kind) { 1207 case lldb::eMemberFunctionKindUnknown: 1208 return false; 1209 case lldb::eMemberFunctionKindConstructor: 1210 stream.Printf("constructor for %s", 1211 m_type.GetTypeName().AsCString("<unknown>")); 1212 break; 1213 case lldb::eMemberFunctionKindDestructor: 1214 stream.Printf("destructor for %s", 1215 m_type.GetTypeName().AsCString("<unknown>")); 1216 break; 1217 case lldb::eMemberFunctionKindInstanceMethod: 1218 stream.Printf("instance method %s of type %s", m_name.AsCString(), 1219 m_decl.GetDeclContext().GetName().AsCString()); 1220 break; 1221 case lldb::eMemberFunctionKindStaticMethod: 1222 stream.Printf("static method %s of type %s", m_name.AsCString(), 1223 m_decl.GetDeclContext().GetName().AsCString()); 1224 break; 1225 } 1226 return true; 1227 } 1228 1229 CompilerType TypeMemberFunctionImpl::GetReturnType() const { 1230 if (m_type) 1231 return m_type.GetFunctionReturnType(); 1232 return m_decl.GetFunctionReturnType(); 1233 } 1234 1235 size_t TypeMemberFunctionImpl::GetNumArguments() const { 1236 if (m_type) 1237 return m_type.GetNumberOfFunctionArguments(); 1238 else 1239 return m_decl.GetNumFunctionArguments(); 1240 } 1241 1242 CompilerType TypeMemberFunctionImpl::GetArgumentAtIndex(size_t idx) const { 1243 if (m_type) 1244 return m_type.GetFunctionArgumentAtIndex(idx); 1245 else 1246 return m_decl.GetFunctionArgumentType(idx); 1247 } 1248 1249 TypeEnumMemberImpl::TypeEnumMemberImpl(const lldb::TypeImplSP &integer_type_sp, 1250 ConstString name, 1251 const llvm::APSInt &value) 1252 : m_integer_type_sp(integer_type_sp), m_name(name), m_value(value), 1253 m_valid((bool)name && (bool)integer_type_sp) 1254 1255 {} 1256