Type.h revision 314564
1//===-- Type.h --------------------------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef liblldb_Type_h_ 11#define liblldb_Type_h_ 12 13#include "lldb/Core/ClangForward.h" 14#include "lldb/Core/ConstString.h" 15#include "lldb/Core/UserID.h" 16#include "lldb/Symbol/CompilerDecl.h" 17#include "lldb/Symbol/CompilerType.h" 18#include "lldb/Symbol/Declaration.h" 19#include "lldb/lldb-private.h" 20 21#include "llvm/ADT/APSInt.h" 22 23#include <set> 24 25namespace lldb_private { 26 27//---------------------------------------------------------------------- 28// CompilerContext allows an array of these items to be passed to 29// perform detailed lookups in SymbolVendor and SymbolFile functions. 30//---------------------------------------------------------------------- 31struct CompilerContext { 32 CompilerContext(CompilerContextKind t, const ConstString &n) 33 : type(t), name(n) {} 34 35 bool operator==(const CompilerContext &rhs) const { 36 return type == rhs.type && name == rhs.name; 37 } 38 39 void Dump() const; 40 41 CompilerContextKind type; 42 ConstString name; 43}; 44 45class SymbolFileType : public std::enable_shared_from_this<SymbolFileType>, 46 public UserID { 47public: 48 SymbolFileType(SymbolFile &symbol_file, lldb::user_id_t uid) 49 : UserID(uid), m_symbol_file(symbol_file) {} 50 51 SymbolFileType(SymbolFile &symbol_file, const lldb::TypeSP &type_sp); 52 53 ~SymbolFileType() {} 54 55 Type *operator->() { return GetType(); } 56 57 Type *GetType(); 58 59protected: 60 SymbolFile &m_symbol_file; 61 lldb::TypeSP m_type_sp; 62}; 63 64class Type : public std::enable_shared_from_this<Type>, public UserID { 65public: 66 typedef enum EncodingDataTypeTag { 67 eEncodingInvalid, 68 eEncodingIsUID, ///< This type is the type whose UID is m_encoding_uid 69 eEncodingIsConstUID, ///< This type is the type whose UID is m_encoding_uid 70 ///with the const qualifier added 71 eEncodingIsRestrictUID, ///< This type is the type whose UID is 72 ///m_encoding_uid with the restrict qualifier added 73 eEncodingIsVolatileUID, ///< This type is the type whose UID is 74 ///m_encoding_uid with the volatile qualifier added 75 eEncodingIsTypedefUID, ///< This type is pointer to a type whose UID is 76 ///m_encoding_uid 77 eEncodingIsPointerUID, ///< This type is pointer to a type whose UID is 78 ///m_encoding_uid 79 eEncodingIsLValueReferenceUID, ///< This type is L value reference to a type 80 ///whose UID is m_encoding_uid 81 eEncodingIsRValueReferenceUID, ///< This type is R value reference to a type 82 ///whose UID is m_encoding_uid 83 eEncodingIsSyntheticUID 84 } EncodingDataType; 85 86 // We must force the underlying type of the enum to be unsigned here. Not all 87 // compilers 88 // behave the same with regards to the default underlying type of an enum, but 89 // because 90 // this enum is used in an enum bitfield and integer comparisons are done with 91 // the value 92 // we need to guarantee that it's always unsigned so that, for example, 93 // eResolveStateFull 94 // doesn't compare less than eResolveStateUnresolved when used in a 2-bit 95 // bitfield. 96 typedef enum ResolveStateTag : unsigned { 97 eResolveStateUnresolved = 0, 98 eResolveStateForward = 1, 99 eResolveStateLayout = 2, 100 eResolveStateFull = 3 101 } ResolveState; 102 103 Type(lldb::user_id_t uid, SymbolFile *symbol_file, const ConstString &name, 104 uint64_t byte_size, SymbolContextScope *context, 105 lldb::user_id_t encoding_uid, EncodingDataType encoding_uid_type, 106 const Declaration &decl, const CompilerType &compiler_qual_type, 107 ResolveState compiler_type_resolve_state); 108 109 // This makes an invalid type. Used for functions that return a Type when 110 // they 111 // get an error. 112 Type(); 113 114 Type(const Type &rhs); 115 116 const Type &operator=(const Type &rhs); 117 118 void Dump(Stream *s, bool show_context); 119 120 void DumpTypeName(Stream *s); 121 122 // Since Type instances only keep a "SymbolFile *" internally, other classes 123 // like TypeImpl need make sure the module is still around before playing with 124 // Type instances. They can store a weak pointer to the Module; 125 lldb::ModuleSP GetModule(); 126 127 void GetDescription(Stream *s, lldb::DescriptionLevel level, bool show_name); 128 129 SymbolFile *GetSymbolFile() { return m_symbol_file; } 130 const SymbolFile *GetSymbolFile() const { return m_symbol_file; } 131 132 TypeList *GetTypeList(); 133 134 const ConstString &GetName(); 135 136 uint64_t GetByteSize(); 137 138 uint32_t GetNumChildren(bool omit_empty_base_classes); 139 140 bool IsAggregateType(); 141 142 bool IsValidType() { return m_encoding_uid_type != eEncodingInvalid; } 143 144 bool IsTypedef() { return m_encoding_uid_type == eEncodingIsTypedefUID; } 145 146 lldb::TypeSP GetTypedefType(); 147 148 const ConstString &GetName() const { return m_name; } 149 150 ConstString GetQualifiedName(); 151 152 void DumpValue(ExecutionContext *exe_ctx, Stream *s, 153 const DataExtractor &data, uint32_t data_offset, 154 bool show_type, bool show_summary, bool verbose, 155 lldb::Format format = lldb::eFormatDefault); 156 157 bool DumpValueInMemory(ExecutionContext *exe_ctx, Stream *s, 158 lldb::addr_t address, AddressType address_type, 159 bool show_types, bool show_summary, bool verbose); 160 161 bool ReadFromMemory(ExecutionContext *exe_ctx, lldb::addr_t address, 162 AddressType address_type, DataExtractor &data); 163 164 bool WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t address, 165 AddressType address_type, DataExtractor &data); 166 167 bool GetIsDeclaration() const; 168 169 void SetIsDeclaration(bool b); 170 171 bool GetIsExternal() const; 172 173 void SetIsExternal(bool b); 174 175 lldb::Format GetFormat(); 176 177 lldb::Encoding GetEncoding(uint64_t &count); 178 179 SymbolContextScope *GetSymbolContextScope() { return m_context; } 180 const SymbolContextScope *GetSymbolContextScope() const { return m_context; } 181 void SetSymbolContextScope(SymbolContextScope *context) { 182 m_context = context; 183 } 184 185 const lldb_private::Declaration &GetDeclaration() const; 186 187 // Get the clang type, and resolve definitions for any 188 // class/struct/union/enum types completely. 189 CompilerType GetFullCompilerType(); 190 191 // Get the clang type, and resolve definitions enough so that the type could 192 // have layout performed. This allows ptrs and refs to class/struct/union/enum 193 // types remain forward declarations. 194 CompilerType GetLayoutCompilerType(); 195 196 // Get the clang type and leave class/struct/union/enum types as forward 197 // declarations if they haven't already been fully defined. 198 CompilerType GetForwardCompilerType(); 199 200 static int Compare(const Type &a, const Type &b); 201 202 // From a fully qualified typename, split the type into the type basename 203 // and the remaining type scope (namespaces/classes). 204 static bool GetTypeScopeAndBasename(const llvm::StringRef& name, 205 llvm::StringRef &scope, 206 llvm::StringRef &basename, 207 lldb::TypeClass &type_class); 208 void SetEncodingType(Type *encoding_type) { m_encoding_type = encoding_type; } 209 210 uint32_t GetEncodingMask(); 211 212 bool IsCompleteObjCClass() { return m_flags.is_complete_objc_class; } 213 214 void SetIsCompleteObjCClass(bool is_complete_objc_class) { 215 m_flags.is_complete_objc_class = is_complete_objc_class; 216 } 217 218protected: 219 ConstString m_name; 220 SymbolFile *m_symbol_file; 221 SymbolContextScope 222 *m_context; // The symbol context in which this type is defined 223 Type *m_encoding_type; 224 lldb::user_id_t m_encoding_uid; 225 EncodingDataType m_encoding_uid_type; 226 uint64_t m_byte_size; 227 Declaration m_decl; 228 CompilerType m_compiler_type; 229 230 struct Flags { 231#ifdef __GNUC__ 232 // using unsigned type here to work around a very noisy gcc warning 233 unsigned compiler_type_resolve_state : 2; 234#else 235 ResolveState compiler_type_resolve_state : 2; 236#endif 237 bool is_complete_objc_class : 1; 238 } m_flags; 239 240 Type *GetEncodingType(); 241 242 bool ResolveClangType(ResolveState compiler_type_resolve_state); 243}; 244 245// these classes are used to back the SBType* objects 246 247class TypePair { 248public: 249 TypePair() : compiler_type(), type_sp() {} 250 251 TypePair(CompilerType type) : compiler_type(type), type_sp() {} 252 253 TypePair(lldb::TypeSP type) : compiler_type(), type_sp(type) { 254 compiler_type = type_sp->GetForwardCompilerType(); 255 } 256 257 bool IsValid() const { 258 return compiler_type.IsValid() || (type_sp.get() != nullptr); 259 } 260 261 explicit operator bool() const { return IsValid(); } 262 263 bool operator==(const TypePair &rhs) const { 264 return compiler_type == rhs.compiler_type && 265 type_sp.get() == rhs.type_sp.get(); 266 } 267 268 bool operator!=(const TypePair &rhs) const { 269 return compiler_type != rhs.compiler_type || 270 type_sp.get() != rhs.type_sp.get(); 271 } 272 273 void Clear() { 274 compiler_type.Clear(); 275 type_sp.reset(); 276 } 277 278 ConstString GetName() const { 279 if (type_sp) 280 return type_sp->GetName(); 281 if (compiler_type) 282 return compiler_type.GetTypeName(); 283 return ConstString(); 284 } 285 286 ConstString GetDisplayTypeName() const { 287 if (type_sp) 288 return type_sp->GetForwardCompilerType().GetDisplayTypeName(); 289 if (compiler_type) 290 return compiler_type.GetDisplayTypeName(); 291 return ConstString(); 292 } 293 294 void SetType(CompilerType type) { 295 type_sp.reset(); 296 compiler_type = type; 297 } 298 299 void SetType(lldb::TypeSP type) { 300 type_sp = type; 301 if (type_sp) 302 compiler_type = type_sp->GetForwardCompilerType(); 303 else 304 compiler_type.Clear(); 305 } 306 307 lldb::TypeSP GetTypeSP() const { return type_sp; } 308 309 CompilerType GetCompilerType() const { return compiler_type; } 310 311 CompilerType GetPointerType() const { 312 if (type_sp) 313 return type_sp->GetForwardCompilerType().GetPointerType(); 314 return compiler_type.GetPointerType(); 315 } 316 317 CompilerType GetPointeeType() const { 318 if (type_sp) 319 return type_sp->GetForwardCompilerType().GetPointeeType(); 320 return compiler_type.GetPointeeType(); 321 } 322 323 CompilerType GetReferenceType() const { 324 if (type_sp) 325 return type_sp->GetForwardCompilerType().GetLValueReferenceType(); 326 else 327 return compiler_type.GetLValueReferenceType(); 328 } 329 330 CompilerType GetTypedefedType() const { 331 if (type_sp) 332 return type_sp->GetForwardCompilerType().GetTypedefedType(); 333 else 334 return compiler_type.GetTypedefedType(); 335 } 336 337 CompilerType GetDereferencedType() const { 338 if (type_sp) 339 return type_sp->GetForwardCompilerType().GetNonReferenceType(); 340 else 341 return compiler_type.GetNonReferenceType(); 342 } 343 344 CompilerType GetUnqualifiedType() const { 345 if (type_sp) 346 return type_sp->GetForwardCompilerType().GetFullyUnqualifiedType(); 347 else 348 return compiler_type.GetFullyUnqualifiedType(); 349 } 350 351 CompilerType GetCanonicalType() const { 352 if (type_sp) 353 return type_sp->GetForwardCompilerType().GetCanonicalType(); 354 return compiler_type.GetCanonicalType(); 355 } 356 357 TypeSystem *GetTypeSystem() const { return compiler_type.GetTypeSystem(); } 358 359 lldb::ModuleSP GetModule() const { 360 if (type_sp) 361 return type_sp->GetModule(); 362 return lldb::ModuleSP(); 363 } 364 365protected: 366 CompilerType compiler_type; 367 lldb::TypeSP type_sp; 368}; 369 370// the two classes here are used by the public API as a backend to 371// the SBType and SBTypeList classes 372 373class TypeImpl { 374public: 375 TypeImpl(); 376 377 ~TypeImpl() {} 378 379 TypeImpl(const TypeImpl &rhs); 380 381 TypeImpl(const lldb::TypeSP &type_sp); 382 383 TypeImpl(const CompilerType &compiler_type); 384 385 TypeImpl(const lldb::TypeSP &type_sp, const CompilerType &dynamic); 386 387 TypeImpl(const CompilerType &compiler_type, const CompilerType &dynamic); 388 389 TypeImpl(const TypePair &pair, const CompilerType &dynamic); 390 391 void SetType(const lldb::TypeSP &type_sp); 392 393 void SetType(const CompilerType &compiler_type); 394 395 void SetType(const lldb::TypeSP &type_sp, const CompilerType &dynamic); 396 397 void SetType(const CompilerType &compiler_type, const CompilerType &dynamic); 398 399 void SetType(const TypePair &pair, const CompilerType &dynamic); 400 401 TypeImpl &operator=(const TypeImpl &rhs); 402 403 bool operator==(const TypeImpl &rhs) const; 404 405 bool operator!=(const TypeImpl &rhs) const; 406 407 bool IsValid() const; 408 409 explicit operator bool() const; 410 411 void Clear(); 412 413 ConstString GetName() const; 414 415 ConstString GetDisplayTypeName() const; 416 417 TypeImpl GetPointerType() const; 418 419 TypeImpl GetPointeeType() const; 420 421 TypeImpl GetReferenceType() const; 422 423 TypeImpl GetTypedefedType() const; 424 425 TypeImpl GetDereferencedType() const; 426 427 TypeImpl GetUnqualifiedType() const; 428 429 TypeImpl GetCanonicalType() const; 430 431 CompilerType GetCompilerType(bool prefer_dynamic); 432 433 TypeSystem *GetTypeSystem(bool prefer_dynamic); 434 435 bool GetDescription(lldb_private::Stream &strm, 436 lldb::DescriptionLevel description_level); 437 438private: 439 bool CheckModule(lldb::ModuleSP &module_sp) const; 440 441 lldb::ModuleWP m_module_wp; 442 TypePair m_static_type; 443 CompilerType m_dynamic_type; 444}; 445 446class TypeListImpl { 447public: 448 TypeListImpl() : m_content() {} 449 450 void Append(const lldb::TypeImplSP &type) { m_content.push_back(type); } 451 452 class AppendVisitor { 453 public: 454 AppendVisitor(TypeListImpl &type_list) : m_type_list(type_list) {} 455 456 void operator()(const lldb::TypeImplSP &type) { m_type_list.Append(type); } 457 458 private: 459 TypeListImpl &m_type_list; 460 }; 461 462 void Append(const lldb_private::TypeList &type_list); 463 464 lldb::TypeImplSP GetTypeAtIndex(size_t idx) { 465 lldb::TypeImplSP type_sp; 466 if (idx < GetSize()) 467 type_sp = m_content[idx]; 468 return type_sp; 469 } 470 471 size_t GetSize() { return m_content.size(); } 472 473private: 474 std::vector<lldb::TypeImplSP> m_content; 475}; 476 477class TypeMemberImpl { 478public: 479 TypeMemberImpl() 480 : m_type_impl_sp(), m_bit_offset(0), m_name(), m_bitfield_bit_size(0), 481 m_is_bitfield(false) 482 483 {} 484 485 TypeMemberImpl(const lldb::TypeImplSP &type_impl_sp, uint64_t bit_offset, 486 const ConstString &name, uint32_t bitfield_bit_size = 0, 487 bool is_bitfield = false) 488 : m_type_impl_sp(type_impl_sp), m_bit_offset(bit_offset), m_name(name), 489 m_bitfield_bit_size(bitfield_bit_size), m_is_bitfield(is_bitfield) {} 490 491 TypeMemberImpl(const lldb::TypeImplSP &type_impl_sp, uint64_t bit_offset) 492 : m_type_impl_sp(type_impl_sp), m_bit_offset(bit_offset), m_name(), 493 m_bitfield_bit_size(0), m_is_bitfield(false) { 494 if (m_type_impl_sp) 495 m_name = m_type_impl_sp->GetName(); 496 } 497 498 const lldb::TypeImplSP &GetTypeImpl() { return m_type_impl_sp; } 499 500 const ConstString &GetName() const { return m_name; } 501 502 uint64_t GetBitOffset() const { return m_bit_offset; } 503 504 uint32_t GetBitfieldBitSize() const { return m_bitfield_bit_size; } 505 506 void SetBitfieldBitSize(uint32_t bitfield_bit_size) { 507 m_bitfield_bit_size = bitfield_bit_size; 508 } 509 510 bool GetIsBitfield() const { return m_is_bitfield; } 511 512 void SetIsBitfield(bool is_bitfield) { m_is_bitfield = is_bitfield; } 513 514protected: 515 lldb::TypeImplSP m_type_impl_sp; 516 uint64_t m_bit_offset; 517 ConstString m_name; 518 uint32_t m_bitfield_bit_size; // Bit size for bitfield members only 519 bool m_is_bitfield; 520}; 521 522/// 523/// Sometimes you can find the name of the type corresponding to an object, but 524/// we don't have debug 525/// information for it. If that is the case, you can return one of these 526/// objects, and then if it 527/// has a full type, you can use that, but if not at least you can print the 528/// name for informational 529/// purposes. 530/// 531 532class TypeAndOrName { 533public: 534 TypeAndOrName(); 535 TypeAndOrName(lldb::TypeSP &type_sp); 536 TypeAndOrName(const CompilerType &compiler_type); 537 TypeAndOrName(const char *type_str); 538 TypeAndOrName(const TypeAndOrName &rhs); 539 TypeAndOrName(ConstString &type_const_string); 540 541 TypeAndOrName &operator=(const TypeAndOrName &rhs); 542 543 bool operator==(const TypeAndOrName &other) const; 544 545 bool operator!=(const TypeAndOrName &other) const; 546 547 ConstString GetName() const; 548 549 lldb::TypeSP GetTypeSP() const { return m_type_pair.GetTypeSP(); } 550 551 CompilerType GetCompilerType() const { return m_type_pair.GetCompilerType(); } 552 553 void SetName(const ConstString &type_name); 554 555 void SetName(const char *type_name_cstr); 556 557 void SetTypeSP(lldb::TypeSP type_sp); 558 559 void SetCompilerType(CompilerType compiler_type); 560 561 bool IsEmpty() const; 562 563 bool HasName() const; 564 565 bool HasTypeSP() const; 566 567 bool HasCompilerType() const; 568 569 bool HasType() const { return HasTypeSP() || HasCompilerType(); } 570 571 void Clear(); 572 573 explicit operator bool() { return !IsEmpty(); } 574 575private: 576 TypePair m_type_pair; 577 ConstString m_type_name; 578}; 579 580class TypeMemberFunctionImpl { 581public: 582 TypeMemberFunctionImpl() 583 : m_type(), m_decl(), m_name(), m_kind(lldb::eMemberFunctionKindUnknown) { 584 } 585 586 TypeMemberFunctionImpl(const CompilerType &type, const CompilerDecl &decl, 587 const std::string &name, 588 const lldb::MemberFunctionKind &kind) 589 : m_type(type), m_decl(decl), m_name(name), m_kind(kind) {} 590 591 bool IsValid(); 592 593 ConstString GetName() const; 594 595 ConstString GetMangledName() const; 596 597 CompilerType GetType() const; 598 599 CompilerType GetReturnType() const; 600 601 size_t GetNumArguments() const; 602 603 CompilerType GetArgumentAtIndex(size_t idx) const; 604 605 lldb::MemberFunctionKind GetKind() const; 606 607 bool GetDescription(Stream &stream); 608 609protected: 610 std::string GetPrintableTypeName(); 611 612private: 613 CompilerType m_type; 614 CompilerDecl m_decl; 615 ConstString m_name; 616 lldb::MemberFunctionKind m_kind; 617}; 618 619class TypeEnumMemberImpl { 620public: 621 TypeEnumMemberImpl() 622 : m_integer_type_sp(), m_name("<invalid>"), m_value(), m_valid(false) {} 623 624 TypeEnumMemberImpl(const lldb::TypeImplSP &integer_type_sp, 625 const ConstString &name, const llvm::APSInt &value); 626 627 TypeEnumMemberImpl(const TypeEnumMemberImpl &rhs) 628 : m_integer_type_sp(rhs.m_integer_type_sp), m_name(rhs.m_name), 629 m_value(rhs.m_value), m_valid(rhs.m_valid) {} 630 631 TypeEnumMemberImpl &operator=(const TypeEnumMemberImpl &rhs); 632 633 bool IsValid() { return m_valid; } 634 635 const ConstString &GetName() const { return m_name; } 636 637 const lldb::TypeImplSP &GetIntegerType() const { return m_integer_type_sp; } 638 639 uint64_t GetValueAsUnsigned() const { return m_value.getZExtValue(); } 640 641 int64_t GetValueAsSigned() const { return m_value.getSExtValue(); } 642 643protected: 644 lldb::TypeImplSP m_integer_type_sp; 645 ConstString m_name; 646 llvm::APSInt m_value; 647 bool m_valid; 648}; 649 650class TypeEnumMemberListImpl { 651public: 652 TypeEnumMemberListImpl() : m_content() {} 653 654 void Append(const lldb::TypeEnumMemberImplSP &type) { 655 m_content.push_back(type); 656 } 657 658 void Append(const lldb_private::TypeEnumMemberListImpl &type_list); 659 660 lldb::TypeEnumMemberImplSP GetTypeEnumMemberAtIndex(size_t idx) { 661 lldb::TypeEnumMemberImplSP enum_member; 662 if (idx < GetSize()) 663 enum_member = m_content[idx]; 664 return enum_member; 665 } 666 667 size_t GetSize() { return m_content.size(); } 668 669private: 670 std::vector<lldb::TypeEnumMemberImplSP> m_content; 671}; 672 673} // namespace lldb_private 674 675#endif // liblldb_Type_h_ 676