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 if ((bool)m_type_name || (bool)m_type_pair) 750 return false; 751 else 752 return true; 753 } 754 755 void TypeAndOrName::Clear() { 756 m_type_name.Clear(); 757 m_type_pair.Clear(); 758 } 759 760 bool TypeAndOrName::HasName() const { return (bool)m_type_name; } 761 762 bool TypeAndOrName::HasTypeSP() const { 763 return m_type_pair.GetTypeSP().get() != nullptr; 764 } 765 766 bool TypeAndOrName::HasCompilerType() const { 767 return m_type_pair.GetCompilerType().IsValid(); 768 } 769 770 TypeImpl::TypeImpl() : m_module_wp(), m_static_type(), m_dynamic_type() {} 771 772 TypeImpl::TypeImpl(const TypeImpl &rhs) 773 : m_module_wp(rhs.m_module_wp), m_static_type(rhs.m_static_type), 774 m_dynamic_type(rhs.m_dynamic_type) {} 775 776 TypeImpl::TypeImpl(const lldb::TypeSP &type_sp) 777 : m_module_wp(), m_static_type(), m_dynamic_type() { 778 SetType(type_sp); 779 } 780 781 TypeImpl::TypeImpl(const CompilerType &compiler_type) 782 : m_module_wp(), m_static_type(), m_dynamic_type() { 783 SetType(compiler_type); 784 } 785 786 TypeImpl::TypeImpl(const lldb::TypeSP &type_sp, const CompilerType &dynamic) 787 : m_module_wp(), m_static_type(type_sp), m_dynamic_type(dynamic) { 788 SetType(type_sp, dynamic); 789 } 790 791 TypeImpl::TypeImpl(const CompilerType &static_type, 792 const CompilerType &dynamic_type) 793 : m_module_wp(), m_static_type(), m_dynamic_type() { 794 SetType(static_type, dynamic_type); 795 } 796 797 TypeImpl::TypeImpl(const TypePair &pair, const CompilerType &dynamic) 798 : m_module_wp(), m_static_type(), m_dynamic_type() { 799 SetType(pair, dynamic); 800 } 801 802 void TypeImpl::SetType(const lldb::TypeSP &type_sp) { 803 m_static_type.SetType(type_sp); 804 if (type_sp) 805 m_module_wp = type_sp->GetModule(); 806 else 807 m_module_wp = lldb::ModuleWP(); 808 } 809 810 void TypeImpl::SetType(const CompilerType &compiler_type) { 811 m_module_wp = lldb::ModuleWP(); 812 m_static_type.SetType(compiler_type); 813 } 814 815 void TypeImpl::SetType(const lldb::TypeSP &type_sp, 816 const CompilerType &dynamic) { 817 SetType(type_sp); 818 m_dynamic_type = dynamic; 819 } 820 821 void TypeImpl::SetType(const CompilerType &compiler_type, 822 const CompilerType &dynamic) { 823 m_module_wp = lldb::ModuleWP(); 824 m_static_type.SetType(compiler_type); 825 m_dynamic_type = dynamic; 826 } 827 828 void TypeImpl::SetType(const TypePair &pair, const CompilerType &dynamic) { 829 m_module_wp = pair.GetModule(); 830 m_static_type = pair; 831 m_dynamic_type = dynamic; 832 } 833 834 TypeImpl &TypeImpl::operator=(const TypeImpl &rhs) { 835 if (rhs != *this) { 836 m_module_wp = rhs.m_module_wp; 837 m_static_type = rhs.m_static_type; 838 m_dynamic_type = rhs.m_dynamic_type; 839 } 840 return *this; 841 } 842 843 bool TypeImpl::CheckModule(lldb::ModuleSP &module_sp) const { 844 // Check if we have a module for this type. If we do and the shared pointer 845 // is can be successfully initialized with m_module_wp, return true. Else 846 // return false if we didn't have a module, or if we had a module and it has 847 // been deleted. Any functions doing anything with a TypeSP in this TypeImpl 848 // class should call this function and only do anything with the ivars if 849 // this function returns true. If we have a module, the "module_sp" will be 850 // filled in with a strong reference to the module so that the module will at 851 // least stay around long enough for the type query to succeed. 852 module_sp = m_module_wp.lock(); 853 if (!module_sp) { 854 lldb::ModuleWP empty_module_wp; 855 // If either call to "std::weak_ptr::owner_before(...) value returns true, 856 // this indicates that m_module_wp once contained (possibly still does) a 857 // reference to a valid shared pointer. This helps us know if we had a 858 // valid reference to a section which is now invalid because the module it 859 // was in was deleted 860 if (empty_module_wp.owner_before(m_module_wp) || 861 m_module_wp.owner_before(empty_module_wp)) { 862 // m_module_wp had a valid reference to a module, but all strong 863 // references have been released and the module has been deleted 864 return false; 865 } 866 } 867 // We either successfully locked the module, or didn't have one to begin with 868 return true; 869 } 870 871 bool TypeImpl::operator==(const TypeImpl &rhs) const { 872 return m_static_type == rhs.m_static_type && 873 m_dynamic_type == rhs.m_dynamic_type; 874 } 875 876 bool TypeImpl::operator!=(const TypeImpl &rhs) const { 877 return m_static_type != rhs.m_static_type || 878 m_dynamic_type != rhs.m_dynamic_type; 879 } 880 881 bool TypeImpl::IsValid() const { 882 // just a name is not valid 883 ModuleSP module_sp; 884 if (CheckModule(module_sp)) 885 return m_static_type.IsValid() || m_dynamic_type.IsValid(); 886 return false; 887 } 888 889 TypeImpl::operator bool() const { return IsValid(); } 890 891 void TypeImpl::Clear() { 892 m_module_wp = lldb::ModuleWP(); 893 m_static_type.Clear(); 894 m_dynamic_type.Clear(); 895 } 896 897 ConstString TypeImpl::GetName() const { 898 ModuleSP module_sp; 899 if (CheckModule(module_sp)) { 900 if (m_dynamic_type) 901 return m_dynamic_type.GetTypeName(); 902 return m_static_type.GetName(); 903 } 904 return ConstString(); 905 } 906 907 ConstString TypeImpl::GetDisplayTypeName() const { 908 ModuleSP module_sp; 909 if (CheckModule(module_sp)) { 910 if (m_dynamic_type) 911 return m_dynamic_type.GetDisplayTypeName(); 912 return m_static_type.GetDisplayTypeName(); 913 } 914 return ConstString(); 915 } 916 917 TypeImpl TypeImpl::GetPointerType() const { 918 ModuleSP module_sp; 919 if (CheckModule(module_sp)) { 920 if (m_dynamic_type.IsValid()) { 921 return TypeImpl(m_static_type.GetPointerType(), 922 m_dynamic_type.GetPointerType()); 923 } 924 return TypeImpl(m_static_type.GetPointerType()); 925 } 926 return TypeImpl(); 927 } 928 929 TypeImpl TypeImpl::GetPointeeType() const { 930 ModuleSP module_sp; 931 if (CheckModule(module_sp)) { 932 if (m_dynamic_type.IsValid()) { 933 return TypeImpl(m_static_type.GetPointeeType(), 934 m_dynamic_type.GetPointeeType()); 935 } 936 return TypeImpl(m_static_type.GetPointeeType()); 937 } 938 return TypeImpl(); 939 } 940 941 TypeImpl TypeImpl::GetReferenceType() const { 942 ModuleSP module_sp; 943 if (CheckModule(module_sp)) { 944 if (m_dynamic_type.IsValid()) { 945 return TypeImpl(m_static_type.GetReferenceType(), 946 m_dynamic_type.GetLValueReferenceType()); 947 } 948 return TypeImpl(m_static_type.GetReferenceType()); 949 } 950 return TypeImpl(); 951 } 952 953 TypeImpl TypeImpl::GetTypedefedType() const { 954 ModuleSP module_sp; 955 if (CheckModule(module_sp)) { 956 if (m_dynamic_type.IsValid()) { 957 return TypeImpl(m_static_type.GetTypedefedType(), 958 m_dynamic_type.GetTypedefedType()); 959 } 960 return TypeImpl(m_static_type.GetTypedefedType()); 961 } 962 return TypeImpl(); 963 } 964 965 TypeImpl TypeImpl::GetDereferencedType() const { 966 ModuleSP module_sp; 967 if (CheckModule(module_sp)) { 968 if (m_dynamic_type.IsValid()) { 969 return TypeImpl(m_static_type.GetDereferencedType(), 970 m_dynamic_type.GetNonReferenceType()); 971 } 972 return TypeImpl(m_static_type.GetDereferencedType()); 973 } 974 return TypeImpl(); 975 } 976 977 TypeImpl TypeImpl::GetUnqualifiedType() const { 978 ModuleSP module_sp; 979 if (CheckModule(module_sp)) { 980 if (m_dynamic_type.IsValid()) { 981 return TypeImpl(m_static_type.GetUnqualifiedType(), 982 m_dynamic_type.GetFullyUnqualifiedType()); 983 } 984 return TypeImpl(m_static_type.GetUnqualifiedType()); 985 } 986 return TypeImpl(); 987 } 988 989 TypeImpl TypeImpl::GetCanonicalType() const { 990 ModuleSP module_sp; 991 if (CheckModule(module_sp)) { 992 if (m_dynamic_type.IsValid()) { 993 return TypeImpl(m_static_type.GetCanonicalType(), 994 m_dynamic_type.GetCanonicalType()); 995 } 996 return TypeImpl(m_static_type.GetCanonicalType()); 997 } 998 return TypeImpl(); 999 } 1000 1001 CompilerType TypeImpl::GetCompilerType(bool prefer_dynamic) { 1002 ModuleSP module_sp; 1003 if (CheckModule(module_sp)) { 1004 if (prefer_dynamic) { 1005 if (m_dynamic_type.IsValid()) 1006 return m_dynamic_type; 1007 } 1008 return m_static_type.GetCompilerType(); 1009 } 1010 return CompilerType(); 1011 } 1012 1013 TypeSystem *TypeImpl::GetTypeSystem(bool prefer_dynamic) { 1014 ModuleSP module_sp; 1015 if (CheckModule(module_sp)) { 1016 if (prefer_dynamic) { 1017 if (m_dynamic_type.IsValid()) 1018 return m_dynamic_type.GetTypeSystem(); 1019 } 1020 return m_static_type.GetCompilerType().GetTypeSystem(); 1021 } 1022 return NULL; 1023 } 1024 1025 bool TypeImpl::GetDescription(lldb_private::Stream &strm, 1026 lldb::DescriptionLevel description_level) { 1027 ModuleSP module_sp; 1028 if (CheckModule(module_sp)) { 1029 if (m_dynamic_type.IsValid()) { 1030 strm.Printf("Dynamic:\n"); 1031 m_dynamic_type.DumpTypeDescription(&strm); 1032 strm.Printf("\nStatic:\n"); 1033 } 1034 m_static_type.GetCompilerType().DumpTypeDescription(&strm); 1035 } else { 1036 strm.PutCString("Invalid TypeImpl module for type has been deleted\n"); 1037 } 1038 return true; 1039 } 1040 1041 bool TypeMemberFunctionImpl::IsValid() { 1042 return m_type.IsValid() && m_kind != lldb::eMemberFunctionKindUnknown; 1043 } 1044 1045 ConstString TypeMemberFunctionImpl::GetName() const { return m_name; } 1046 1047 ConstString TypeMemberFunctionImpl::GetMangledName() const { 1048 return m_decl.GetMangledName(); 1049 } 1050 1051 CompilerType TypeMemberFunctionImpl::GetType() const { return m_type; } 1052 1053 lldb::MemberFunctionKind TypeMemberFunctionImpl::GetKind() const { 1054 return m_kind; 1055 } 1056 1057 bool TypeMemberFunctionImpl::GetDescription(Stream &stream) { 1058 switch (m_kind) { 1059 case lldb::eMemberFunctionKindUnknown: 1060 return false; 1061 case lldb::eMemberFunctionKindConstructor: 1062 stream.Printf("constructor for %s", 1063 m_type.GetTypeName().AsCString("<unknown>")); 1064 break; 1065 case lldb::eMemberFunctionKindDestructor: 1066 stream.Printf("destructor for %s", 1067 m_type.GetTypeName().AsCString("<unknown>")); 1068 break; 1069 case lldb::eMemberFunctionKindInstanceMethod: 1070 stream.Printf("instance method %s of type %s", m_name.AsCString(), 1071 m_decl.GetDeclContext().GetName().AsCString()); 1072 break; 1073 case lldb::eMemberFunctionKindStaticMethod: 1074 stream.Printf("static method %s of type %s", m_name.AsCString(), 1075 m_decl.GetDeclContext().GetName().AsCString()); 1076 break; 1077 } 1078 return true; 1079 } 1080 1081 CompilerType TypeMemberFunctionImpl::GetReturnType() const { 1082 if (m_type) 1083 return m_type.GetFunctionReturnType(); 1084 return m_decl.GetFunctionReturnType(); 1085 } 1086 1087 size_t TypeMemberFunctionImpl::GetNumArguments() const { 1088 if (m_type) 1089 return m_type.GetNumberOfFunctionArguments(); 1090 else 1091 return m_decl.GetNumFunctionArguments(); 1092 } 1093 1094 CompilerType TypeMemberFunctionImpl::GetArgumentAtIndex(size_t idx) const { 1095 if (m_type) 1096 return m_type.GetFunctionArgumentAtIndex(idx); 1097 else 1098 return m_decl.GetFunctionArgumentType(idx); 1099 } 1100 1101 TypeEnumMemberImpl::TypeEnumMemberImpl(const lldb::TypeImplSP &integer_type_sp, 1102 const ConstString &name, 1103 const llvm::APSInt &value) 1104 : m_integer_type_sp(integer_type_sp), m_name(name), m_value(value), 1105 m_valid((bool)name && (bool)integer_type_sp) 1106 1107 {} 1108