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