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