ClangASTContext.h revision 327952
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#include <stdint.h> 15 16// C++ Includes 17#include <functional> 18#include <initializer_list> 19#include <map> 20#include <memory> 21#include <set> 22#include <string> 23#include <utility> 24#include <vector> 25 26// Other libraries and framework includes 27#include "clang/AST/ASTContext.h" 28#include "clang/AST/ExternalASTMerger.h" 29#include "clang/AST/TemplateBase.h" 30#include "llvm/ADT/SmallVector.h" 31 32// Project includes 33#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h" 34#include "lldb/Core/ClangForward.h" 35#include "lldb/Symbol/CompilerType.h" 36#include "lldb/Symbol/TypeSystem.h" 37#include "lldb/Utility/ConstString.h" 38#include "lldb/lldb-enumerations.h" 39 40class DWARFASTParserClang; 41#ifdef LLDB_ENABLE_ALL 42class PDBASTParser; 43#endif // LLDB_ENABLE_ALL 44 45namespace lldb_private { 46 47class Declaration; 48 49class ClangASTContext : public TypeSystem { 50public: 51 typedef void (*CompleteTagDeclCallback)(void *baton, clang::TagDecl *); 52 typedef void (*CompleteObjCInterfaceDeclCallback)(void *baton, 53 clang::ObjCInterfaceDecl *); 54 55 //------------------------------------------------------------------ 56 // llvm casting support 57 //------------------------------------------------------------------ 58 static bool classof(const TypeSystem *ts) { 59 return ts->getKind() == TypeSystem::eKindClang; 60 } 61 62 //------------------------------------------------------------------ 63 // Constructors and Destructors 64 //------------------------------------------------------------------ 65 ClangASTContext(const char *triple = nullptr); 66 67 ~ClangASTContext() override; 68 69 void Finalize() override; 70 71 //------------------------------------------------------------------ 72 // PluginInterface functions 73 //------------------------------------------------------------------ 74 ConstString GetPluginName() override; 75 76 uint32_t GetPluginVersion() override; 77 78 static ConstString GetPluginNameStatic(); 79 80 static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language, 81 Module *module, Target *target); 82 83 static void EnumerateSupportedLanguages( 84 std::set<lldb::LanguageType> &languages_for_types, 85 std::set<lldb::LanguageType> &languages_for_expressions); 86 87 static void Initialize(); 88 89 static void Terminate(); 90 91 static ClangASTContext *GetASTContext(clang::ASTContext *ast_ctx); 92 93 clang::ASTContext *getASTContext(); 94 95 void setASTContext(clang::ASTContext *ast_ctx); 96 97 clang::Builtin::Context *getBuiltinContext(); 98 99 clang::IdentifierTable *getIdentifierTable(); 100 101 clang::LangOptions *getLanguageOptions(); 102 103 clang::SelectorTable *getSelectorTable(); 104 105 clang::FileManager *getFileManager(); 106 107 clang::SourceManager *getSourceManager(); 108 109 clang::DiagnosticsEngine *getDiagnosticsEngine(); 110 111 clang::DiagnosticConsumer *getDiagnosticConsumer(); 112 113 clang::MangleContext *getMangleContext(); 114 115 std::shared_ptr<clang::TargetOptions> &getTargetOptions(); 116 117 clang::TargetInfo *getTargetInfo(); 118 119 void Clear(); 120 121 const char *GetTargetTriple(); 122 123 void SetTargetTriple(const char *target_triple); 124 125 void SetArchitecture(const ArchSpec &arch); 126 127 bool HasExternalSource(); 128 129 void SetExternalSource( 130 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> &ast_source_ap); 131 132 void RemoveExternalSource(); 133 134 bool GetCompleteDecl(clang::Decl *decl) { 135 return ClangASTContext::GetCompleteDecl(getASTContext(), decl); 136 } 137 138 static void DumpDeclHiearchy(clang::Decl *decl); 139 140 static void DumpDeclContextHiearchy(clang::DeclContext *decl_ctx); 141 142 static bool DeclsAreEquivalent(clang::Decl *lhs_decl, clang::Decl *rhs_decl); 143 144 static bool GetCompleteDecl(clang::ASTContext *ast, clang::Decl *decl); 145 146 void SetMetadataAsUserID(const void *object, lldb::user_id_t user_id); 147 148 void SetMetadata(const void *object, ClangASTMetadata &meta_data) { 149 SetMetadata(getASTContext(), object, meta_data); 150 } 151 152 static void SetMetadata(clang::ASTContext *ast, const void *object, 153 ClangASTMetadata &meta_data); 154 155 ClangASTMetadata *GetMetadata(const void *object) { 156 return GetMetadata(getASTContext(), object); 157 } 158 159 static ClangASTMetadata *GetMetadata(clang::ASTContext *ast, 160 const void *object); 161 162 //------------------------------------------------------------------ 163 // Basic Types 164 //------------------------------------------------------------------ 165 CompilerType GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding, 166 size_t bit_size) override; 167 168 static CompilerType GetBuiltinTypeForEncodingAndBitSize( 169 clang::ASTContext *ast, lldb::Encoding encoding, uint32_t bit_size); 170 171 CompilerType GetBasicType(lldb::BasicType type); 172 173 static CompilerType GetBasicType(clang::ASTContext *ast, 174 lldb::BasicType type); 175 176 static CompilerType GetBasicType(clang::ASTContext *ast, 177 const ConstString &name); 178 179 static lldb::BasicType GetBasicTypeEnumeration(const ConstString &name); 180 181 CompilerType GetBuiltinTypeForDWARFEncodingAndBitSize(const char *type_name, 182 uint32_t dw_ate, 183 uint32_t bit_size); 184 185 CompilerType GetCStringType(bool is_const); 186 187 static CompilerType GetUnknownAnyType(clang::ASTContext *ast); 188 189 CompilerType GetUnknownAnyType() { 190 return ClangASTContext::GetUnknownAnyType(getASTContext()); 191 } 192 193 static clang::DeclContext *GetDeclContextForType(clang::QualType type); 194 195 static clang::DeclContext *GetDeclContextForType(const CompilerType &type); 196 197 uint32_t GetPointerByteSize() override; 198 199 static clang::DeclContext *GetTranslationUnitDecl(clang::ASTContext *ast); 200 201 clang::DeclContext *GetTranslationUnitDecl() { 202 return GetTranslationUnitDecl(getASTContext()); 203 } 204 205 static clang::Decl *CopyDecl(clang::ASTContext *dest_context, 206 clang::ASTContext *source_context, 207 clang::Decl *source_decl); 208 209 static bool AreTypesSame(CompilerType type1, CompilerType type2, 210 bool ignore_qualifiers = false); 211 212 static CompilerType GetTypeForDecl(clang::NamedDecl *decl); 213 214 static CompilerType GetTypeForDecl(clang::TagDecl *decl); 215 216 static CompilerType GetTypeForDecl(clang::ObjCInterfaceDecl *objc_decl); 217 218 template <typename RecordDeclType> 219 CompilerType GetTypeForIdentifier(const ConstString &type_name) { 220 CompilerType compiler_type; 221 222 if (type_name.GetLength()) { 223 clang::ASTContext *ast = getASTContext(); 224 if (ast) { 225 clang::IdentifierInfo &myIdent = 226 ast->Idents.get(type_name.GetCString()); 227 clang::DeclarationName myName = 228 ast->DeclarationNames.getIdentifier(&myIdent); 229 230 clang::DeclContext::lookup_result result = 231 ast->getTranslationUnitDecl()->lookup(myName); 232 233 if (!result.empty()) { 234 clang::NamedDecl *named_decl = result[0]; 235 if (const RecordDeclType *record_decl = 236 llvm::dyn_cast<RecordDeclType>(named_decl)) 237 compiler_type.SetCompilerType( 238 ast, clang::QualType(record_decl->getTypeForDecl(), 0)); 239 } 240 } 241 } 242 243 return compiler_type; 244 } 245 246 CompilerType CreateStructForIdentifier( 247 const ConstString &type_name, 248 const std::initializer_list<std::pair<const char *, CompilerType>> 249 &type_fields, 250 bool packed = false); 251 252 CompilerType GetOrCreateStructForIdentifier( 253 const ConstString &type_name, 254 const std::initializer_list<std::pair<const char *, CompilerType>> 255 &type_fields, 256 bool packed = false); 257 258 //------------------------------------------------------------------ 259 // Structure, Unions, Classes 260 //------------------------------------------------------------------ 261 262 static clang::AccessSpecifier 263 ConvertAccessTypeToAccessSpecifier(lldb::AccessType access); 264 265 static clang::AccessSpecifier 266 UnifyAccessSpecifiers(clang::AccessSpecifier lhs, clang::AccessSpecifier rhs); 267 268 static uint32_t GetNumBaseClasses(const clang::CXXRecordDecl *cxx_record_decl, 269 bool omit_empty_base_classes); 270 271 CompilerType CreateRecordType(clang::DeclContext *decl_ctx, 272 lldb::AccessType access_type, const char *name, 273 int kind, lldb::LanguageType language, 274 ClangASTMetadata *metadata = nullptr); 275 276 class TemplateParameterInfos { 277 public: 278 bool IsValid() const { 279 if (args.empty()) 280 return false; 281 return args.size() == names.size() && 282 ((bool)pack_name == (bool)packed_args) && 283 (!packed_args || !packed_args->packed_args); 284 } 285 286 llvm::SmallVector<const char *, 2> names; 287 llvm::SmallVector<clang::TemplateArgument, 2> args; 288 289 const char * pack_name = nullptr; 290 std::unique_ptr<TemplateParameterInfos> packed_args; 291 }; 292 293 clang::FunctionTemplateDecl * 294 CreateFunctionTemplateDecl(clang::DeclContext *decl_ctx, 295 clang::FunctionDecl *func_decl, const char *name, 296 const TemplateParameterInfos &infos); 297 298 void CreateFunctionTemplateSpecializationInfo( 299 clang::FunctionDecl *func_decl, clang::FunctionTemplateDecl *Template, 300 const TemplateParameterInfos &infos); 301 302 clang::ClassTemplateDecl * 303 CreateClassTemplateDecl(clang::DeclContext *decl_ctx, 304 lldb::AccessType access_type, const char *class_name, 305 int kind, const TemplateParameterInfos &infos); 306 307 clang::ClassTemplateSpecializationDecl *CreateClassTemplateSpecializationDecl( 308 clang::DeclContext *decl_ctx, 309 clang::ClassTemplateDecl *class_template_decl, int kind, 310 const TemplateParameterInfos &infos); 311 312 CompilerType 313 CreateClassTemplateSpecializationType(clang::ClassTemplateSpecializationDecl * 314 class_template_specialization_decl); 315 316 static clang::DeclContext * 317 GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl); 318 319 static clang::DeclContext * 320 GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl); 321 322 static bool CheckOverloadedOperatorKindParameterCount( 323 bool is_method, clang::OverloadedOperatorKind op_kind, 324 uint32_t num_params); 325 326 bool FieldIsBitfield(clang::FieldDecl *field, uint32_t &bitfield_bit_size); 327 328 static bool FieldIsBitfield(clang::ASTContext *ast, clang::FieldDecl *field, 329 uint32_t &bitfield_bit_size); 330 331 static bool RecordHasFields(const clang::RecordDecl *record_decl); 332 333 CompilerType CreateObjCClass(const char *name, clang::DeclContext *decl_ctx, 334 bool isForwardDecl, bool isInternal, 335 ClangASTMetadata *metadata = nullptr); 336 337 bool SetTagTypeKind(clang::QualType type, int kind) const; 338 339 bool SetDefaultAccessForRecordFields(clang::RecordDecl *record_decl, 340 int default_accessibility, 341 int *assigned_accessibilities, 342 size_t num_assigned_accessibilities); 343 344 // Returns a mask containing bits from the ClangASTContext::eTypeXXX 345 // enumerations 346 347 //------------------------------------------------------------------ 348 // Namespace Declarations 349 //------------------------------------------------------------------ 350 351 clang::NamespaceDecl * 352 GetUniqueNamespaceDeclaration(const char *name, clang::DeclContext *decl_ctx); 353 354 static clang::NamespaceDecl * 355 GetUniqueNamespaceDeclaration(clang::ASTContext *ast, const char *name, 356 clang::DeclContext *decl_ctx); 357 358 //------------------------------------------------------------------ 359 // Function Types 360 //------------------------------------------------------------------ 361 362 clang::FunctionDecl * 363 CreateFunctionDeclaration(clang::DeclContext *decl_ctx, const char *name, 364 const CompilerType &function_Type, int storage, 365 bool is_inline); 366 367 static CompilerType CreateFunctionType(clang::ASTContext *ast, 368 const CompilerType &result_type, 369 const CompilerType *args, 370 unsigned num_args, bool is_variadic, 371 unsigned type_quals); 372 373 CompilerType CreateFunctionType(const CompilerType &result_type, 374 const CompilerType *args, unsigned num_args, 375 bool is_variadic, unsigned type_quals) { 376 return ClangASTContext::CreateFunctionType( 377 getASTContext(), result_type, args, num_args, is_variadic, type_quals); 378 } 379 380 clang::ParmVarDecl *CreateParameterDeclaration(const char *name, 381 const CompilerType ¶m_type, 382 int storage); 383 384 void SetFunctionParameters(clang::FunctionDecl *function_decl, 385 clang::ParmVarDecl **params, unsigned num_params); 386 387 CompilerType CreateBlockPointerType(const CompilerType &function_type); 388 389 //------------------------------------------------------------------ 390 // Array Types 391 //------------------------------------------------------------------ 392 393 CompilerType CreateArrayType(const CompilerType &element_type, 394 size_t element_count, bool is_vector); 395 396 //------------------------------------------------------------------ 397 // Enumeration Types 398 //------------------------------------------------------------------ 399 CompilerType CreateEnumerationType(const char *name, 400 clang::DeclContext *decl_ctx, 401 const Declaration &decl, 402 const CompilerType &integer_qual_type, 403 bool is_scoped); 404 405 //------------------------------------------------------------------ 406 // Integer type functions 407 //------------------------------------------------------------------ 408 409 static CompilerType GetIntTypeFromBitSize(clang::ASTContext *ast, 410 size_t bit_size, bool is_signed); 411 412 CompilerType GetPointerSizedIntType(bool is_signed) { 413 return GetPointerSizedIntType(getASTContext(), is_signed); 414 } 415 416 static CompilerType GetPointerSizedIntType(clang::ASTContext *ast, 417 bool is_signed); 418 419 //------------------------------------------------------------------ 420 // Floating point functions 421 //------------------------------------------------------------------ 422 423 static CompilerType GetFloatTypeFromBitSize(clang::ASTContext *ast, 424 size_t bit_size); 425 426 //------------------------------------------------------------------ 427 // TypeSystem methods 428 //------------------------------------------------------------------ 429 DWARFASTParser *GetDWARFParser() override; 430#ifdef LLDB_ENABLE_ALL 431 PDBASTParser *GetPDBParser(); 432#endif // LLDB_ENABLE_ALL 433 434 //------------------------------------------------------------------ 435 // ClangASTContext callbacks for external source lookups. 436 //------------------------------------------------------------------ 437 static void CompleteTagDecl(void *baton, clang::TagDecl *); 438 439 static void CompleteObjCInterfaceDecl(void *baton, 440 clang::ObjCInterfaceDecl *); 441 442 static bool LayoutRecordType( 443 void *baton, const clang::RecordDecl *record_decl, uint64_t &size, 444 uint64_t &alignment, 445 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets, 446 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> 447 &base_offsets, 448 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> 449 &vbase_offsets); 450 451 //---------------------------------------------------------------------- 452 // CompilerDecl override functions 453 //---------------------------------------------------------------------- 454 ConstString DeclGetName(void *opaque_decl) override; 455 456 ConstString DeclGetMangledName(void *opaque_decl) override; 457 458 CompilerDeclContext DeclGetDeclContext(void *opaque_decl) override; 459 460 CompilerType DeclGetFunctionReturnType(void *opaque_decl) override; 461 462 size_t DeclGetFunctionNumArguments(void *opaque_decl) override; 463 464 CompilerType DeclGetFunctionArgumentType(void *opaque_decl, 465 size_t arg_idx) override; 466 467 //---------------------------------------------------------------------- 468 // CompilerDeclContext override functions 469 //---------------------------------------------------------------------- 470 471 std::vector<CompilerDecl> 472 DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name, 473 const bool ignore_using_decls) override; 474 475 bool DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) override; 476 477 ConstString DeclContextGetName(void *opaque_decl_ctx) override; 478 479 ConstString DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) override; 480 481 bool DeclContextIsClassMethod(void *opaque_decl_ctx, 482 lldb::LanguageType *language_ptr, 483 bool *is_instance_method_ptr, 484 ConstString *language_object_name_ptr) override; 485 486 //---------------------------------------------------------------------- 487 // Clang specific clang::DeclContext functions 488 //---------------------------------------------------------------------- 489 490 static clang::DeclContext * 491 DeclContextGetAsDeclContext(const CompilerDeclContext &dc); 492 493 static clang::ObjCMethodDecl * 494 DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc); 495 496 static clang::CXXMethodDecl * 497 DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc); 498 499 static clang::FunctionDecl * 500 DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc); 501 502 static clang::NamespaceDecl * 503 DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc); 504 505 static ClangASTMetadata *DeclContextGetMetaData(const CompilerDeclContext &dc, 506 const void *object); 507 508 static clang::ASTContext * 509 DeclContextGetClangASTContext(const CompilerDeclContext &dc); 510 511 //---------------------------------------------------------------------- 512 // Tests 513 //---------------------------------------------------------------------- 514 515 bool IsArrayType(lldb::opaque_compiler_type_t type, 516 CompilerType *element_type, uint64_t *size, 517 bool *is_incomplete) override; 518 519 bool IsVectorType(lldb::opaque_compiler_type_t type, 520 CompilerType *element_type, uint64_t *size) override; 521 522 bool IsAggregateType(lldb::opaque_compiler_type_t type) override; 523 524 bool IsAnonymousType(lldb::opaque_compiler_type_t type) override; 525 526 bool IsBeingDefined(lldb::opaque_compiler_type_t type) override; 527 528 bool IsCharType(lldb::opaque_compiler_type_t type) override; 529 530 bool IsCompleteType(lldb::opaque_compiler_type_t type) override; 531 532 bool IsConst(lldb::opaque_compiler_type_t type) override; 533 534 bool IsCStringType(lldb::opaque_compiler_type_t type, 535 uint32_t &length) override; 536 537 static bool IsCXXClassType(const CompilerType &type); 538 539 bool IsDefined(lldb::opaque_compiler_type_t type) override; 540 541 bool IsFloatingPointType(lldb::opaque_compiler_type_t type, uint32_t &count, 542 bool &is_complex) override; 543 544 bool IsFunctionType(lldb::opaque_compiler_type_t type, 545 bool *is_variadic_ptr) override; 546 547 uint32_t IsHomogeneousAggregate(lldb::opaque_compiler_type_t type, 548 CompilerType *base_type_ptr) override; 549 550 size_t 551 GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) override; 552 553 CompilerType GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type, 554 const size_t index) override; 555 556 bool IsFunctionPointerType(lldb::opaque_compiler_type_t type) override; 557 558 bool IsBlockPointerType(lldb::opaque_compiler_type_t type, 559 CompilerType *function_pointer_type_ptr) override; 560 561 bool IsIntegerType(lldb::opaque_compiler_type_t type, 562 bool &is_signed) override; 563 564 bool IsEnumerationType(lldb::opaque_compiler_type_t type, 565 bool &is_signed) override; 566 567 static bool IsObjCClassType(const CompilerType &type); 568 569 static bool IsObjCClassTypeAndHasIVars(const CompilerType &type, 570 bool check_superclass); 571 572 static bool IsObjCObjectOrInterfaceType(const CompilerType &type); 573 574 static bool IsObjCObjectPointerType(const CompilerType &type, 575 CompilerType *target_type = nullptr); 576 577 bool IsPolymorphicClass(lldb::opaque_compiler_type_t type) override; 578 579 static bool IsClassType(lldb::opaque_compiler_type_t type); 580 581 static bool IsEnumType(lldb::opaque_compiler_type_t type); 582 583 bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type, 584 CompilerType *target_type, // Can pass nullptr 585 bool check_cplusplus, bool check_objc) override; 586 587 bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) override; 588 589 bool IsPointerType(lldb::opaque_compiler_type_t type, 590 CompilerType *pointee_type) override; 591 592 bool IsPointerOrReferenceType(lldb::opaque_compiler_type_t type, 593 CompilerType *pointee_type) override; 594 595 bool IsReferenceType(lldb::opaque_compiler_type_t type, 596 CompilerType *pointee_type, bool *is_rvalue) override; 597 598 bool IsScalarType(lldb::opaque_compiler_type_t type) override; 599 600 bool IsTypedefType(lldb::opaque_compiler_type_t type) override; 601 602 bool IsVoidType(lldb::opaque_compiler_type_t type) override; 603 604 bool SupportsLanguage(lldb::LanguageType language) override; 605 606 static bool GetCXXClassName(const CompilerType &type, 607 std::string &class_name); 608 609 static bool GetObjCClassName(const CompilerType &type, 610 std::string &class_name); 611 612 //---------------------------------------------------------------------- 613 // Type Completion 614 //---------------------------------------------------------------------- 615 616 bool GetCompleteType(lldb::opaque_compiler_type_t type) override; 617 618 //---------------------------------------------------------------------- 619 // Accessors 620 //---------------------------------------------------------------------- 621 622 ConstString GetTypeName(lldb::opaque_compiler_type_t type) override; 623 624 uint32_t GetTypeInfo(lldb::opaque_compiler_type_t type, 625 CompilerType *pointee_or_element_compiler_type) override; 626 627 lldb::LanguageType 628 GetMinimumLanguage(lldb::opaque_compiler_type_t type) override; 629 630 lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) override; 631 632 unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) override; 633 634 //---------------------------------------------------------------------- 635 // Creating related types 636 //---------------------------------------------------------------------- 637 638 // Using the current type, create a new typedef to that type using 639 // "typedef_name" 640 // as the name and "decl_ctx" as the decl context. 641 static CompilerType 642 CreateTypedefType(const CompilerType &type, const char *typedef_name, 643 const CompilerDeclContext &compiler_decl_ctx); 644 645 CompilerType GetArrayElementType(lldb::opaque_compiler_type_t type, 646 uint64_t *stride) override; 647 648 CompilerType GetArrayType(lldb::opaque_compiler_type_t type, 649 uint64_t size) override; 650 651 CompilerType GetCanonicalType(lldb::opaque_compiler_type_t type) override; 652 653 CompilerType 654 GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) override; 655 656 // Returns -1 if this isn't a function of if the function doesn't have a 657 // prototype 658 // Returns a value >= 0 if there is a prototype. 659 int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) override; 660 661 CompilerType GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type, 662 size_t idx) override; 663 664 CompilerType 665 GetFunctionReturnType(lldb::opaque_compiler_type_t type) override; 666 667 size_t GetNumMemberFunctions(lldb::opaque_compiler_type_t type) override; 668 669 TypeMemberFunctionImpl 670 GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, 671 size_t idx) override; 672 673 CompilerType GetNonReferenceType(lldb::opaque_compiler_type_t type) override; 674 675 CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) override; 676 677 CompilerType GetPointerType(lldb::opaque_compiler_type_t type) override; 678 679 CompilerType 680 GetLValueReferenceType(lldb::opaque_compiler_type_t type) override; 681 682 CompilerType 683 GetRValueReferenceType(lldb::opaque_compiler_type_t type) override; 684 685 CompilerType AddConstModifier(lldb::opaque_compiler_type_t type) override; 686 687 CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type) override; 688 689 CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type) override; 690 691 CompilerType CreateTypedef(lldb::opaque_compiler_type_t type, 692 const char *name, 693 const CompilerDeclContext &decl_ctx) override; 694 695 // If the current object represents a typedef type, get the underlying type 696 CompilerType GetTypedefedType(lldb::opaque_compiler_type_t type) override; 697 698 //---------------------------------------------------------------------- 699 // Create related types using the current type's AST 700 //---------------------------------------------------------------------- 701 CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) override; 702 703 //---------------------------------------------------------------------- 704 // Exploring the type 705 //---------------------------------------------------------------------- 706 707 uint64_t GetByteSize(lldb::opaque_compiler_type_t type, 708 ExecutionContextScope *exe_scope) { 709 return (GetBitSize(type, exe_scope) + 7) / 8; 710 } 711 712 uint64_t GetBitSize(lldb::opaque_compiler_type_t type, 713 ExecutionContextScope *exe_scope) override; 714 715 lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type, 716 uint64_t &count) override; 717 718 lldb::Format GetFormat(lldb::opaque_compiler_type_t type) override; 719 720 size_t GetTypeBitAlign(lldb::opaque_compiler_type_t type) override; 721 722 uint32_t GetNumChildren(lldb::opaque_compiler_type_t type, 723 bool omit_empty_base_classes) override; 724 725 CompilerType GetBuiltinTypeByName(const ConstString &name) override; 726 727 lldb::BasicType 728 GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) override; 729 730 static lldb::BasicType 731 GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type, 732 const ConstString &name); 733 734 void ForEachEnumerator( 735 lldb::opaque_compiler_type_t type, 736 std::function<bool(const CompilerType &integer_type, 737 const ConstString &name, 738 const llvm::APSInt &value)> const &callback) override; 739 740 uint32_t GetNumFields(lldb::opaque_compiler_type_t type) override; 741 742 CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type, size_t idx, 743 std::string &name, uint64_t *bit_offset_ptr, 744 uint32_t *bitfield_bit_size_ptr, 745 bool *is_bitfield_ptr) override; 746 747 uint32_t GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) override; 748 749 uint32_t GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) override; 750 751 CompilerType GetDirectBaseClassAtIndex(lldb::opaque_compiler_type_t type, 752 size_t idx, 753 uint32_t *bit_offset_ptr) override; 754 755 CompilerType GetVirtualBaseClassAtIndex(lldb::opaque_compiler_type_t type, 756 size_t idx, 757 uint32_t *bit_offset_ptr) override; 758 759 static uint32_t GetNumPointeeChildren(clang::QualType type); 760 761 CompilerType GetChildCompilerTypeAtIndex( 762 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx, 763 bool transparent_pointers, bool omit_empty_base_classes, 764 bool ignore_array_bounds, std::string &child_name, 765 uint32_t &child_byte_size, int32_t &child_byte_offset, 766 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset, 767 bool &child_is_base_class, bool &child_is_deref_of_parent, 768 ValueObject *valobj, uint64_t &language_flags) override; 769 770 // Lookup a child given a name. This function will match base class names 771 // and member member names in "clang_type" only, not descendants. 772 uint32_t GetIndexOfChildWithName(lldb::opaque_compiler_type_t type, 773 const char *name, 774 bool omit_empty_base_classes) override; 775 776 // Lookup a child member given a name. This function will match member names 777 // only and will descend into "clang_type" children in search for the first 778 // member in this class, or any base class that matches "name". 779 // TODO: Return all matches for a given name by returning a 780 // vector<vector<uint32_t>> 781 // so we catch all names that match a given child name, not just the first. 782 size_t 783 GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type, 784 const char *name, bool omit_empty_base_classes, 785 std::vector<uint32_t> &child_indexes) override; 786 787 size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type) override; 788 789 lldb::TemplateArgumentKind 790 GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, 791 size_t idx) override; 792 CompilerType GetTypeTemplateArgument(lldb::opaque_compiler_type_t type, 793 size_t idx) override; 794 llvm::Optional<CompilerType::IntegralTemplateArgument> 795 GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, 796 size_t idx) override; 797 798 CompilerType GetTypeForFormatters(void *type) override; 799 800#define LLDB_INVALID_DECL_LEVEL UINT32_MAX 801 // LLDB_INVALID_DECL_LEVEL is returned by CountDeclLevels if 802 // child_decl_ctx could not be found in decl_ctx. 803 uint32_t CountDeclLevels(clang::DeclContext *frame_decl_ctx, 804 clang::DeclContext *child_decl_ctx, 805 ConstString *child_name = nullptr, 806 CompilerType *child_type = nullptr); 807 808 //---------------------------------------------------------------------- 809 // Modifying RecordType 810 //---------------------------------------------------------------------- 811 static clang::FieldDecl *AddFieldToRecordType(const CompilerType &type, 812 const char *name, 813 const CompilerType &field_type, 814 lldb::AccessType access, 815 uint32_t bitfield_bit_size); 816 817 static void BuildIndirectFields(const CompilerType &type); 818 819 static void SetIsPacked(const CompilerType &type); 820 821 static clang::VarDecl *AddVariableToRecordType(const CompilerType &type, 822 const char *name, 823 const CompilerType &var_type, 824 lldb::AccessType access); 825 826 clang::CXXMethodDecl * 827 AddMethodToCXXRecordType(lldb::opaque_compiler_type_t type, const char *name, 828 const CompilerType &method_type, 829 lldb::AccessType access, bool is_virtual, 830 bool is_static, bool is_inline, bool is_explicit, 831 bool is_attr_used, bool is_artificial); 832 833 // C++ Base Classes 834 clang::CXXBaseSpecifier * 835 CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type, 836 lldb::AccessType access, bool is_virtual, 837 bool base_of_class); 838 839 static void DeleteBaseClassSpecifiers(clang::CXXBaseSpecifier **base_classes, 840 unsigned num_base_classes); 841 842 bool 843 SetBaseClassesForClassType(lldb::opaque_compiler_type_t type, 844 clang::CXXBaseSpecifier const *const *base_classes, 845 unsigned num_base_classes); 846 847 static bool SetObjCSuperClass(const CompilerType &type, 848 const CompilerType &superclass_compiler_type); 849 850 static bool AddObjCClassProperty(const CompilerType &type, 851 const char *property_name, 852 const CompilerType &property_compiler_type, 853 clang::ObjCIvarDecl *ivar_decl, 854 const char *property_setter_name, 855 const char *property_getter_name, 856 uint32_t property_attributes, 857 ClangASTMetadata *metadata); 858 859 static clang::ObjCMethodDecl *AddMethodToObjCObjectType( 860 const CompilerType &type, 861 const char *name, // the full symbol name as seen in the symbol table 862 // (lldb::opaque_compiler_type_t type, "-[NString 863 // stringWithCString:]") 864 const CompilerType &method_compiler_type, lldb::AccessType access, 865 bool is_artificial, bool is_variadic); 866 867 static bool SetHasExternalStorage(lldb::opaque_compiler_type_t type, 868 bool has_extern); 869 870 static bool GetHasExternalStorage(const CompilerType &type); 871 //------------------------------------------------------------------ 872 // Tag Declarations 873 //------------------------------------------------------------------ 874 static bool StartTagDeclarationDefinition(const CompilerType &type); 875 876 static bool CompleteTagDeclarationDefinition(const CompilerType &type); 877 878 //---------------------------------------------------------------------- 879 // Modifying Enumeration types 880 //---------------------------------------------------------------------- 881 bool AddEnumerationValueToEnumerationType( 882 lldb::opaque_compiler_type_t type, 883 const CompilerType &enumerator_qual_type, const Declaration &decl, 884 const char *name, int64_t enum_value, uint32_t enum_value_bit_size); 885 886 CompilerType GetEnumerationIntegerType(lldb::opaque_compiler_type_t type); 887 888 //------------------------------------------------------------------ 889 // Pointers & References 890 //------------------------------------------------------------------ 891 892 // Call this function using the class type when you want to make a 893 // member pointer type to pointee_type. 894 static CompilerType CreateMemberPointerType(const CompilerType &type, 895 const CompilerType &pointee_type); 896 897 // Converts "s" to a floating point value and place resulting floating 898 // point bytes in the "dst" buffer. 899 size_t ConvertStringToFloatValue(lldb::opaque_compiler_type_t type, 900 const char *s, uint8_t *dst, 901 size_t dst_size) override; 902 903 //---------------------------------------------------------------------- 904 // Dumping types 905 //---------------------------------------------------------------------- 906 void DumpValue(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, 907 Stream *s, lldb::Format format, const DataExtractor &data, 908 lldb::offset_t data_offset, size_t data_byte_size, 909 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, 910 bool show_types, bool show_summary, bool verbose, 911 uint32_t depth) override; 912 913 bool DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s, 914 lldb::Format format, const DataExtractor &data, 915 lldb::offset_t data_offset, size_t data_byte_size, 916 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, 917 ExecutionContextScope *exe_scope) override; 918 919 void DumpSummary(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, 920 Stream *s, const DataExtractor &data, 921 lldb::offset_t data_offset, size_t data_byte_size) override; 922 923 void DumpTypeDescription( 924 lldb::opaque_compiler_type_t type) override; // Dump to stdout 925 926 void DumpTypeDescription(lldb::opaque_compiler_type_t type, 927 Stream *s) override; 928 929 static void DumpTypeName(const CompilerType &type); 930 931 static clang::EnumDecl *GetAsEnumDecl(const CompilerType &type); 932 933 static clang::RecordDecl *GetAsRecordDecl(const CompilerType &type); 934 935 static clang::TagDecl *GetAsTagDecl(const CompilerType &type); 936 937 clang::CXXRecordDecl *GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type); 938 939 static clang::ObjCInterfaceDecl * 940 GetAsObjCInterfaceDecl(const CompilerType &type); 941 942 clang::ClassTemplateDecl *ParseClassTemplateDecl( 943 clang::DeclContext *decl_ctx, lldb::AccessType access_type, 944 const char *parent_name, int tag_decl_kind, 945 const ClangASTContext::TemplateParameterInfos &template_param_infos); 946 947 clang::BlockDecl *CreateBlockDeclaration(clang::DeclContext *ctx); 948 949 clang::UsingDirectiveDecl * 950 CreateUsingDirectiveDeclaration(clang::DeclContext *decl_ctx, 951 clang::NamespaceDecl *ns_decl); 952 953 clang::UsingDecl *CreateUsingDeclaration(clang::DeclContext *current_decl_ctx, 954 clang::NamedDecl *target); 955 956 clang::VarDecl *CreateVariableDeclaration(clang::DeclContext *decl_context, 957 const char *name, 958 clang::QualType type); 959 960 static lldb::opaque_compiler_type_t 961 GetOpaqueCompilerType(clang::ASTContext *ast, lldb::BasicType basic_type); 962 963 static clang::QualType GetQualType(lldb::opaque_compiler_type_t type) { 964 if (type) 965 return clang::QualType::getFromOpaquePtr(type); 966 return clang::QualType(); 967 } 968 969 static clang::QualType 970 GetCanonicalQualType(lldb::opaque_compiler_type_t type) { 971 if (type) 972 return clang::QualType::getFromOpaquePtr(type).getCanonicalType(); 973 return clang::QualType(); 974 } 975 976 clang::DeclarationName 977 GetDeclarationName(const char *name, const CompilerType &function_clang_type); 978 979 virtual const clang::ExternalASTMerger::OriginMap &GetOriginMap() { 980 return m_origins; 981 } 982protected: 983 const clang::ClassTemplateSpecializationDecl * 984 GetAsTemplateSpecialization(lldb::opaque_compiler_type_t type); 985 986 //------------------------------------------------------------------ 987 // Classes that inherit from ClangASTContext can see and modify these 988 //------------------------------------------------------------------ 989 // clang-format off 990 std::string m_target_triple; 991 std::unique_ptr<clang::ASTContext> m_ast_ap; 992 std::unique_ptr<clang::LangOptions> m_language_options_ap; 993 std::unique_ptr<clang::FileManager> m_file_manager_ap; 994 std::unique_ptr<clang::FileSystemOptions> m_file_system_options_ap; 995 std::unique_ptr<clang::SourceManager> m_source_manager_ap; 996 std::unique_ptr<clang::DiagnosticsEngine> m_diagnostics_engine_ap; 997 std::unique_ptr<clang::DiagnosticConsumer> m_diagnostic_consumer_ap; 998 std::shared_ptr<clang::TargetOptions> m_target_options_rp; 999 std::unique_ptr<clang::TargetInfo> m_target_info_ap; 1000 std::unique_ptr<clang::IdentifierTable> m_identifier_table_ap; 1001 std::unique_ptr<clang::SelectorTable> m_selector_table_ap; 1002 std::unique_ptr<clang::Builtin::Context> m_builtins_ap; 1003 std::unique_ptr<DWARFASTParserClang> m_dwarf_ast_parser_ap; 1004#ifdef LLDB_ENABLE_ALL 1005 std::unique_ptr<PDBASTParser> m_pdb_ast_parser_ap; 1006#endif // LLDB_ENABLE_ALL 1007 std::unique_ptr<ClangASTSource> m_scratch_ast_source_ap; 1008 std::unique_ptr<clang::MangleContext> m_mangle_ctx_ap; 1009 CompleteTagDeclCallback m_callback_tag_decl; 1010 CompleteObjCInterfaceDeclCallback m_callback_objc_decl; 1011 void * m_callback_baton; 1012 clang::ExternalASTMerger::OriginMap m_origins; 1013 uint32_t m_pointer_byte_size; 1014 bool m_ast_owned; 1015 bool m_can_evaluate_expressions; 1016 // clang-format on 1017private: 1018 //------------------------------------------------------------------ 1019 // For ClangASTContext only 1020 //------------------------------------------------------------------ 1021 ClangASTContext(const ClangASTContext &); 1022 const ClangASTContext &operator=(const ClangASTContext &); 1023}; 1024 1025class ClangASTContextForExpressions : public ClangASTContext { 1026public: 1027 ClangASTContextForExpressions(Target &target); 1028 1029 ~ClangASTContextForExpressions() override = default; 1030 1031 UserExpression * 1032 GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix, 1033 lldb::LanguageType language, 1034 Expression::ResultType desired_type, 1035 const EvaluateExpressionOptions &options) override; 1036 1037 FunctionCaller *GetFunctionCaller(const CompilerType &return_type, 1038 const Address &function_address, 1039 const ValueList &arg_value_list, 1040 const char *name) override; 1041 1042 UtilityFunction *GetUtilityFunction(const char *text, 1043 const char *name) override; 1044 1045 PersistentExpressionState *GetPersistentExpressionState() override; 1046 1047 clang::ExternalASTMerger &GetMergerUnchecked(); 1048 1049 const clang::ExternalASTMerger::OriginMap &GetOriginMap() override { 1050 return GetMergerUnchecked().GetOrigins(); 1051 } 1052private: 1053 lldb::TargetWP m_target_wp; 1054 lldb::ClangPersistentVariablesUP m_persistent_variables; ///< These are the 1055 ///persistent 1056 ///variables 1057 ///associated with 1058 ///this process for 1059 ///the expression 1060 ///parser. 1061}; 1062 1063} // namespace lldb_private 1064 1065#endif // liblldb_ClangASTContext_h_ 1066