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