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(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 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(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: 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 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 #ifdef LLDB_ENABLE_ALL 457 PDBASTParser *GetPDBParser() override; 458 npdb::PdbAstBuilder *GetNativePDBParser() override; 459 #endif // LLDB_ENABLE_ALL 460 461 // TypeSystemClang callbacks for external source lookups. 462 void CompleteTagDecl(clang::TagDecl *); 463 464 void CompleteObjCInterfaceDecl(clang::ObjCInterfaceDecl *); 465 466 bool LayoutRecordType( 467 const clang::RecordDecl *record_decl, uint64_t &size, uint64_t &alignment, 468 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets, 469 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> 470 &base_offsets, 471 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> 472 &vbase_offsets); 473 474 /// Creates a CompilerDecl from the given Decl with the current 475 /// TypeSystemClang instance as its typesystem. 476 /// The Decl has to come from the ASTContext of this 477 /// TypeSystemClang. 478 CompilerDecl GetCompilerDecl(clang::Decl *decl) { 479 assert(&decl->getASTContext() == &getASTContext() && 480 "CreateCompilerDecl for Decl from wrong ASTContext?"); 481 return CompilerDecl(this, decl); 482 } 483 484 // CompilerDecl override functions 485 ConstString DeclGetName(void *opaque_decl) override; 486 487 ConstString DeclGetMangledName(void *opaque_decl) override; 488 489 CompilerDeclContext DeclGetDeclContext(void *opaque_decl) override; 490 491 CompilerType DeclGetFunctionReturnType(void *opaque_decl) override; 492 493 size_t DeclGetFunctionNumArguments(void *opaque_decl) override; 494 495 CompilerType DeclGetFunctionArgumentType(void *opaque_decl, 496 size_t arg_idx) override; 497 498 CompilerType GetTypeForDecl(void *opaque_decl) override; 499 500 // CompilerDeclContext override functions 501 502 /// Creates a CompilerDeclContext from the given DeclContext 503 /// with the current TypeSystemClang instance as its typesystem. 504 /// The DeclContext has to come from the ASTContext of this 505 /// TypeSystemClang. 506 CompilerDeclContext CreateDeclContext(clang::DeclContext *ctx); 507 508 /// Set the owning module for \p decl. 509 static void SetOwningModule(clang::Decl *decl, 510 OptionalClangModuleID owning_module); 511 512 std::vector<CompilerDecl> 513 DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name, 514 const bool ignore_using_decls) override; 515 516 ConstString DeclContextGetName(void *opaque_decl_ctx) override; 517 518 ConstString DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) override; 519 520 bool DeclContextIsClassMethod(void *opaque_decl_ctx, 521 lldb::LanguageType *language_ptr, 522 bool *is_instance_method_ptr, 523 ConstString *language_object_name_ptr) override; 524 525 bool DeclContextIsContainedInLookup(void *opaque_decl_ctx, 526 void *other_opaque_decl_ctx) override; 527 528 // Clang specific clang::DeclContext functions 529 530 static clang::DeclContext * 531 DeclContextGetAsDeclContext(const CompilerDeclContext &dc); 532 533 static clang::ObjCMethodDecl * 534 DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc); 535 536 static clang::CXXMethodDecl * 537 DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc); 538 539 static clang::FunctionDecl * 540 DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc); 541 542 static clang::NamespaceDecl * 543 DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc); 544 545 static ClangASTMetadata *DeclContextGetMetaData(const CompilerDeclContext &dc, 546 const clang::Decl *object); 547 548 static clang::ASTContext * 549 DeclContextGetTypeSystemClang(const CompilerDeclContext &dc); 550 551 // Tests 552 553 #ifndef NDEBUG 554 bool Verify(lldb::opaque_compiler_type_t type) override; 555 #endif 556 557 bool IsArrayType(lldb::opaque_compiler_type_t type, 558 CompilerType *element_type, uint64_t *size, 559 bool *is_incomplete) override; 560 561 bool IsVectorType(lldb::opaque_compiler_type_t type, 562 CompilerType *element_type, uint64_t *size) override; 563 564 bool IsAggregateType(lldb::opaque_compiler_type_t type) override; 565 566 bool IsAnonymousType(lldb::opaque_compiler_type_t type) override; 567 568 bool IsBeingDefined(lldb::opaque_compiler_type_t type) override; 569 570 bool IsCharType(lldb::opaque_compiler_type_t type) override; 571 572 bool IsCompleteType(lldb::opaque_compiler_type_t type) override; 573 574 bool IsConst(lldb::opaque_compiler_type_t type) override; 575 576 bool IsCStringType(lldb::opaque_compiler_type_t type, 577 uint32_t &length) override; 578 579 static bool IsCXXClassType(const CompilerType &type); 580 581 bool IsDefined(lldb::opaque_compiler_type_t type) override; 582 583 bool IsFloatingPointType(lldb::opaque_compiler_type_t type, uint32_t &count, 584 bool &is_complex) override; 585 586 bool IsFunctionType(lldb::opaque_compiler_type_t type) override; 587 588 uint32_t IsHomogeneousAggregate(lldb::opaque_compiler_type_t type, 589 CompilerType *base_type_ptr) override; 590 591 size_t 592 GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) override; 593 594 CompilerType GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type, 595 const size_t index) override; 596 597 bool IsFunctionPointerType(lldb::opaque_compiler_type_t type) override; 598 599 bool IsBlockPointerType(lldb::opaque_compiler_type_t type, 600 CompilerType *function_pointer_type_ptr) override; 601 602 bool IsIntegerType(lldb::opaque_compiler_type_t type, 603 bool &is_signed) override; 604 605 bool IsEnumerationType(lldb::opaque_compiler_type_t type, 606 bool &is_signed) override; 607 608 bool IsScopedEnumerationType(lldb::opaque_compiler_type_t type) override; 609 610 static bool IsObjCClassType(const CompilerType &type); 611 612 static bool IsObjCClassTypeAndHasIVars(const CompilerType &type, 613 bool check_superclass); 614 615 static bool IsObjCObjectOrInterfaceType(const CompilerType &type); 616 617 static bool IsObjCObjectPointerType(const CompilerType &type, 618 CompilerType *target_type = nullptr); 619 620 bool IsPolymorphicClass(lldb::opaque_compiler_type_t type) override; 621 622 static bool IsClassType(lldb::opaque_compiler_type_t type); 623 624 static bool IsEnumType(lldb::opaque_compiler_type_t type); 625 626 bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type, 627 CompilerType *target_type, // Can pass nullptr 628 bool check_cplusplus, bool check_objc) override; 629 630 bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) override; 631 632 bool IsPointerType(lldb::opaque_compiler_type_t type, 633 CompilerType *pointee_type) override; 634 635 bool IsPointerOrReferenceType(lldb::opaque_compiler_type_t type, 636 CompilerType *pointee_type) override; 637 638 bool IsReferenceType(lldb::opaque_compiler_type_t type, 639 CompilerType *pointee_type, bool *is_rvalue) override; 640 641 bool IsScalarType(lldb::opaque_compiler_type_t type) override; 642 643 bool IsTypedefType(lldb::opaque_compiler_type_t type) override; 644 645 bool IsVoidType(lldb::opaque_compiler_type_t type) override; 646 647 bool CanPassInRegisters(const CompilerType &type) override; 648 649 bool SupportsLanguage(lldb::LanguageType language) override; 650 651 static std::optional<std::string> GetCXXClassName(const CompilerType &type); 652 653 // Type Completion 654 655 bool GetCompleteType(lldb::opaque_compiler_type_t type) override; 656 657 bool IsForcefullyCompleted(lldb::opaque_compiler_type_t type) override; 658 659 // Accessors 660 661 ConstString GetTypeName(lldb::opaque_compiler_type_t type, 662 bool base_only) override; 663 664 ConstString GetDisplayTypeName(lldb::opaque_compiler_type_t type) override; 665 666 uint32_t GetTypeInfo(lldb::opaque_compiler_type_t type, 667 CompilerType *pointee_or_element_compiler_type) override; 668 669 lldb::LanguageType 670 GetMinimumLanguage(lldb::opaque_compiler_type_t type) override; 671 672 lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) override; 673 674 unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) override; 675 676 // Creating related types 677 678 CompilerType GetArrayElementType(lldb::opaque_compiler_type_t type, 679 ExecutionContextScope *exe_scope) override; 680 681 CompilerType GetArrayType(lldb::opaque_compiler_type_t type, 682 uint64_t size) override; 683 684 CompilerType GetCanonicalType(lldb::opaque_compiler_type_t type) override; 685 686 CompilerType 687 GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) override; 688 689 CompilerType 690 GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) override; 691 692 // Returns -1 if this isn't a function of if the function doesn't have a 693 // prototype Returns a value >= 0 if there is a prototype. 694 int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) override; 695 696 CompilerType GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type, 697 size_t idx) override; 698 699 CompilerType 700 GetFunctionReturnType(lldb::opaque_compiler_type_t type) override; 701 702 size_t GetNumMemberFunctions(lldb::opaque_compiler_type_t type) override; 703 704 TypeMemberFunctionImpl 705 GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, 706 size_t idx) override; 707 708 CompilerType GetNonReferenceType(lldb::opaque_compiler_type_t type) override; 709 710 CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) override; 711 712 CompilerType GetPointerType(lldb::opaque_compiler_type_t type) override; 713 714 CompilerType 715 GetLValueReferenceType(lldb::opaque_compiler_type_t type) override; 716 717 CompilerType 718 GetRValueReferenceType(lldb::opaque_compiler_type_t type) override; 719 720 CompilerType GetAtomicType(lldb::opaque_compiler_type_t type) override; 721 722 CompilerType AddConstModifier(lldb::opaque_compiler_type_t type) override; 723 724 CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type) override; 725 726 CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type) override; 727 728 /// Using the current type, create a new typedef to that type using 729 /// "typedef_name" as the name and "decl_ctx" as the decl context. 730 /// \param opaque_payload is an opaque TypePayloadClang. 731 CompilerType CreateTypedef(lldb::opaque_compiler_type_t type, 732 const char *name, 733 const CompilerDeclContext &decl_ctx, 734 uint32_t opaque_payload) override; 735 736 // If the current object represents a typedef type, get the underlying type 737 CompilerType GetTypedefedType(lldb::opaque_compiler_type_t type) override; 738 739 // Create related types using the current type's AST 740 CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) override; 741 742 // Exploring the type 743 744 const llvm::fltSemantics &GetFloatTypeSemantics(size_t byte_size) override; 745 746 std::optional<uint64_t> GetByteSize(lldb::opaque_compiler_type_t type, 747 ExecutionContextScope *exe_scope) { 748 if (std::optional<uint64_t> bit_size = GetBitSize(type, exe_scope)) 749 return (*bit_size + 7) / 8; 750 return std::nullopt; 751 } 752 753 std::optional<uint64_t> GetBitSize(lldb::opaque_compiler_type_t type, 754 ExecutionContextScope *exe_scope) override; 755 756 lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type, 757 uint64_t &count) override; 758 759 lldb::Format GetFormat(lldb::opaque_compiler_type_t type) override; 760 761 std::optional<size_t> 762 GetTypeBitAlign(lldb::opaque_compiler_type_t type, 763 ExecutionContextScope *exe_scope) override; 764 765 uint32_t GetNumChildren(lldb::opaque_compiler_type_t type, 766 bool omit_empty_base_classes, 767 const ExecutionContext *exe_ctx) override; 768 769 CompilerType GetBuiltinTypeByName(ConstString name) override; 770 771 lldb::BasicType 772 GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) override; 773 774 static lldb::BasicType 775 GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type, 776 ConstString name); 777 778 void ForEachEnumerator( 779 lldb::opaque_compiler_type_t type, 780 std::function<bool(const CompilerType &integer_type, 781 ConstString name, 782 const llvm::APSInt &value)> const &callback) override; 783 784 uint32_t GetNumFields(lldb::opaque_compiler_type_t type) override; 785 786 CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type, size_t idx, 787 std::string &name, uint64_t *bit_offset_ptr, 788 uint32_t *bitfield_bit_size_ptr, 789 bool *is_bitfield_ptr) override; 790 791 uint32_t GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) override; 792 793 uint32_t GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) override; 794 795 CompilerType GetDirectBaseClassAtIndex(lldb::opaque_compiler_type_t type, 796 size_t idx, 797 uint32_t *bit_offset_ptr) override; 798 799 CompilerType GetVirtualBaseClassAtIndex(lldb::opaque_compiler_type_t type, 800 size_t idx, 801 uint32_t *bit_offset_ptr) override; 802 803 static uint32_t GetNumPointeeChildren(clang::QualType type); 804 805 CompilerType GetChildCompilerTypeAtIndex( 806 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx, 807 bool transparent_pointers, bool omit_empty_base_classes, 808 bool ignore_array_bounds, std::string &child_name, 809 uint32_t &child_byte_size, int32_t &child_byte_offset, 810 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset, 811 bool &child_is_base_class, bool &child_is_deref_of_parent, 812 ValueObject *valobj, uint64_t &language_flags) override; 813 814 // Lookup a child given a name. This function will match base class names and 815 // member member names in "clang_type" only, not descendants. 816 uint32_t GetIndexOfChildWithName(lldb::opaque_compiler_type_t type, 817 const char *name, 818 bool omit_empty_base_classes) override; 819 820 // Lookup a child member given a name. This function will match member names 821 // only and will descend into "clang_type" children in search for the first 822 // member in this class, or any base class that matches "name". 823 // TODO: Return all matches for a given name by returning a 824 // vector<vector<uint32_t>> 825 // so we catch all names that match a given child name, not just the first. 826 size_t 827 GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type, 828 const char *name, bool omit_empty_base_classes, 829 std::vector<uint32_t> &child_indexes) override; 830 831 bool IsTemplateType(lldb::opaque_compiler_type_t type) override; 832 833 size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type, 834 bool expand_pack) override; 835 836 lldb::TemplateArgumentKind 837 GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, size_t idx, 838 bool expand_pack) override; 839 CompilerType GetTypeTemplateArgument(lldb::opaque_compiler_type_t type, 840 size_t idx, bool expand_pack) override; 841 std::optional<CompilerType::IntegralTemplateArgument> 842 GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx, 843 bool expand_pack) override; 844 845 CompilerType GetTypeForFormatters(void *type) override; 846 847 #define LLDB_INVALID_DECL_LEVEL UINT32_MAX 848 // LLDB_INVALID_DECL_LEVEL is returned by CountDeclLevels if child_decl_ctx 849 // could not be found in decl_ctx. 850 uint32_t CountDeclLevels(clang::DeclContext *frame_decl_ctx, 851 clang::DeclContext *child_decl_ctx, 852 ConstString *child_name = nullptr, 853 CompilerType *child_type = nullptr); 854 855 // Modifying RecordType 856 static clang::FieldDecl *AddFieldToRecordType(const CompilerType &type, 857 llvm::StringRef name, 858 const CompilerType &field_type, 859 lldb::AccessType access, 860 uint32_t bitfield_bit_size); 861 862 static void BuildIndirectFields(const CompilerType &type); 863 864 static void SetIsPacked(const CompilerType &type); 865 866 static clang::VarDecl *AddVariableToRecordType(const CompilerType &type, 867 llvm::StringRef name, 868 const CompilerType &var_type, 869 lldb::AccessType access); 870 871 /// Initializes a variable with an integer value. 872 /// \param var The variable to initialize. Must not already have an 873 /// initializer and must have an integer or enum type. 874 /// \param init_value The integer value that the variable should be 875 /// initialized to. Has to match the bit width of the 876 /// variable type. 877 static void SetIntegerInitializerForVariable(clang::VarDecl *var, 878 const llvm::APInt &init_value); 879 880 /// Initializes a variable with a floating point value. 881 /// \param var The variable to initialize. Must not already have an 882 /// initializer and must have a floating point type. 883 /// \param init_value The float value that the variable should be 884 /// initialized to. 885 static void 886 SetFloatingInitializerForVariable(clang::VarDecl *var, 887 const llvm::APFloat &init_value); 888 889 clang::CXXMethodDecl *AddMethodToCXXRecordType( 890 lldb::opaque_compiler_type_t type, llvm::StringRef name, 891 const char *mangled_name, const CompilerType &method_type, 892 lldb::AccessType access, bool is_virtual, bool is_static, bool is_inline, 893 bool is_explicit, bool is_attr_used, bool is_artificial); 894 895 void AddMethodOverridesForCXXRecordType(lldb::opaque_compiler_type_t type); 896 897 // C++ Base Classes 898 std::unique_ptr<clang::CXXBaseSpecifier> 899 CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type, 900 lldb::AccessType access, bool is_virtual, 901 bool base_of_class); 902 903 bool TransferBaseClasses( 904 lldb::opaque_compiler_type_t type, 905 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases); 906 907 static bool SetObjCSuperClass(const CompilerType &type, 908 const CompilerType &superclass_compiler_type); 909 910 static bool AddObjCClassProperty(const CompilerType &type, 911 const char *property_name, 912 const CompilerType &property_compiler_type, 913 clang::ObjCIvarDecl *ivar_decl, 914 const char *property_setter_name, 915 const char *property_getter_name, 916 uint32_t property_attributes, 917 ClangASTMetadata *metadata); 918 919 static clang::ObjCMethodDecl *AddMethodToObjCObjectType( 920 const CompilerType &type, 921 const char *name, // the full symbol name as seen in the symbol table 922 // (lldb::opaque_compiler_type_t type, "-[NString 923 // stringWithCString:]") 924 const CompilerType &method_compiler_type, lldb::AccessType access, 925 bool is_artificial, bool is_variadic, bool is_objc_direct_call); 926 927 static bool SetHasExternalStorage(lldb::opaque_compiler_type_t type, 928 bool has_extern); 929 930 // Tag Declarations 931 static bool StartTagDeclarationDefinition(const CompilerType &type); 932 933 static bool CompleteTagDeclarationDefinition(const CompilerType &type); 934 935 // Modifying Enumeration types 936 clang::EnumConstantDecl *AddEnumerationValueToEnumerationType( 937 const CompilerType &enum_type, const Declaration &decl, const char *name, 938 int64_t enum_value, uint32_t enum_value_bit_size); 939 clang::EnumConstantDecl *AddEnumerationValueToEnumerationType( 940 const CompilerType &enum_type, const Declaration &decl, const char *name, 941 const llvm::APSInt &value); 942 943 /// Returns the underlying integer type for an enum type. If the given type 944 /// is invalid or not an enum-type, the function returns an invalid 945 /// CompilerType. 946 CompilerType GetEnumerationIntegerType(CompilerType type); 947 948 // Pointers & References 949 950 // Call this function using the class type when you want to make a member 951 // pointer type to pointee_type. 952 static CompilerType CreateMemberPointerType(const CompilerType &type, 953 const CompilerType &pointee_type); 954 955 // Dumping types 956 #ifndef NDEBUG 957 /// Convenience LLVM-style dump method for use in the debugger only. 958 /// In contrast to the other \p Dump() methods this directly invokes 959 /// \p clang::QualType::dump(). 960 LLVM_DUMP_METHOD void dump(lldb::opaque_compiler_type_t type) const override; 961 #endif 962 963 /// \see lldb_private::TypeSystem::Dump 964 void Dump(llvm::raw_ostream &output) override; 965 966 /// Dump clang AST types from the symbol file. 967 /// 968 /// \param[in] s 969 /// A stream to send the dumped AST node(s) to 970 /// \param[in] symbol_name 971 /// The name of the symbol to dump, if it is empty dump all the symbols 972 void DumpFromSymbolFile(Stream &s, llvm::StringRef symbol_name); 973 974 void DumpValue(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, 975 Stream *s, lldb::Format format, const DataExtractor &data, 976 lldb::offset_t data_offset, size_t data_byte_size, 977 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, 978 bool show_types, bool show_summary, bool verbose, 979 uint32_t depth) override; 980 981 bool DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s, 982 lldb::Format format, const DataExtractor &data, 983 lldb::offset_t data_offset, size_t data_byte_size, 984 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, 985 ExecutionContextScope *exe_scope) override; 986 987 void DumpSummary(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, 988 Stream *s, const DataExtractor &data, 989 lldb::offset_t data_offset, size_t data_byte_size) override; 990 991 void DumpTypeDescription( 992 lldb::opaque_compiler_type_t type, 993 lldb::DescriptionLevel level = lldb::eDescriptionLevelFull) override; 994 995 void DumpTypeDescription( 996 lldb::opaque_compiler_type_t type, Stream *s, 997 lldb::DescriptionLevel level = lldb::eDescriptionLevelFull) override; 998 999 static void DumpTypeName(const CompilerType &type); 1000 1001 static clang::EnumDecl *GetAsEnumDecl(const CompilerType &type); 1002 1003 static clang::RecordDecl *GetAsRecordDecl(const CompilerType &type); 1004 1005 static clang::TagDecl *GetAsTagDecl(const CompilerType &type); 1006 1007 static clang::TypedefNameDecl *GetAsTypedefDecl(const CompilerType &type); 1008 1009 static clang::CXXRecordDecl * 1010 GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type); 1011 1012 static clang::ObjCInterfaceDecl * 1013 GetAsObjCInterfaceDecl(const CompilerType &type); 1014 1015 clang::ClassTemplateDecl *ParseClassTemplateDecl( 1016 clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, 1017 lldb::AccessType access_type, const char *parent_name, int tag_decl_kind, 1018 const TypeSystemClang::TemplateParameterInfos &template_param_infos); 1019 1020 clang::BlockDecl *CreateBlockDeclaration(clang::DeclContext *ctx, 1021 OptionalClangModuleID owning_module); 1022 1023 clang::UsingDirectiveDecl * 1024 CreateUsingDirectiveDeclaration(clang::DeclContext *decl_ctx, 1025 OptionalClangModuleID owning_module, 1026 clang::NamespaceDecl *ns_decl); 1027 1028 clang::UsingDecl *CreateUsingDeclaration(clang::DeclContext *current_decl_ctx, 1029 OptionalClangModuleID owning_module, 1030 clang::NamedDecl *target); 1031 1032 clang::VarDecl *CreateVariableDeclaration(clang::DeclContext *decl_context, 1033 OptionalClangModuleID owning_module, 1034 const char *name, 1035 clang::QualType type); 1036 1037 static lldb::opaque_compiler_type_t 1038 GetOpaqueCompilerType(clang::ASTContext *ast, lldb::BasicType basic_type); 1039 1040 static clang::QualType GetQualType(lldb::opaque_compiler_type_t type) { 1041 if (type) 1042 return clang::QualType::getFromOpaquePtr(type); 1043 return clang::QualType(); 1044 } 1045 1046 static clang::QualType 1047 GetCanonicalQualType(lldb::opaque_compiler_type_t type) { 1048 if (type) 1049 return clang::QualType::getFromOpaquePtr(type).getCanonicalType(); 1050 return clang::QualType(); 1051 } 1052 1053 clang::DeclarationName 1054 GetDeclarationName(llvm::StringRef name, 1055 const CompilerType &function_clang_type); 1056 1057 clang::LangOptions *GetLangOpts() const { 1058 return m_language_options_up.get(); 1059 } 1060 clang::SourceManager *GetSourceMgr() const { 1061 return m_source_manager_up.get(); 1062 } 1063 1064 /// Complete a type from debug info, or mark it as forcefully completed if 1065 /// there is no definition of the type in the current Module. Call this 1066 /// function in contexts where the usual C++ rules require a type to be 1067 /// complete (base class, member, etc.). 1068 static void RequireCompleteType(CompilerType type); 1069 1070 bool SetDeclIsForcefullyCompleted(const clang::TagDecl *td); 1071 1072 /// Return the template parameters (including surrounding <>) in string form. 1073 std::string 1074 PrintTemplateParams(const TemplateParameterInfos &template_param_infos); 1075 1076 private: 1077 /// Returns the PrintingPolicy used when generating the internal type names. 1078 /// These type names are mostly used for the formatter selection. 1079 clang::PrintingPolicy GetTypePrintingPolicy(); 1080 /// Returns the internal type name for the given NamedDecl using the 1081 /// type printing policy. 1082 std::string GetTypeNameForDecl(const clang::NamedDecl *named_decl, 1083 bool qualified = true); 1084 1085 const clang::ClassTemplateSpecializationDecl * 1086 GetAsTemplateSpecialization(lldb::opaque_compiler_type_t type); 1087 1088 // Classes that inherit from TypeSystemClang can see and modify these 1089 std::string m_target_triple; 1090 std::unique_ptr<clang::ASTContext> m_ast_up; 1091 std::unique_ptr<clang::LangOptions> m_language_options_up; 1092 std::unique_ptr<clang::FileManager> m_file_manager_up; 1093 std::unique_ptr<clang::SourceManager> m_source_manager_up; 1094 std::unique_ptr<clang::DiagnosticsEngine> m_diagnostics_engine_up; 1095 std::unique_ptr<clang::DiagnosticConsumer> m_diagnostic_consumer_up; 1096 std::shared_ptr<clang::TargetOptions> m_target_options_rp; 1097 std::unique_ptr<clang::TargetInfo> m_target_info_up; 1098 std::unique_ptr<clang::IdentifierTable> m_identifier_table_up; 1099 std::unique_ptr<clang::SelectorTable> m_selector_table_up; 1100 std::unique_ptr<clang::Builtin::Context> m_builtins_up; 1101 std::unique_ptr<clang::HeaderSearch> m_header_search_up; 1102 std::unique_ptr<clang::ModuleMap> m_module_map_up; 1103 std::unique_ptr<DWARFASTParserClang> m_dwarf_ast_parser_up; 1104 #ifdef LLDB_ENABLE_ALL 1105 std::unique_ptr<PDBASTParser> m_pdb_ast_parser_up; 1106 std::unique_ptr<npdb::PdbAstBuilder> m_native_pdb_ast_parser_up; 1107 #endif // LLDB_ENABLE_ALL 1108 std::unique_ptr<clang::MangleContext> m_mangle_ctx_up; 1109 uint32_t m_pointer_byte_size = 0; 1110 bool m_ast_owned = false; 1111 /// A string describing what this TypeSystemClang represents (e.g., 1112 /// AST for debug information, an expression, some other utility ClangAST). 1113 /// Useful for logging and debugging. 1114 std::string m_display_name; 1115 1116 typedef llvm::DenseMap<const clang::Decl *, ClangASTMetadata> DeclMetadataMap; 1117 /// Maps Decls to their associated ClangASTMetadata. 1118 DeclMetadataMap m_decl_metadata; 1119 1120 typedef llvm::DenseMap<const clang::Type *, ClangASTMetadata> TypeMetadataMap; 1121 /// Maps Types to their associated ClangASTMetadata. 1122 TypeMetadataMap m_type_metadata; 1123 1124 typedef llvm::DenseMap<const clang::CXXRecordDecl *, clang::AccessSpecifier> 1125 CXXRecordDeclAccessMap; 1126 /// Maps CXXRecordDecl to their most recent added method/field's 1127 /// AccessSpecifier. 1128 CXXRecordDeclAccessMap m_cxx_record_decl_access; 1129 1130 /// The sema associated that is currently used to build this ASTContext. 1131 /// May be null if we are already done parsing this ASTContext or the 1132 /// ASTContext wasn't created by parsing source code. 1133 clang::Sema *m_sema = nullptr; 1134 1135 // For TypeSystemClang only 1136 TypeSystemClang(const TypeSystemClang &); 1137 const TypeSystemClang &operator=(const TypeSystemClang &); 1138 /// Creates the internal ASTContext. 1139 void CreateASTContext(); 1140 void SetTargetTriple(llvm::StringRef target_triple); 1141 }; 1142 1143 /// The TypeSystemClang instance used for the scratch ASTContext in a 1144 /// lldb::Target. 1145 class ScratchTypeSystemClang : public TypeSystemClang { 1146 /// LLVM RTTI support 1147 static char ID; 1148 1149 public: 1150 ScratchTypeSystemClang(Target &target, llvm::Triple triple); 1151 1152 ~ScratchTypeSystemClang() override = default; 1153 1154 void Finalize() override; 1155 1156 /// The different kinds of isolated ASTs within the scratch TypeSystem. 1157 /// 1158 /// These ASTs are isolated from the main scratch AST and are each 1159 /// dedicated to a special language option/feature that makes the contained 1160 /// AST nodes incompatible with other AST nodes. 1161 enum IsolatedASTKind { 1162 /// The isolated AST for declarations/types from expressions that imported 1163 /// type information from a C++ module. The templates from a C++ module 1164 /// often conflict with the templates we generate from debug information, 1165 /// so we put these types in their own AST. 1166 CppModules 1167 }; 1168 1169 /// Alias for requesting the default scratch TypeSystemClang in GetForTarget. 1170 // This isn't constexpr as gtest/std::optional comparison logic is trying 1171 // to get the address of this for pretty-printing. 1172 static const std::nullopt_t DefaultAST; 1173 1174 /// Infers the appropriate sub-AST from Clang's LangOptions. 1175 static std::optional<IsolatedASTKind> 1176 InferIsolatedASTKindFromLangOpts(const clang::LangOptions &l) { 1177 // If modules are activated we want the dedicated C++ module AST. 1178 // See IsolatedASTKind::CppModules for more info. 1179 if (l.Modules) 1180 return IsolatedASTKind::CppModules; 1181 return DefaultAST; 1182 } 1183 1184 /// Returns the scratch TypeSystemClang for the given target. 1185 /// \param target The Target which scratch TypeSystemClang should be returned. 1186 /// \param ast_kind Allows requesting a specific sub-AST instead of the 1187 /// default scratch AST. See also `IsolatedASTKind`. 1188 /// \param create_on_demand If the scratch TypeSystemClang instance can be 1189 /// created by this call if it doesn't exist yet. If it doesn't exist yet and 1190 /// this parameter is false, this function returns a nullptr. 1191 /// \return The scratch type system of the target or a nullptr in case an 1192 /// error occurred. 1193 static lldb::TypeSystemClangSP 1194 GetForTarget(Target &target, 1195 std::optional<IsolatedASTKind> ast_kind = DefaultAST, 1196 bool create_on_demand = true); 1197 1198 /// Returns the scratch TypeSystemClang for the given target. The returned 1199 /// TypeSystemClang will be the scratch AST or a sub-AST, depending on which 1200 /// fits best to the passed LangOptions. 1201 /// \param target The Target which scratch TypeSystemClang should be returned. 1202 /// \param lang_opts The LangOptions of a clang ASTContext that the caller 1203 /// wants to export type information from. This is used to 1204 /// find the best matching sub-AST that will be returned. 1205 static lldb::TypeSystemClangSP 1206 GetForTarget(Target &target, const clang::LangOptions &lang_opts) { 1207 return GetForTarget(target, InferIsolatedASTKindFromLangOpts(lang_opts)); 1208 } 1209 1210 /// \see lldb_private::TypeSystem::Dump 1211 void Dump(llvm::raw_ostream &output) override; 1212 1213 UserExpression * 1214 GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix, 1215 lldb::LanguageType language, 1216 Expression::ResultType desired_type, 1217 const EvaluateExpressionOptions &options, 1218 ValueObject *ctx_obj) override; 1219 1220 FunctionCaller *GetFunctionCaller(const CompilerType &return_type, 1221 const Address &function_address, 1222 const ValueList &arg_value_list, 1223 const char *name) override; 1224 1225 std::unique_ptr<UtilityFunction> 1226 CreateUtilityFunction(std::string text, std::string name) override; 1227 1228 PersistentExpressionState *GetPersistentExpressionState() override; 1229 1230 /// Unregisters the given ASTContext as a source from the scratch AST (and 1231 /// all sub-ASTs). 1232 /// \see ClangASTImporter::ForgetSource 1233 void ForgetSource(clang::ASTContext *src_ctx, ClangASTImporter &importer); 1234 1235 // llvm casting support 1236 bool isA(const void *ClassID) const override { 1237 return ClassID == &ID || TypeSystemClang::isA(ClassID); 1238 } 1239 static bool classof(const TypeSystem *ts) { return ts->isA(&ID); } 1240 1241 private: 1242 std::unique_ptr<ClangASTSource> CreateASTSource(); 1243 /// Returns the requested sub-AST. 1244 /// Will lazily create the sub-AST if it hasn't been created before. 1245 TypeSystemClang &GetIsolatedAST(IsolatedASTKind feature); 1246 1247 /// The target triple. 1248 /// This was potentially adjusted and might not be identical to the triple 1249 /// of `m_target_wp`. 1250 llvm::Triple m_triple; 1251 lldb::TargetWP m_target_wp; 1252 /// The persistent variables associated with this process for the expression 1253 /// parser. 1254 std::unique_ptr<ClangPersistentVariables> m_persistent_variables; 1255 /// The ExternalASTSource that performs lookups and completes minimally 1256 /// imported types. 1257 std::unique_ptr<ClangASTSource> m_scratch_ast_source_up; 1258 1259 // FIXME: GCC 5.x doesn't support enum as map keys. 1260 typedef int IsolatedASTKey; 1261 1262 /// Map from IsolatedASTKind to their actual TypeSystemClang instance. 1263 /// This map is lazily filled with sub-ASTs and should be accessed via 1264 /// `GetSubAST` (which lazily fills this map). 1265 llvm::DenseMap<IsolatedASTKey, std::shared_ptr<TypeSystemClang>> 1266 m_isolated_asts; 1267 }; 1268 1269 } // namespace lldb_private 1270 1271 #endif // LLDB_SOURCE_PLUGINS_TYPESYSTEM_CLANG_TYPESYSTEMCLANG_H 1272