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