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) { 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()) 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, GetByteSize().getValueOr(0), 327 0, // Bitfield bit size 328 0, // Bitfield bit offset 329 show_types, show_summary, verbose, 0); 330 } 331 } 332 333 Type *Type::GetEncodingType() { 334 if (m_encoding_type == nullptr && m_encoding_uid != LLDB_INVALID_UID) 335 m_encoding_type = m_symbol_file->ResolveTypeUID(m_encoding_uid); 336 return m_encoding_type; 337 } 338 339 llvm::Optional<uint64_t> Type::GetByteSize() { 340 if (m_byte_size_has_value) 341 return m_byte_size; 342 343 switch (m_encoding_uid_type) { 344 case eEncodingInvalid: 345 case eEncodingIsSyntheticUID: 346 break; 347 case eEncodingIsUID: 348 case eEncodingIsConstUID: 349 case eEncodingIsRestrictUID: 350 case eEncodingIsVolatileUID: 351 case eEncodingIsAtomicUID: 352 case eEncodingIsTypedefUID: { 353 Type *encoding_type = GetEncodingType(); 354 if (encoding_type) 355 if (llvm::Optional<uint64_t> size = encoding_type->GetByteSize()) { 356 m_byte_size = *size; 357 m_byte_size_has_value = true; 358 return m_byte_size; 359 } 360 361 if (llvm::Optional<uint64_t> size = 362 GetLayoutCompilerType().GetByteSize(nullptr)) { 363 m_byte_size = *size; 364 m_byte_size_has_value = true; 365 return m_byte_size; 366 } 367 } break; 368 369 // If we are a pointer or reference, then this is just a pointer size; 370 case eEncodingIsPointerUID: 371 case eEncodingIsLValueReferenceUID: 372 case eEncodingIsRValueReferenceUID: { 373 if (ArchSpec arch = m_symbol_file->GetObjectFile()->GetArchitecture()) { 374 m_byte_size = arch.GetAddressByteSize(); 375 m_byte_size_has_value = true; 376 } 377 } break; 378 } 379 return {}; 380 } 381 382 uint32_t Type::GetNumChildren(bool omit_empty_base_classes) { 383 return GetForwardCompilerType().GetNumChildren(omit_empty_base_classes, nullptr); 384 } 385 386 bool Type::IsAggregateType() { 387 return GetForwardCompilerType().IsAggregateType(); 388 } 389 390 lldb::TypeSP Type::GetTypedefType() { 391 lldb::TypeSP type_sp; 392 if (IsTypedef()) { 393 Type *typedef_type = m_symbol_file->ResolveTypeUID(m_encoding_uid); 394 if (typedef_type) 395 type_sp = typedef_type->shared_from_this(); 396 } 397 return type_sp; 398 } 399 400 lldb::Format Type::GetFormat() { return GetForwardCompilerType().GetFormat(); } 401 402 lldb::Encoding Type::GetEncoding(uint64_t &count) { 403 // Make sure we resolve our type if it already hasn't been. 404 return GetForwardCompilerType().GetEncoding(count); 405 } 406 407 bool Type::DumpValueInMemory(ExecutionContext *exe_ctx, Stream *s, 408 lldb::addr_t address, AddressType address_type, 409 bool show_types, bool show_summary, bool verbose) { 410 if (address != LLDB_INVALID_ADDRESS) { 411 DataExtractor data; 412 Target *target = nullptr; 413 if (exe_ctx) 414 target = exe_ctx->GetTargetPtr(); 415 if (target) 416 data.SetByteOrder(target->GetArchitecture().GetByteOrder()); 417 if (ReadFromMemory(exe_ctx, address, address_type, data)) { 418 DumpValue(exe_ctx, s, data, 0, show_types, show_summary, verbose); 419 return true; 420 } 421 } 422 return false; 423 } 424 425 bool Type::ReadFromMemory(ExecutionContext *exe_ctx, lldb::addr_t addr, 426 AddressType address_type, DataExtractor &data) { 427 if (address_type == eAddressTypeFile) { 428 // Can't convert a file address to anything valid without more context 429 // (which Module it came from) 430 return false; 431 } 432 433 const uint64_t byte_size = GetByteSize().getValueOr(0); 434 if (data.GetByteSize() < byte_size) { 435 lldb::DataBufferSP data_sp(new DataBufferHeap(byte_size, '\0')); 436 data.SetData(data_sp); 437 } 438 439 uint8_t *dst = const_cast<uint8_t *>(data.PeekData(0, byte_size)); 440 if (dst != nullptr) { 441 if (address_type == eAddressTypeHost) { 442 // The address is an address in this process, so just copy it 443 if (addr == 0) 444 return false; 445 memcpy(dst, reinterpret_cast<uint8_t *>(addr), byte_size); 446 return true; 447 } else { 448 if (exe_ctx) { 449 Process *process = exe_ctx->GetProcessPtr(); 450 if (process) { 451 Status error; 452 return exe_ctx->GetProcessPtr()->ReadMemory(addr, dst, byte_size, 453 error) == byte_size; 454 } 455 } 456 } 457 } 458 return false; 459 } 460 461 bool Type::WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t addr, 462 AddressType address_type, DataExtractor &data) { 463 return false; 464 } 465 466 const Declaration &Type::GetDeclaration() const { return m_decl; } 467 468 bool Type::ResolveCompilerType(ResolveState compiler_type_resolve_state) { 469 // TODO: This needs to consider the correct type system to use. 470 Type *encoding_type = nullptr; 471 if (!m_compiler_type.IsValid()) { 472 encoding_type = GetEncodingType(); 473 if (encoding_type) { 474 switch (m_encoding_uid_type) { 475 case eEncodingIsUID: { 476 CompilerType encoding_compiler_type = 477 encoding_type->GetForwardCompilerType(); 478 if (encoding_compiler_type.IsValid()) { 479 m_compiler_type = encoding_compiler_type; 480 m_compiler_type_resolve_state = 481 encoding_type->m_compiler_type_resolve_state; 482 } 483 } break; 484 485 case eEncodingIsConstUID: 486 m_compiler_type = 487 encoding_type->GetForwardCompilerType().AddConstModifier(); 488 break; 489 490 case eEncodingIsRestrictUID: 491 m_compiler_type = 492 encoding_type->GetForwardCompilerType().AddRestrictModifier(); 493 break; 494 495 case eEncodingIsVolatileUID: 496 m_compiler_type = 497 encoding_type->GetForwardCompilerType().AddVolatileModifier(); 498 break; 499 500 case eEncodingIsAtomicUID: 501 m_compiler_type = 502 encoding_type->GetForwardCompilerType().GetAtomicType(); 503 break; 504 505 case eEncodingIsTypedefUID: 506 m_compiler_type = encoding_type->GetForwardCompilerType().CreateTypedef( 507 m_name.AsCString("__lldb_invalid_typedef_name"), 508 GetSymbolFile()->GetDeclContextContainingUID(GetID()), m_payload); 509 m_name.Clear(); 510 break; 511 512 case eEncodingIsPointerUID: 513 m_compiler_type = 514 encoding_type->GetForwardCompilerType().GetPointerType(); 515 break; 516 517 case eEncodingIsLValueReferenceUID: 518 m_compiler_type = 519 encoding_type->GetForwardCompilerType().GetLValueReferenceType(); 520 break; 521 522 case eEncodingIsRValueReferenceUID: 523 m_compiler_type = 524 encoding_type->GetForwardCompilerType().GetRValueReferenceType(); 525 break; 526 527 default: 528 llvm_unreachable("Unhandled encoding_data_type."); 529 } 530 } else { 531 // We have no encoding type, return void? 532 auto type_system_or_err = 533 m_symbol_file->GetTypeSystemForLanguage(eLanguageTypeC); 534 if (auto err = type_system_or_err.takeError()) { 535 LLDB_LOG_ERROR( 536 lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_SYMBOLS), 537 std::move(err), 538 "Unable to construct void type from TypeSystemClang"); 539 } else { 540 CompilerType void_compiler_type = 541 type_system_or_err->GetBasicTypeFromAST(eBasicTypeVoid); 542 switch (m_encoding_uid_type) { 543 case eEncodingIsUID: 544 m_compiler_type = void_compiler_type; 545 break; 546 547 case eEncodingIsConstUID: 548 m_compiler_type = void_compiler_type.AddConstModifier(); 549 break; 550 551 case eEncodingIsRestrictUID: 552 m_compiler_type = void_compiler_type.AddRestrictModifier(); 553 break; 554 555 case eEncodingIsVolatileUID: 556 m_compiler_type = void_compiler_type.AddVolatileModifier(); 557 break; 558 559 case eEncodingIsAtomicUID: 560 m_compiler_type = void_compiler_type.GetAtomicType(); 561 break; 562 563 case eEncodingIsTypedefUID: 564 m_compiler_type = void_compiler_type.CreateTypedef( 565 m_name.AsCString("__lldb_invalid_typedef_name"), 566 GetSymbolFile()->GetDeclContextContainingUID(GetID()), m_payload); 567 break; 568 569 case eEncodingIsPointerUID: 570 m_compiler_type = void_compiler_type.GetPointerType(); 571 break; 572 573 case eEncodingIsLValueReferenceUID: 574 m_compiler_type = void_compiler_type.GetLValueReferenceType(); 575 break; 576 577 case eEncodingIsRValueReferenceUID: 578 m_compiler_type = void_compiler_type.GetRValueReferenceType(); 579 break; 580 581 default: 582 llvm_unreachable("Unhandled encoding_data_type."); 583 } 584 } 585 } 586 587 // When we have a EncodingUID, our "m_flags.compiler_type_resolve_state" is 588 // set to eResolveStateUnresolved so we need to update it to say that we 589 // now have a forward declaration since that is what we created above. 590 if (m_compiler_type.IsValid()) 591 m_compiler_type_resolve_state = ResolveState::Forward; 592 } 593 594 // Check if we have a forward reference to a class/struct/union/enum? 595 if (compiler_type_resolve_state == ResolveState::Layout || 596 compiler_type_resolve_state == ResolveState::Full) { 597 // Check if we have a forward reference to a class/struct/union/enum? 598 if (m_compiler_type.IsValid() && 599 m_compiler_type_resolve_state < compiler_type_resolve_state) { 600 m_compiler_type_resolve_state = ResolveState::Full; 601 if (!m_compiler_type.IsDefined()) { 602 // We have a forward declaration, we need to resolve it to a complete 603 // definition. 604 m_symbol_file->CompleteType(m_compiler_type); 605 } 606 } 607 } 608 609 // If we have an encoding type, then we need to make sure it is resolved 610 // appropriately. 611 if (m_encoding_uid != LLDB_INVALID_UID) { 612 if (encoding_type == nullptr) 613 encoding_type = GetEncodingType(); 614 if (encoding_type) { 615 ResolveState encoding_compiler_type_resolve_state = 616 compiler_type_resolve_state; 617 618 if (compiler_type_resolve_state == ResolveState::Layout) { 619 switch (m_encoding_uid_type) { 620 case eEncodingIsPointerUID: 621 case eEncodingIsLValueReferenceUID: 622 case eEncodingIsRValueReferenceUID: 623 encoding_compiler_type_resolve_state = ResolveState::Forward; 624 break; 625 default: 626 break; 627 } 628 } 629 encoding_type->ResolveCompilerType(encoding_compiler_type_resolve_state); 630 } 631 } 632 return m_compiler_type.IsValid(); 633 } 634 uint32_t Type::GetEncodingMask() { 635 uint32_t encoding_mask = 1u << m_encoding_uid_type; 636 Type *encoding_type = GetEncodingType(); 637 assert(encoding_type != this); 638 if (encoding_type) 639 encoding_mask |= encoding_type->GetEncodingMask(); 640 return encoding_mask; 641 } 642 643 CompilerType Type::GetFullCompilerType() { 644 ResolveCompilerType(ResolveState::Full); 645 return m_compiler_type; 646 } 647 648 CompilerType Type::GetLayoutCompilerType() { 649 ResolveCompilerType(ResolveState::Layout); 650 return m_compiler_type; 651 } 652 653 CompilerType Type::GetForwardCompilerType() { 654 ResolveCompilerType(ResolveState::Forward); 655 return m_compiler_type; 656 } 657 658 ConstString Type::GetQualifiedName() { 659 return GetForwardCompilerType().GetTypeName(); 660 } 661 662 bool Type::GetTypeScopeAndBasename(const llvm::StringRef& name, 663 llvm::StringRef &scope, 664 llvm::StringRef &basename, 665 TypeClass &type_class) { 666 type_class = eTypeClassAny; 667 668 if (name.empty()) 669 return false; 670 671 basename = name; 672 if (basename.consume_front("struct ")) 673 type_class = eTypeClassStruct; 674 else if (basename.consume_front("class ")) 675 type_class = eTypeClassClass; 676 else if (basename.consume_front("union ")) 677 type_class = eTypeClassUnion; 678 else if (basename.consume_front("enum ")) 679 type_class = eTypeClassEnumeration; 680 else if (basename.consume_front("typedef ")) 681 type_class = eTypeClassTypedef; 682 683 size_t namespace_separator = basename.find("::"); 684 if (namespace_separator == llvm::StringRef::npos) 685 return false; 686 687 size_t template_begin = basename.find('<'); 688 while (namespace_separator != llvm::StringRef::npos) { 689 if (template_begin != llvm::StringRef::npos && 690 namespace_separator > template_begin) { 691 size_t template_depth = 1; 692 llvm::StringRef template_arg = 693 basename.drop_front(template_begin + 1); 694 while (template_depth > 0 && !template_arg.empty()) { 695 if (template_arg.front() == '<') 696 template_depth++; 697 else if (template_arg.front() == '>') 698 template_depth--; 699 template_arg = template_arg.drop_front(1); 700 } 701 if (template_depth != 0) 702 return false; // We have an invalid type name. Bail out. 703 if (template_arg.empty()) 704 break; // The template ends at the end of the full name. 705 basename = template_arg; 706 } else { 707 basename = basename.drop_front(namespace_separator + 2); 708 } 709 template_begin = basename.find('<'); 710 namespace_separator = basename.find("::"); 711 } 712 if (basename.size() < name.size()) { 713 scope = name.take_front(name.size() - basename.size()); 714 return true; 715 } 716 return false; 717 } 718 719 ModuleSP Type::GetModule() { 720 if (m_symbol_file) 721 return m_symbol_file->GetObjectFile()->GetModule(); 722 return ModuleSP(); 723 } 724 725 TypeAndOrName::TypeAndOrName(TypeSP &in_type_sp) { 726 if (in_type_sp) { 727 m_compiler_type = in_type_sp->GetForwardCompilerType(); 728 m_type_name = in_type_sp->GetName(); 729 } 730 } 731 732 TypeAndOrName::TypeAndOrName(const char *in_type_str) 733 : m_type_name(in_type_str) {} 734 735 TypeAndOrName::TypeAndOrName(ConstString &in_type_const_string) 736 : m_type_name(in_type_const_string) {} 737 738 bool TypeAndOrName::operator==(const TypeAndOrName &other) const { 739 if (m_compiler_type != other.m_compiler_type) 740 return false; 741 if (m_type_name != other.m_type_name) 742 return false; 743 return true; 744 } 745 746 bool TypeAndOrName::operator!=(const TypeAndOrName &other) const { 747 return !(*this == other); 748 } 749 750 ConstString TypeAndOrName::GetName() const { 751 if (m_type_name) 752 return m_type_name; 753 if (m_compiler_type) 754 return m_compiler_type.GetTypeName(); 755 return ConstString("<invalid>"); 756 } 757 758 void TypeAndOrName::SetName(ConstString type_name) { 759 m_type_name = type_name; 760 } 761 762 void TypeAndOrName::SetName(const char *type_name_cstr) { 763 m_type_name.SetCString(type_name_cstr); 764 } 765 766 void TypeAndOrName::SetTypeSP(lldb::TypeSP type_sp) { 767 if (type_sp) { 768 m_compiler_type = type_sp->GetForwardCompilerType(); 769 m_type_name = type_sp->GetName(); 770 } else 771 Clear(); 772 } 773 774 void TypeAndOrName::SetCompilerType(CompilerType compiler_type) { 775 m_compiler_type = compiler_type; 776 if (m_compiler_type) 777 m_type_name = m_compiler_type.GetTypeName(); 778 } 779 780 bool TypeAndOrName::IsEmpty() const { 781 return !((bool)m_type_name || (bool)m_compiler_type); 782 } 783 784 void TypeAndOrName::Clear() { 785 m_type_name.Clear(); 786 m_compiler_type.Clear(); 787 } 788 789 bool TypeAndOrName::HasName() const { return (bool)m_type_name; } 790 791 bool TypeAndOrName::HasCompilerType() const { 792 return m_compiler_type.IsValid(); 793 } 794 795 TypeImpl::TypeImpl(const lldb::TypeSP &type_sp) 796 : m_module_wp(), m_static_type(), m_dynamic_type() { 797 SetType(type_sp); 798 } 799 800 TypeImpl::TypeImpl(const CompilerType &compiler_type) 801 : m_module_wp(), m_static_type(), m_dynamic_type() { 802 SetType(compiler_type); 803 } 804 805 TypeImpl::TypeImpl(const lldb::TypeSP &type_sp, const CompilerType &dynamic) 806 : m_module_wp(), m_static_type(), m_dynamic_type(dynamic) { 807 SetType(type_sp, dynamic); 808 } 809 810 TypeImpl::TypeImpl(const CompilerType &static_type, 811 const CompilerType &dynamic_type) 812 : m_module_wp(), m_static_type(), m_dynamic_type() { 813 SetType(static_type, dynamic_type); 814 } 815 816 void TypeImpl::SetType(const lldb::TypeSP &type_sp) { 817 if (type_sp) { 818 m_static_type = type_sp->GetForwardCompilerType(); 819 m_module_wp = type_sp->GetModule(); 820 } else { 821 m_static_type.Clear(); 822 m_module_wp = lldb::ModuleWP(); 823 } 824 } 825 826 void TypeImpl::SetType(const CompilerType &compiler_type) { 827 m_module_wp = lldb::ModuleWP(); 828 m_static_type = compiler_type; 829 } 830 831 void TypeImpl::SetType(const lldb::TypeSP &type_sp, 832 const CompilerType &dynamic) { 833 SetType(type_sp); 834 m_dynamic_type = dynamic; 835 } 836 837 void TypeImpl::SetType(const CompilerType &compiler_type, 838 const CompilerType &dynamic) { 839 m_module_wp = lldb::ModuleWP(); 840 m_static_type = compiler_type; 841 m_dynamic_type = dynamic; 842 } 843 844 bool TypeImpl::CheckModule(lldb::ModuleSP &module_sp) const { 845 // Check if we have a module for this type. If we do and the shared pointer 846 // is can be successfully initialized with m_module_wp, return true. Else 847 // return false if we didn't have a module, or if we had a module and it has 848 // been deleted. Any functions doing anything with a TypeSP in this TypeImpl 849 // class should call this function and only do anything with the ivars if 850 // this function returns true. If we have a module, the "module_sp" will be 851 // filled in with a strong reference to the module so that the module will at 852 // least stay around long enough for the type query to succeed. 853 module_sp = m_module_wp.lock(); 854 if (!module_sp) { 855 lldb::ModuleWP empty_module_wp; 856 // If either call to "std::weak_ptr::owner_before(...) value returns true, 857 // this indicates that m_module_wp once contained (possibly still does) a 858 // reference to a valid shared pointer. This helps us know if we had a 859 // valid reference to a section which is now invalid because the module it 860 // was in was deleted 861 if (empty_module_wp.owner_before(m_module_wp) || 862 m_module_wp.owner_before(empty_module_wp)) { 863 // m_module_wp had a valid reference to a module, but all strong 864 // references have been released and the module has been deleted 865 return false; 866 } 867 } 868 // We either successfully locked the module, or didn't have one to begin with 869 return true; 870 } 871 872 bool TypeImpl::operator==(const TypeImpl &rhs) const { 873 return m_static_type == rhs.m_static_type && 874 m_dynamic_type == rhs.m_dynamic_type; 875 } 876 877 bool TypeImpl::operator!=(const TypeImpl &rhs) const { 878 return !(*this == rhs); 879 } 880 881 bool TypeImpl::IsValid() const { 882 // just a name is not valid 883 ModuleSP module_sp; 884 if (CheckModule(module_sp)) 885 return m_static_type.IsValid() || m_dynamic_type.IsValid(); 886 return false; 887 } 888 889 TypeImpl::operator bool() const { return IsValid(); } 890 891 void TypeImpl::Clear() { 892 m_module_wp = lldb::ModuleWP(); 893 m_static_type.Clear(); 894 m_dynamic_type.Clear(); 895 } 896 897 ConstString TypeImpl::GetName() const { 898 ModuleSP module_sp; 899 if (CheckModule(module_sp)) { 900 if (m_dynamic_type) 901 return m_dynamic_type.GetTypeName(); 902 return m_static_type.GetTypeName(); 903 } 904 return ConstString(); 905 } 906 907 ConstString TypeImpl::GetDisplayTypeName() const { 908 ModuleSP module_sp; 909 if (CheckModule(module_sp)) { 910 if (m_dynamic_type) 911 return m_dynamic_type.GetDisplayTypeName(); 912 return m_static_type.GetDisplayTypeName(); 913 } 914 return ConstString(); 915 } 916 917 TypeImpl TypeImpl::GetPointerType() const { 918 ModuleSP module_sp; 919 if (CheckModule(module_sp)) { 920 if (m_dynamic_type.IsValid()) { 921 return TypeImpl(m_static_type.GetPointerType(), 922 m_dynamic_type.GetPointerType()); 923 } 924 return TypeImpl(m_static_type.GetPointerType()); 925 } 926 return TypeImpl(); 927 } 928 929 TypeImpl TypeImpl::GetPointeeType() const { 930 ModuleSP module_sp; 931 if (CheckModule(module_sp)) { 932 if (m_dynamic_type.IsValid()) { 933 return TypeImpl(m_static_type.GetPointeeType(), 934 m_dynamic_type.GetPointeeType()); 935 } 936 return TypeImpl(m_static_type.GetPointeeType()); 937 } 938 return TypeImpl(); 939 } 940 941 TypeImpl TypeImpl::GetReferenceType() const { 942 ModuleSP module_sp; 943 if (CheckModule(module_sp)) { 944 if (m_dynamic_type.IsValid()) { 945 return TypeImpl(m_static_type.GetLValueReferenceType(), 946 m_dynamic_type.GetLValueReferenceType()); 947 } 948 return TypeImpl(m_static_type.GetLValueReferenceType()); 949 } 950 return TypeImpl(); 951 } 952 953 TypeImpl TypeImpl::GetTypedefedType() const { 954 ModuleSP module_sp; 955 if (CheckModule(module_sp)) { 956 if (m_dynamic_type.IsValid()) { 957 return TypeImpl(m_static_type.GetTypedefedType(), 958 m_dynamic_type.GetTypedefedType()); 959 } 960 return TypeImpl(m_static_type.GetTypedefedType()); 961 } 962 return TypeImpl(); 963 } 964 965 TypeImpl TypeImpl::GetDereferencedType() const { 966 ModuleSP module_sp; 967 if (CheckModule(module_sp)) { 968 if (m_dynamic_type.IsValid()) { 969 return TypeImpl(m_static_type.GetNonReferenceType(), 970 m_dynamic_type.GetNonReferenceType()); 971 } 972 return TypeImpl(m_static_type.GetNonReferenceType()); 973 } 974 return TypeImpl(); 975 } 976 977 TypeImpl TypeImpl::GetUnqualifiedType() const { 978 ModuleSP module_sp; 979 if (CheckModule(module_sp)) { 980 if (m_dynamic_type.IsValid()) { 981 return TypeImpl(m_static_type.GetFullyUnqualifiedType(), 982 m_dynamic_type.GetFullyUnqualifiedType()); 983 } 984 return TypeImpl(m_static_type.GetFullyUnqualifiedType()); 985 } 986 return TypeImpl(); 987 } 988 989 TypeImpl TypeImpl::GetCanonicalType() const { 990 ModuleSP module_sp; 991 if (CheckModule(module_sp)) { 992 if (m_dynamic_type.IsValid()) { 993 return TypeImpl(m_static_type.GetCanonicalType(), 994 m_dynamic_type.GetCanonicalType()); 995 } 996 return TypeImpl(m_static_type.GetCanonicalType()); 997 } 998 return TypeImpl(); 999 } 1000 1001 CompilerType TypeImpl::GetCompilerType(bool prefer_dynamic) { 1002 ModuleSP module_sp; 1003 if (CheckModule(module_sp)) { 1004 if (prefer_dynamic) { 1005 if (m_dynamic_type.IsValid()) 1006 return m_dynamic_type; 1007 } 1008 return m_static_type; 1009 } 1010 return CompilerType(); 1011 } 1012 1013 TypeSystem *TypeImpl::GetTypeSystem(bool prefer_dynamic) { 1014 ModuleSP module_sp; 1015 if (CheckModule(module_sp)) { 1016 if (prefer_dynamic) { 1017 if (m_dynamic_type.IsValid()) 1018 return m_dynamic_type.GetTypeSystem(); 1019 } 1020 return m_static_type.GetTypeSystem(); 1021 } 1022 return nullptr; 1023 } 1024 1025 bool TypeImpl::GetDescription(lldb_private::Stream &strm, 1026 lldb::DescriptionLevel description_level) { 1027 ModuleSP module_sp; 1028 if (CheckModule(module_sp)) { 1029 if (m_dynamic_type.IsValid()) { 1030 strm.Printf("Dynamic:\n"); 1031 m_dynamic_type.DumpTypeDescription(&strm); 1032 strm.Printf("\nStatic:\n"); 1033 } 1034 m_static_type.DumpTypeDescription(&strm); 1035 } else { 1036 strm.PutCString("Invalid TypeImpl module for type has been deleted\n"); 1037 } 1038 return true; 1039 } 1040 1041 bool TypeMemberFunctionImpl::IsValid() { 1042 return m_type.IsValid() && m_kind != lldb::eMemberFunctionKindUnknown; 1043 } 1044 1045 ConstString TypeMemberFunctionImpl::GetName() const { return m_name; } 1046 1047 ConstString TypeMemberFunctionImpl::GetMangledName() const { 1048 return m_decl.GetMangledName(); 1049 } 1050 1051 CompilerType TypeMemberFunctionImpl::GetType() const { return m_type; } 1052 1053 lldb::MemberFunctionKind TypeMemberFunctionImpl::GetKind() const { 1054 return m_kind; 1055 } 1056 1057 bool TypeMemberFunctionImpl::GetDescription(Stream &stream) { 1058 switch (m_kind) { 1059 case lldb::eMemberFunctionKindUnknown: 1060 return false; 1061 case lldb::eMemberFunctionKindConstructor: 1062 stream.Printf("constructor for %s", 1063 m_type.GetTypeName().AsCString("<unknown>")); 1064 break; 1065 case lldb::eMemberFunctionKindDestructor: 1066 stream.Printf("destructor for %s", 1067 m_type.GetTypeName().AsCString("<unknown>")); 1068 break; 1069 case lldb::eMemberFunctionKindInstanceMethod: 1070 stream.Printf("instance method %s of type %s", m_name.AsCString(), 1071 m_decl.GetDeclContext().GetName().AsCString()); 1072 break; 1073 case lldb::eMemberFunctionKindStaticMethod: 1074 stream.Printf("static method %s of type %s", m_name.AsCString(), 1075 m_decl.GetDeclContext().GetName().AsCString()); 1076 break; 1077 } 1078 return true; 1079 } 1080 1081 CompilerType TypeMemberFunctionImpl::GetReturnType() const { 1082 if (m_type) 1083 return m_type.GetFunctionReturnType(); 1084 return m_decl.GetFunctionReturnType(); 1085 } 1086 1087 size_t TypeMemberFunctionImpl::GetNumArguments() const { 1088 if (m_type) 1089 return m_type.GetNumberOfFunctionArguments(); 1090 else 1091 return m_decl.GetNumFunctionArguments(); 1092 } 1093 1094 CompilerType TypeMemberFunctionImpl::GetArgumentAtIndex(size_t idx) const { 1095 if (m_type) 1096 return m_type.GetFunctionArgumentAtIndex(idx); 1097 else 1098 return m_decl.GetFunctionArgumentType(idx); 1099 } 1100 1101 TypeEnumMemberImpl::TypeEnumMemberImpl(const lldb::TypeImplSP &integer_type_sp, 1102 ConstString name, 1103 const llvm::APSInt &value) 1104 : m_integer_type_sp(integer_type_sp), m_name(name), m_value(value), 1105 m_valid((bool)name && (bool)integer_type_sp) 1106 1107 {} 1108