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