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