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