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 <algorithm> 10 #include <cstdio> 11 #include <iterator> 12 #include <optional> 13 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" 21 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" 30 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" 36 37 #include "llvm/ADT/StringRef.h" 38 39 using namespace lldb; 40 using namespace lldb_private; 41 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(); 47 } 48 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; 63 return result; 64 } 65 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; 71 if (!scope.empty()) { 72 if (scope[0] == "::") { 73 m_options |= e_exact_match; 74 scope = scope.drop_front(); 75 } 76 for (llvm::StringRef s : scope) { 77 m_context.push_back( 78 {CompilerContextKind::AnyDeclContext, ConstString(s)}); 79 } 80 } 81 m_context.push_back({ConvertTypeClass(parsed_name->type_class), 82 ConstString(parsed_name->basename)}); 83 } else { 84 m_context.push_back({CompilerContextKind::AnyType, ConstString(name)}); 85 } 86 } 87 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}); 95 } 96 97 TypeQuery::TypeQuery( 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; 103 } 104 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(); 110 } 111 112 ConstString TypeQuery::GetTypeBasename() const { 113 if (m_context.empty()) 114 return ConstString(); 115 return m_context.back().name; 116 } 117 118 void TypeQuery::AddLanguage(LanguageType language) { 119 if (!m_languages) 120 m_languages = LanguageSet(); 121 m_languages->Insert(language); 122 } 123 124 void TypeQuery::SetLanguages(LanguageSet languages) { 125 m_languages = std::move(languages); 126 } 127 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(); 132 pat != pat_end;) { 133 134 if (ctx == ctx_end) 135 return false; // Pattern too long. 136 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. 143 ++pat; 144 } 145 // Otherwise, only advance the context to skip over the anonymous 146 // namespace, and try matching again. 147 ++ctx; 148 continue; 149 } 150 151 // See if there is a kind mismatch; they should have 1 bit in common. 152 if ((ctx->kind & pat->kind) == CompilerContextKind()) 153 return false; 154 155 if (ctx->name != pat->name) 156 return false; 157 158 ++ctx; 159 ++pat; 160 } 161 162 // Skip over any remaining module and anonymous namespace entries if we were 163 // asked to do that. 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(); 169 return false; 170 }; 171 ctx = std::find_if_not(ctx, ctx_end, should_skip); 172 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()) 176 return true; 177 178 // We have an exact match if we've exhausted the target context as well. 179 return ctx == ctx_end; 180 } 181 182 bool TypeQuery::LanguageMatches(lldb::LanguageType language) const { 183 // If we have no language filterm language always matches. 184 if (!m_languages.has_value()) 185 return true; 186 return (*m_languages)[language]; 187 } 188 189 bool TypeResults::AlreadySearched(lldb_private::SymbolFile *sym_file) { 190 return !m_searched_symbol_files.insert(sym_file).second; 191 } 192 193 bool TypeResults::InsertUnique(const lldb::TypeSP &type_sp) { 194 if (type_sp) 195 return m_type_map.InsertUnique(type_sp); 196 return false; 197 } 198 199 bool TypeResults::Done(const TypeQuery &query) const { 200 if (query.GetFindOne()) 201 return !m_type_map.Empty(); 202 return false; 203 } 204 205 void CompilerContext::Dump(Stream &s) const { 206 switch (kind) { 207 default: 208 s << "Invalid"; 209 break; 210 case CompilerContextKind::TranslationUnit: 211 s << "TranslationUnit"; 212 break; 213 case CompilerContextKind::Module: 214 s << "Module"; 215 break; 216 case CompilerContextKind::Namespace: 217 s << "Namespace"; 218 break; 219 case CompilerContextKind::ClassOrStruct: 220 s << "ClassOrStruct"; 221 break; 222 case CompilerContextKind::Union: 223 s << "Union"; 224 break; 225 case CompilerContextKind::Function: 226 s << "Function"; 227 break; 228 case CompilerContextKind::Variable: 229 s << "Variable"; 230 break; 231 case CompilerContextKind::Enum: 232 s << "Enumeration"; 233 break; 234 case CompilerContextKind::Typedef: 235 s << "Typedef"; 236 break; 237 case CompilerContextKind::AnyType: 238 s << "AnyType"; 239 break; 240 } 241 s << "(" << name << ")"; 242 } 243 244 class TypeAppendVisitor { 245 public: 246 TypeAppendVisitor(TypeListImpl &type_list) : m_type_list(type_list) {} 247 248 bool operator()(const lldb::TypeSP &type) { 249 m_type_list.Append(TypeImplSP(new TypeImpl(type))); 250 return true; 251 } 252 253 private: 254 TypeListImpl &m_type_list; 255 }; 256 257 void TypeListImpl::Append(const lldb_private::TypeList &type_list) { 258 TypeAppendVisitor cb(*this); 259 type_list.ForEach(cb); 260 } 261 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) {} 266 267 Type *SymbolFileType::GetType() { 268 if (!m_type_sp) { 269 Type *resolved_type = m_symbol_file.ResolveTypeUID(GetID()); 270 if (resolved_type) 271 m_type_sp = resolved_type->shared_from_this(); 272 } 273 return m_type_sp.get(); 274 } 275 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) { 288 if (byte_size) { 289 m_byte_size = *byte_size; 290 m_byte_size_has_value = true; 291 } else { 292 m_byte_size = 0; 293 m_byte_size_has_value = false; 294 } 295 } 296 297 Type::Type() 298 : std::enable_shared_from_this<Type>(), UserID(0), m_name("<INVALID TYPE>"), 299 m_payload(0) { 300 m_byte_size = 0; 301 m_byte_size_has_value = false; 302 } 303 304 void Type::GetDescription(Stream *s, lldb::DescriptionLevel level, 305 bool show_name, ExecutionContextScope *exe_scope) { 306 *s << "id = " << (const UserID &)*this; 307 308 // Call the name accessor to make sure we resolve the type name 309 if (show_name) { 310 ConstString type_name = GetName(); 311 if (type_name) { 312 *s << ", name = \"" << type_name << '"'; 313 ConstString qualified_type_name(GetQualifiedName()); 314 if (qualified_type_name != type_name) { 315 *s << ", qualified = \"" << qualified_type_name << '"'; 316 } 317 } 318 } 319 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); 325 326 if (m_compiler_type.IsValid()) { 327 *s << ", compiler_type = \""; 328 GetForwardCompilerType().DumpTypeDescription(s); 329 *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: 334 break; 335 case eEncodingIsUID: 336 s->PutCString(" (unresolved type)"); 337 break; 338 case eEncodingIsConstUID: 339 s->PutCString(" (unresolved const type)"); 340 break; 341 case eEncodingIsRestrictUID: 342 s->PutCString(" (unresolved restrict type)"); 343 break; 344 case eEncodingIsVolatileUID: 345 s->PutCString(" (unresolved volatile type)"); 346 break; 347 case eEncodingIsAtomicUID: 348 s->PutCString(" (unresolved atomic type)"); 349 break; 350 case eEncodingIsTypedefUID: 351 s->PutCString(" (unresolved typedef)"); 352 break; 353 case eEncodingIsPointerUID: 354 s->PutCString(" (unresolved pointer)"); 355 break; 356 case eEncodingIsLValueReferenceUID: 357 s->PutCString(" (unresolved L value reference)"); 358 break; 359 case eEncodingIsRValueReferenceUID: 360 s->PutCString(" (unresolved R value reference)"); 361 break; 362 case eEncodingIsSyntheticUID: 363 s->PutCString(" (synthetic type)"); 364 break; 365 case eEncodingIsLLVMPtrAuthUID: 366 s->PutCString(" (ptrauth type)"); 367 break; 368 } 369 } 370 } 371 372 void Type::Dump(Stream *s, bool show_context, lldb::DescriptionLevel level) { 373 s->Printf("%p: ", static_cast<void *>(this)); 374 s->Indent(); 375 *s << "Type" << static_cast<const UserID &>(*this) << ' '; 376 if (m_name) 377 *s << ", name = \"" << m_name << "\""; 378 379 if (m_byte_size_has_value) 380 s->Printf(", size = %" PRIu64, m_byte_size); 381 382 if (show_context && m_context != nullptr) { 383 s->PutCString(", context = ( "); 384 m_context->DumpSymbolContext(s); 385 s->PutCString(" )"); 386 } 387 388 bool show_fullpaths = false; 389 m_decl.Dump(s, show_fullpaths); 390 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: 398 break; 399 case eEncodingIsUID: 400 s->PutCString(" (unresolved type)"); 401 break; 402 case eEncodingIsConstUID: 403 s->PutCString(" (unresolved const type)"); 404 break; 405 case eEncodingIsRestrictUID: 406 s->PutCString(" (unresolved restrict type)"); 407 break; 408 case eEncodingIsVolatileUID: 409 s->PutCString(" (unresolved volatile type)"); 410 break; 411 case eEncodingIsAtomicUID: 412 s->PutCString(" (unresolved atomic type)"); 413 break; 414 case eEncodingIsTypedefUID: 415 s->PutCString(" (unresolved typedef)"); 416 break; 417 case eEncodingIsPointerUID: 418 s->PutCString(" (unresolved pointer)"); 419 break; 420 case eEncodingIsLValueReferenceUID: 421 s->PutCString(" (unresolved L value reference)"); 422 break; 423 case eEncodingIsRValueReferenceUID: 424 s->PutCString(" (unresolved R value reference)"); 425 break; 426 case eEncodingIsSyntheticUID: 427 s->PutCString(" (synthetic type)"); 428 break; 429 case eEncodingIsLLVMPtrAuthUID: 430 s->PutCString(" (ptrauth type)"); 431 } 432 } 433 434 // 435 // if (m_access) 436 // s->Printf(", access = %u", m_access); 437 s->EOL(); 438 } 439 440 ConstString Type::GetName() { 441 if (!m_name) 442 m_name = GetForwardCompilerType().GetTypeName(); 443 return m_name; 444 } 445 446 ConstString Type::GetBaseName() { 447 return GetForwardCompilerType().GetTypeName(/*BaseOnly*/ true); 448 } 449 450 void Type::DumpTypeName(Stream *s) { GetName().Dump(s, "<invalid-type-name>"); } 451 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; 456 } 457 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); 461 462 switch (m_encoding_uid_type) { 463 case eEncodingInvalid: 464 case eEncodingIsSyntheticUID: 465 break; 466 case eEncodingIsUID: 467 case eEncodingIsConstUID: 468 case eEncodingIsRestrictUID: 469 case eEncodingIsVolatileUID: 470 case eEncodingIsAtomicUID: 471 case eEncodingIsTypedefUID: { 472 Type *encoding_type = GetEncodingType(); 473 if (encoding_type) 474 if (std::optional<uint64_t> size = 475 encoding_type->GetByteSize(exe_scope)) { 476 m_byte_size = *size; 477 m_byte_size_has_value = true; 478 return static_cast<uint64_t>(m_byte_size); 479 } 480 481 if (std::optional<uint64_t> size = 482 GetLayoutCompilerType().GetByteSize(exe_scope)) { 483 m_byte_size = *size; 484 m_byte_size_has_value = true; 485 return static_cast<uint64_t>(m_byte_size); 486 } 487 } break; 488 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); 498 } 499 } break; 500 } 501 return {}; 502 } 503 504 llvm::Expected<uint32_t> Type::GetNumChildren(bool omit_empty_base_classes) { 505 return GetForwardCompilerType().GetNumChildren(omit_empty_base_classes, nullptr); 506 } 507 508 bool Type::IsAggregateType() { 509 return GetForwardCompilerType().IsAggregateType(); 510 } 511 512 bool Type::IsTemplateType() { 513 return GetForwardCompilerType().IsTemplateType(); 514 } 515 516 lldb::TypeSP Type::GetTypedefType() { 517 lldb::TypeSP type_sp; 518 if (IsTypedef()) { 519 Type *typedef_type = m_symbol_file->ResolveTypeUID(m_encoding_uid); 520 if (typedef_type) 521 type_sp = typedef_type->shared_from_this(); 522 } 523 return type_sp; 524 } 525 526 lldb::Format Type::GetFormat() { return GetForwardCompilerType().GetFormat(); } 527 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); 531 } 532 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) 538 return false; 539 } 540 541 const uint64_t byte_size = 542 GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr) 543 .value_or(0); 544 if (data.GetByteSize() < byte_size) { 545 lldb::DataBufferSP data_sp(new DataBufferHeap(byte_size, '\0')); 546 data.SetData(data_sp); 547 } 548 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 553 if (addr == 0) 554 return false; 555 memcpy(dst, reinterpret_cast<uint8_t *>(addr), byte_size); 556 return true; 557 } else { 558 if (exe_ctx) { 559 Process *process = exe_ctx->GetProcessPtr(); 560 if (process) { 561 Status error; 562 return exe_ctx->GetProcessPtr()->ReadMemory(addr, dst, byte_size, 563 error) == byte_size; 564 } 565 } 566 } 567 } 568 return false; 569 } 570 571 bool Type::WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t addr, 572 AddressType address_type, DataExtractor &data) { 573 return false; 574 } 575 576 const Declaration &Type::GetDeclaration() const { return m_decl; } 577 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(); 583 if (encoding_type) { 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; 592 } 593 } break; 594 595 case eEncodingIsConstUID: 596 m_compiler_type = 597 encoding_type->GetForwardCompilerType().AddConstModifier(); 598 break; 599 600 case eEncodingIsRestrictUID: 601 m_compiler_type = 602 encoding_type->GetForwardCompilerType().AddRestrictModifier(); 603 break; 604 605 case eEncodingIsVolatileUID: 606 m_compiler_type = 607 encoding_type->GetForwardCompilerType().AddVolatileModifier(); 608 break; 609 610 case eEncodingIsAtomicUID: 611 m_compiler_type = 612 encoding_type->GetForwardCompilerType().GetAtomicType(); 613 break; 614 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); 619 m_name.Clear(); 620 break; 621 622 case eEncodingIsPointerUID: 623 m_compiler_type = 624 encoding_type->GetForwardCompilerType().GetPointerType(); 625 break; 626 627 case eEncodingIsLValueReferenceUID: 628 m_compiler_type = 629 encoding_type->GetForwardCompilerType().GetLValueReferenceType(); 630 break; 631 632 case eEncodingIsRValueReferenceUID: 633 m_compiler_type = 634 encoding_type->GetForwardCompilerType().GetRValueReferenceType(); 635 break; 636 637 case eEncodingIsLLVMPtrAuthUID: 638 m_compiler_type = 639 encoding_type->GetForwardCompilerType().AddPtrAuthModifier( 640 m_payload); 641 break; 642 643 default: 644 llvm_unreachable("Unhandled encoding_data_type."); 645 } 646 } else { 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()) { 651 LLDB_LOG_ERROR( 652 GetLog(LLDBLog::Symbols), std::move(err), 653 "Unable to construct void type from TypeSystemClang: {0}"); 654 } else { 655 CompilerType void_compiler_type; 656 auto ts = *type_system_or_err; 657 if (ts) 658 void_compiler_type = ts->GetBasicTypeFromAST(eBasicTypeVoid); 659 switch (m_encoding_uid_type) { 660 case eEncodingIsUID: 661 m_compiler_type = void_compiler_type; 662 break; 663 664 case eEncodingIsConstUID: 665 m_compiler_type = void_compiler_type.AddConstModifier(); 666 break; 667 668 case eEncodingIsRestrictUID: 669 m_compiler_type = void_compiler_type.AddRestrictModifier(); 670 break; 671 672 case eEncodingIsVolatileUID: 673 m_compiler_type = void_compiler_type.AddVolatileModifier(); 674 break; 675 676 case eEncodingIsAtomicUID: 677 m_compiler_type = void_compiler_type.GetAtomicType(); 678 break; 679 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); 684 break; 685 686 case eEncodingIsPointerUID: 687 m_compiler_type = void_compiler_type.GetPointerType(); 688 break; 689 690 case eEncodingIsLValueReferenceUID: 691 m_compiler_type = void_compiler_type.GetLValueReferenceType(); 692 break; 693 694 case eEncodingIsRValueReferenceUID: 695 m_compiler_type = void_compiler_type.GetRValueReferenceType(); 696 break; 697 698 case eEncodingIsLLVMPtrAuthUID: 699 llvm_unreachable("Cannot handle eEncodingIsLLVMPtrAuthUID without " 700 "valid encoding_type"); 701 702 default: 703 llvm_unreachable("Unhandled encoding_data_type."); 704 } 705 } 706 } 707 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; 713 } 714 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 724 // definition. 725 m_symbol_file->CompleteType(m_compiler_type); 726 } 727 } 728 } 729 730 // If we have an encoding type, then we need to make sure it is resolved 731 // appropriately. 732 if (m_encoding_uid != LLDB_INVALID_UID) { 733 if (encoding_type == nullptr) 734 encoding_type = GetEncodingType(); 735 if (encoding_type) { 736 ResolveState encoding_compiler_type_resolve_state = 737 compiler_type_resolve_state; 738 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; 745 break; 746 default: 747 break; 748 } 749 } 750 encoding_type->ResolveCompilerType(encoding_compiler_type_resolve_state); 751 } 752 } 753 return m_compiler_type.IsValid(); 754 } 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); 759 if (encoding_type) 760 encoding_mask |= encoding_type->GetEncodingMask(); 761 return encoding_mask; 762 } 763 764 CompilerType Type::GetFullCompilerType() { 765 ResolveCompilerType(ResolveState::Full); 766 return m_compiler_type; 767 } 768 769 CompilerType Type::GetLayoutCompilerType() { 770 ResolveCompilerType(ResolveState::Layout); 771 return m_compiler_type; 772 } 773 774 CompilerType Type::GetForwardCompilerType() { 775 ResolveCompilerType(ResolveState::Forward); 776 return m_compiler_type; 777 } 778 779 ConstString Type::GetQualifiedName() { 780 return GetForwardCompilerType().GetTypeName(); 781 } 782 783 std::optional<Type::ParsedName> 784 Type::GetTypeScopeAndBasename(llvm::StringRef name) { 785 ParsedName result; 786 787 if (name.empty()) 788 return std::nullopt; 789 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; 800 801 if (name.consume_front("::")) 802 result.scope.push_back("::"); 803 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()) { 809 case ':': 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)") 816 scope_name = ""; 817 result.scope.push_back(scope_name); 818 name_begin = pos.index() + 1; 819 } 820 break; 821 case '<': 822 ++template_depth; 823 break; 824 case '>': 825 if (template_depth == 0) 826 return std::nullopt; // Invalid name. 827 --template_depth; 828 break; 829 } 830 prev_is_colon = pos.value() == ':'; 831 } 832 833 if (name_begin < name.size() && template_depth == 0) 834 result.basename = name.substr(name_begin); 835 else 836 return std::nullopt; 837 838 return result; 839 } 840 841 ModuleSP Type::GetModule() { 842 if (m_symbol_file) 843 return m_symbol_file->GetObjectFile()->GetModule(); 844 return ModuleSP(); 845 } 846 847 ModuleSP Type::GetExeModule() { 848 if (m_compiler_type) { 849 auto ts = m_compiler_type.GetTypeSystem(); 850 if (!ts) 851 return {}; 852 SymbolFile *symbol_file = ts->GetSymbolFile(); 853 if (symbol_file) 854 return symbol_file->GetObjectFile()->GetModule(); 855 } 856 return {}; 857 } 858 859 TypeAndOrName::TypeAndOrName(TypeSP &in_type_sp) { 860 if (in_type_sp) { 861 m_compiler_type = in_type_sp->GetForwardCompilerType(); 862 m_type_name = in_type_sp->GetName(); 863 } 864 } 865 866 TypeAndOrName::TypeAndOrName(const char *in_type_str) 867 : m_type_name(in_type_str) {} 868 869 TypeAndOrName::TypeAndOrName(ConstString &in_type_const_string) 870 : m_type_name(in_type_const_string) {} 871 872 bool TypeAndOrName::operator==(const TypeAndOrName &other) const { 873 if (m_compiler_type != other.m_compiler_type) 874 return false; 875 if (m_type_name != other.m_type_name) 876 return false; 877 return true; 878 } 879 880 bool TypeAndOrName::operator!=(const TypeAndOrName &other) const { 881 return !(*this == other); 882 } 883 884 ConstString TypeAndOrName::GetName() const { 885 if (m_type_name) 886 return m_type_name; 887 if (m_compiler_type) 888 return m_compiler_type.GetTypeName(); 889 return ConstString("<invalid>"); 890 } 891 892 void TypeAndOrName::SetName(ConstString type_name) { 893 m_type_name = type_name; 894 } 895 896 void TypeAndOrName::SetName(const char *type_name_cstr) { 897 m_type_name.SetCString(type_name_cstr); 898 } 899 900 void TypeAndOrName::SetName(llvm::StringRef type_name) { 901 m_type_name.SetString(type_name); 902 } 903 904 void TypeAndOrName::SetTypeSP(lldb::TypeSP type_sp) { 905 if (type_sp) { 906 m_compiler_type = type_sp->GetForwardCompilerType(); 907 m_type_name = type_sp->GetName(); 908 } else 909 Clear(); 910 } 911 912 void TypeAndOrName::SetCompilerType(CompilerType compiler_type) { 913 m_compiler_type = compiler_type; 914 if (m_compiler_type) 915 m_type_name = m_compiler_type.GetTypeName(); 916 } 917 918 bool TypeAndOrName::IsEmpty() const { 919 return !((bool)m_type_name || (bool)m_compiler_type); 920 } 921 922 void TypeAndOrName::Clear() { 923 m_type_name.Clear(); 924 m_compiler_type.Clear(); 925 } 926 927 bool TypeAndOrName::HasName() const { return (bool)m_type_name; } 928 929 bool TypeAndOrName::HasCompilerType() const { 930 return m_compiler_type.IsValid(); 931 } 932 933 TypeImpl::TypeImpl(const lldb::TypeSP &type_sp) 934 : m_module_wp(), m_static_type(), m_dynamic_type() { 935 SetType(type_sp); 936 } 937 938 TypeImpl::TypeImpl(const CompilerType &compiler_type) 939 : m_module_wp(), m_static_type(), m_dynamic_type() { 940 SetType(compiler_type); 941 } 942 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); 946 } 947 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); 952 } 953 954 void TypeImpl::SetType(const lldb::TypeSP &type_sp) { 955 if (type_sp) { 956 m_static_type = type_sp->GetForwardCompilerType(); 957 m_exe_module_wp = type_sp->GetExeModule(); 958 m_module_wp = type_sp->GetModule(); 959 } else { 960 m_static_type.Clear(); 961 m_module_wp = lldb::ModuleWP(); 962 } 963 } 964 965 void TypeImpl::SetType(const CompilerType &compiler_type) { 966 m_module_wp = lldb::ModuleWP(); 967 m_static_type = compiler_type; 968 } 969 970 void TypeImpl::SetType(const lldb::TypeSP &type_sp, 971 const CompilerType &dynamic) { 972 SetType(type_sp); 973 m_dynamic_type = dynamic; 974 } 975 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; 981 } 982 983 bool TypeImpl::CheckModule(lldb::ModuleSP &module_sp) const { 984 return CheckModuleCommon(m_module_wp, module_sp); 985 } 986 987 bool TypeImpl::CheckExeModule(lldb::ModuleSP &module_sp) const { 988 return CheckModuleCommon(m_exe_module_wp, module_sp); 989 } 990 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(); 1002 if (!module_sp) { 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 1013 return false; 1014 } 1015 } 1016 // We either successfully locked the module, or didn't have one to begin with 1017 return true; 1018 } 1019 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; 1023 } 1024 1025 bool TypeImpl::operator!=(const TypeImpl &rhs) const { 1026 return !(*this == rhs); 1027 } 1028 1029 bool TypeImpl::IsValid() const { 1030 // just a name is not valid 1031 ModuleSP module_sp; 1032 if (CheckModule(module_sp)) 1033 return m_static_type.IsValid() || m_dynamic_type.IsValid(); 1034 return false; 1035 } 1036 1037 TypeImpl::operator bool() const { return IsValid(); } 1038 1039 void TypeImpl::Clear() { 1040 m_module_wp = lldb::ModuleWP(); 1041 m_static_type.Clear(); 1042 m_dynamic_type.Clear(); 1043 } 1044 1045 ModuleSP TypeImpl::GetModule() const { 1046 lldb::ModuleSP module_sp; 1047 if (CheckExeModule(module_sp)) 1048 return module_sp; 1049 return nullptr; 1050 } 1051 1052 ConstString TypeImpl::GetName() const { 1053 ModuleSP module_sp; 1054 if (CheckModule(module_sp)) { 1055 if (m_dynamic_type) 1056 return m_dynamic_type.GetTypeName(); 1057 return m_static_type.GetTypeName(); 1058 } 1059 return ConstString(); 1060 } 1061 1062 ConstString TypeImpl::GetDisplayTypeName() const { 1063 ModuleSP module_sp; 1064 if (CheckModule(module_sp)) { 1065 if (m_dynamic_type) 1066 return m_dynamic_type.GetDisplayTypeName(); 1067 return m_static_type.GetDisplayTypeName(); 1068 } 1069 return ConstString(); 1070 } 1071 1072 TypeImpl TypeImpl::GetPointerType() const { 1073 ModuleSP module_sp; 1074 if (CheckModule(module_sp)) { 1075 if (m_dynamic_type.IsValid()) { 1076 return TypeImpl(m_static_type.GetPointerType(), 1077 m_dynamic_type.GetPointerType()); 1078 } 1079 return TypeImpl(m_static_type.GetPointerType()); 1080 } 1081 return TypeImpl(); 1082 } 1083 1084 TypeImpl TypeImpl::GetPointeeType() const { 1085 ModuleSP module_sp; 1086 if (CheckModule(module_sp)) { 1087 if (m_dynamic_type.IsValid()) { 1088 return TypeImpl(m_static_type.GetPointeeType(), 1089 m_dynamic_type.GetPointeeType()); 1090 } 1091 return TypeImpl(m_static_type.GetPointeeType()); 1092 } 1093 return TypeImpl(); 1094 } 1095 1096 TypeImpl TypeImpl::GetReferenceType() const { 1097 ModuleSP module_sp; 1098 if (CheckModule(module_sp)) { 1099 if (m_dynamic_type.IsValid()) { 1100 return TypeImpl(m_static_type.GetLValueReferenceType(), 1101 m_dynamic_type.GetLValueReferenceType()); 1102 } 1103 return TypeImpl(m_static_type.GetLValueReferenceType()); 1104 } 1105 return TypeImpl(); 1106 } 1107 1108 TypeImpl TypeImpl::GetTypedefedType() const { 1109 ModuleSP module_sp; 1110 if (CheckModule(module_sp)) { 1111 if (m_dynamic_type.IsValid()) { 1112 return TypeImpl(m_static_type.GetTypedefedType(), 1113 m_dynamic_type.GetTypedefedType()); 1114 } 1115 return TypeImpl(m_static_type.GetTypedefedType()); 1116 } 1117 return TypeImpl(); 1118 } 1119 1120 TypeImpl TypeImpl::GetDereferencedType() const { 1121 ModuleSP module_sp; 1122 if (CheckModule(module_sp)) { 1123 if (m_dynamic_type.IsValid()) { 1124 return TypeImpl(m_static_type.GetNonReferenceType(), 1125 m_dynamic_type.GetNonReferenceType()); 1126 } 1127 return TypeImpl(m_static_type.GetNonReferenceType()); 1128 } 1129 return TypeImpl(); 1130 } 1131 1132 TypeImpl TypeImpl::GetUnqualifiedType() const { 1133 ModuleSP module_sp; 1134 if (CheckModule(module_sp)) { 1135 if (m_dynamic_type.IsValid()) { 1136 return TypeImpl(m_static_type.GetFullyUnqualifiedType(), 1137 m_dynamic_type.GetFullyUnqualifiedType()); 1138 } 1139 return TypeImpl(m_static_type.GetFullyUnqualifiedType()); 1140 } 1141 return TypeImpl(); 1142 } 1143 1144 TypeImpl TypeImpl::GetCanonicalType() const { 1145 ModuleSP module_sp; 1146 if (CheckModule(module_sp)) { 1147 if (m_dynamic_type.IsValid()) { 1148 return TypeImpl(m_static_type.GetCanonicalType(), 1149 m_dynamic_type.GetCanonicalType()); 1150 } 1151 return TypeImpl(m_static_type.GetCanonicalType()); 1152 } 1153 return TypeImpl(); 1154 } 1155 1156 CompilerType TypeImpl::GetCompilerType(bool prefer_dynamic) { 1157 ModuleSP module_sp; 1158 if (CheckModule(module_sp)) { 1159 if (prefer_dynamic) { 1160 if (m_dynamic_type.IsValid()) 1161 return m_dynamic_type; 1162 } 1163 return m_static_type; 1164 } 1165 return CompilerType(); 1166 } 1167 1168 CompilerType::TypeSystemSPWrapper TypeImpl::GetTypeSystem(bool prefer_dynamic) { 1169 ModuleSP module_sp; 1170 if (CheckModule(module_sp)) { 1171 if (prefer_dynamic) { 1172 if (m_dynamic_type.IsValid()) 1173 return m_dynamic_type.GetTypeSystem(); 1174 } 1175 return m_static_type.GetTypeSystem(); 1176 } 1177 return {}; 1178 } 1179 1180 bool TypeImpl::GetDescription(lldb_private::Stream &strm, 1181 lldb::DescriptionLevel description_level) { 1182 ModuleSP module_sp; 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"); 1188 } 1189 m_static_type.DumpTypeDescription(&strm); 1190 } else { 1191 strm.PutCString("Invalid TypeImpl module for type has been deleted\n"); 1192 } 1193 return true; 1194 } 1195 1196 CompilerType TypeImpl::FindDirectNestedType(llvm::StringRef name) { 1197 if (name.empty()) 1198 return CompilerType(); 1199 return GetCompilerType(/*prefer_dynamic=*/false) 1200 .GetDirectNestedTypeWithName(name); 1201 } 1202 1203 bool TypeMemberFunctionImpl::IsValid() { 1204 return m_type.IsValid() && m_kind != lldb::eMemberFunctionKindUnknown; 1205 } 1206 1207 ConstString TypeMemberFunctionImpl::GetName() const { return m_name; } 1208 1209 ConstString TypeMemberFunctionImpl::GetMangledName() const { 1210 return m_decl.GetMangledName(); 1211 } 1212 1213 CompilerType TypeMemberFunctionImpl::GetType() const { return m_type; } 1214 1215 lldb::MemberFunctionKind TypeMemberFunctionImpl::GetKind() const { 1216 return m_kind; 1217 } 1218 1219 bool TypeMemberFunctionImpl::GetDescription(Stream &stream) { 1220 switch (m_kind) { 1221 case lldb::eMemberFunctionKindUnknown: 1222 return false; 1223 case lldb::eMemberFunctionKindConstructor: 1224 stream.Printf("constructor for %s", 1225 m_type.GetTypeName().AsCString("<unknown>")); 1226 break; 1227 case lldb::eMemberFunctionKindDestructor: 1228 stream.Printf("destructor for %s", 1229 m_type.GetTypeName().AsCString("<unknown>")); 1230 break; 1231 case lldb::eMemberFunctionKindInstanceMethod: 1232 stream.Printf("instance method %s of type %s", m_name.AsCString(), 1233 m_decl.GetDeclContext().GetName().AsCString()); 1234 break; 1235 case lldb::eMemberFunctionKindStaticMethod: 1236 stream.Printf("static method %s of type %s", m_name.AsCString(), 1237 m_decl.GetDeclContext().GetName().AsCString()); 1238 break; 1239 } 1240 return true; 1241 } 1242 1243 CompilerType TypeMemberFunctionImpl::GetReturnType() const { 1244 if (m_type) 1245 return m_type.GetFunctionReturnType(); 1246 return m_decl.GetFunctionReturnType(); 1247 } 1248 1249 size_t TypeMemberFunctionImpl::GetNumArguments() const { 1250 if (m_type) 1251 return m_type.GetNumberOfFunctionArguments(); 1252 else 1253 return m_decl.GetNumFunctionArguments(); 1254 } 1255 1256 CompilerType TypeMemberFunctionImpl::GetArgumentAtIndex(size_t idx) const { 1257 if (m_type) 1258 return m_type.GetFunctionArgumentAtIndex(idx); 1259 else 1260 return m_decl.GetFunctionArgumentType(idx); 1261 } 1262 1263 TypeEnumMemberImpl::TypeEnumMemberImpl(const lldb::TypeImplSP &integer_type_sp, 1264 ConstString name, 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) 1268 1269 {} 1270