1 //===-- TypeSystemClang.h ---------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLDB_SOURCE_PLUGINS_TYPESYSTEM_CLANG_TYPESYSTEMCLANG_H 10 #define LLDB_SOURCE_PLUGINS_TYPESYSTEM_CLANG_TYPESYSTEMCLANG_H 11 12 #include <cstdint> 13 14 #include <functional> 15 #include <initializer_list> 16 #include <map> 17 #include <memory> 18 #include <set> 19 #include <string> 20 #include <utility> 21 #include <vector> 22 23 #include "clang/AST/ASTContext.h" 24 #include "clang/AST/ASTFwd.h" 25 #include "clang/AST/TemplateBase.h" 26 #include "clang/Basic/TargetInfo.h" 27 #include "llvm/ADT/APSInt.h" 28 #include "llvm/ADT/SmallVector.h" 29 30 #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h" 31 #include "lldb/Expression/ExpressionVariable.h" 32 #include "lldb/Symbol/CompilerType.h" 33 #include "lldb/Symbol/TypeSystem.h" 34 #include "lldb/Target/Target.h" 35 #include "lldb/Utility/ConstString.h" 36 #include "lldb/Utility/Flags.h" 37 #include "lldb/Utility/Log.h" 38 #include "lldb/lldb-enumerations.h" 39 40 class DWARFASTParserClang; 41 class PDBASTParser; 42 43 namespace clang { 44 class FileManager; 45 class HeaderSearch; 46 class ModuleMap; 47 } // namespace clang 48 49 namespace lldb_private { 50 51 class ClangASTMetadata; 52 class ClangASTSource; 53 class Declaration; 54 55 /// A Clang module ID. 56 class OptionalClangModuleID { 57 unsigned m_id = 0; 58 59 public: 60 OptionalClangModuleID() = default; OptionalClangModuleID(unsigned id)61 explicit OptionalClangModuleID(unsigned id) : m_id(id) {} HasValue()62 bool HasValue() const { return m_id != 0; } GetValue()63 unsigned GetValue() const { return m_id; } 64 }; 65 66 /// The implementation of lldb::Type's m_payload field for TypeSystemClang. 67 class TypePayloadClang { 68 /// The Layout is as follows: 69 /// \verbatim 70 /// bit 0..30 ... Owning Module ID. 71 /// bit 31 ...... IsCompleteObjCClass. 72 /// \endverbatim 73 Type::Payload m_payload = 0; 74 75 public: 76 TypePayloadClang() = default; 77 explicit TypePayloadClang(OptionalClangModuleID owning_module, 78 bool is_complete_objc_class = false); TypePayloadClang(uint32_t opaque_payload)79 explicit TypePayloadClang(uint32_t opaque_payload) : m_payload(opaque_payload) {} Payload()80 operator Type::Payload() { return m_payload; } 81 82 static constexpr unsigned ObjCClassBit = 1 << 31; IsCompleteObjCClass()83 bool IsCompleteObjCClass() { return Flags(m_payload).Test(ObjCClassBit); } SetIsCompleteObjCClass(bool is_complete_objc_class)84 void SetIsCompleteObjCClass(bool is_complete_objc_class) { 85 m_payload = is_complete_objc_class ? Flags(m_payload).Set(ObjCClassBit) 86 : Flags(m_payload).Clear(ObjCClassBit); 87 } GetOwningModule()88 OptionalClangModuleID GetOwningModule() { 89 return OptionalClangModuleID(Flags(m_payload).Clear(ObjCClassBit)); 90 } 91 void SetOwningModule(OptionalClangModuleID id); 92 /// \} 93 }; 94 95 /// A TypeSystem implementation based on Clang. 96 /// 97 /// This class uses a single clang::ASTContext as the backend for storing 98 /// its types and declarations. Every clang::ASTContext should also just have 99 /// a single associated TypeSystemClang instance that manages it. 100 /// 101 /// The clang::ASTContext instance can either be created by TypeSystemClang 102 /// itself or it can adopt an existing clang::ASTContext (for example, when 103 /// it is necessary to provide a TypeSystem interface for an existing 104 /// clang::ASTContext that was created by clang::CompilerInstance). 105 class TypeSystemClang : public TypeSystem { 106 // LLVM RTTI support 107 static char ID; 108 109 public: 110 typedef void (*CompleteTagDeclCallback)(void *baton, clang::TagDecl *); 111 typedef void (*CompleteObjCInterfaceDeclCallback)(void *baton, 112 clang::ObjCInterfaceDecl *); 113 114 // llvm casting support isA(const void * ClassID)115 bool isA(const void *ClassID) const override { return ClassID == &ID; } classof(const TypeSystem * ts)116 static bool classof(const TypeSystem *ts) { return ts->isA(&ID); } 117 118 /// Constructs a TypeSystemClang with an ASTContext using the given triple. 119 /// 120 /// \param name The name for the TypeSystemClang (for logging purposes) 121 /// \param triple The llvm::Triple used for the ASTContext. The triple defines 122 /// certain characteristics of the ASTContext and its types 123 /// (e.g., whether certain primitive types exist or what their 124 /// signedness is). 125 explicit TypeSystemClang(llvm::StringRef name, llvm::Triple triple); 126 127 /// Constructs a TypeSystemClang that uses an existing ASTContext internally. 128 /// Useful when having an existing ASTContext created by Clang. 129 /// 130 /// \param name The name for the TypeSystemClang (for logging purposes) 131 /// \param existing_ctxt An existing ASTContext. 132 explicit TypeSystemClang(llvm::StringRef name, 133 clang::ASTContext &existing_ctxt); 134 135 ~TypeSystemClang() override; 136 137 void Finalize() override; 138 139 // PluginInterface functions GetPluginName()140 llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); } 141 GetPluginNameStatic()142 static llvm::StringRef GetPluginNameStatic() { return "clang"; } 143 144 static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language, 145 Module *module, Target *target); 146 147 static LanguageSet GetSupportedLanguagesForTypes(); 148 static LanguageSet GetSupportedLanguagesForExpressions(); 149 150 static void Initialize(); 151 152 static void Terminate(); 153 154 static TypeSystemClang *GetASTContext(clang::ASTContext *ast_ctx); 155 156 /// Returns the display name of this TypeSystemClang that indicates what 157 /// purpose it serves in LLDB. Used for example in logs. getDisplayName()158 llvm::StringRef getDisplayName() const { return m_display_name; } 159 160 /// Returns the clang::ASTContext instance managed by this TypeSystemClang. 161 clang::ASTContext &getASTContext(); 162 163 clang::MangleContext *getMangleContext(); 164 165 std::shared_ptr<clang::TargetOptions> &getTargetOptions(); 166 167 clang::TargetInfo *getTargetInfo(); 168 169 void setSema(clang::Sema *s); getSema()170 clang::Sema *getSema() { return m_sema; } 171 172 const char *GetTargetTriple(); 173 174 void SetExternalSource( 175 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> &ast_source_up); 176 GetCompleteDecl(clang::Decl * decl)177 bool GetCompleteDecl(clang::Decl *decl) { 178 return TypeSystemClang::GetCompleteDecl(&getASTContext(), decl); 179 } 180 181 static void DumpDeclHiearchy(clang::Decl *decl); 182 183 static void DumpDeclContextHiearchy(clang::DeclContext *decl_ctx); 184 185 static bool DeclsAreEquivalent(clang::Decl *lhs_decl, clang::Decl *rhs_decl); 186 187 static bool GetCompleteDecl(clang::ASTContext *ast, clang::Decl *decl); 188 189 void SetMetadataAsUserID(const clang::Decl *decl, lldb::user_id_t user_id); 190 void SetMetadataAsUserID(const clang::Type *type, lldb::user_id_t user_id); 191 192 void SetMetadata(const clang::Decl *object, ClangASTMetadata &meta_data); 193 194 void SetMetadata(const clang::Type *object, ClangASTMetadata &meta_data); 195 ClangASTMetadata *GetMetadata(const clang::Decl *object); 196 ClangASTMetadata *GetMetadata(const clang::Type *object); 197 198 void SetCXXRecordDeclAccess(const clang::CXXRecordDecl *object, 199 clang::AccessSpecifier access); 200 clang::AccessSpecifier 201 GetCXXRecordDeclAccess(const clang::CXXRecordDecl *object); 202 203 // Basic Types 204 CompilerType GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding, 205 size_t bit_size) override; 206 207 CompilerType GetBasicType(lldb::BasicType type); 208 209 static lldb::BasicType GetBasicTypeEnumeration(ConstString name); 210 211 CompilerType 212 GetBuiltinTypeForDWARFEncodingAndBitSize(llvm::StringRef type_name, 213 uint32_t dw_ate, uint32_t bit_size); 214 215 CompilerType GetCStringType(bool is_const); 216 217 static clang::DeclContext *GetDeclContextForType(clang::QualType type); 218 219 static clang::DeclContext *GetDeclContextForType(const CompilerType &type); 220 221 uint32_t GetPointerByteSize() override; 222 GetTranslationUnitDecl()223 clang::TranslationUnitDecl *GetTranslationUnitDecl() { 224 return getASTContext().getTranslationUnitDecl(); 225 } 226 227 static bool AreTypesSame(CompilerType type1, CompilerType type2, 228 bool ignore_qualifiers = false); 229 230 /// Creates a CompilerType form the given QualType with the current 231 /// TypeSystemClang instance as the CompilerType's typesystem. 232 /// \param qt The QualType for a type that belongs to the ASTContext of this 233 /// TypeSystemClang. 234 /// \return The CompilerType representing the given QualType. If the 235 /// QualType's type pointer is a nullptr then the function returns an 236 /// invalid CompilerType. GetType(clang::QualType qt)237 CompilerType GetType(clang::QualType qt) { 238 if (qt.getTypePtrOrNull() == nullptr) 239 return CompilerType(); 240 // Check that the type actually belongs to this TypeSystemClang. 241 assert(qt->getAsTagDecl() == nullptr || 242 &qt->getAsTagDecl()->getASTContext() == &getASTContext()); 243 return CompilerType(this, qt.getAsOpaquePtr()); 244 } 245 246 CompilerType GetTypeForDecl(clang::NamedDecl *decl); 247 248 CompilerType GetTypeForDecl(clang::TagDecl *decl); 249 250 CompilerType GetTypeForDecl(clang::ObjCInterfaceDecl *objc_decl); 251 252 template <typename RecordDeclType> 253 CompilerType 254 GetTypeForIdentifier(ConstString type_name, 255 clang::DeclContext *decl_context = nullptr) { 256 CompilerType compiler_type; 257 258 if (type_name.GetLength()) { 259 clang::ASTContext &ast = getASTContext(); 260 if (!decl_context) 261 decl_context = ast.getTranslationUnitDecl(); 262 263 clang::IdentifierInfo &myIdent = ast.Idents.get(type_name.GetCString()); 264 clang::DeclarationName myName = 265 ast.DeclarationNames.getIdentifier(&myIdent); 266 267 clang::DeclContext::lookup_result result = decl_context->lookup(myName); 268 269 if (!result.empty()) { 270 clang::NamedDecl *named_decl = *result.begin(); 271 if (const RecordDeclType *record_decl = 272 llvm::dyn_cast<RecordDeclType>(named_decl)) 273 compiler_type.SetCompilerType( 274 this, clang::QualType(record_decl->getTypeForDecl(), 0) 275 .getAsOpaquePtr()); 276 } 277 } 278 279 return compiler_type; 280 } 281 282 CompilerType CreateStructForIdentifier( 283 ConstString type_name, 284 const std::initializer_list<std::pair<const char *, CompilerType>> 285 &type_fields, 286 bool packed = false); 287 288 CompilerType GetOrCreateStructForIdentifier( 289 ConstString type_name, 290 const std::initializer_list<std::pair<const char *, CompilerType>> 291 &type_fields, 292 bool packed = false); 293 294 static bool IsOperator(llvm::StringRef name, 295 clang::OverloadedOperatorKind &op_kind); 296 297 // Structure, Unions, Classes 298 299 static clang::AccessSpecifier 300 ConvertAccessTypeToAccessSpecifier(lldb::AccessType access); 301 302 static clang::AccessSpecifier 303 UnifyAccessSpecifiers(clang::AccessSpecifier lhs, clang::AccessSpecifier rhs); 304 305 static uint32_t GetNumBaseClasses(const clang::CXXRecordDecl *cxx_record_decl, 306 bool omit_empty_base_classes); 307 308 /// Synthesize a clang::Module and return its ID or a default-constructed ID. 309 OptionalClangModuleID GetOrCreateClangModule(llvm::StringRef name, 310 OptionalClangModuleID parent, 311 bool is_framework = false, 312 bool is_explicit = false); 313 314 CompilerType CreateRecordType(clang::DeclContext *decl_ctx, 315 OptionalClangModuleID owning_module, 316 lldb::AccessType access_type, 317 llvm::StringRef name, int kind, 318 lldb::LanguageType language, 319 ClangASTMetadata *metadata = nullptr, 320 bool exports_symbols = false); 321 322 class TemplateParameterInfos { 323 public: IsValid()324 bool IsValid() const { 325 // Having a pack name but no packed args doesn't make sense, so mark 326 // these template parameters as invalid. 327 if (pack_name && !packed_args) 328 return false; 329 return args.size() == names.size() && 330 (!packed_args || !packed_args->packed_args); 331 } 332 hasParameterPack()333 bool hasParameterPack() const { return static_cast<bool>(packed_args); } 334 335 llvm::SmallVector<const char *, 2> names; 336 llvm::SmallVector<clang::TemplateArgument, 2> args; 337 338 const char * pack_name = nullptr; 339 std::unique_ptr<TemplateParameterInfos> packed_args; 340 }; 341 342 clang::FunctionTemplateDecl *CreateFunctionTemplateDecl( 343 clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, 344 clang::FunctionDecl *func_decl, const TemplateParameterInfos &infos); 345 346 void CreateFunctionTemplateSpecializationInfo( 347 clang::FunctionDecl *func_decl, clang::FunctionTemplateDecl *Template, 348 const TemplateParameterInfos &infos); 349 350 clang::ClassTemplateDecl *CreateClassTemplateDecl( 351 clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, 352 lldb::AccessType access_type, llvm::StringRef class_name, int kind, 353 const TemplateParameterInfos &infos); 354 355 clang::TemplateTemplateParmDecl * 356 CreateTemplateTemplateParmDecl(const char *template_name); 357 358 clang::ClassTemplateSpecializationDecl *CreateClassTemplateSpecializationDecl( 359 clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, 360 clang::ClassTemplateDecl *class_template_decl, int kind, 361 const TemplateParameterInfos &infos); 362 363 CompilerType 364 CreateClassTemplateSpecializationType(clang::ClassTemplateSpecializationDecl * 365 class_template_specialization_decl); 366 367 static clang::DeclContext * 368 GetAsDeclContext(clang::FunctionDecl *function_decl); 369 370 static bool CheckOverloadedOperatorKindParameterCount( 371 bool is_method, clang::OverloadedOperatorKind op_kind, 372 uint32_t num_params); 373 374 bool FieldIsBitfield(clang::FieldDecl *field, uint32_t &bitfield_bit_size); 375 376 static bool RecordHasFields(const clang::RecordDecl *record_decl); 377 378 CompilerType CreateObjCClass(llvm::StringRef name, 379 clang::DeclContext *decl_ctx, 380 OptionalClangModuleID owning_module, 381 bool isForwardDecl, bool isInternal, 382 ClangASTMetadata *metadata = nullptr); 383 384 // Returns a mask containing bits from the TypeSystemClang::eTypeXXX 385 // enumerations 386 387 // Namespace Declarations 388 389 clang::NamespaceDecl * 390 GetUniqueNamespaceDeclaration(const char *name, clang::DeclContext *decl_ctx, 391 OptionalClangModuleID owning_module, 392 bool is_inline = false); 393 394 // Function Types 395 396 clang::FunctionDecl *CreateFunctionDeclaration( 397 clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, 398 llvm::StringRef name, const CompilerType &function_Type, 399 clang::StorageClass storage, bool is_inline); 400 401 CompilerType CreateFunctionType(const CompilerType &result_type, 402 const CompilerType *args, unsigned num_args, 403 bool is_variadic, unsigned type_quals, 404 clang::CallingConv cc = clang::CC_C); 405 406 clang::ParmVarDecl * 407 CreateParameterDeclaration(clang::DeclContext *decl_ctx, 408 OptionalClangModuleID owning_module, 409 const char *name, const CompilerType ¶m_type, 410 int storage, bool add_decl = false); 411 412 void SetFunctionParameters(clang::FunctionDecl *function_decl, 413 llvm::ArrayRef<clang::ParmVarDecl *> params); 414 415 CompilerType CreateBlockPointerType(const CompilerType &function_type); 416 417 // Array Types 418 419 CompilerType CreateArrayType(const CompilerType &element_type, 420 size_t element_count, bool is_vector); 421 422 // Enumeration Types 423 CompilerType CreateEnumerationType(llvm::StringRef name, 424 clang::DeclContext *decl_ctx, 425 OptionalClangModuleID owning_module, 426 const Declaration &decl, 427 const CompilerType &integer_qual_type, 428 bool is_scoped); 429 430 // Integer type functions 431 432 CompilerType GetIntTypeFromBitSize(size_t bit_size, bool is_signed); 433 434 CompilerType GetPointerSizedIntType(bool is_signed); 435 436 // Floating point functions 437 438 static CompilerType GetFloatTypeFromBitSize(clang::ASTContext *ast, 439 size_t bit_size); 440 441 // TypeSystem methods 442 DWARFASTParser *GetDWARFParser() override; 443 PDBASTParser *GetPDBParser() override; 444 445 // TypeSystemClang callbacks for external source lookups. 446 void CompleteTagDecl(clang::TagDecl *); 447 448 void CompleteObjCInterfaceDecl(clang::ObjCInterfaceDecl *); 449 450 bool LayoutRecordType( 451 const clang::RecordDecl *record_decl, uint64_t &size, uint64_t &alignment, 452 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets, 453 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> 454 &base_offsets, 455 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> 456 &vbase_offsets); 457 458 /// Creates a CompilerDecl from the given Decl with the current 459 /// TypeSystemClang instance as its typesystem. 460 /// The Decl has to come from the ASTContext of this 461 /// TypeSystemClang. GetCompilerDecl(clang::Decl * decl)462 CompilerDecl GetCompilerDecl(clang::Decl *decl) { 463 assert(&decl->getASTContext() == &getASTContext() && 464 "CreateCompilerDecl for Decl from wrong ASTContext?"); 465 return CompilerDecl(this, decl); 466 } 467 468 // CompilerDecl override functions 469 ConstString DeclGetName(void *opaque_decl) override; 470 471 ConstString DeclGetMangledName(void *opaque_decl) override; 472 473 CompilerDeclContext DeclGetDeclContext(void *opaque_decl) override; 474 475 CompilerType DeclGetFunctionReturnType(void *opaque_decl) override; 476 477 size_t DeclGetFunctionNumArguments(void *opaque_decl) override; 478 479 CompilerType DeclGetFunctionArgumentType(void *opaque_decl, 480 size_t arg_idx) override; 481 482 CompilerType GetTypeForDecl(void *opaque_decl) override; 483 484 // CompilerDeclContext override functions 485 486 /// Creates a CompilerDeclContext from the given DeclContext 487 /// with the current TypeSystemClang instance as its typesystem. 488 /// The DeclContext has to come from the ASTContext of this 489 /// TypeSystemClang. 490 CompilerDeclContext CreateDeclContext(clang::DeclContext *ctx); 491 492 /// Set the owning module for \p decl. 493 static void SetOwningModule(clang::Decl *decl, 494 OptionalClangModuleID owning_module); 495 496 std::vector<CompilerDecl> 497 DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name, 498 const bool ignore_using_decls) override; 499 500 ConstString DeclContextGetName(void *opaque_decl_ctx) override; 501 502 ConstString DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) override; 503 504 bool DeclContextIsClassMethod(void *opaque_decl_ctx, 505 lldb::LanguageType *language_ptr, 506 bool *is_instance_method_ptr, 507 ConstString *language_object_name_ptr) override; 508 509 bool DeclContextIsContainedInLookup(void *opaque_decl_ctx, 510 void *other_opaque_decl_ctx) override; 511 512 // Clang specific clang::DeclContext functions 513 514 static clang::DeclContext * 515 DeclContextGetAsDeclContext(const CompilerDeclContext &dc); 516 517 static clang::ObjCMethodDecl * 518 DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc); 519 520 static clang::CXXMethodDecl * 521 DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc); 522 523 static clang::FunctionDecl * 524 DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc); 525 526 static clang::NamespaceDecl * 527 DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc); 528 529 static ClangASTMetadata *DeclContextGetMetaData(const CompilerDeclContext &dc, 530 const clang::Decl *object); 531 532 static clang::ASTContext * 533 DeclContextGetTypeSystemClang(const CompilerDeclContext &dc); 534 535 // Tests 536 537 #ifndef NDEBUG 538 bool Verify(lldb::opaque_compiler_type_t type) override; 539 #endif 540 541 bool IsArrayType(lldb::opaque_compiler_type_t type, 542 CompilerType *element_type, uint64_t *size, 543 bool *is_incomplete) override; 544 545 bool IsVectorType(lldb::opaque_compiler_type_t type, 546 CompilerType *element_type, uint64_t *size) override; 547 548 bool IsAggregateType(lldb::opaque_compiler_type_t type) override; 549 550 bool IsAnonymousType(lldb::opaque_compiler_type_t type) override; 551 552 bool IsBeingDefined(lldb::opaque_compiler_type_t type) override; 553 554 bool IsCharType(lldb::opaque_compiler_type_t type) override; 555 556 bool IsCompleteType(lldb::opaque_compiler_type_t type) override; 557 558 bool IsConst(lldb::opaque_compiler_type_t type) override; 559 560 bool IsCStringType(lldb::opaque_compiler_type_t type, 561 uint32_t &length) override; 562 563 static bool IsCXXClassType(const CompilerType &type); 564 565 bool IsDefined(lldb::opaque_compiler_type_t type) override; 566 567 bool IsFloatingPointType(lldb::opaque_compiler_type_t type, uint32_t &count, 568 bool &is_complex) override; 569 570 bool IsFunctionType(lldb::opaque_compiler_type_t type) override; 571 572 uint32_t IsHomogeneousAggregate(lldb::opaque_compiler_type_t type, 573 CompilerType *base_type_ptr) override; 574 575 size_t 576 GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) override; 577 578 CompilerType GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type, 579 const size_t index) override; 580 581 bool IsFunctionPointerType(lldb::opaque_compiler_type_t type) override; 582 583 bool IsBlockPointerType(lldb::opaque_compiler_type_t type, 584 CompilerType *function_pointer_type_ptr) override; 585 586 bool IsIntegerType(lldb::opaque_compiler_type_t type, 587 bool &is_signed) override; 588 589 bool IsEnumerationType(lldb::opaque_compiler_type_t type, 590 bool &is_signed) override; 591 592 bool IsScopedEnumerationType(lldb::opaque_compiler_type_t type) override; 593 594 static bool IsObjCClassType(const CompilerType &type); 595 596 static bool IsObjCClassTypeAndHasIVars(const CompilerType &type, 597 bool check_superclass); 598 599 static bool IsObjCObjectOrInterfaceType(const CompilerType &type); 600 601 static bool IsObjCObjectPointerType(const CompilerType &type, 602 CompilerType *target_type = nullptr); 603 604 bool IsPolymorphicClass(lldb::opaque_compiler_type_t type) override; 605 606 static bool IsClassType(lldb::opaque_compiler_type_t type); 607 608 static bool IsEnumType(lldb::opaque_compiler_type_t type); 609 610 bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type, 611 CompilerType *target_type, // Can pass nullptr 612 bool check_cplusplus, bool check_objc) override; 613 614 bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) override; 615 616 bool IsPointerType(lldb::opaque_compiler_type_t type, 617 CompilerType *pointee_type) override; 618 619 bool IsPointerOrReferenceType(lldb::opaque_compiler_type_t type, 620 CompilerType *pointee_type) override; 621 622 bool IsReferenceType(lldb::opaque_compiler_type_t type, 623 CompilerType *pointee_type, bool *is_rvalue) override; 624 625 bool IsScalarType(lldb::opaque_compiler_type_t type) override; 626 627 bool IsTypedefType(lldb::opaque_compiler_type_t type) override; 628 629 bool IsVoidType(lldb::opaque_compiler_type_t type) override; 630 631 bool CanPassInRegisters(const CompilerType &type) override; 632 633 bool SupportsLanguage(lldb::LanguageType language) override; 634 635 static llvm::Optional<std::string> GetCXXClassName(const CompilerType &type); 636 637 // Type Completion 638 639 bool GetCompleteType(lldb::opaque_compiler_type_t type) override; 640 641 // Accessors 642 643 ConstString GetTypeName(lldb::opaque_compiler_type_t type) override; 644 645 ConstString GetDisplayTypeName(lldb::opaque_compiler_type_t type) override; 646 647 uint32_t GetTypeInfo(lldb::opaque_compiler_type_t type, 648 CompilerType *pointee_or_element_compiler_type) override; 649 650 lldb::LanguageType 651 GetMinimumLanguage(lldb::opaque_compiler_type_t type) override; 652 653 lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) override; 654 655 unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) override; 656 657 // Creating related types 658 659 CompilerType GetArrayElementType(lldb::opaque_compiler_type_t type, 660 ExecutionContextScope *exe_scope) override; 661 662 CompilerType GetArrayType(lldb::opaque_compiler_type_t type, 663 uint64_t size) override; 664 665 CompilerType GetCanonicalType(lldb::opaque_compiler_type_t type) override; 666 667 CompilerType 668 GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) override; 669 670 CompilerType 671 GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) override; 672 673 // Returns -1 if this isn't a function of if the function doesn't have a 674 // prototype Returns a value >= 0 if there is a prototype. 675 int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) override; 676 677 CompilerType GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type, 678 size_t idx) override; 679 680 CompilerType 681 GetFunctionReturnType(lldb::opaque_compiler_type_t type) override; 682 683 size_t GetNumMemberFunctions(lldb::opaque_compiler_type_t type) override; 684 685 TypeMemberFunctionImpl 686 GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, 687 size_t idx) override; 688 689 CompilerType GetNonReferenceType(lldb::opaque_compiler_type_t type) override; 690 691 CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) override; 692 693 CompilerType GetPointerType(lldb::opaque_compiler_type_t type) override; 694 695 CompilerType 696 GetLValueReferenceType(lldb::opaque_compiler_type_t type) override; 697 698 CompilerType 699 GetRValueReferenceType(lldb::opaque_compiler_type_t type) override; 700 701 CompilerType GetAtomicType(lldb::opaque_compiler_type_t type) override; 702 703 CompilerType AddConstModifier(lldb::opaque_compiler_type_t type) override; 704 705 CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type) override; 706 707 CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type) override; 708 709 /// Using the current type, create a new typedef to that type using 710 /// "typedef_name" as the name and "decl_ctx" as the decl context. 711 /// \param opaque_payload is an opaque TypePayloadClang. 712 CompilerType CreateTypedef(lldb::opaque_compiler_type_t type, 713 const char *name, 714 const CompilerDeclContext &decl_ctx, 715 uint32_t opaque_payload) override; 716 717 // If the current object represents a typedef type, get the underlying type 718 CompilerType GetTypedefedType(lldb::opaque_compiler_type_t type) override; 719 720 // Create related types using the current type's AST 721 CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) override; 722 723 // Exploring the type 724 725 const llvm::fltSemantics &GetFloatTypeSemantics(size_t byte_size) override; 726 GetByteSize(lldb::opaque_compiler_type_t type,ExecutionContextScope * exe_scope)727 llvm::Optional<uint64_t> GetByteSize(lldb::opaque_compiler_type_t type, 728 ExecutionContextScope *exe_scope) { 729 if (llvm::Optional<uint64_t> bit_size = GetBitSize(type, exe_scope)) 730 return (*bit_size + 7) / 8; 731 return llvm::None; 732 } 733 734 llvm::Optional<uint64_t> 735 GetBitSize(lldb::opaque_compiler_type_t type, 736 ExecutionContextScope *exe_scope) override; 737 738 lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type, 739 uint64_t &count) override; 740 741 lldb::Format GetFormat(lldb::opaque_compiler_type_t type) override; 742 743 llvm::Optional<size_t> 744 GetTypeBitAlign(lldb::opaque_compiler_type_t type, 745 ExecutionContextScope *exe_scope) override; 746 747 uint32_t GetNumChildren(lldb::opaque_compiler_type_t type, 748 bool omit_empty_base_classes, 749 const ExecutionContext *exe_ctx) override; 750 751 CompilerType GetBuiltinTypeByName(ConstString name) override; 752 753 lldb::BasicType 754 GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) override; 755 756 static lldb::BasicType 757 GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type, 758 ConstString name); 759 760 void ForEachEnumerator( 761 lldb::opaque_compiler_type_t type, 762 std::function<bool(const CompilerType &integer_type, 763 ConstString name, 764 const llvm::APSInt &value)> const &callback) override; 765 766 uint32_t GetNumFields(lldb::opaque_compiler_type_t type) override; 767 768 CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type, size_t idx, 769 std::string &name, uint64_t *bit_offset_ptr, 770 uint32_t *bitfield_bit_size_ptr, 771 bool *is_bitfield_ptr) override; 772 773 uint32_t GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) override; 774 775 uint32_t GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) override; 776 777 CompilerType GetDirectBaseClassAtIndex(lldb::opaque_compiler_type_t type, 778 size_t idx, 779 uint32_t *bit_offset_ptr) override; 780 781 CompilerType GetVirtualBaseClassAtIndex(lldb::opaque_compiler_type_t type, 782 size_t idx, 783 uint32_t *bit_offset_ptr) override; 784 785 static uint32_t GetNumPointeeChildren(clang::QualType type); 786 787 CompilerType GetChildCompilerTypeAtIndex( 788 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx, 789 bool transparent_pointers, bool omit_empty_base_classes, 790 bool ignore_array_bounds, std::string &child_name, 791 uint32_t &child_byte_size, int32_t &child_byte_offset, 792 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset, 793 bool &child_is_base_class, bool &child_is_deref_of_parent, 794 ValueObject *valobj, uint64_t &language_flags) override; 795 796 // Lookup a child given a name. This function will match base class names and 797 // member member names in "clang_type" only, not descendants. 798 uint32_t GetIndexOfChildWithName(lldb::opaque_compiler_type_t type, 799 const char *name, 800 bool omit_empty_base_classes) override; 801 802 // Lookup a child member given a name. This function will match member names 803 // only and will descend into "clang_type" children in search for the first 804 // member in this class, or any base class that matches "name". 805 // TODO: Return all matches for a given name by returning a 806 // vector<vector<uint32_t>> 807 // so we catch all names that match a given child name, not just the first. 808 size_t 809 GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type, 810 const char *name, bool omit_empty_base_classes, 811 std::vector<uint32_t> &child_indexes) override; 812 813 size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type, 814 bool expand_pack) override; 815 816 lldb::TemplateArgumentKind 817 GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, size_t idx, 818 bool expand_pack) override; 819 CompilerType GetTypeTemplateArgument(lldb::opaque_compiler_type_t type, 820 size_t idx, bool expand_pack) override; 821 llvm::Optional<CompilerType::IntegralTemplateArgument> 822 GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx, 823 bool expand_pack) override; 824 825 CompilerType GetTypeForFormatters(void *type) override; 826 827 #define LLDB_INVALID_DECL_LEVEL UINT32_MAX 828 // LLDB_INVALID_DECL_LEVEL is returned by CountDeclLevels if child_decl_ctx 829 // could not be found in decl_ctx. 830 uint32_t CountDeclLevels(clang::DeclContext *frame_decl_ctx, 831 clang::DeclContext *child_decl_ctx, 832 ConstString *child_name = nullptr, 833 CompilerType *child_type = nullptr); 834 835 // Modifying RecordType 836 static clang::FieldDecl *AddFieldToRecordType(const CompilerType &type, 837 llvm::StringRef name, 838 const CompilerType &field_type, 839 lldb::AccessType access, 840 uint32_t bitfield_bit_size); 841 842 static void BuildIndirectFields(const CompilerType &type); 843 844 static void SetIsPacked(const CompilerType &type); 845 846 static clang::VarDecl *AddVariableToRecordType(const CompilerType &type, 847 llvm::StringRef name, 848 const CompilerType &var_type, 849 lldb::AccessType access); 850 851 /// Initializes a variable with an integer value. 852 /// \param var The variable to initialize. Must not already have an 853 /// initializer and must have an integer or enum type. 854 /// \param init_value The integer value that the variable should be 855 /// initialized to. Has to match the bit width of the 856 /// variable type. 857 static void SetIntegerInitializerForVariable(clang::VarDecl *var, 858 const llvm::APInt &init_value); 859 860 /// Initializes a variable with a floating point value. 861 /// \param var The variable to initialize. Must not already have an 862 /// initializer and must have a floating point type. 863 /// \param init_value The float value that the variable should be 864 /// initialized to. 865 static void 866 SetFloatingInitializerForVariable(clang::VarDecl *var, 867 const llvm::APFloat &init_value); 868 869 clang::CXXMethodDecl *AddMethodToCXXRecordType( 870 lldb::opaque_compiler_type_t type, llvm::StringRef name, 871 const char *mangled_name, const CompilerType &method_type, 872 lldb::AccessType access, bool is_virtual, bool is_static, bool is_inline, 873 bool is_explicit, bool is_attr_used, bool is_artificial); 874 875 void AddMethodOverridesForCXXRecordType(lldb::opaque_compiler_type_t type); 876 877 // C++ Base Classes 878 std::unique_ptr<clang::CXXBaseSpecifier> 879 CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type, 880 lldb::AccessType access, bool is_virtual, 881 bool base_of_class); 882 883 bool TransferBaseClasses( 884 lldb::opaque_compiler_type_t type, 885 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases); 886 887 static bool SetObjCSuperClass(const CompilerType &type, 888 const CompilerType &superclass_compiler_type); 889 890 static bool AddObjCClassProperty(const CompilerType &type, 891 const char *property_name, 892 const CompilerType &property_compiler_type, 893 clang::ObjCIvarDecl *ivar_decl, 894 const char *property_setter_name, 895 const char *property_getter_name, 896 uint32_t property_attributes, 897 ClangASTMetadata *metadata); 898 899 static clang::ObjCMethodDecl *AddMethodToObjCObjectType( 900 const CompilerType &type, 901 const char *name, // the full symbol name as seen in the symbol table 902 // (lldb::opaque_compiler_type_t type, "-[NString 903 // stringWithCString:]") 904 const CompilerType &method_compiler_type, lldb::AccessType access, 905 bool is_artificial, bool is_variadic, bool is_objc_direct_call); 906 907 static bool SetHasExternalStorage(lldb::opaque_compiler_type_t type, 908 bool has_extern); 909 910 // Tag Declarations 911 static bool StartTagDeclarationDefinition(const CompilerType &type); 912 913 static bool CompleteTagDeclarationDefinition(const CompilerType &type); 914 915 // Modifying Enumeration types 916 clang::EnumConstantDecl *AddEnumerationValueToEnumerationType( 917 const CompilerType &enum_type, const Declaration &decl, const char *name, 918 int64_t enum_value, uint32_t enum_value_bit_size); 919 clang::EnumConstantDecl *AddEnumerationValueToEnumerationType( 920 const CompilerType &enum_type, const Declaration &decl, const char *name, 921 const llvm::APSInt &value); 922 923 /// Returns the underlying integer type for an enum type. If the given type 924 /// is invalid or not an enum-type, the function returns an invalid 925 /// CompilerType. 926 CompilerType GetEnumerationIntegerType(CompilerType type); 927 928 // Pointers & References 929 930 // Call this function using the class type when you want to make a member 931 // pointer type to pointee_type. 932 static CompilerType CreateMemberPointerType(const CompilerType &type, 933 const CompilerType &pointee_type); 934 935 // Dumping types 936 #ifndef NDEBUG 937 /// Convenience LLVM-style dump method for use in the debugger only. 938 /// In contrast to the other \p Dump() methods this directly invokes 939 /// \p clang::QualType::dump(). 940 LLVM_DUMP_METHOD void dump(lldb::opaque_compiler_type_t type) const override; 941 #endif 942 943 /// \see lldb_private::TypeSystem::Dump 944 void Dump(llvm::raw_ostream &output) override; 945 946 /// Dump clang AST types from the symbol file. 947 /// 948 /// \param[in] s 949 /// A stream to send the dumped AST node(s) to 950 /// \param[in] symbol_name 951 /// The name of the symbol to dump, if it is empty dump all the symbols 952 void DumpFromSymbolFile(Stream &s, llvm::StringRef symbol_name); 953 954 void DumpValue(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, 955 Stream *s, lldb::Format format, const DataExtractor &data, 956 lldb::offset_t data_offset, size_t data_byte_size, 957 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, 958 bool show_types, bool show_summary, bool verbose, 959 uint32_t depth) override; 960 961 bool DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s, 962 lldb::Format format, const DataExtractor &data, 963 lldb::offset_t data_offset, size_t data_byte_size, 964 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, 965 ExecutionContextScope *exe_scope) override; 966 967 void DumpSummary(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, 968 Stream *s, const DataExtractor &data, 969 lldb::offset_t data_offset, size_t data_byte_size) override; 970 971 void DumpTypeDescription( 972 lldb::opaque_compiler_type_t type, 973 lldb::DescriptionLevel level = lldb::eDescriptionLevelFull) override; 974 975 void DumpTypeDescription( 976 lldb::opaque_compiler_type_t type, Stream *s, 977 lldb::DescriptionLevel level = lldb::eDescriptionLevelFull) override; 978 979 static void DumpTypeName(const CompilerType &type); 980 981 static clang::EnumDecl *GetAsEnumDecl(const CompilerType &type); 982 983 static clang::RecordDecl *GetAsRecordDecl(const CompilerType &type); 984 985 static clang::TagDecl *GetAsTagDecl(const CompilerType &type); 986 987 static clang::TypedefNameDecl *GetAsTypedefDecl(const CompilerType &type); 988 989 static clang::CXXRecordDecl * 990 GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type); 991 992 static clang::ObjCInterfaceDecl * 993 GetAsObjCInterfaceDecl(const CompilerType &type); 994 995 clang::ClassTemplateDecl *ParseClassTemplateDecl( 996 clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, 997 lldb::AccessType access_type, const char *parent_name, int tag_decl_kind, 998 const TypeSystemClang::TemplateParameterInfos &template_param_infos); 999 1000 clang::BlockDecl *CreateBlockDeclaration(clang::DeclContext *ctx, 1001 OptionalClangModuleID owning_module); 1002 1003 clang::UsingDirectiveDecl * 1004 CreateUsingDirectiveDeclaration(clang::DeclContext *decl_ctx, 1005 OptionalClangModuleID owning_module, 1006 clang::NamespaceDecl *ns_decl); 1007 1008 clang::UsingDecl *CreateUsingDeclaration(clang::DeclContext *current_decl_ctx, 1009 OptionalClangModuleID owning_module, 1010 clang::NamedDecl *target); 1011 1012 clang::VarDecl *CreateVariableDeclaration(clang::DeclContext *decl_context, 1013 OptionalClangModuleID owning_module, 1014 const char *name, 1015 clang::QualType type); 1016 1017 static lldb::opaque_compiler_type_t 1018 GetOpaqueCompilerType(clang::ASTContext *ast, lldb::BasicType basic_type); 1019 GetQualType(lldb::opaque_compiler_type_t type)1020 static clang::QualType GetQualType(lldb::opaque_compiler_type_t type) { 1021 if (type) 1022 return clang::QualType::getFromOpaquePtr(type); 1023 return clang::QualType(); 1024 } 1025 1026 static clang::QualType GetCanonicalQualType(lldb::opaque_compiler_type_t type)1027 GetCanonicalQualType(lldb::opaque_compiler_type_t type) { 1028 if (type) 1029 return clang::QualType::getFromOpaquePtr(type).getCanonicalType(); 1030 return clang::QualType(); 1031 } 1032 1033 clang::DeclarationName 1034 GetDeclarationName(llvm::StringRef name, 1035 const CompilerType &function_clang_type); 1036 GetLangOpts()1037 clang::LangOptions *GetLangOpts() const { 1038 return m_language_options_up.get(); 1039 } GetSourceMgr()1040 clang::SourceManager *GetSourceMgr() const { 1041 return m_source_manager_up.get(); 1042 } 1043 1044 private: 1045 /// Returns the PrintingPolicy used when generating the internal type names. 1046 /// These type names are mostly used for the formatter selection. 1047 clang::PrintingPolicy GetTypePrintingPolicy(); 1048 /// Returns the internal type name for the given NamedDecl using the 1049 /// type printing policy. 1050 std::string GetTypeNameForDecl(const clang::NamedDecl *named_decl); 1051 1052 const clang::ClassTemplateSpecializationDecl * 1053 GetAsTemplateSpecialization(lldb::opaque_compiler_type_t type); 1054 1055 // Classes that inherit from TypeSystemClang can see and modify these 1056 std::string m_target_triple; 1057 std::unique_ptr<clang::ASTContext> m_ast_up; 1058 std::unique_ptr<clang::LangOptions> m_language_options_up; 1059 std::unique_ptr<clang::FileManager> m_file_manager_up; 1060 std::unique_ptr<clang::SourceManager> m_source_manager_up; 1061 std::unique_ptr<clang::DiagnosticsEngine> m_diagnostics_engine_up; 1062 std::unique_ptr<clang::DiagnosticConsumer> m_diagnostic_consumer_up; 1063 std::shared_ptr<clang::TargetOptions> m_target_options_rp; 1064 std::unique_ptr<clang::TargetInfo> m_target_info_up; 1065 std::unique_ptr<clang::IdentifierTable> m_identifier_table_up; 1066 std::unique_ptr<clang::SelectorTable> m_selector_table_up; 1067 std::unique_ptr<clang::Builtin::Context> m_builtins_up; 1068 std::unique_ptr<clang::HeaderSearch> m_header_search_up; 1069 std::unique_ptr<clang::ModuleMap> m_module_map_up; 1070 std::unique_ptr<DWARFASTParserClang> m_dwarf_ast_parser_up; 1071 std::unique_ptr<PDBASTParser> m_pdb_ast_parser_up; 1072 std::unique_ptr<clang::MangleContext> m_mangle_ctx_up; 1073 uint32_t m_pointer_byte_size = 0; 1074 bool m_ast_owned = false; 1075 /// A string describing what this TypeSystemClang represents (e.g., 1076 /// AST for debug information, an expression, some other utility ClangAST). 1077 /// Useful for logging and debugging. 1078 std::string m_display_name; 1079 1080 typedef llvm::DenseMap<const clang::Decl *, ClangASTMetadata> DeclMetadataMap; 1081 /// Maps Decls to their associated ClangASTMetadata. 1082 DeclMetadataMap m_decl_metadata; 1083 1084 typedef llvm::DenseMap<const clang::Type *, ClangASTMetadata> TypeMetadataMap; 1085 /// Maps Types to their associated ClangASTMetadata. 1086 TypeMetadataMap m_type_metadata; 1087 1088 typedef llvm::DenseMap<const clang::CXXRecordDecl *, clang::AccessSpecifier> 1089 CXXRecordDeclAccessMap; 1090 /// Maps CXXRecordDecl to their most recent added method/field's 1091 /// AccessSpecifier. 1092 CXXRecordDeclAccessMap m_cxx_record_decl_access; 1093 1094 /// The sema associated that is currently used to build this ASTContext. 1095 /// May be null if we are already done parsing this ASTContext or the 1096 /// ASTContext wasn't created by parsing source code. 1097 clang::Sema *m_sema = nullptr; 1098 1099 // For TypeSystemClang only 1100 TypeSystemClang(const TypeSystemClang &); 1101 const TypeSystemClang &operator=(const TypeSystemClang &); 1102 /// Creates the internal ASTContext. 1103 void CreateASTContext(); 1104 void SetTargetTriple(llvm::StringRef target_triple); 1105 }; 1106 1107 /// The TypeSystemClang instance used for the scratch ASTContext in a 1108 /// lldb::Target. 1109 class ScratchTypeSystemClang : public TypeSystemClang { 1110 /// LLVM RTTI support 1111 static char ID; 1112 1113 public: 1114 ScratchTypeSystemClang(Target &target, llvm::Triple triple); 1115 1116 ~ScratchTypeSystemClang() override = default; 1117 1118 void Finalize() override; 1119 1120 /// The different kinds of isolated ASTs within the scratch TypeSystem. 1121 /// 1122 /// These ASTs are isolated from the main scratch AST and are each 1123 /// dedicated to a special language option/feature that makes the contained 1124 /// AST nodes incompatible with other AST nodes. 1125 enum IsolatedASTKind { 1126 /// The isolated AST for declarations/types from expressions that imported 1127 /// type information from a C++ module. The templates from a C++ module 1128 /// often conflict with the templates we generate from debug information, 1129 /// so we put these types in their own AST. 1130 CppModules 1131 }; 1132 1133 /// Alias for requesting the default scratch TypeSystemClang in GetForTarget. 1134 // This isn't constexpr as gtest/llvm::Optional comparison logic is trying 1135 // to get the address of this for pretty-printing. 1136 static const llvm::NoneType DefaultAST; 1137 1138 /// Infers the appropriate sub-AST from Clang's LangOptions. 1139 static llvm::Optional<IsolatedASTKind> InferIsolatedASTKindFromLangOpts(const clang::LangOptions & l)1140 InferIsolatedASTKindFromLangOpts(const clang::LangOptions &l) { 1141 // If modules are activated we want the dedicated C++ module AST. 1142 // See IsolatedASTKind::CppModules for more info. 1143 if (l.Modules) 1144 return IsolatedASTKind::CppModules; 1145 return DefaultAST; 1146 } 1147 1148 /// Returns the scratch TypeSystemClang for the given target. 1149 /// \param target The Target which scratch TypeSystemClang should be returned. 1150 /// \param ast_kind Allows requesting a specific sub-AST instead of the 1151 /// default scratch AST. See also `IsolatedASTKind`. 1152 /// \param create_on_demand If the scratch TypeSystemClang instance can be 1153 /// created by this call if it doesn't exist yet. If it doesn't exist yet and 1154 /// this parameter is false, this function returns a nullptr. 1155 /// \return The scratch type system of the target or a nullptr in case an 1156 /// error occurred. 1157 static TypeSystemClang * 1158 GetForTarget(Target &target, 1159 llvm::Optional<IsolatedASTKind> ast_kind = DefaultAST, 1160 bool create_on_demand = true); 1161 1162 /// Returns the scratch TypeSystemClang for the given target. The returned 1163 /// TypeSystemClang will be the scratch AST or a sub-AST, depending on which 1164 /// fits best to the passed LangOptions. 1165 /// \param target The Target which scratch TypeSystemClang should be returned. 1166 /// \param lang_opts The LangOptions of a clang ASTContext that the caller 1167 /// wants to export type information from. This is used to 1168 /// find the best matching sub-AST that will be returned. GetForTarget(Target & target,const clang::LangOptions & lang_opts)1169 static TypeSystemClang *GetForTarget(Target &target, 1170 const clang::LangOptions &lang_opts) { 1171 return GetForTarget(target, InferIsolatedASTKindFromLangOpts(lang_opts)); 1172 } 1173 1174 /// \see lldb_private::TypeSystem::Dump 1175 void Dump(llvm::raw_ostream &output) override; 1176 1177 UserExpression * 1178 GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix, 1179 lldb::LanguageType language, 1180 Expression::ResultType desired_type, 1181 const EvaluateExpressionOptions &options, 1182 ValueObject *ctx_obj) override; 1183 1184 FunctionCaller *GetFunctionCaller(const CompilerType &return_type, 1185 const Address &function_address, 1186 const ValueList &arg_value_list, 1187 const char *name) override; 1188 1189 std::unique_ptr<UtilityFunction> 1190 CreateUtilityFunction(std::string text, std::string name) override; 1191 1192 PersistentExpressionState *GetPersistentExpressionState() override; 1193 1194 /// Unregisters the given ASTContext as a source from the scratch AST (and 1195 /// all sub-ASTs). 1196 /// \see ClangASTImporter::ForgetSource 1197 void ForgetSource(clang::ASTContext *src_ctx, ClangASTImporter &importer); 1198 1199 // llvm casting support isA(const void * ClassID)1200 bool isA(const void *ClassID) const override { 1201 return ClassID == &ID || TypeSystemClang::isA(ClassID); 1202 } classof(const TypeSystem * ts)1203 static bool classof(const TypeSystem *ts) { return ts->isA(&ID); } 1204 1205 private: 1206 std::unique_ptr<ClangASTSource> CreateASTSource(); 1207 /// Returns the requested sub-AST. 1208 /// Will lazily create the sub-AST if it hasn't been created before. 1209 TypeSystemClang &GetIsolatedAST(IsolatedASTKind feature); 1210 1211 /// The target triple. 1212 /// This was potentially adjusted and might not be identical to the triple 1213 /// of `m_target_wp`. 1214 llvm::Triple m_triple; 1215 lldb::TargetWP m_target_wp; 1216 /// The persistent variables associated with this process for the expression 1217 /// parser. 1218 std::unique_ptr<ClangPersistentVariables> m_persistent_variables; 1219 /// The ExternalASTSource that performs lookups and completes minimally 1220 /// imported types. 1221 std::unique_ptr<ClangASTSource> m_scratch_ast_source_up; 1222 1223 // FIXME: GCC 5.x doesn't support enum as map keys. 1224 typedef int IsolatedASTKey; 1225 1226 /// Map from IsolatedASTKind to their actual TypeSystemClang instance. 1227 /// This map is lazily filled with sub-ASTs and should be accessed via 1228 /// `GetSubAST` (which lazily fills this map). 1229 std::unordered_map<IsolatedASTKey, std::unique_ptr<TypeSystemClang>> 1230 m_isolated_asts; 1231 }; 1232 1233 } // namespace lldb_private 1234 1235 #endif // LLDB_SOURCE_PLUGINS_TYPESYSTEM_CLANG_TYPESYSTEMCLANG_H 1236