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