ClangASTContext.h revision 1a102087f54079c25c8827afac6153a44ca535da
1//===-- ClangASTContext.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_ClangASTContext_h_ 11#define liblldb_ClangASTContext_h_ 12 13// C Includes 14// C++ Includes 15#include <string> 16#include <vector> 17#include <memory> 18#include <stdint.h> 19 20// Other libraries and framework includes 21#include "llvm/ADT/OwningPtr.h" 22 23// Project includes 24#include "lldb/lldb-enumerations.h" 25#include "lldb/Core/ClangForward.h" 26#include "lldb/Symbol/ClangASTType.h" 27 28namespace lldb_private { 29 30class Declaration; 31 32class ClangASTContext 33{ 34public: 35 enum { 36 eTypeHasChildren = (1u << 0), 37 eTypeHasValue = (1u << 1), 38 eTypeIsArray = (1u << 2), 39 eTypeIsBlock = (1u << 3), 40 eTypeIsBuiltIn = (1u << 4), 41 eTypeIsClass = (1u << 5), 42 eTypeIsCPlusPlus = (1u << 6), 43 eTypeIsEnumeration = (1u << 7), 44 eTypeIsFuncPrototype = (1u << 8), 45 eTypeIsMember = (1u << 9), 46 eTypeIsObjC = (1u << 10), 47 eTypeIsPointer = (1u << 11), 48 eTypeIsReference = (1u << 12), 49 eTypeIsStructUnion = (1u << 13), 50 eTypeIsTemplate = (1u << 14), 51 eTypeIsTypedef = (1u << 15), 52 eTypeIsVector = (1u << 16), 53 eTypeIsScalar = (1u << 17), 54 }; 55 56 typedef void (*CompleteTagDeclCallback)(void *baton, clang::TagDecl *); 57 typedef void (*CompleteObjCInterfaceDeclCallback)(void *baton, clang::ObjCInterfaceDecl *); 58 59 //------------------------------------------------------------------ 60 // Constructors and Destructors 61 //------------------------------------------------------------------ 62 ClangASTContext (const char *triple = NULL); 63 64 ~ClangASTContext(); 65 66 clang::ASTContext * 67 getASTContext(); 68 69 clang::Builtin::Context * 70 getBuiltinContext(); 71 72 clang::IdentifierTable * 73 getIdentifierTable(); 74 75 clang::LangOptions * 76 getLanguageOptions(); 77 78 clang::SelectorTable * 79 getSelectorTable(); 80 81 clang::FileManager * 82 getFileManager(); 83 84 clang::SourceManager * 85 getSourceManager(); 86 87 clang::Diagnostic * 88 getDiagnostic(); 89 90 clang::DiagnosticClient * 91 getDiagnosticClient(); 92 93 clang::TargetOptions * 94 getTargetOptions(); 95 96 clang::TargetInfo * 97 getTargetInfo(); 98 99 void 100 Clear(); 101 102 const char * 103 GetTargetTriple (); 104 105 void 106 SetTargetTriple (const char *target_triple); 107 108 void 109 SetArchitecture (const ArchSpec &arch); 110 111 bool 112 HasExternalSource (); 113 114 void 115 SetExternalSource (llvm::OwningPtr<clang::ExternalASTSource> &ast_source_ap); 116 117 void 118 RemoveExternalSource (); 119 120 bool 121 GetCompleteType (lldb::clang_type_t clang_type); 122 123 static bool 124 GetCompleteType (clang::ASTContext *ast, 125 lldb::clang_type_t clang_type); 126 127 bool 128 GetCompleteDecl (clang::Decl *decl) 129 { 130 return ClangASTContext::GetCompleteDecl(getASTContext(), decl); 131 } 132 133 static bool 134 GetCompleteDecl (clang::ASTContext *ast, 135 clang::Decl *decl); 136 137 //------------------------------------------------------------------ 138 // Basic Types 139 //------------------------------------------------------------------ 140 141 lldb::clang_type_t 142 GetBuiltinTypeForEncodingAndBitSize (lldb::Encoding encoding, 143 uint32_t bit_size); 144 145 static lldb::clang_type_t 146 GetBuiltinTypeForEncodingAndBitSize (clang::ASTContext *ast, 147 lldb::Encoding encoding, 148 uint32_t bit_size); 149 150 lldb::clang_type_t 151 GetBuiltinTypeForDWARFEncodingAndBitSize ( 152 const char *type_name, 153 uint32_t dw_ate, 154 uint32_t bit_size); 155 156 static lldb::clang_type_t 157 GetBuiltInType_void(clang::ASTContext *ast); 158 159 lldb::clang_type_t 160 GetBuiltInType_void() 161 { 162 return GetBuiltInType_void(getASTContext()); 163 } 164 165 lldb::clang_type_t 166 GetBuiltInType_bool(); 167 168 lldb::clang_type_t 169 GetBuiltInType_objc_id(); 170 171 lldb::clang_type_t 172 GetBuiltInType_objc_Class(); 173 174 static lldb::clang_type_t 175 GetUnknownAnyType(clang::ASTContext *ast); 176 177 lldb::clang_type_t 178 GetUnknownAnyType() 179 { 180 return ClangASTContext::GetUnknownAnyType(getASTContext()); 181 } 182 183 lldb::clang_type_t 184 GetBuiltInType_objc_selector(); 185 186 lldb::clang_type_t 187 GetCStringType(bool is_const); 188 189 lldb::clang_type_t 190 GetVoidPtrType(bool is_const); 191 192 static lldb::clang_type_t 193 GetVoidPtrType(clang::ASTContext *ast, bool is_const); 194 195 static lldb::clang_type_t 196 CopyType(clang::ASTContext *dest_context, 197 clang::ASTContext *source_context, 198 lldb::clang_type_t clang_type); 199 200 static clang::Decl * 201 CopyDecl (clang::ASTContext *dest_context, 202 clang::ASTContext *source_context, 203 clang::Decl *source_decl); 204 205 static bool 206 AreTypesSame(clang::ASTContext *ast, 207 lldb::clang_type_t type1, 208 lldb::clang_type_t type2); 209 210 bool 211 AreTypesSame(lldb::clang_type_t type1, 212 lldb::clang_type_t type2) 213 { 214 return ClangASTContext::AreTypesSame(getASTContext(), type1, type2); 215 } 216 217 218 lldb::clang_type_t 219 GetTypeForDecl (clang::TagDecl *decl); 220 221 lldb::clang_type_t 222 GetTypeForDecl (clang::ObjCInterfaceDecl *objc_decl); 223 224 //------------------------------------------------------------------ 225 // CVR modifiers 226 //------------------------------------------------------------------ 227 228 static lldb::clang_type_t 229 AddConstModifier (lldb::clang_type_t clang_type); 230 231 static lldb::clang_type_t 232 AddRestrictModifier (lldb::clang_type_t clang_type); 233 234 static lldb::clang_type_t 235 AddVolatileModifier (lldb::clang_type_t clang_type); 236 237 //------------------------------------------------------------------ 238 // Structure, Unions, Classes 239 //------------------------------------------------------------------ 240 241 lldb::clang_type_t 242 CreateRecordType (const char *name, 243 int kind, 244 clang::DeclContext *decl_ctx, 245 lldb::LanguageType language); 246 247 static bool 248 AddFieldToRecordType (clang::ASTContext *ast, 249 lldb::clang_type_t record_qual_type, 250 const char *name, 251 lldb::clang_type_t field_type, 252 lldb::AccessType access, 253 uint32_t bitfield_bit_size); 254 255 bool 256 AddFieldToRecordType (lldb::clang_type_t record_qual_type, 257 const char *name, 258 lldb::clang_type_t field_type, 259 lldb::AccessType access, 260 uint32_t bitfield_bit_size) 261 { 262 return ClangASTContext::AddFieldToRecordType (getASTContext(), 263 record_qual_type, 264 name, 265 field_type, 266 access, 267 bitfield_bit_size); 268 } 269 270 static clang::CXXMethodDecl * 271 AddMethodToCXXRecordType (clang::ASTContext *ast, 272 lldb::clang_type_t record_opaque_type, 273 const char *name, 274 lldb::clang_type_t method_type, 275 lldb::AccessType access, 276 bool is_virtual, 277 bool is_static, 278 bool is_inline, 279 bool is_explicit); 280 281 clang::CXXMethodDecl * 282 AddMethodToCXXRecordType (lldb::clang_type_t record_opaque_type, 283 const char *name, 284 lldb::clang_type_t method_type, 285 lldb::AccessType access, 286 bool is_virtual, 287 bool is_static, 288 bool is_inline, 289 bool is_explicit) 290 291 { 292 return ClangASTContext::AddMethodToCXXRecordType (getASTContext(), 293 record_opaque_type, 294 name, 295 method_type, 296 access, 297 is_virtual, 298 is_static, 299 is_inline, 300 is_explicit); 301 } 302 303 static bool 304 CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, 305 uint32_t num_params); 306 307 bool 308 FieldIsBitfield (clang::FieldDecl* field, 309 uint32_t& bitfield_bit_size); 310 311 static bool 312 FieldIsBitfield (clang::ASTContext *ast, 313 clang::FieldDecl* field, 314 uint32_t& bitfield_bit_size); 315 316 static bool 317 RecordHasFields (const clang::RecordDecl *record_decl); 318 319 void 320 SetDefaultAccessForRecordFields (lldb::clang_type_t clang_type, 321 int default_accessibility, 322 int *assigned_accessibilities, 323 size_t num_assigned_accessibilities); 324 325 lldb::clang_type_t 326 CreateObjCClass (const char *name, 327 clang::DeclContext *decl_ctx, 328 bool isForwardDecl, 329 bool isInternal); 330 331 static bool 332 AddObjCClassIVar (clang::ASTContext *ast, 333 lldb::clang_type_t class_opaque_type, 334 const char *name, 335 lldb::clang_type_t ivar_opaque_type, 336 lldb::AccessType access, 337 uint32_t bitfield_bit_size, 338 bool isSynthesized); 339 340 bool 341 AddObjCClassIVar (lldb::clang_type_t class_opaque_type, 342 const char *name, 343 lldb::clang_type_t ivar_opaque_type, 344 lldb::AccessType access, 345 uint32_t bitfield_bit_size, 346 bool isSynthesized) 347 { 348 return ClangASTContext::AddObjCClassIVar (getASTContext(), 349 class_opaque_type, 350 name, 351 ivar_opaque_type, 352 access, 353 bitfield_bit_size, 354 isSynthesized); 355 } 356 357 bool 358 SetObjCSuperClass (lldb::clang_type_t class_clang_type, 359 lldb::clang_type_t superclass_clang_type); 360 361 static bool 362 ObjCTypeHasIVars (lldb::clang_type_t class_clang_type, bool check_superclass); 363 364 static bool 365 ObjCDeclHasIVars (clang::ObjCInterfaceDecl *class_interface_decl, 366 bool check_superclass); 367 368 369 static clang::ObjCMethodDecl * 370 AddMethodToObjCObjectType (clang::ASTContext *ast, 371 lldb::clang_type_t class_opaque_type, 372 const char *name, // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]") 373 lldb::clang_type_t method_opaque_type, 374 lldb::AccessType access); 375 376 clang::ObjCMethodDecl * 377 AddMethodToObjCObjectType (lldb::clang_type_t class_opaque_type, 378 const char *name, // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]") 379 lldb::clang_type_t method_opaque_type, 380 lldb::AccessType access) 381 { 382 return AddMethodToObjCObjectType (getASTContext(), 383 class_opaque_type, 384 name, 385 method_opaque_type, 386 access); 387 } 388 389 static bool 390 SetHasExternalStorage (lldb::clang_type_t clang_type, bool has_extern); 391 392 //------------------------------------------------------------------ 393 // Aggregate Types 394 //------------------------------------------------------------------ 395 static bool 396 IsAggregateType (lldb::clang_type_t clang_type); 397 398 // Returns a mask containing bits from the ClangASTContext::eTypeXXX enumerations 399 static uint32_t 400 GetTypeInfo (lldb::clang_type_t clang_type, 401 clang::ASTContext *ast, // The AST for clang_type (can be NULL) 402 lldb::clang_type_t *pointee_or_element_type); // (can be NULL) 403 404 static uint32_t 405 GetNumChildren (clang::ASTContext *ast, 406 lldb::clang_type_t clang_type, 407 bool omit_empty_base_classes); 408 409 static uint32_t 410 GetNumPointeeChildren (lldb::clang_type_t clang_type); 411 412 lldb::clang_type_t 413 GetChildClangTypeAtIndex (ExecutionContext *exe_ctx, 414 const char *parent_name, 415 lldb::clang_type_t parent_clang_type, 416 uint32_t idx, 417 bool transparent_pointers, 418 bool omit_empty_base_classes, 419 bool ignore_array_bounds, 420 std::string& child_name, 421 uint32_t &child_byte_size, 422 int32_t &child_byte_offset, 423 uint32_t &child_bitfield_bit_size, 424 uint32_t &child_bitfield_bit_offset, 425 bool &child_is_base_class, 426 bool &child_is_deref_of_parent); 427 428 static lldb::clang_type_t 429 GetChildClangTypeAtIndex (ExecutionContext *exe_ctx, 430 clang::ASTContext *ast, 431 const char *parent_name, 432 lldb::clang_type_t parent_clang_type, 433 uint32_t idx, 434 bool transparent_pointers, 435 bool omit_empty_base_classes, 436 bool ignore_array_bounds, 437 std::string& child_name, 438 uint32_t &child_byte_size, 439 int32_t &child_byte_offset, 440 uint32_t &child_bitfield_bit_size, 441 uint32_t &child_bitfield_bit_offset, 442 bool &child_is_base_class, 443 bool &child_is_deref_of_parent); 444 445 // Lookup a child given a name. This function will match base class names 446 // and member member names in "clang_type" only, not descendants. 447 static uint32_t 448 GetIndexOfChildWithName (clang::ASTContext *ast, 449 lldb::clang_type_t clang_type, 450 const char *name, 451 bool omit_empty_base_classes); 452 453 // Lookup a child member given a name. This function will match member names 454 // only and will descend into "clang_type" children in search for the first 455 // member in this class, or any base class that matches "name". 456 // TODO: Return all matches for a given name by returning a vector<vector<uint32_t>> 457 // so we catch all names that match a given child name, not just the first. 458 static size_t 459 GetIndexOfChildMemberWithName (clang::ASTContext *ast, 460 lldb::clang_type_t clang_type, 461 const char *name, 462 bool omit_empty_base_classes, 463 std::vector<uint32_t>& child_indexes); 464 465 //------------------------------------------------------------------ 466 // clang::TagType 467 //------------------------------------------------------------------ 468 469 bool 470 SetTagTypeKind (lldb::clang_type_t tag_qual_type, 471 int kind); 472 473 //------------------------------------------------------------------ 474 // C++ Base Classes 475 //------------------------------------------------------------------ 476 477 clang::CXXBaseSpecifier * 478 CreateBaseClassSpecifier (lldb::clang_type_t base_class_type, 479 lldb::AccessType access, 480 bool is_virtual, 481 bool base_of_class); 482 483 static void 484 DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes, 485 unsigned num_base_classes); 486 487 bool 488 SetBaseClassesForClassType (lldb::clang_type_t class_clang_type, 489 clang::CXXBaseSpecifier const * const *base_classes, 490 unsigned num_base_classes); 491 492 //------------------------------------------------------------------ 493 // DeclContext Functions 494 //------------------------------------------------------------------ 495 496 static clang::DeclContext * 497 GetDeclContextForType (lldb::clang_type_t qual_type); 498 499 //------------------------------------------------------------------ 500 // Namespace Declarations 501 //------------------------------------------------------------------ 502 503 clang::NamespaceDecl * 504 GetUniqueNamespaceDeclaration (const char *name, 505 const Declaration &decl, 506 clang::DeclContext *decl_ctx); 507 508 //------------------------------------------------------------------ 509 // Function Types 510 //------------------------------------------------------------------ 511 512 clang::FunctionDecl * 513 CreateFunctionDeclaration (const char *name, 514 lldb::clang_type_t function_Type, 515 int storage, 516 bool is_inline); 517 518 static lldb::clang_type_t 519 CreateFunctionType (clang::ASTContext *ast, 520 lldb::clang_type_t result_type, 521 lldb::clang_type_t *args, 522 unsigned num_args, 523 bool is_variadic, 524 unsigned type_quals); 525 526 lldb::clang_type_t 527 CreateFunctionType (lldb::clang_type_t result_type, 528 lldb::clang_type_t *args, 529 unsigned num_args, 530 bool is_variadic, 531 unsigned type_quals) 532 { 533 return ClangASTContext::CreateFunctionType(getASTContext(), 534 result_type, 535 args, 536 num_args, 537 is_variadic, 538 type_quals); 539 } 540 541 clang::ParmVarDecl * 542 CreateParameterDeclaration (const char *name, 543 lldb::clang_type_t param_type, 544 int storage); 545 546 void 547 SetFunctionParameters (clang::FunctionDecl *function_decl, 548 clang::ParmVarDecl **params, 549 unsigned num_params); 550 551 //------------------------------------------------------------------ 552 // Array Types 553 //------------------------------------------------------------------ 554 555 lldb::clang_type_t 556 CreateArrayType (lldb::clang_type_t element_type, 557 size_t element_count, 558 uint32_t bit_stride); 559 560 //------------------------------------------------------------------ 561 // Tag Declarations 562 //------------------------------------------------------------------ 563 bool 564 StartTagDeclarationDefinition (lldb::clang_type_t qual_type); 565 566 bool 567 CompleteTagDeclarationDefinition (lldb::clang_type_t qual_type); 568 569 //------------------------------------------------------------------ 570 // Enumeration Types 571 //------------------------------------------------------------------ 572 lldb::clang_type_t 573 CreateEnumerationType (const char *name, 574 clang::DeclContext *decl_ctx, 575 const Declaration &decl, 576 lldb::clang_type_t integer_qual_type); 577 578 static lldb::clang_type_t 579 GetEnumerationIntegerType (lldb::clang_type_t enum_clang_type); 580 581 bool 582 AddEnumerationValueToEnumerationType (lldb::clang_type_t enum_qual_type, 583 lldb::clang_type_t enumerator_qual_type, 584 const Declaration &decl, 585 const char *name, 586 int64_t enum_value, 587 uint32_t enum_value_bit_size); 588 589 //------------------------------------------------------------------ 590 // Pointers & References 591 //------------------------------------------------------------------ 592 lldb::clang_type_t 593 CreatePointerType (lldb::clang_type_t clang_type); 594 595 static lldb::clang_type_t 596 CreatePointerType (clang::ASTContext *ast, 597 lldb::clang_type_t clang_type); 598 599 static lldb::clang_type_t 600 CreateLValueReferenceType (clang::ASTContext *ast_context, 601 lldb::clang_type_t clang_type); 602 603 static lldb::clang_type_t 604 CreateRValueReferenceType (clang::ASTContext *ast_context, 605 lldb::clang_type_t clang_type); 606 607 lldb::clang_type_t 608 CreateLValueReferenceType (lldb::clang_type_t clang_type) 609 { 610 return ClangASTContext::CreateLValueReferenceType(getASTContext(), clang_type); 611 } 612 613 lldb::clang_type_t 614 CreateRValueReferenceType (lldb::clang_type_t clang_type) 615 { 616 return ClangASTContext::CreateRValueReferenceType(getASTContext(), clang_type); 617 } 618 619 lldb::clang_type_t 620 CreateMemberPointerType (lldb::clang_type_t clang_pointee_type, 621 lldb::clang_type_t clang_class_type); 622 623 uint32_t 624 GetPointerBitSize (); 625 626 static bool 627 IsIntegerType (lldb::clang_type_t clang_type, bool &is_signed); 628 629 static bool 630 IsPointerType (lldb::clang_type_t clang_type, lldb::clang_type_t *target_type = NULL); 631 632 static bool 633 IsPointerOrReferenceType (lldb::clang_type_t clang_type, lldb::clang_type_t *target_type = NULL); 634 635 static bool 636 IsPossibleCPlusPlusDynamicType (clang::ASTContext *ast, 637 lldb::clang_type_t clang_type, 638 lldb::clang_type_t *target_type = NULL); 639 640 static bool 641 IsPossibleDynamicType (clang::ASTContext *ast, 642 lldb::clang_type_t clang_type, 643 lldb::clang_type_t *dynamic_pointee_type = NULL); 644 645 static bool 646 IsCStringType (lldb::clang_type_t clang_type, uint32_t &length); 647 648 static bool 649 IsFunctionPointerType (lldb::clang_type_t clang_type); 650 651 static bool 652 IsArrayType (lldb::clang_type_t clang_type, lldb::clang_type_t *member_type = NULL, uint64_t *size = NULL); 653 654 //------------------------------------------------------------------ 655 // Typedefs 656 //------------------------------------------------------------------ 657 lldb::clang_type_t 658 CreateTypedefType (const char *name, 659 lldb::clang_type_t clang_type, 660 clang::DeclContext *decl_ctx); 661 662 //------------------------------------------------------------------ 663 // Type names 664 //------------------------------------------------------------------ 665 static bool 666 IsFloatingPointType (lldb::clang_type_t clang_type, uint32_t &count, bool &is_complex); 667 668 // true iff this is one of the types that can "fit" 669 // in a Scalar object 670 static bool 671 IsScalarType (lldb::clang_type_t clang_type); 672 673 static bool 674 IsPointerToScalarType (lldb::clang_type_t clang_type); 675 676 static bool 677 IsArrayOfScalarType (lldb::clang_type_t clang_type); 678 679 static bool 680 GetCXXClassName (lldb::clang_type_t clang_type, 681 std::string &class_name); 682 683 static bool 684 IsCXXClassType (lldb::clang_type_t clang_type); 685 686 static bool 687 IsObjCClassType (lldb::clang_type_t clang_type); 688 689 static bool 690 IsCharType (lldb::clang_type_t clang_type); 691 692 static size_t 693 GetArraySize (lldb::clang_type_t clang_type); 694 695 //static bool 696 //ConvertFloatValueToString (clang::ASTContext *ast, 697 // lldb::clang_type_t clang_type, 698 // const uint8_t* bytes, 699 // size_t byte_size, 700 // int apint_byte_order, 701 // std::string &float_str); 702 703 static size_t 704 ConvertStringToFloatValue (clang::ASTContext *ast, 705 lldb::clang_type_t clang_type, 706 const char *s, 707 uint8_t *dst, 708 size_t dst_size); 709 710 //------------------------------------------------------------------ 711 // Qualifiers 712 //------------------------------------------------------------------ 713 static unsigned 714 GetTypeQualifiers(lldb::clang_type_t clang_type); 715protected: 716 //------------------------------------------------------------------ 717 // Classes that inherit from ClangASTContext can see and modify these 718 //------------------------------------------------------------------ 719 std::string m_target_triple; 720 std::auto_ptr<clang::ASTContext> m_ast_ap; 721 std::auto_ptr<clang::LangOptions> m_language_options_ap; 722 std::auto_ptr<clang::FileManager> m_file_manager_ap; 723 std::auto_ptr<clang::FileSystemOptions> m_file_system_options_ap; 724 std::auto_ptr<clang::SourceManager> m_source_manager_ap; 725 std::auto_ptr<clang::Diagnostic> m_diagnostic_ap; 726 std::auto_ptr<clang::DiagnosticClient> m_diagnostic_client_ap; 727 std::auto_ptr<clang::TargetOptions> m_target_options_ap; 728 std::auto_ptr<clang::TargetInfo> m_target_info_ap; 729 std::auto_ptr<clang::IdentifierTable> m_identifier_table_ap; 730 std::auto_ptr<clang::SelectorTable> m_selector_table_ap; 731 std::auto_ptr<clang::Builtin::Context> m_builtins_ap; 732 CompleteTagDeclCallback m_callback_tag_decl; 733 CompleteObjCInterfaceDeclCallback m_callback_objc_decl; 734 void * m_callback_baton; 735private: 736 //------------------------------------------------------------------ 737 // For ClangASTContext only 738 //------------------------------------------------------------------ 739 ClangASTContext(const ClangASTContext&); 740 const ClangASTContext& operator=(const ClangASTContext&); 741}; 742 743} // namespace lldb_private 744 745#endif // liblldb_ClangASTContext_h_ 746