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