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