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