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 llvm_unreachable("Unhandled encoding_data_type."); 488 } 489 } else { 490 // We have no encoding type, return void? 491 TypeSystem *type_system = 492 m_symbol_file->GetTypeSystemForLanguage(eLanguageTypeC); 493 CompilerType void_compiler_type = 494 type_system->GetBasicTypeFromAST(eBasicTypeVoid); 495 switch (m_encoding_uid_type) { 496 case eEncodingIsUID: 497 m_compiler_type = void_compiler_type; 498 break; 499 500 case eEncodingIsConstUID: 501 m_compiler_type = void_compiler_type.AddConstModifier(); 502 break; 503 504 case eEncodingIsRestrictUID: 505 m_compiler_type = void_compiler_type.AddRestrictModifier(); 506 break; 507 508 case eEncodingIsVolatileUID: 509 m_compiler_type = void_compiler_type.AddVolatileModifier(); 510 break; 511 512 case eEncodingIsTypedefUID: 513 m_compiler_type = void_compiler_type.CreateTypedef( 514 m_name.AsCString("__lldb_invalid_typedef_name"), 515 GetSymbolFile()->GetDeclContextContainingUID(GetID())); 516 break; 517 518 case eEncodingIsPointerUID: 519 m_compiler_type = void_compiler_type.GetPointerType(); 520 break; 521 522 case eEncodingIsLValueReferenceUID: 523 m_compiler_type = void_compiler_type.GetLValueReferenceType(); 524 break; 525 526 case eEncodingIsRValueReferenceUID: 527 m_compiler_type = void_compiler_type.GetRValueReferenceType(); 528 break; 529 530 default: 531 llvm_unreachable("Unhandled encoding_data_type."); 532 } 533 } 534 535 // When we have a EncodingUID, our "m_flags.compiler_type_resolve_state" is 536 // set to eResolveStateUnresolved 537 // so we need to update it to say that we now have a forward declaration 538 // since that is what we created 539 // above. 540 if (m_compiler_type.IsValid()) 541 m_flags.compiler_type_resolve_state = eResolveStateForward; 542 } 543 544 // Check if we have a forward reference to a class/struct/union/enum? 545 if (compiler_type_resolve_state == eResolveStateLayout || 546 compiler_type_resolve_state == eResolveStateFull) { 547 // Check if we have a forward reference to a class/struct/union/enum? 548 if (m_compiler_type.IsValid() && 549 m_flags.compiler_type_resolve_state < compiler_type_resolve_state) { 550 m_flags.compiler_type_resolve_state = eResolveStateFull; 551 if (!m_compiler_type.IsDefined()) { 552 // We have a forward declaration, we need to resolve it to a complete 553 // definition. 554 m_symbol_file->CompleteType(m_compiler_type); 555 } 556 } 557 } 558 559 // If we have an encoding type, then we need to make sure it is 560 // resolved appropriately. 561 if (m_encoding_uid != LLDB_INVALID_UID) { 562 if (encoding_type == nullptr) 563 encoding_type = GetEncodingType(); 564 if (encoding_type) { 565 ResolveState encoding_compiler_type_resolve_state = 566 compiler_type_resolve_state; 567 568 if (compiler_type_resolve_state == eResolveStateLayout) { 569 switch (m_encoding_uid_type) { 570 case eEncodingIsPointerUID: 571 case eEncodingIsLValueReferenceUID: 572 case eEncodingIsRValueReferenceUID: 573 encoding_compiler_type_resolve_state = eResolveStateForward; 574 break; 575 default: 576 break; 577 } 578 } 579 encoding_type->ResolveClangType(encoding_compiler_type_resolve_state); 580 } 581 } 582 return m_compiler_type.IsValid(); 583 } 584 uint32_t Type::GetEncodingMask() { 585 uint32_t encoding_mask = 1u << m_encoding_uid_type; 586 Type *encoding_type = GetEncodingType(); 587 assert(encoding_type != this); 588 if (encoding_type) 589 encoding_mask |= encoding_type->GetEncodingMask(); 590 return encoding_mask; 591 } 592 593 CompilerType Type::GetFullCompilerType() { 594 ResolveClangType(eResolveStateFull); 595 return m_compiler_type; 596 } 597 598 CompilerType Type::GetLayoutCompilerType() { 599 ResolveClangType(eResolveStateLayout); 600 return m_compiler_type; 601 } 602 603 CompilerType Type::GetForwardCompilerType() { 604 ResolveClangType(eResolveStateForward); 605 return m_compiler_type; 606 } 607 608 int Type::Compare(const Type &a, const Type &b) { 609 // Just compare the UID values for now... 610 lldb::user_id_t a_uid = a.GetID(); 611 lldb::user_id_t b_uid = b.GetID(); 612 if (a_uid < b_uid) 613 return -1; 614 if (a_uid > b_uid) 615 return 1; 616 return 0; 617 } 618 619 ConstString Type::GetQualifiedName() { 620 return GetForwardCompilerType().GetConstTypeName(); 621 } 622 623 bool Type::GetTypeScopeAndBasename(const char *&name_cstr, std::string &scope, 624 std::string &basename, 625 TypeClass &type_class) { 626 // Protect against null c string. 627 628 type_class = eTypeClassAny; 629 630 if (name_cstr && name_cstr[0]) { 631 llvm::StringRef name_strref(name_cstr); 632 if (name_strref.startswith("struct ")) { 633 name_cstr += 7; 634 type_class = eTypeClassStruct; 635 } else if (name_strref.startswith("class ")) { 636 name_cstr += 6; 637 type_class = eTypeClassClass; 638 } else if (name_strref.startswith("union ")) { 639 name_cstr += 6; 640 type_class = eTypeClassUnion; 641 } else if (name_strref.startswith("enum ")) { 642 name_cstr += 5; 643 type_class = eTypeClassEnumeration; 644 } else if (name_strref.startswith("typedef ")) { 645 name_cstr += 8; 646 type_class = eTypeClassTypedef; 647 } 648 const char *basename_cstr = name_cstr; 649 const char *namespace_separator = ::strstr(basename_cstr, "::"); 650 if (namespace_separator) { 651 const char *template_arg_char = ::strchr(basename_cstr, '<'); 652 while (namespace_separator != nullptr) { 653 if (template_arg_char && 654 namespace_separator > template_arg_char) // but namespace'd template 655 // arguments are still good 656 // to go 657 break; 658 basename_cstr = namespace_separator + 2; 659 namespace_separator = strstr(basename_cstr, "::"); 660 } 661 if (basename_cstr > name_cstr) { 662 scope.assign(name_cstr, basename_cstr - name_cstr); 663 basename.assign(basename_cstr); 664 return true; 665 } 666 } 667 } 668 return false; 669 } 670 671 ModuleSP Type::GetModule() { 672 if (m_symbol_file) 673 return m_symbol_file->GetObjectFile()->GetModule(); 674 return ModuleSP(); 675 } 676 677 TypeAndOrName::TypeAndOrName() : m_type_pair(), m_type_name() {} 678 679 TypeAndOrName::TypeAndOrName(TypeSP &in_type_sp) : m_type_pair(in_type_sp) { 680 if (in_type_sp) 681 m_type_name = in_type_sp->GetName(); 682 } 683 684 TypeAndOrName::TypeAndOrName(const char *in_type_str) 685 : m_type_name(in_type_str) {} 686 687 TypeAndOrName::TypeAndOrName(const TypeAndOrName &rhs) 688 : m_type_pair(rhs.m_type_pair), m_type_name(rhs.m_type_name) {} 689 690 TypeAndOrName::TypeAndOrName(ConstString &in_type_const_string) 691 : m_type_name(in_type_const_string) {} 692 693 TypeAndOrName &TypeAndOrName::operator=(const TypeAndOrName &rhs) { 694 if (this != &rhs) { 695 m_type_name = rhs.m_type_name; 696 m_type_pair = rhs.m_type_pair; 697 } 698 return *this; 699 } 700 701 bool TypeAndOrName::operator==(const TypeAndOrName &other) const { 702 if (m_type_pair != other.m_type_pair) 703 return false; 704 if (m_type_name != other.m_type_name) 705 return false; 706 return true; 707 } 708 709 bool TypeAndOrName::operator!=(const TypeAndOrName &other) const { 710 if (m_type_pair != other.m_type_pair) 711 return true; 712 if (m_type_name != other.m_type_name) 713 return true; 714 return false; 715 } 716 717 ConstString TypeAndOrName::GetName() const { 718 if (m_type_name) 719 return m_type_name; 720 if (m_type_pair) 721 return m_type_pair.GetName(); 722 return ConstString("<invalid>"); 723 } 724 725 void TypeAndOrName::SetName(const ConstString &type_name) { 726 m_type_name = type_name; 727 } 728 729 void TypeAndOrName::SetName(const char *type_name_cstr) { 730 m_type_name.SetCString(type_name_cstr); 731 } 732 733 void TypeAndOrName::SetTypeSP(lldb::TypeSP type_sp) { 734 m_type_pair.SetType(type_sp); 735 if (m_type_pair) 736 m_type_name = m_type_pair.GetName(); 737 } 738 739 void TypeAndOrName::SetCompilerType(CompilerType compiler_type) { 740 m_type_pair.SetType(compiler_type); 741 if (m_type_pair) 742 m_type_name = m_type_pair.GetName(); 743 } 744 745 bool TypeAndOrName::IsEmpty() const { 746 if ((bool)m_type_name || (bool)m_type_pair) 747 return false; 748 else 749 return true; 750 } 751 752 void TypeAndOrName::Clear() { 753 m_type_name.Clear(); 754 m_type_pair.Clear(); 755 } 756 757 bool TypeAndOrName::HasName() const { return (bool)m_type_name; } 758 759 bool TypeAndOrName::HasTypeSP() const { 760 return m_type_pair.GetTypeSP().get() != nullptr; 761 } 762 763 bool TypeAndOrName::HasCompilerType() const { 764 return m_type_pair.GetCompilerType().IsValid(); 765 } 766 767 TypeImpl::TypeImpl() : m_module_wp(), m_static_type(), m_dynamic_type() {} 768 769 TypeImpl::TypeImpl(const TypeImpl &rhs) 770 : m_module_wp(rhs.m_module_wp), m_static_type(rhs.m_static_type), 771 m_dynamic_type(rhs.m_dynamic_type) {} 772 773 TypeImpl::TypeImpl(const lldb::TypeSP &type_sp) 774 : m_module_wp(), m_static_type(), m_dynamic_type() { 775 SetType(type_sp); 776 } 777 778 TypeImpl::TypeImpl(const CompilerType &compiler_type) 779 : m_module_wp(), m_static_type(), m_dynamic_type() { 780 SetType(compiler_type); 781 } 782 783 TypeImpl::TypeImpl(const lldb::TypeSP &type_sp, const CompilerType &dynamic) 784 : m_module_wp(), m_static_type(type_sp), m_dynamic_type(dynamic) { 785 SetType(type_sp, dynamic); 786 } 787 788 TypeImpl::TypeImpl(const CompilerType &static_type, 789 const CompilerType &dynamic_type) 790 : m_module_wp(), m_static_type(), m_dynamic_type() { 791 SetType(static_type, dynamic_type); 792 } 793 794 TypeImpl::TypeImpl(const TypePair &pair, const CompilerType &dynamic) 795 : m_module_wp(), m_static_type(), m_dynamic_type() { 796 SetType(pair, dynamic); 797 } 798 799 void TypeImpl::SetType(const lldb::TypeSP &type_sp) { 800 m_static_type.SetType(type_sp); 801 if (type_sp) 802 m_module_wp = type_sp->GetModule(); 803 else 804 m_module_wp = lldb::ModuleWP(); 805 } 806 807 void TypeImpl::SetType(const CompilerType &compiler_type) { 808 m_module_wp = lldb::ModuleWP(); 809 m_static_type.SetType(compiler_type); 810 } 811 812 void TypeImpl::SetType(const lldb::TypeSP &type_sp, 813 const CompilerType &dynamic) { 814 SetType(type_sp); 815 m_dynamic_type = dynamic; 816 } 817 818 void TypeImpl::SetType(const CompilerType &compiler_type, 819 const CompilerType &dynamic) { 820 m_module_wp = lldb::ModuleWP(); 821 m_static_type.SetType(compiler_type); 822 m_dynamic_type = dynamic; 823 } 824 825 void TypeImpl::SetType(const TypePair &pair, const CompilerType &dynamic) { 826 m_module_wp = pair.GetModule(); 827 m_static_type = pair; 828 m_dynamic_type = dynamic; 829 } 830 831 TypeImpl &TypeImpl::operator=(const TypeImpl &rhs) { 832 if (rhs != *this) { 833 m_module_wp = rhs.m_module_wp; 834 m_static_type = rhs.m_static_type; 835 m_dynamic_type = rhs.m_dynamic_type; 836 } 837 return *this; 838 } 839 840 bool TypeImpl::CheckModule(lldb::ModuleSP &module_sp) const { 841 // Check if we have a module for this type. If we do and the shared pointer is 842 // can be successfully initialized with m_module_wp, return true. Else return 843 // false 844 // if we didn't have a module, or if we had a module and it has been deleted. 845 // Any 846 // functions doing anything with a TypeSP in this TypeImpl class should call 847 // this 848 // function and only do anything with the ivars if this function returns true. 849 // If 850 // we have a module, the "module_sp" will be filled in with a strong reference 851 // to the 852 // module so that the module will at least stay around long enough for the 853 // type 854 // query to succeed. 855 module_sp = m_module_wp.lock(); 856 if (!module_sp) { 857 lldb::ModuleWP empty_module_wp; 858 // If either call to "std::weak_ptr::owner_before(...) value returns true, 859 // this 860 // indicates that m_module_wp once contained (possibly still does) a 861 // reference 862 // to a valid shared pointer. This helps us know if we had a valid reference 863 // to 864 // a section which is now invalid because the module it was in was deleted 865 if (empty_module_wp.owner_before(m_module_wp) || 866 m_module_wp.owner_before(empty_module_wp)) { 867 // m_module_wp had a valid reference to a module, but all strong 868 // references 869 // have been released and the module has been deleted 870 return false; 871 } 872 } 873 // We either successfully locked the module, or didn't have one to begin with 874 return true; 875 } 876 877 bool TypeImpl::operator==(const TypeImpl &rhs) const { 878 return m_static_type == rhs.m_static_type && 879 m_dynamic_type == rhs.m_dynamic_type; 880 } 881 882 bool TypeImpl::operator!=(const TypeImpl &rhs) const { 883 return m_static_type != rhs.m_static_type || 884 m_dynamic_type != rhs.m_dynamic_type; 885 } 886 887 bool TypeImpl::IsValid() const { 888 // just a name is not valid 889 ModuleSP module_sp; 890 if (CheckModule(module_sp)) 891 return m_static_type.IsValid() || m_dynamic_type.IsValid(); 892 return false; 893 } 894 895 TypeImpl::operator bool() const { return IsValid(); } 896 897 void TypeImpl::Clear() { 898 m_module_wp = lldb::ModuleWP(); 899 m_static_type.Clear(); 900 m_dynamic_type.Clear(); 901 } 902 903 ConstString TypeImpl::GetName() const { 904 ModuleSP module_sp; 905 if (CheckModule(module_sp)) { 906 if (m_dynamic_type) 907 return m_dynamic_type.GetTypeName(); 908 return m_static_type.GetName(); 909 } 910 return ConstString(); 911 } 912 913 ConstString TypeImpl::GetDisplayTypeName() const { 914 ModuleSP module_sp; 915 if (CheckModule(module_sp)) { 916 if (m_dynamic_type) 917 return m_dynamic_type.GetDisplayTypeName(); 918 return m_static_type.GetDisplayTypeName(); 919 } 920 return ConstString(); 921 } 922 923 TypeImpl TypeImpl::GetPointerType() const { 924 ModuleSP module_sp; 925 if (CheckModule(module_sp)) { 926 if (m_dynamic_type.IsValid()) { 927 return TypeImpl(m_static_type.GetPointerType(), 928 m_dynamic_type.GetPointerType()); 929 } 930 return TypeImpl(m_static_type.GetPointerType()); 931 } 932 return TypeImpl(); 933 } 934 935 TypeImpl TypeImpl::GetPointeeType() const { 936 ModuleSP module_sp; 937 if (CheckModule(module_sp)) { 938 if (m_dynamic_type.IsValid()) { 939 return TypeImpl(m_static_type.GetPointeeType(), 940 m_dynamic_type.GetPointeeType()); 941 } 942 return TypeImpl(m_static_type.GetPointeeType()); 943 } 944 return TypeImpl(); 945 } 946 947 TypeImpl TypeImpl::GetReferenceType() const { 948 ModuleSP module_sp; 949 if (CheckModule(module_sp)) { 950 if (m_dynamic_type.IsValid()) { 951 return TypeImpl(m_static_type.GetReferenceType(), 952 m_dynamic_type.GetLValueReferenceType()); 953 } 954 return TypeImpl(m_static_type.GetReferenceType()); 955 } 956 return TypeImpl(); 957 } 958 959 TypeImpl TypeImpl::GetTypedefedType() const { 960 ModuleSP module_sp; 961 if (CheckModule(module_sp)) { 962 if (m_dynamic_type.IsValid()) { 963 return TypeImpl(m_static_type.GetTypedefedType(), 964 m_dynamic_type.GetTypedefedType()); 965 } 966 return TypeImpl(m_static_type.GetTypedefedType()); 967 } 968 return TypeImpl(); 969 } 970 971 TypeImpl TypeImpl::GetDereferencedType() const { 972 ModuleSP module_sp; 973 if (CheckModule(module_sp)) { 974 if (m_dynamic_type.IsValid()) { 975 return TypeImpl(m_static_type.GetDereferencedType(), 976 m_dynamic_type.GetNonReferenceType()); 977 } 978 return TypeImpl(m_static_type.GetDereferencedType()); 979 } 980 return TypeImpl(); 981 } 982 983 TypeImpl TypeImpl::GetUnqualifiedType() const { 984 ModuleSP module_sp; 985 if (CheckModule(module_sp)) { 986 if (m_dynamic_type.IsValid()) { 987 return TypeImpl(m_static_type.GetUnqualifiedType(), 988 m_dynamic_type.GetFullyUnqualifiedType()); 989 } 990 return TypeImpl(m_static_type.GetUnqualifiedType()); 991 } 992 return TypeImpl(); 993 } 994 995 TypeImpl TypeImpl::GetCanonicalType() const { 996 ModuleSP module_sp; 997 if (CheckModule(module_sp)) { 998 if (m_dynamic_type.IsValid()) { 999 return TypeImpl(m_static_type.GetCanonicalType(), 1000 m_dynamic_type.GetCanonicalType()); 1001 } 1002 return TypeImpl(m_static_type.GetCanonicalType()); 1003 } 1004 return TypeImpl(); 1005 } 1006 1007 CompilerType TypeImpl::GetCompilerType(bool prefer_dynamic) { 1008 ModuleSP module_sp; 1009 if (CheckModule(module_sp)) { 1010 if (prefer_dynamic) { 1011 if (m_dynamic_type.IsValid()) 1012 return m_dynamic_type; 1013 } 1014 return m_static_type.GetCompilerType(); 1015 } 1016 return CompilerType(); 1017 } 1018 1019 TypeSystem *TypeImpl::GetTypeSystem(bool prefer_dynamic) { 1020 ModuleSP module_sp; 1021 if (CheckModule(module_sp)) { 1022 if (prefer_dynamic) { 1023 if (m_dynamic_type.IsValid()) 1024 return m_dynamic_type.GetTypeSystem(); 1025 } 1026 return m_static_type.GetCompilerType().GetTypeSystem(); 1027 } 1028 return NULL; 1029 } 1030 1031 bool TypeImpl::GetDescription(lldb_private::Stream &strm, 1032 lldb::DescriptionLevel description_level) { 1033 ModuleSP module_sp; 1034 if (CheckModule(module_sp)) { 1035 if (m_dynamic_type.IsValid()) { 1036 strm.Printf("Dynamic:\n"); 1037 m_dynamic_type.DumpTypeDescription(&strm); 1038 strm.Printf("\nStatic:\n"); 1039 } 1040 m_static_type.GetCompilerType().DumpTypeDescription(&strm); 1041 } else { 1042 strm.PutCString("Invalid TypeImpl module for type has been deleted\n"); 1043 } 1044 return true; 1045 } 1046 1047 bool TypeMemberFunctionImpl::IsValid() { 1048 return m_type.IsValid() && m_kind != lldb::eMemberFunctionKindUnknown; 1049 } 1050 1051 ConstString TypeMemberFunctionImpl::GetName() const { return m_name; } 1052 1053 ConstString TypeMemberFunctionImpl::GetMangledName() const { 1054 return m_decl.GetMangledName(); 1055 } 1056 1057 CompilerType TypeMemberFunctionImpl::GetType() const { return m_type; } 1058 1059 lldb::MemberFunctionKind TypeMemberFunctionImpl::GetKind() const { 1060 return m_kind; 1061 } 1062 1063 bool TypeMemberFunctionImpl::GetDescription(Stream &stream) { 1064 switch (m_kind) { 1065 case lldb::eMemberFunctionKindUnknown: 1066 return false; 1067 case lldb::eMemberFunctionKindConstructor: 1068 stream.Printf("constructor for %s", 1069 m_type.GetTypeName().AsCString("<unknown>")); 1070 break; 1071 case lldb::eMemberFunctionKindDestructor: 1072 stream.Printf("destructor for %s", 1073 m_type.GetTypeName().AsCString("<unknown>")); 1074 break; 1075 case lldb::eMemberFunctionKindInstanceMethod: 1076 stream.Printf("instance method %s of type %s", m_name.AsCString(), 1077 m_decl.GetDeclContext().GetName().AsCString()); 1078 break; 1079 case lldb::eMemberFunctionKindStaticMethod: 1080 stream.Printf("static method %s of type %s", m_name.AsCString(), 1081 m_decl.GetDeclContext().GetName().AsCString()); 1082 break; 1083 } 1084 return true; 1085 } 1086 1087 CompilerType TypeMemberFunctionImpl::GetReturnType() const { 1088 if (m_type) 1089 return m_type.GetFunctionReturnType(); 1090 return m_decl.GetFunctionReturnType(); 1091 } 1092 1093 size_t TypeMemberFunctionImpl::GetNumArguments() const { 1094 if (m_type) 1095 return m_type.GetNumberOfFunctionArguments(); 1096 else 1097 return m_decl.GetNumFunctionArguments(); 1098 } 1099 1100 CompilerType TypeMemberFunctionImpl::GetArgumentAtIndex(size_t idx) const { 1101 if (m_type) 1102 return m_type.GetFunctionArgumentAtIndex(idx); 1103 else 1104 return m_decl.GetFunctionArgumentType(idx); 1105 } 1106 1107 TypeEnumMemberImpl::TypeEnumMemberImpl(const lldb::TypeImplSP &integer_type_sp, 1108 const ConstString &name, 1109 const llvm::APSInt &value) 1110 : m_integer_type_sp(integer_type_sp), m_name(name), m_value(value), 1111 m_valid((bool)name && (bool)integer_type_sp) 1112 1113 {} 1114