Type.h revision 8c3391ba76c5e3440a0cc10296dbde1b44300509
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/lldb-private.h" 14#include "lldb/Core/ClangForward.h" 15#include "lldb/Core/ConstString.h" 16#include "lldb/Core/UserID.h" 17#include "lldb/Symbol/ClangASTType.h" 18#include "lldb/Symbol/Declaration.h" 19#include <set> 20 21namespace lldb_private { 22 23class SymbolFileType : 24 public std::enable_shared_from_this<SymbolFileType>, 25 public UserID 26 { 27 public: 28 SymbolFileType (SymbolFile &symbol_file, lldb::user_id_t uid) : 29 UserID (uid), 30 m_symbol_file (symbol_file) 31 { 32 } 33 34 ~SymbolFileType () 35 { 36 } 37 38 Type * 39 operator->() 40 { 41 return GetType (); 42 } 43 44 Type * 45 GetType (); 46 47 protected: 48 SymbolFile &m_symbol_file; 49 lldb::TypeSP m_type_sp; 50 }; 51 52class Type : 53 public std::enable_shared_from_this<Type>, 54 public UserID 55{ 56public: 57 typedef enum EncodingDataTypeTag 58 { 59 eEncodingInvalid, 60 eEncodingIsUID, ///< This type is the type whose UID is m_encoding_uid 61 eEncodingIsConstUID, ///< This type is the type whose UID is m_encoding_uid with the const qualifier added 62 eEncodingIsRestrictUID, ///< This type is the type whose UID is m_encoding_uid with the restrict qualifier added 63 eEncodingIsVolatileUID, ///< This type is the type whose UID is m_encoding_uid with the volatile qualifier added 64 eEncodingIsTypedefUID, ///< This type is pointer to a type whose UID is m_encoding_uid 65 eEncodingIsPointerUID, ///< This type is pointer to a type whose UID is m_encoding_uid 66 eEncodingIsLValueReferenceUID, ///< This type is L value reference to a type whose UID is m_encoding_uid 67 eEncodingIsRValueReferenceUID, ///< This type is R value reference to a type whose UID is m_encoding_uid 68 eEncodingIsSyntheticUID 69 } EncodingDataType; 70 71 typedef enum ResolveStateTag 72 { 73 eResolveStateUnresolved = 0, 74 eResolveStateForward = 1, 75 eResolveStateLayout = 2, 76 eResolveStateFull = 3 77 } ResolveState; 78 79 Type (lldb::user_id_t uid, 80 SymbolFile* symbol_file, 81 const ConstString &name, 82 uint64_t byte_size, 83 SymbolContextScope *context, 84 lldb::user_id_t encoding_uid, 85 EncodingDataType encoding_uid_type, 86 const Declaration& decl, 87 lldb::clang_type_t clang_qual_type, 88 ResolveState clang_type_resolve_state); 89 90 // This makes an invalid type. Used for functions that return a Type when they 91 // get an error. 92 Type(); 93 94 Type (const Type &rhs); 95 96 const Type& 97 operator= (const Type& rhs); 98 99 void 100 Dump(Stream *s, bool show_context); 101 102 void 103 DumpTypeName(Stream *s); 104 105 106 void 107 GetDescription (Stream *s, lldb::DescriptionLevel level, bool show_name); 108 109 SymbolFile * 110 GetSymbolFile() 111 { 112 return m_symbol_file; 113 } 114 const SymbolFile * 115 GetSymbolFile() const 116 { 117 return m_symbol_file; 118 } 119 120 TypeList* 121 GetTypeList(); 122 123 const ConstString& 124 GetName(); 125 126 uint64_t 127 GetByteSize(); 128 129 uint32_t 130 GetNumChildren (bool omit_empty_base_classes); 131 132 bool 133 IsAggregateType (); 134 135 bool 136 IsValidType () 137 { 138 return m_encoding_uid_type != eEncodingInvalid; 139 } 140 141 bool 142 IsTypedef () 143 { 144 return m_encoding_uid_type == eEncodingIsTypedefUID; 145 } 146 147 lldb::TypeSP 148 GetTypedefType(); 149 150 const ConstString & 151 GetName () const 152 { 153 return m_name; 154 } 155 156 ConstString 157 GetQualifiedName (); 158 159 void 160 DumpValue(ExecutionContext *exe_ctx, 161 Stream *s, 162 const DataExtractor &data, 163 uint32_t data_offset, 164 bool show_type, 165 bool show_summary, 166 bool verbose, 167 lldb::Format format = lldb::eFormatDefault); 168 169 bool 170 DumpValueInMemory(ExecutionContext *exe_ctx, 171 Stream *s, 172 lldb::addr_t address, 173 AddressType address_type, 174 bool show_types, 175 bool show_summary, 176 bool verbose); 177 178 bool 179 ReadFromMemory (ExecutionContext *exe_ctx, 180 lldb::addr_t address, 181 AddressType address_type, 182 DataExtractor &data); 183 184 bool 185 WriteToMemory (ExecutionContext *exe_ctx, 186 lldb::addr_t address, 187 AddressType address_type, 188 DataExtractor &data); 189 190 bool 191 GetIsDeclaration() const; 192 193 void 194 SetIsDeclaration(bool b); 195 196 bool 197 GetIsExternal() const; 198 199 void 200 SetIsExternal(bool b); 201 202 lldb::Format 203 GetFormat (); 204 205 lldb::Encoding 206 GetEncoding (uint64_t &count); 207 208 SymbolContextScope * 209 GetSymbolContextScope() 210 { 211 return m_context; 212 } 213 const SymbolContextScope * 214 GetSymbolContextScope() const 215 { 216 return m_context; 217 } 218 void 219 SetSymbolContextScope(SymbolContextScope *context) 220 { 221 m_context = context; 222 } 223 224 const lldb_private::Declaration & 225 GetDeclaration () const; 226 227 // Get the clang type, and resolve definitions for any 228 // class/struct/union/enum types completely. 229 lldb::clang_type_t 230 GetClangFullType (); 231 232 // Get the clang type, and resolve definitions enough so that the type could 233 // have layout performed. This allows ptrs and refs to class/struct/union/enum 234 // types remain forward declarations. 235 lldb::clang_type_t 236 GetClangLayoutType (); 237 238 // Get the clang type and leave class/struct/union/enum types as forward 239 // declarations if they haven't already been fully defined. 240 lldb::clang_type_t 241 GetClangForwardType (); 242 243 clang::ASTContext * 244 GetClangAST (); 245 246 ClangASTContext & 247 GetClangASTContext (); 248 249 static int 250 Compare(const Type &a, const Type &b); 251 252 // From a fully qualified typename, split the type into the type basename 253 // and the remaining type scope (namespaces/classes). 254 static bool 255 GetTypeScopeAndBasename (const char* &name_cstr, 256 std::string &scope, 257 std::string &basename, 258 lldb::TypeClass &type_class); 259 void 260 SetEncodingType (Type *encoding_type) 261 { 262 m_encoding_type = encoding_type; 263 } 264 265 uint32_t 266 GetEncodingMask (); 267 268 void * 269 CreateClangPointerType (Type *type); 270 271 void * 272 CreateClangTypedefType (Type *typedef_type, Type *base_type); 273 274 // For C++98 references (&) 275 void * 276 CreateClangLValueReferenceType (Type *type); 277 278 // For C++0x references (&&) 279 void * 280 CreateClangRValueReferenceType (Type *type); 281 282 bool 283 IsRealObjCClass(); 284 285 bool 286 IsCompleteObjCClass() 287 { 288 return m_flags.is_complete_objc_class; 289 } 290 291 void 292 SetIsCompleteObjCClass(bool is_complete_objc_class) 293 { 294 m_flags.is_complete_objc_class = is_complete_objc_class; 295 } 296 297protected: 298 ConstString m_name; 299 SymbolFile *m_symbol_file; 300 SymbolContextScope *m_context; // The symbol context in which this type is defined 301 Type *m_encoding_type; 302 lldb::user_id_t m_encoding_uid; 303 EncodingDataType m_encoding_uid_type; 304 uint64_t m_byte_size; 305 Declaration m_decl; 306 lldb::clang_type_t m_clang_type; 307 308 struct Flags { 309 ResolveState clang_type_resolve_state : 2; 310 bool is_complete_objc_class : 1; 311 } m_flags; 312 313 Type * 314 GetEncodingType (); 315 316 bool 317 ResolveClangType (ResolveState clang_type_resolve_state); 318}; 319 320 321/// 322/// Sometimes you can find the name of the type corresponding to an object, but we don't have debug 323/// information for it. If that is the case, you can return one of these objects, and then if it 324/// has a full type, you can use that, but if not at least you can print the name for informational 325/// purposes. 326/// 327 328class TypeAndOrName 329{ 330public: 331 TypeAndOrName (); 332 TypeAndOrName (lldb::TypeSP &type_sp); 333 TypeAndOrName (const char *type_str); 334 TypeAndOrName (const TypeAndOrName &rhs); 335 TypeAndOrName (ConstString &type_const_string); 336 337 TypeAndOrName & 338 operator= (const TypeAndOrName &rhs); 339 340 bool 341 operator==(const TypeAndOrName &other) const; 342 343 bool 344 operator!=(const TypeAndOrName &other) const; 345 346 ConstString GetName () const; 347 348 lldb::TypeSP 349 GetTypeSP () const 350 { 351 return m_type_sp; 352 } 353 354 void 355 SetName (const ConstString &type_name); 356 357 void 358 SetName (const char *type_name_cstr); 359 360 void 361 SetTypeSP (lldb::TypeSP type_sp); 362 363 bool 364 IsEmpty (); 365 366 bool 367 HasName (); 368 369 bool 370 HasTypeSP (); 371 372 void 373 Clear (); 374 375 operator 376 bool () 377 { 378 return !IsEmpty(); 379 } 380 381private: 382 lldb::TypeSP m_type_sp; 383 ConstString m_type_name; 384}; 385 386// the two classes here are used by the public API as a backend to 387// the SBType and SBTypeList classes 388 389class TypeImpl 390{ 391public: 392 393 TypeImpl() : 394 m_clang_ast_type(), 395 m_type_sp() 396 { 397 } 398 399 TypeImpl(const TypeImpl& rhs) : 400 m_clang_ast_type(rhs.m_clang_ast_type), 401 m_type_sp(rhs.m_type_sp) 402 { 403 } 404 405 TypeImpl(const lldb_private::ClangASTType& type); 406 407 TypeImpl(const lldb::TypeSP& type); 408 409 TypeImpl& 410 operator = (const TypeImpl& rhs); 411 412 bool 413 operator == (const TypeImpl& rhs) 414 { 415 return m_clang_ast_type == rhs.m_clang_ast_type && m_type_sp.get() == rhs.m_type_sp.get(); 416 } 417 418 bool 419 operator != (const TypeImpl& rhs) 420 { 421 return m_clang_ast_type != rhs.m_clang_ast_type || m_type_sp.get() != rhs.m_type_sp.get(); 422 } 423 424 bool 425 IsValid() 426 { 427 return m_type_sp.get() != NULL || m_clang_ast_type.IsValid(); 428 } 429 430 const lldb_private::ClangASTType & 431 GetClangASTType() const 432 { 433 return m_clang_ast_type; 434 } 435 436 clang::ASTContext* 437 GetASTContext(); 438 439 lldb::clang_type_t 440 GetOpaqueQualType(); 441 442 lldb::TypeSP 443 GetTypeSP () 444 { 445 return m_type_sp; 446 } 447 448 ConstString 449 GetName (); 450 451 bool 452 GetDescription (lldb_private::Stream &strm, 453 lldb::DescriptionLevel description_level); 454 455 void 456 SetType (const lldb::TypeSP &type_sp); 457 458private: 459 ClangASTType m_clang_ast_type; 460 lldb::TypeSP m_type_sp; 461}; 462 463class TypeListImpl 464{ 465public: 466 TypeListImpl() : 467 m_content() 468 { 469 } 470 471 void 472 Append (const lldb::TypeImplSP& type) 473 { 474 m_content.push_back(type); 475 } 476 477 lldb::TypeImplSP 478 GetTypeAtIndex(size_t idx) 479 { 480 lldb::TypeImplSP type_sp; 481 if (idx < GetSize()) 482 type_sp = m_content[idx]; 483 return type_sp; 484 } 485 486 size_t 487 GetSize() 488 { 489 return m_content.size(); 490 } 491 492private: 493 std::vector<lldb::TypeImplSP> m_content; 494}; 495 496class TypeMemberImpl 497{ 498public: 499 TypeMemberImpl () : 500 m_type_impl_sp (), 501 m_bit_offset (0), 502 m_name (), 503 m_bitfield_bit_size (0), 504 m_is_bitfield (false) 505 506 { 507 } 508 509 TypeMemberImpl (const lldb::TypeImplSP &type_impl_sp, 510 uint64_t bit_offset, 511 const ConstString &name, 512 uint32_t bitfield_bit_size = 0, 513 bool is_bitfield = false) : 514 m_type_impl_sp (type_impl_sp), 515 m_bit_offset (bit_offset), 516 m_name (name), 517 m_bitfield_bit_size (bitfield_bit_size), 518 m_is_bitfield (is_bitfield) 519 { 520 } 521 522 TypeMemberImpl (const lldb::TypeImplSP &type_impl_sp, 523 uint64_t bit_offset): 524 m_type_impl_sp (type_impl_sp), 525 m_bit_offset (bit_offset), 526 m_name (), 527 m_bitfield_bit_size (0), 528 m_is_bitfield (false) 529 { 530 if (m_type_impl_sp) 531 m_name = m_type_impl_sp->GetName(); 532 } 533 534 const lldb::TypeImplSP & 535 GetTypeImpl () 536 { 537 return m_type_impl_sp; 538 } 539 540 const ConstString & 541 GetName () const 542 { 543 return m_name; 544 } 545 546 uint64_t 547 GetBitOffset () const 548 { 549 return m_bit_offset; 550 } 551 552 uint32_t 553 GetBitfieldBitSize () const 554 { 555 return m_bitfield_bit_size; 556 } 557 558 void 559 SetBitfieldBitSize (uint32_t bitfield_bit_size) 560 { 561 m_bitfield_bit_size = bitfield_bit_size; 562 } 563 564 bool 565 GetIsBitfield () const 566 { 567 return m_is_bitfield; 568 } 569 570 void 571 SetIsBitfield (bool is_bitfield) 572 { 573 m_is_bitfield = is_bitfield; 574 } 575 576protected: 577 lldb::TypeImplSP m_type_impl_sp; 578 uint64_t m_bit_offset; 579 ConstString m_name; 580 uint32_t m_bitfield_bit_size; // Bit size for bitfield members only 581 bool m_is_bitfield; 582}; 583 584 585} // namespace lldb_private 586 587#endif // liblldb_Type_h_ 588 589