1 //===-- ClangASTType.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_ClangASTType_h_ 11 #define liblldb_ClangASTType_h_ 12 13 #include <string> 14 #include "lldb/lldb-private.h" 15 #include "lldb/Core/ClangForward.h" 16 #include "clang/AST/Type.h" 17 18 namespace lldb_private { 19 20 //---------------------------------------------------------------------- 21 // A class that can carry around a clang ASTContext and a opaque clang 22 // QualType. A clang::QualType can be easily reconstructed from an 23 // opaque clang type and often the ASTContext is needed when doing 24 // various type related tasks, so this class allows both items to travel 25 // in a single very lightweight class that can be used. There are many 26 // static equivalents of the member functions that allow the ASTContext 27 // and the opaque clang QualType to be specified for ease of use and 28 // to avoid code duplication. 29 //---------------------------------------------------------------------- 30 class ClangASTType 31 { 32 public: 33 //---------------------------------------------------------------------- 34 // Constructors and Destructors 35 //---------------------------------------------------------------------- ClangASTType(clang::ASTContext * ast_context,lldb::clang_type_t type)36 ClangASTType (clang::ASTContext *ast_context, lldb::clang_type_t type) : 37 m_type (type), 38 m_ast (ast_context) 39 { 40 } 41 42 ClangASTType (clang::ASTContext *ast_context, clang::QualType qual_type); 43 ClangASTType(const ClangASTType & rhs)44 ClangASTType (const ClangASTType &rhs) : 45 m_type (rhs.m_type), 46 m_ast (rhs.m_ast) 47 { 48 } 49 ClangASTType()50 ClangASTType () : 51 m_type (0), 52 m_ast (0) 53 { 54 } 55 56 ~ClangASTType(); 57 58 //---------------------------------------------------------------------- 59 // Operators 60 //---------------------------------------------------------------------- 61 62 const ClangASTType & 63 operator= (const ClangASTType &rhs) 64 { 65 m_type = rhs.m_type; 66 m_ast = rhs.m_ast; 67 return *this; 68 } 69 70 71 //---------------------------------------------------------------------- 72 // Tests 73 //---------------------------------------------------------------------- 74 75 explicit operator bool () const 76 { 77 return m_type != NULL && m_ast != NULL; 78 } 79 80 bool 81 operator < (const ClangASTType &rhs) const 82 { 83 if (m_ast == rhs.m_ast) 84 return m_type < rhs.m_type; 85 return m_ast < rhs.m_ast; 86 } 87 88 bool IsValid()89 IsValid () const 90 { 91 return m_type != NULL && m_ast != NULL; 92 } 93 94 bool 95 IsArrayType (ClangASTType *element_type, 96 uint64_t *size, 97 bool *is_incomplete) const; 98 99 bool 100 IsVectorType (ClangASTType *element_type, 101 uint64_t *size) const; 102 103 bool 104 IsArrayOfScalarType () const; 105 106 bool 107 IsAggregateType () const; 108 109 bool 110 IsBeingDefined () const; 111 112 bool 113 IsCharType () const; 114 115 bool 116 IsCompleteType () const; 117 118 bool 119 IsConst() const; 120 121 bool 122 IsCStringType (uint32_t &length) const; 123 124 bool 125 IsCXXClassType () const; 126 127 bool 128 IsDefined() const; 129 130 bool 131 IsFloatingPointType (uint32_t &count, bool &is_complex) const; 132 133 bool 134 IsFunctionType (bool *is_variadic_ptr = NULL) const; 135 136 uint32_t 137 IsHomogeneousAggregate (ClangASTType* base_type_ptr) const; 138 139 size_t 140 GetNumberOfFunctionArguments () const; 141 142 ClangASTType 143 GetFunctionArgumentAtIndex (const size_t index) const; 144 145 bool 146 IsVariadicFunctionType () const; 147 148 bool 149 IsFunctionPointerType () const; 150 151 bool 152 IsIntegerType (bool &is_signed) const; 153 154 bool 155 IsObjCClassType () const; 156 157 bool 158 IsObjCClassTypeAndHasIVars (bool check_superclass) const; 159 160 bool 161 IsObjCObjectOrInterfaceType () const; 162 163 bool 164 IsObjCObjectPointerType (ClangASTType *target_type = NULL); 165 166 bool 167 IsPolymorphicClass () const; 168 169 bool 170 IsPossibleCPlusPlusDynamicType (ClangASTType *target_type = NULL) const 171 { 172 return IsPossibleDynamicType (target_type, true, false); 173 } 174 175 bool 176 IsPossibleDynamicType (ClangASTType *target_type, // Can pass NULL 177 bool check_cplusplus, 178 bool check_objc) const; 179 180 181 bool 182 IsPointerToScalarType () const; 183 184 bool 185 IsRuntimeGeneratedType () const; 186 187 bool 188 IsPointerType (ClangASTType *pointee_type = NULL) const; 189 190 bool 191 IsPointerOrReferenceType (ClangASTType *pointee_type = NULL) const; 192 193 bool 194 IsReferenceType (ClangASTType *pointee_type = nullptr, bool* is_rvalue = nullptr) const; 195 196 bool 197 IsScalarType () const; 198 199 bool 200 IsTypedefType () const; 201 202 bool 203 IsVoidType () const; 204 205 bool 206 GetCXXClassName (std::string &class_name) const; 207 208 bool 209 GetObjCClassName (std::string &class_name); 210 211 212 //---------------------------------------------------------------------- 213 // Type Completion 214 //---------------------------------------------------------------------- 215 216 bool 217 GetCompleteType () const; 218 219 //---------------------------------------------------------------------- 220 // AST related queries 221 //---------------------------------------------------------------------- 222 223 size_t 224 GetPointerByteSize () const; 225 226 //---------------------------------------------------------------------- 227 // Accessors 228 //---------------------------------------------------------------------- 229 230 clang::ASTContext * GetASTContext()231 GetASTContext() const 232 { 233 return m_ast; 234 } 235 236 ConstString 237 GetConstQualifiedTypeName () const; 238 239 ConstString 240 GetConstTypeName () const; 241 242 ConstString 243 GetTypeName () const; 244 245 ConstString 246 GetDisplayTypeName () const; 247 248 uint32_t 249 GetTypeInfo (ClangASTType *pointee_or_element_clang_type = NULL) const; 250 251 lldb::LanguageType 252 GetMinimumLanguage (); 253 254 lldb::clang_type_t GetOpaqueQualType()255 GetOpaqueQualType() const 256 { 257 return m_type; 258 } 259 260 lldb::TypeClass 261 GetTypeClass () const; 262 263 void SetClangType(clang::ASTContext * ast,lldb::clang_type_t type)264 SetClangType (clang::ASTContext *ast, lldb::clang_type_t type) 265 { 266 m_ast = ast; 267 m_type = type; 268 } 269 270 void 271 SetClangType (clang::ASTContext *ast, clang::QualType qual_type); 272 273 unsigned 274 GetTypeQualifiers() const; 275 276 //---------------------------------------------------------------------- 277 // Creating related types 278 //---------------------------------------------------------------------- 279 280 ClangASTType 281 AddConstModifier () const; 282 283 ClangASTType 284 AddRestrictModifier () const; 285 286 ClangASTType 287 AddVolatileModifier () const; 288 289 // Using the current type, create a new typedef to that type using "typedef_name" 290 // as the name and "decl_ctx" as the decl context. 291 ClangASTType 292 CreateTypedefType (const char *typedef_name, 293 clang::DeclContext *decl_ctx) const; 294 295 ClangASTType 296 GetArrayElementType (uint64_t *stride = nullptr) const; 297 298 ClangASTType 299 GetCanonicalType () const; 300 301 ClangASTType 302 GetFullyUnqualifiedType () const; 303 304 // Returns -1 if this isn't a function of if the function doesn't have a prototype 305 // Returns a value >= 0 if there is a prototype. 306 int 307 GetFunctionArgumentCount () const; 308 309 ClangASTType 310 GetFunctionArgumentTypeAtIndex (size_t idx) const; 311 312 ClangASTType 313 GetFunctionReturnType () const; 314 315 size_t 316 GetNumMemberFunctions () const; 317 318 TypeMemberFunctionImpl 319 GetMemberFunctionAtIndex (size_t idx); 320 321 ClangASTType 322 GetLValueReferenceType () const; 323 324 ClangASTType 325 GetNonReferenceType () const; 326 327 ClangASTType 328 GetPointeeType () const; 329 330 ClangASTType 331 GetPointerType () const; 332 333 ClangASTType 334 GetRValueReferenceType () const; 335 336 // If the current object represents a typedef type, get the underlying type 337 ClangASTType 338 GetTypedefedType () const; 339 340 ClangASTType 341 RemoveFastQualifiers () const; 342 343 //---------------------------------------------------------------------- 344 // Create related types using the current type's AST 345 //---------------------------------------------------------------------- 346 ClangASTType 347 GetBasicTypeFromAST (lldb::BasicType basic_type) const; 348 349 //---------------------------------------------------------------------- 350 // Exploring the type 351 //---------------------------------------------------------------------- 352 353 uint64_t 354 GetByteSize (ExecutionContextScope *exe_scope) const; 355 356 uint64_t 357 GetBitSize (ExecutionContextScope *exe_scope) const; 358 359 lldb::Encoding 360 GetEncoding (uint64_t &count) const; 361 362 lldb::Format 363 GetFormat () const; 364 365 size_t 366 GetTypeBitAlign () const; 367 368 uint32_t 369 GetNumChildren (bool omit_empty_base_classes) const; 370 371 lldb::BasicType 372 GetBasicTypeEnumeration () const; 373 374 static lldb::BasicType 375 GetBasicTypeEnumeration (const ConstString &name); 376 377 uint32_t 378 GetNumDirectBaseClasses () const; 379 380 uint32_t 381 GetNumVirtualBaseClasses () const; 382 383 uint32_t 384 GetNumFields () const; 385 386 ClangASTType 387 GetDirectBaseClassAtIndex (size_t idx, 388 uint32_t *bit_offset_ptr) const; 389 390 ClangASTType 391 GetVirtualBaseClassAtIndex (size_t idx, 392 uint32_t *bit_offset_ptr) const; 393 394 ClangASTType 395 GetFieldAtIndex (size_t idx, 396 std::string& name, 397 uint64_t *bit_offset_ptr, 398 uint32_t *bitfield_bit_size_ptr, 399 bool *is_bitfield_ptr) const; 400 401 uint32_t 402 GetIndexOfFieldWithName (const char* name, 403 ClangASTType* field_clang_type = NULL, 404 uint64_t *bit_offset_ptr = NULL, 405 uint32_t *bitfield_bit_size_ptr = NULL, 406 bool *is_bitfield_ptr = NULL) const; 407 408 uint32_t 409 GetNumPointeeChildren () const; 410 411 ClangASTType 412 GetChildClangTypeAtIndex (ExecutionContext *exe_ctx, 413 size_t idx, 414 bool transparent_pointers, 415 bool omit_empty_base_classes, 416 bool ignore_array_bounds, 417 std::string& child_name, 418 uint32_t &child_byte_size, 419 int32_t &child_byte_offset, 420 uint32_t &child_bitfield_bit_size, 421 uint32_t &child_bitfield_bit_offset, 422 bool &child_is_base_class, 423 bool &child_is_deref_of_parent, 424 ValueObject *valobj) const; 425 426 // Lookup a child given a name. This function will match base class names 427 // and member member names in "clang_type" only, not descendants. 428 uint32_t 429 GetIndexOfChildWithName (const char *name, 430 bool omit_empty_base_classes) const; 431 432 // Lookup a child member given a name. This function will match member names 433 // only and will descend into "clang_type" children in search for the first 434 // member in this class, or any base class that matches "name". 435 // TODO: Return all matches for a given name by returning a vector<vector<uint32_t>> 436 // so we catch all names that match a given child name, not just the first. 437 size_t 438 GetIndexOfChildMemberWithName (const char *name, 439 bool omit_empty_base_classes, 440 std::vector<uint32_t>& child_indexes) const; 441 442 size_t 443 GetNumTemplateArguments () const; 444 445 ClangASTType 446 GetTemplateArgument (size_t idx, 447 lldb::TemplateArgumentKind &kind) const; 448 449 450 //---------------------------------------------------------------------- 451 // Modifying RecordType 452 //---------------------------------------------------------------------- 453 clang::FieldDecl * 454 AddFieldToRecordType (const char *name, 455 const ClangASTType &field_type, 456 lldb::AccessType access, 457 uint32_t bitfield_bit_size); 458 459 void 460 BuildIndirectFields (); 461 462 void 463 SetIsPacked (); 464 465 clang::VarDecl * 466 AddVariableToRecordType (const char *name, 467 const ClangASTType &var_type, 468 lldb::AccessType access); 469 470 clang::CXXMethodDecl * 471 AddMethodToCXXRecordType (const char *name, 472 const ClangASTType &method_type, 473 lldb::AccessType access, 474 bool is_virtual, 475 bool is_static, 476 bool is_inline, 477 bool is_explicit, 478 bool is_attr_used, 479 bool is_artificial); 480 481 // C++ Base Classes 482 clang::CXXBaseSpecifier * 483 CreateBaseClassSpecifier (lldb::AccessType access, 484 bool is_virtual, 485 bool base_of_class); 486 487 static void 488 DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes, 489 unsigned num_base_classes); 490 491 bool 492 SetBaseClassesForClassType (clang::CXXBaseSpecifier const * const *base_classes, 493 unsigned num_base_classes); 494 495 496 bool 497 SetObjCSuperClass (const ClangASTType &superclass_clang_type); 498 499 bool 500 AddObjCClassProperty (const char *property_name, 501 const ClangASTType &property_clang_type, 502 clang::ObjCIvarDecl *ivar_decl, 503 const char *property_setter_name, 504 const char *property_getter_name, 505 uint32_t property_attributes, 506 ClangASTMetadata *metadata); 507 508 clang::ObjCMethodDecl * 509 AddMethodToObjCObjectType (const char *name, // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]") 510 const ClangASTType &method_clang_type, 511 lldb::AccessType access, 512 bool is_artificial); 513 514 clang::DeclContext * 515 GetDeclContextForType () const; 516 517 518 bool 519 SetDefaultAccessForRecordFields (int default_accessibility, 520 int *assigned_accessibilities, 521 size_t num_assigned_accessibilities); 522 523 bool 524 SetHasExternalStorage (bool has_extern); 525 526 527 //------------------------------------------------------------------ 528 // clang::TagType 529 //------------------------------------------------------------------ 530 531 bool 532 SetTagTypeKind (int kind) const; 533 534 //------------------------------------------------------------------ 535 // Tag Declarations 536 //------------------------------------------------------------------ 537 bool 538 StartTagDeclarationDefinition (); 539 540 bool 541 CompleteTagDeclarationDefinition (); 542 543 //---------------------------------------------------------------------- 544 // Modifying Enumeration types 545 //---------------------------------------------------------------------- 546 bool 547 AddEnumerationValueToEnumerationType (const ClangASTType &enumerator_qual_type, 548 const Declaration &decl, 549 const char *name, 550 int64_t enum_value, 551 uint32_t enum_value_bit_size); 552 553 554 555 ClangASTType 556 GetEnumerationIntegerType () const; 557 558 559 //------------------------------------------------------------------ 560 // Pointers & References 561 //------------------------------------------------------------------ 562 563 // Call this function using the class type when you want to make a 564 // member pointer type to pointee_type. 565 ClangASTType 566 CreateMemberPointerType (const ClangASTType &pointee_type) const; 567 568 569 // Converts "s" to a floating point value and place resulting floating 570 // point bytes in the "dst" buffer. 571 size_t 572 ConvertStringToFloatValue (const char *s, 573 uint8_t *dst, 574 size_t dst_size) const; 575 //---------------------------------------------------------------------- 576 // Dumping types 577 //---------------------------------------------------------------------- 578 void 579 DumpValue (ExecutionContext *exe_ctx, 580 Stream *s, 581 lldb::Format format, 582 const DataExtractor &data, 583 lldb::offset_t data_offset, 584 size_t data_byte_size, 585 uint32_t bitfield_bit_size, 586 uint32_t bitfield_bit_offset, 587 bool show_types, 588 bool show_summary, 589 bool verbose, 590 uint32_t depth); 591 592 bool 593 DumpTypeValue (Stream *s, 594 lldb::Format format, 595 const DataExtractor &data, 596 lldb::offset_t data_offset, 597 size_t data_byte_size, 598 uint32_t bitfield_bit_size, 599 uint32_t bitfield_bit_offset, 600 ExecutionContextScope *exe_scope); 601 602 void 603 DumpSummary (ExecutionContext *exe_ctx, 604 Stream *s, 605 const DataExtractor &data, 606 lldb::offset_t data_offset, 607 size_t data_byte_size); 608 609 void 610 DumpTypeDescription () const; // Dump to stdout 611 612 void 613 DumpTypeDescription (Stream *s) const; 614 615 bool 616 GetValueAsScalar (const DataExtractor &data, 617 lldb::offset_t data_offset, 618 size_t data_byte_size, 619 Scalar &value) const; 620 621 bool 622 SetValueFromScalar (const Scalar &value, 623 Stream &strm); 624 625 bool 626 ReadFromMemory (ExecutionContext *exe_ctx, 627 lldb::addr_t addr, 628 AddressType address_type, 629 DataExtractor &data); 630 631 bool 632 WriteToMemory (ExecutionContext *exe_ctx, 633 lldb::addr_t addr, 634 AddressType address_type, 635 StreamString &new_value); 636 637 clang::EnumDecl * 638 GetAsEnumDecl () const; 639 640 641 clang::RecordDecl * 642 GetAsRecordDecl () const; 643 644 clang::CXXRecordDecl * 645 GetAsCXXRecordDecl () const; 646 647 clang::ObjCInterfaceDecl * 648 GetAsObjCInterfaceDecl () const; 649 650 void Clear()651 Clear() 652 { 653 m_type = NULL; 654 m_ast = NULL; 655 } 656 657 clang::QualType GetQualType()658 GetQualType () const 659 { 660 if (m_type) 661 return clang::QualType::getFromOpaquePtr(m_type); 662 return clang::QualType(); 663 } 664 clang::QualType GetCanonicalQualType()665 GetCanonicalQualType () const 666 { 667 if (m_type) 668 return clang::QualType::getFromOpaquePtr(m_type).getCanonicalType(); 669 return clang::QualType(); 670 } 671 672 private: 673 lldb::clang_type_t m_type; 674 clang::ASTContext *m_ast; 675 676 }; 677 678 bool operator == (const ClangASTType &lhs, const ClangASTType &rhs); 679 bool operator != (const ClangASTType &lhs, const ClangASTType &rhs); 680 681 682 } // namespace lldb_private 683 684 #endif // #ifndef liblldb_ClangASTType_h_ 685