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