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 <stdint.h> 13 14 #include <functional> 15 #include <initializer_list> 16 #include <map> 17 #include <memory> 18 #include <set> 19 #include <string> 20 #include <utility> 21 #include <vector> 22 23 #include "clang/AST/ASTContext.h" 24 #include "clang/AST/ASTFwd.h" 25 #include "clang/AST/TemplateBase.h" 26 #include "clang/Basic/TargetInfo.h" 27 #include "llvm/ADT/APSInt.h" 28 #include "llvm/ADT/SmallVector.h" 29 30 #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h" 31 #include "lldb/Expression/ExpressionVariable.h" 32 #include "lldb/Symbol/CompilerType.h" 33 #include "lldb/Symbol/TypeSystem.h" 34 #include "lldb/Target/Target.h" 35 #include "lldb/Utility/ConstString.h" 36 #include "lldb/Utility/Flags.h" 37 #include "lldb/Utility/Log.h" 38 #include "lldb/Utility/Logging.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 ConstString GetPluginName() override; 142 143 uint32_t GetPluginVersion() override; 144 145 static ConstString GetPluginNameStatic(); 146 147 static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language, 148 Module *module, Target *target); 149 150 static LanguageSet GetSupportedLanguagesForTypes(); 151 static LanguageSet GetSupportedLanguagesForExpressions(); 152 153 static void Initialize(); 154 155 static void Terminate(); 156 157 static TypeSystemClang *GetASTContext(clang::ASTContext *ast_ctx); 158 159 static TypeSystemClang *GetScratch(Target &target, 160 bool create_on_demand = true) { 161 auto type_system_or_err = target.GetScratchTypeSystemForLanguage( 162 lldb::eLanguageTypeC, create_on_demand); 163 if (auto err = type_system_or_err.takeError()) { 164 LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET), 165 std::move(err), "Couldn't get scratch TypeSystemClang"); 166 return nullptr; 167 } 168 return llvm::dyn_cast<TypeSystemClang>(&type_system_or_err.get()); 169 } 170 171 /// Returns the display name of this TypeSystemClang that indicates what 172 /// purpose it serves in LLDB. Used for example in logs. 173 llvm::StringRef getDisplayName() const { return m_display_name; } 174 175 /// Returns the clang::ASTContext instance managed by this TypeSystemClang. 176 clang::ASTContext &getASTContext(); 177 178 clang::MangleContext *getMangleContext(); 179 180 std::shared_ptr<clang::TargetOptions> &getTargetOptions(); 181 182 clang::TargetInfo *getTargetInfo(); 183 184 void setSema(clang::Sema *s); 185 clang::Sema *getSema() { return m_sema; } 186 187 const char *GetTargetTriple(); 188 189 void SetExternalSource( 190 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> &ast_source_up); 191 192 bool GetCompleteDecl(clang::Decl *decl) { 193 return TypeSystemClang::GetCompleteDecl(&getASTContext(), decl); 194 } 195 196 static void DumpDeclHiearchy(clang::Decl *decl); 197 198 static void DumpDeclContextHiearchy(clang::DeclContext *decl_ctx); 199 200 static bool DeclsAreEquivalent(clang::Decl *lhs_decl, clang::Decl *rhs_decl); 201 202 static bool GetCompleteDecl(clang::ASTContext *ast, clang::Decl *decl); 203 204 void SetMetadataAsUserID(const clang::Decl *decl, lldb::user_id_t user_id); 205 void SetMetadataAsUserID(const clang::Type *type, lldb::user_id_t user_id); 206 207 void SetMetadata(const clang::Decl *object, ClangASTMetadata &meta_data); 208 209 void SetMetadata(const clang::Type *object, ClangASTMetadata &meta_data); 210 ClangASTMetadata *GetMetadata(const clang::Decl *object); 211 ClangASTMetadata *GetMetadata(const clang::Type *object); 212 213 // Basic Types 214 CompilerType GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding, 215 size_t bit_size) override; 216 217 CompilerType GetBasicType(lldb::BasicType type); 218 219 static lldb::BasicType GetBasicTypeEnumeration(ConstString name); 220 221 CompilerType 222 GetBuiltinTypeForDWARFEncodingAndBitSize(llvm::StringRef type_name, 223 uint32_t dw_ate, uint32_t bit_size); 224 225 CompilerType GetCStringType(bool is_const); 226 227 static clang::DeclContext *GetDeclContextForType(clang::QualType type); 228 229 static clang::DeclContext *GetDeclContextForType(const CompilerType &type); 230 231 uint32_t GetPointerByteSize() override; 232 233 clang::TranslationUnitDecl *GetTranslationUnitDecl() { 234 return getASTContext().getTranslationUnitDecl(); 235 } 236 237 static bool AreTypesSame(CompilerType type1, CompilerType type2, 238 bool ignore_qualifiers = false); 239 240 /// Creates a CompilerType form the given QualType with the current 241 /// TypeSystemClang instance as the CompilerType's typesystem. 242 /// \param qt The QualType for a type that belongs to the ASTContext of this 243 /// TypeSystemClang. 244 /// \return The CompilerType representing the given QualType. If the 245 /// QualType's type pointer is a nullptr then the function returns an 246 /// invalid CompilerType. 247 CompilerType GetType(clang::QualType qt) { 248 if (qt.getTypePtrOrNull() == nullptr) 249 return CompilerType(); 250 // Check that the type actually belongs to this TypeSystemClang. 251 assert(qt->getAsTagDecl() == nullptr || 252 &qt->getAsTagDecl()->getASTContext() == &getASTContext()); 253 return CompilerType(this, qt.getAsOpaquePtr()); 254 } 255 256 CompilerType GetTypeForDecl(clang::NamedDecl *decl); 257 258 CompilerType GetTypeForDecl(clang::TagDecl *decl); 259 260 CompilerType GetTypeForDecl(clang::ObjCInterfaceDecl *objc_decl); 261 262 template <typename RecordDeclType> 263 CompilerType 264 GetTypeForIdentifier(ConstString type_name, 265 clang::DeclContext *decl_context = nullptr) { 266 CompilerType compiler_type; 267 268 if (type_name.GetLength()) { 269 clang::ASTContext &ast = getASTContext(); 270 if (!decl_context) 271 decl_context = ast.getTranslationUnitDecl(); 272 273 clang::IdentifierInfo &myIdent = ast.Idents.get(type_name.GetCString()); 274 clang::DeclarationName myName = 275 ast.DeclarationNames.getIdentifier(&myIdent); 276 277 clang::DeclContext::lookup_result result = decl_context->lookup(myName); 278 279 if (!result.empty()) { 280 clang::NamedDecl *named_decl = result[0]; 281 if (const RecordDeclType *record_decl = 282 llvm::dyn_cast<RecordDeclType>(named_decl)) 283 compiler_type.SetCompilerType( 284 this, clang::QualType(record_decl->getTypeForDecl(), 0) 285 .getAsOpaquePtr()); 286 } 287 } 288 289 return compiler_type; 290 } 291 292 CompilerType CreateStructForIdentifier( 293 ConstString type_name, 294 const std::initializer_list<std::pair<const char *, CompilerType>> 295 &type_fields, 296 bool packed = false); 297 298 CompilerType GetOrCreateStructForIdentifier( 299 ConstString type_name, 300 const std::initializer_list<std::pair<const char *, CompilerType>> 301 &type_fields, 302 bool packed = false); 303 304 static bool IsOperator(llvm::StringRef name, 305 clang::OverloadedOperatorKind &op_kind); 306 307 // Structure, Unions, Classes 308 309 static clang::AccessSpecifier 310 ConvertAccessTypeToAccessSpecifier(lldb::AccessType access); 311 312 static clang::AccessSpecifier 313 UnifyAccessSpecifiers(clang::AccessSpecifier lhs, clang::AccessSpecifier rhs); 314 315 static uint32_t GetNumBaseClasses(const clang::CXXRecordDecl *cxx_record_decl, 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 if (args.empty()) 336 return false; 337 return args.size() == names.size() && 338 ((bool)pack_name == (bool)packed_args) && 339 (!packed_args || !packed_args->packed_args); 340 } 341 342 llvm::SmallVector<const char *, 2> names; 343 llvm::SmallVector<clang::TemplateArgument, 2> args; 344 345 const char * pack_name = nullptr; 346 std::unique_ptr<TemplateParameterInfos> packed_args; 347 }; 348 349 clang::FunctionTemplateDecl * 350 CreateFunctionTemplateDecl(clang::DeclContext *decl_ctx, 351 OptionalClangModuleID owning_module, 352 clang::FunctionDecl *func_decl, const char *name, 353 const TemplateParameterInfos &infos); 354 355 void CreateFunctionTemplateSpecializationInfo( 356 clang::FunctionDecl *func_decl, clang::FunctionTemplateDecl *Template, 357 const TemplateParameterInfos &infos); 358 359 clang::ClassTemplateDecl * 360 CreateClassTemplateDecl(clang::DeclContext *decl_ctx, 361 OptionalClangModuleID owning_module, 362 lldb::AccessType access_type, const char *class_name, 363 int kind, 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 static bool RecordHasFields(const clang::RecordDecl *record_decl); 387 388 CompilerType CreateObjCClass(llvm::StringRef name, 389 clang::DeclContext *decl_ctx, 390 OptionalClangModuleID owning_module, 391 bool isForwardDecl, bool isInternal, 392 ClangASTMetadata *metadata = nullptr); 393 394 bool SetTagTypeKind(clang::QualType type, int kind) const; 395 396 bool SetDefaultAccessForRecordFields(clang::RecordDecl *record_decl, 397 int default_accessibility, 398 int *assigned_accessibilities, 399 size_t num_assigned_accessibilities); 400 401 // Returns a mask containing bits from the TypeSystemClang::eTypeXXX 402 // enumerations 403 404 // Namespace Declarations 405 406 clang::NamespaceDecl * 407 GetUniqueNamespaceDeclaration(const char *name, clang::DeclContext *decl_ctx, 408 OptionalClangModuleID owning_module, 409 bool is_inline = false); 410 411 // Function Types 412 413 clang::FunctionDecl * 414 CreateFunctionDeclaration(clang::DeclContext *decl_ctx, 415 OptionalClangModuleID owning_module, 416 const char *name, const CompilerType &function_Type, 417 int storage, bool is_inline); 418 419 CompilerType CreateFunctionType(const CompilerType &result_type, 420 const CompilerType *args, unsigned num_args, 421 bool is_variadic, unsigned type_quals, 422 clang::CallingConv cc); 423 424 CompilerType CreateFunctionType(const CompilerType &result_type, 425 const CompilerType *args, unsigned num_args, 426 bool is_variadic, unsigned type_quals) { 427 return CreateFunctionType(result_type, args, num_args, is_variadic, 428 type_quals, clang::CC_C); 429 } 430 431 clang::ParmVarDecl * 432 CreateParameterDeclaration(clang::DeclContext *decl_ctx, 433 OptionalClangModuleID owning_module, 434 const char *name, const CompilerType ¶m_type, 435 int storage, bool add_decl = false); 436 437 void SetFunctionParameters(clang::FunctionDecl *function_decl, 438 clang::ParmVarDecl **params, unsigned num_params); 439 440 CompilerType CreateBlockPointerType(const CompilerType &function_type); 441 442 // Array Types 443 444 CompilerType CreateArrayType(const CompilerType &element_type, 445 size_t element_count, bool is_vector); 446 447 // Enumeration Types 448 CompilerType CreateEnumerationType(const char *name, 449 clang::DeclContext *decl_ctx, 450 OptionalClangModuleID owning_module, 451 const Declaration &decl, 452 const CompilerType &integer_qual_type, 453 bool is_scoped); 454 455 // Integer type functions 456 457 CompilerType GetIntTypeFromBitSize(size_t bit_size, bool is_signed); 458 459 CompilerType GetPointerSizedIntType(bool is_signed); 460 461 // Floating point functions 462 463 static CompilerType GetFloatTypeFromBitSize(clang::ASTContext *ast, 464 size_t bit_size); 465 466 // TypeSystem methods 467 DWARFASTParser *GetDWARFParser() override; 468 #ifdef LLDB_ENABLE_ALL 469 PDBASTParser *GetPDBParser() override; 470 #endif // LLDB_ENABLE_ALL 471 472 // TypeSystemClang callbacks for external source lookups. 473 void CompleteTagDecl(clang::TagDecl *); 474 475 void CompleteObjCInterfaceDecl(clang::ObjCInterfaceDecl *); 476 477 bool LayoutRecordType( 478 const clang::RecordDecl *record_decl, uint64_t &size, uint64_t &alignment, 479 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets, 480 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> 481 &base_offsets, 482 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> 483 &vbase_offsets); 484 485 /// Creates a CompilerDecl from the given Decl with the current 486 /// TypeSystemClang instance as its typesystem. 487 /// The Decl has to come from the ASTContext of this 488 /// TypeSystemClang. 489 CompilerDecl GetCompilerDecl(clang::Decl *decl) { 490 assert(&decl->getASTContext() == &getASTContext() && 491 "CreateCompilerDecl for Decl from wrong ASTContext?"); 492 return CompilerDecl(this, decl); 493 } 494 495 // CompilerDecl override functions 496 ConstString DeclGetName(void *opaque_decl) override; 497 498 ConstString DeclGetMangledName(void *opaque_decl) override; 499 500 CompilerDeclContext DeclGetDeclContext(void *opaque_decl) override; 501 502 CompilerType DeclGetFunctionReturnType(void *opaque_decl) override; 503 504 size_t DeclGetFunctionNumArguments(void *opaque_decl) override; 505 506 CompilerType DeclGetFunctionArgumentType(void *opaque_decl, 507 size_t arg_idx) override; 508 509 CompilerType GetTypeForDecl(void *opaque_decl) override; 510 511 // CompilerDeclContext override functions 512 513 /// Creates a CompilerDeclContext from the given DeclContext 514 /// with the current TypeSystemClang instance as its typesystem. 515 /// The DeclContext has to come from the ASTContext of this 516 /// TypeSystemClang. 517 CompilerDeclContext CreateDeclContext(clang::DeclContext *ctx); 518 519 /// Set the owning module for \p decl. 520 static void SetOwningModule(clang::Decl *decl, 521 OptionalClangModuleID owning_module); 522 523 std::vector<CompilerDecl> 524 DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name, 525 const bool ignore_using_decls) override; 526 527 ConstString DeclContextGetName(void *opaque_decl_ctx) override; 528 529 ConstString DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) override; 530 531 bool DeclContextIsClassMethod(void *opaque_decl_ctx, 532 lldb::LanguageType *language_ptr, 533 bool *is_instance_method_ptr, 534 ConstString *language_object_name_ptr) override; 535 536 bool DeclContextIsContainedInLookup(void *opaque_decl_ctx, 537 void *other_opaque_decl_ctx) override; 538 539 // Clang specific clang::DeclContext functions 540 541 static clang::DeclContext * 542 DeclContextGetAsDeclContext(const CompilerDeclContext &dc); 543 544 static clang::ObjCMethodDecl * 545 DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc); 546 547 static clang::CXXMethodDecl * 548 DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc); 549 550 static clang::FunctionDecl * 551 DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc); 552 553 static clang::NamespaceDecl * 554 DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc); 555 556 static ClangASTMetadata *DeclContextGetMetaData(const CompilerDeclContext &dc, 557 const clang::Decl *object); 558 559 static clang::ASTContext * 560 DeclContextGetTypeSystemClang(const CompilerDeclContext &dc); 561 562 // Tests 563 564 #ifndef NDEBUG 565 bool Verify(lldb::opaque_compiler_type_t type) override; 566 #endif 567 568 bool IsArrayType(lldb::opaque_compiler_type_t type, 569 CompilerType *element_type, uint64_t *size, 570 bool *is_incomplete) override; 571 572 bool IsVectorType(lldb::opaque_compiler_type_t type, 573 CompilerType *element_type, uint64_t *size) override; 574 575 bool IsAggregateType(lldb::opaque_compiler_type_t type) override; 576 577 bool IsAnonymousType(lldb::opaque_compiler_type_t type) override; 578 579 bool IsBeingDefined(lldb::opaque_compiler_type_t type) override; 580 581 bool IsCharType(lldb::opaque_compiler_type_t type) override; 582 583 bool IsCompleteType(lldb::opaque_compiler_type_t type) override; 584 585 bool IsConst(lldb::opaque_compiler_type_t type) override; 586 587 bool IsCStringType(lldb::opaque_compiler_type_t type, 588 uint32_t &length) override; 589 590 static bool IsCXXClassType(const CompilerType &type); 591 592 bool IsDefined(lldb::opaque_compiler_type_t type) override; 593 594 bool IsFloatingPointType(lldb::opaque_compiler_type_t type, uint32_t &count, 595 bool &is_complex) override; 596 597 bool IsFunctionType(lldb::opaque_compiler_type_t type, 598 bool *is_variadic_ptr) override; 599 600 uint32_t IsHomogeneousAggregate(lldb::opaque_compiler_type_t type, 601 CompilerType *base_type_ptr) override; 602 603 size_t 604 GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) override; 605 606 CompilerType GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type, 607 const size_t index) override; 608 609 bool IsFunctionPointerType(lldb::opaque_compiler_type_t type) override; 610 611 bool IsBlockPointerType(lldb::opaque_compiler_type_t type, 612 CompilerType *function_pointer_type_ptr) override; 613 614 bool IsIntegerType(lldb::opaque_compiler_type_t type, 615 bool &is_signed) override; 616 617 bool IsEnumerationType(lldb::opaque_compiler_type_t type, 618 bool &is_signed) override; 619 620 static bool IsObjCClassType(const CompilerType &type); 621 622 static bool IsObjCClassTypeAndHasIVars(const CompilerType &type, 623 bool check_superclass); 624 625 static bool IsObjCObjectOrInterfaceType(const CompilerType &type); 626 627 static bool IsObjCObjectPointerType(const CompilerType &type, 628 CompilerType *target_type = nullptr); 629 630 bool IsPolymorphicClass(lldb::opaque_compiler_type_t type) override; 631 632 static bool IsClassType(lldb::opaque_compiler_type_t type); 633 634 static bool IsEnumType(lldb::opaque_compiler_type_t type); 635 636 bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type, 637 CompilerType *target_type, // Can pass nullptr 638 bool check_cplusplus, bool check_objc) override; 639 640 bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) override; 641 642 bool IsPointerType(lldb::opaque_compiler_type_t type, 643 CompilerType *pointee_type) override; 644 645 bool IsPointerOrReferenceType(lldb::opaque_compiler_type_t type, 646 CompilerType *pointee_type) override; 647 648 bool IsReferenceType(lldb::opaque_compiler_type_t type, 649 CompilerType *pointee_type, bool *is_rvalue) override; 650 651 bool IsScalarType(lldb::opaque_compiler_type_t type) override; 652 653 bool IsTypedefType(lldb::opaque_compiler_type_t type) override; 654 655 bool IsVoidType(lldb::opaque_compiler_type_t type) override; 656 657 bool CanPassInRegisters(const CompilerType &type) override; 658 659 bool SupportsLanguage(lldb::LanguageType language) override; 660 661 static llvm::Optional<std::string> GetCXXClassName(const CompilerType &type); 662 663 // Type Completion 664 665 bool GetCompleteType(lldb::opaque_compiler_type_t type) override; 666 667 // Accessors 668 669 ConstString GetTypeName(lldb::opaque_compiler_type_t type) override; 670 671 ConstString GetDisplayTypeName(lldb::opaque_compiler_type_t type) override; 672 673 uint32_t GetTypeInfo(lldb::opaque_compiler_type_t type, 674 CompilerType *pointee_or_element_compiler_type) override; 675 676 lldb::LanguageType 677 GetMinimumLanguage(lldb::opaque_compiler_type_t type) override; 678 679 lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) override; 680 681 unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) override; 682 683 // Creating related types 684 685 /// Using the current type, create a new typedef to that type using 686 /// "typedef_name" as the name and "decl_ctx" as the decl context. 687 /// \param payload is an opaque TypePayloadClang. 688 static CompilerType 689 CreateTypedefType(const CompilerType &type, const char *typedef_name, 690 const CompilerDeclContext &compiler_decl_ctx, 691 uint32_t opaque_payload); 692 693 CompilerType GetArrayElementType(lldb::opaque_compiler_type_t type, 694 uint64_t *stride) override; 695 696 CompilerType GetArrayType(lldb::opaque_compiler_type_t type, 697 uint64_t size) override; 698 699 CompilerType GetCanonicalType(lldb::opaque_compiler_type_t type) override; 700 701 CompilerType 702 GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) override; 703 704 // Returns -1 if this isn't a function of if the function doesn't have a 705 // prototype Returns a value >= 0 if there is a prototype. 706 int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) override; 707 708 CompilerType GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type, 709 size_t idx) override; 710 711 CompilerType 712 GetFunctionReturnType(lldb::opaque_compiler_type_t type) override; 713 714 size_t GetNumMemberFunctions(lldb::opaque_compiler_type_t type) override; 715 716 TypeMemberFunctionImpl 717 GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, 718 size_t idx) override; 719 720 CompilerType GetNonReferenceType(lldb::opaque_compiler_type_t type) override; 721 722 CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) override; 723 724 CompilerType GetPointerType(lldb::opaque_compiler_type_t type) override; 725 726 CompilerType 727 GetLValueReferenceType(lldb::opaque_compiler_type_t type) override; 728 729 CompilerType 730 GetRValueReferenceType(lldb::opaque_compiler_type_t type) override; 731 732 CompilerType GetAtomicType(lldb::opaque_compiler_type_t type) override; 733 734 CompilerType AddConstModifier(lldb::opaque_compiler_type_t type) override; 735 736 CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type) override; 737 738 CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type) override; 739 740 CompilerType CreateTypedef(lldb::opaque_compiler_type_t type, 741 const char *name, 742 const CompilerDeclContext &decl_ctx, 743 uint32_t opaque_payload) override; 744 745 // If the current object represents a typedef type, get the underlying type 746 CompilerType GetTypedefedType(lldb::opaque_compiler_type_t type) override; 747 748 // Create related types using the current type's AST 749 CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) override; 750 751 // Exploring the type 752 753 const llvm::fltSemantics &GetFloatTypeSemantics(size_t byte_size) override; 754 755 llvm::Optional<uint64_t> GetByteSize(lldb::opaque_compiler_type_t type, 756 ExecutionContextScope *exe_scope) { 757 if (llvm::Optional<uint64_t> bit_size = GetBitSize(type, exe_scope)) 758 return (*bit_size + 7) / 8; 759 return llvm::None; 760 } 761 762 llvm::Optional<uint64_t> 763 GetBitSize(lldb::opaque_compiler_type_t type, 764 ExecutionContextScope *exe_scope) override; 765 766 lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type, 767 uint64_t &count) override; 768 769 lldb::Format GetFormat(lldb::opaque_compiler_type_t type) override; 770 771 llvm::Optional<size_t> 772 GetTypeBitAlign(lldb::opaque_compiler_type_t type, 773 ExecutionContextScope *exe_scope) override; 774 775 uint32_t GetNumChildren(lldb::opaque_compiler_type_t type, 776 bool omit_empty_base_classes, 777 const ExecutionContext *exe_ctx) override; 778 779 CompilerType GetBuiltinTypeByName(ConstString name) override; 780 781 lldb::BasicType 782 GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) override; 783 784 static lldb::BasicType 785 GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type, 786 ConstString name); 787 788 void ForEachEnumerator( 789 lldb::opaque_compiler_type_t type, 790 std::function<bool(const CompilerType &integer_type, 791 ConstString name, 792 const llvm::APSInt &value)> const &callback) override; 793 794 uint32_t GetNumFields(lldb::opaque_compiler_type_t type) override; 795 796 CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type, size_t idx, 797 std::string &name, uint64_t *bit_offset_ptr, 798 uint32_t *bitfield_bit_size_ptr, 799 bool *is_bitfield_ptr) override; 800 801 uint32_t GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) override; 802 803 uint32_t GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) override; 804 805 CompilerType GetDirectBaseClassAtIndex(lldb::opaque_compiler_type_t type, 806 size_t idx, 807 uint32_t *bit_offset_ptr) override; 808 809 CompilerType GetVirtualBaseClassAtIndex(lldb::opaque_compiler_type_t type, 810 size_t idx, 811 uint32_t *bit_offset_ptr) override; 812 813 static uint32_t GetNumPointeeChildren(clang::QualType type); 814 815 CompilerType GetChildCompilerTypeAtIndex( 816 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx, 817 bool transparent_pointers, bool omit_empty_base_classes, 818 bool ignore_array_bounds, std::string &child_name, 819 uint32_t &child_byte_size, int32_t &child_byte_offset, 820 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset, 821 bool &child_is_base_class, bool &child_is_deref_of_parent, 822 ValueObject *valobj, uint64_t &language_flags) override; 823 824 // Lookup a child given a name. This function will match base class names and 825 // member member names in "clang_type" only, not descendants. 826 uint32_t GetIndexOfChildWithName(lldb::opaque_compiler_type_t type, 827 const char *name, 828 bool omit_empty_base_classes) override; 829 830 // Lookup a child member given a name. This function will match member names 831 // only and will descend into "clang_type" children in search for the first 832 // member in this class, or any base class that matches "name". 833 // TODO: Return all matches for a given name by returning a 834 // vector<vector<uint32_t>> 835 // so we catch all names that match a given child name, not just the first. 836 size_t 837 GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type, 838 const char *name, bool omit_empty_base_classes, 839 std::vector<uint32_t> &child_indexes) override; 840 841 size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type) override; 842 843 lldb::TemplateArgumentKind 844 GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, 845 size_t idx) override; 846 CompilerType GetTypeTemplateArgument(lldb::opaque_compiler_type_t type, 847 size_t idx) override; 848 llvm::Optional<CompilerType::IntegralTemplateArgument> 849 GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, 850 size_t idx) override; 851 852 CompilerType GetTypeForFormatters(void *type) override; 853 854 #define LLDB_INVALID_DECL_LEVEL UINT32_MAX 855 // LLDB_INVALID_DECL_LEVEL is returned by CountDeclLevels if child_decl_ctx 856 // could not be found in decl_ctx. 857 uint32_t CountDeclLevels(clang::DeclContext *frame_decl_ctx, 858 clang::DeclContext *child_decl_ctx, 859 ConstString *child_name = nullptr, 860 CompilerType *child_type = nullptr); 861 862 // Modifying RecordType 863 static clang::FieldDecl *AddFieldToRecordType(const CompilerType &type, 864 llvm::StringRef name, 865 const CompilerType &field_type, 866 lldb::AccessType access, 867 uint32_t bitfield_bit_size); 868 869 static void BuildIndirectFields(const CompilerType &type); 870 871 static void SetIsPacked(const CompilerType &type); 872 873 static clang::VarDecl *AddVariableToRecordType(const CompilerType &type, 874 llvm::StringRef name, 875 const CompilerType &var_type, 876 lldb::AccessType access); 877 878 /// Initializes a variable with an integer value. 879 /// \param var The variable to initialize. Must not already have an 880 /// initializer and must have an integer or enum type. 881 /// \param init_value The integer value that the variable should be 882 /// initialized to. Has to match the bit width of the 883 /// variable type. 884 static void SetIntegerInitializerForVariable(clang::VarDecl *var, 885 const llvm::APInt &init_value); 886 887 /// Initializes a variable with a floating point value. 888 /// \param var The variable to initialize. Must not already have an 889 /// initializer and must have a floating point type. 890 /// \param init_value The float value that the variable should be 891 /// initialized to. 892 static void 893 SetFloatingInitializerForVariable(clang::VarDecl *var, 894 const llvm::APFloat &init_value); 895 896 clang::CXXMethodDecl *AddMethodToCXXRecordType( 897 lldb::opaque_compiler_type_t type, llvm::StringRef name, 898 const char *mangled_name, const CompilerType &method_type, 899 lldb::AccessType access, bool is_virtual, bool is_static, bool is_inline, 900 bool is_explicit, bool is_attr_used, bool is_artificial); 901 902 void AddMethodOverridesForCXXRecordType(lldb::opaque_compiler_type_t type); 903 904 // C++ Base Classes 905 std::unique_ptr<clang::CXXBaseSpecifier> 906 CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type, 907 lldb::AccessType access, bool is_virtual, 908 bool base_of_class); 909 910 bool TransferBaseClasses( 911 lldb::opaque_compiler_type_t type, 912 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases); 913 914 static bool SetObjCSuperClass(const CompilerType &type, 915 const CompilerType &superclass_compiler_type); 916 917 static bool AddObjCClassProperty(const CompilerType &type, 918 const char *property_name, 919 const CompilerType &property_compiler_type, 920 clang::ObjCIvarDecl *ivar_decl, 921 const char *property_setter_name, 922 const char *property_getter_name, 923 uint32_t property_attributes, 924 ClangASTMetadata *metadata); 925 926 static clang::ObjCMethodDecl *AddMethodToObjCObjectType( 927 const CompilerType &type, 928 const char *name, // the full symbol name as seen in the symbol table 929 // (lldb::opaque_compiler_type_t type, "-[NString 930 // stringWithCString:]") 931 const CompilerType &method_compiler_type, lldb::AccessType access, 932 bool is_artificial, bool is_variadic, bool is_objc_direct_call); 933 934 static bool SetHasExternalStorage(lldb::opaque_compiler_type_t type, 935 bool has_extern); 936 937 // Tag Declarations 938 static bool StartTagDeclarationDefinition(const CompilerType &type); 939 940 static bool CompleteTagDeclarationDefinition(const CompilerType &type); 941 942 // Modifying Enumeration types 943 clang::EnumConstantDecl *AddEnumerationValueToEnumerationType( 944 const CompilerType &enum_type, const Declaration &decl, const char *name, 945 int64_t enum_value, uint32_t enum_value_bit_size); 946 clang::EnumConstantDecl *AddEnumerationValueToEnumerationType( 947 const CompilerType &enum_type, const Declaration &decl, const char *name, 948 const llvm::APSInt &value); 949 950 /// Returns the underlying integer type for an enum type. If the given type 951 /// is invalid or not an enum-type, the function returns an invalid 952 /// CompilerType. 953 CompilerType GetEnumerationIntegerType(CompilerType type); 954 955 // Pointers & References 956 957 // Call this function using the class type when you want to make a member 958 // pointer type to pointee_type. 959 static CompilerType CreateMemberPointerType(const CompilerType &type, 960 const CompilerType &pointee_type); 961 962 // Dumping types 963 #ifndef NDEBUG 964 /// Convenience LLVM-style dump method for use in the debugger only. 965 /// In contrast to the other \p Dump() methods this directly invokes 966 /// \p clang::QualType::dump(). 967 LLVM_DUMP_METHOD void dump(lldb::opaque_compiler_type_t type) const override; 968 #endif 969 970 void Dump(Stream &s); 971 972 /// Dump clang AST types from the symbol file. 973 /// 974 /// \param[in] s 975 /// A stream to send the dumped AST node(s) to 976 /// \param[in] symbol_name 977 /// The name of the symbol to dump, if it is empty dump all the symbols 978 void DumpFromSymbolFile(Stream &s, llvm::StringRef symbol_name); 979 980 void DumpValue(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, 981 Stream *s, lldb::Format format, const DataExtractor &data, 982 lldb::offset_t data_offset, size_t data_byte_size, 983 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, 984 bool show_types, bool show_summary, bool verbose, 985 uint32_t depth) override; 986 987 bool DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s, 988 lldb::Format format, const DataExtractor &data, 989 lldb::offset_t data_offset, size_t data_byte_size, 990 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, 991 ExecutionContextScope *exe_scope) override; 992 993 void DumpSummary(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, 994 Stream *s, const DataExtractor &data, 995 lldb::offset_t data_offset, size_t data_byte_size) override; 996 997 void DumpTypeDescription( 998 lldb::opaque_compiler_type_t type, 999 lldb::DescriptionLevel level = lldb::eDescriptionLevelFull) override; 1000 1001 void DumpTypeDescription( 1002 lldb::opaque_compiler_type_t type, Stream *s, 1003 lldb::DescriptionLevel level = lldb::eDescriptionLevelFull) override; 1004 1005 static void DumpTypeName(const CompilerType &type); 1006 1007 static clang::EnumDecl *GetAsEnumDecl(const CompilerType &type); 1008 1009 static clang::RecordDecl *GetAsRecordDecl(const CompilerType &type); 1010 1011 static clang::TagDecl *GetAsTagDecl(const CompilerType &type); 1012 1013 static clang::TypedefNameDecl *GetAsTypedefDecl(const CompilerType &type); 1014 1015 static clang::CXXRecordDecl * 1016 GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type); 1017 1018 static clang::ObjCInterfaceDecl * 1019 GetAsObjCInterfaceDecl(const CompilerType &type); 1020 1021 clang::ClassTemplateDecl *ParseClassTemplateDecl( 1022 clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, 1023 lldb::AccessType access_type, const char *parent_name, int tag_decl_kind, 1024 const TypeSystemClang::TemplateParameterInfos &template_param_infos); 1025 1026 clang::BlockDecl *CreateBlockDeclaration(clang::DeclContext *ctx, 1027 OptionalClangModuleID owning_module); 1028 1029 clang::UsingDirectiveDecl * 1030 CreateUsingDirectiveDeclaration(clang::DeclContext *decl_ctx, 1031 OptionalClangModuleID owning_module, 1032 clang::NamespaceDecl *ns_decl); 1033 1034 clang::UsingDecl *CreateUsingDeclaration(clang::DeclContext *current_decl_ctx, 1035 OptionalClangModuleID owning_module, 1036 clang::NamedDecl *target); 1037 1038 clang::VarDecl *CreateVariableDeclaration(clang::DeclContext *decl_context, 1039 OptionalClangModuleID owning_module, 1040 const char *name, 1041 clang::QualType type); 1042 1043 static lldb::opaque_compiler_type_t 1044 GetOpaqueCompilerType(clang::ASTContext *ast, lldb::BasicType basic_type); 1045 1046 static clang::QualType GetQualType(lldb::opaque_compiler_type_t type) { 1047 if (type) 1048 return clang::QualType::getFromOpaquePtr(type); 1049 return clang::QualType(); 1050 } 1051 1052 static clang::QualType 1053 GetCanonicalQualType(lldb::opaque_compiler_type_t type) { 1054 if (type) 1055 return clang::QualType::getFromOpaquePtr(type).getCanonicalType(); 1056 return clang::QualType(); 1057 } 1058 1059 clang::DeclarationName 1060 GetDeclarationName(const char *name, const CompilerType &function_clang_type); 1061 1062 clang::LangOptions *GetLangOpts() const { 1063 return m_language_options_up.get(); 1064 } 1065 clang::SourceManager *GetSourceMgr() const { 1066 return m_source_manager_up.get(); 1067 } 1068 1069 private: 1070 const clang::ClassTemplateSpecializationDecl * 1071 GetAsTemplateSpecialization(lldb::opaque_compiler_type_t type); 1072 1073 // Classes that inherit from TypeSystemClang can see and modify these 1074 std::string m_target_triple; 1075 std::unique_ptr<clang::ASTContext> m_ast_up; 1076 std::unique_ptr<clang::LangOptions> m_language_options_up; 1077 std::unique_ptr<clang::FileManager> m_file_manager_up; 1078 std::unique_ptr<clang::SourceManager> m_source_manager_up; 1079 std::unique_ptr<clang::DiagnosticsEngine> m_diagnostics_engine_up; 1080 std::unique_ptr<clang::DiagnosticConsumer> m_diagnostic_consumer_up; 1081 std::shared_ptr<clang::TargetOptions> m_target_options_rp; 1082 std::unique_ptr<clang::TargetInfo> m_target_info_up; 1083 std::unique_ptr<clang::IdentifierTable> m_identifier_table_up; 1084 std::unique_ptr<clang::SelectorTable> m_selector_table_up; 1085 std::unique_ptr<clang::Builtin::Context> m_builtins_up; 1086 std::unique_ptr<clang::HeaderSearch> m_header_search_up; 1087 std::unique_ptr<clang::ModuleMap> m_module_map_up; 1088 std::unique_ptr<DWARFASTParserClang> m_dwarf_ast_parser_up; 1089 #ifdef LLDB_ENABLE_ALL 1090 std::unique_ptr<PDBASTParser> m_pdb_ast_parser_up; 1091 #endif // LLDB_ENABLE_ALL 1092 std::unique_ptr<clang::MangleContext> m_mangle_ctx_up; 1093 uint32_t m_pointer_byte_size = 0; 1094 bool m_ast_owned = false; 1095 /// A string describing what this TypeSystemClang represents (e.g., 1096 /// AST for debug information, an expression, some other utility ClangAST). 1097 /// Useful for logging and debugging. 1098 std::string m_display_name; 1099 1100 typedef llvm::DenseMap<const clang::Decl *, ClangASTMetadata> DeclMetadataMap; 1101 /// Maps Decls to their associated ClangASTMetadata. 1102 DeclMetadataMap m_decl_metadata; 1103 1104 typedef llvm::DenseMap<const clang::Type *, ClangASTMetadata> TypeMetadataMap; 1105 /// Maps Types to their associated ClangASTMetadata. 1106 TypeMetadataMap m_type_metadata; 1107 1108 /// The sema associated that is currently used to build this ASTContext. 1109 /// May be null if we are already done parsing this ASTContext or the 1110 /// ASTContext wasn't created by parsing source code. 1111 clang::Sema *m_sema = nullptr; 1112 1113 // For TypeSystemClang only 1114 TypeSystemClang(const TypeSystemClang &); 1115 const TypeSystemClang &operator=(const TypeSystemClang &); 1116 /// Creates the internal ASTContext. 1117 void CreateASTContext(); 1118 void SetTargetTriple(llvm::StringRef target_triple); 1119 }; 1120 1121 /// The TypeSystemClang instance used for the scratch ASTContext in a 1122 /// lldb::Target. 1123 class TypeSystemClangForExpressions : public TypeSystemClang { 1124 public: 1125 TypeSystemClangForExpressions(Target &target, llvm::Triple triple); 1126 1127 ~TypeSystemClangForExpressions() override = default; 1128 1129 void Finalize() override; 1130 1131 UserExpression * 1132 GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix, 1133 lldb::LanguageType language, 1134 Expression::ResultType desired_type, 1135 const EvaluateExpressionOptions &options, 1136 ValueObject *ctx_obj) override; 1137 1138 FunctionCaller *GetFunctionCaller(const CompilerType &return_type, 1139 const Address &function_address, 1140 const ValueList &arg_value_list, 1141 const char *name) override; 1142 1143 UtilityFunction *GetUtilityFunction(const char *text, 1144 const char *name) override; 1145 1146 PersistentExpressionState *GetPersistentExpressionState() override; 1147 private: 1148 lldb::TargetWP m_target_wp; 1149 std::unique_ptr<ClangPersistentVariables> 1150 m_persistent_variables; // These are the persistent variables associated 1151 // with this process for the expression parser 1152 std::unique_ptr<ClangASTSource> m_scratch_ast_source_up; 1153 }; 1154 1155 } // namespace lldb_private 1156 1157 #endif // LLDB_SOURCE_PLUGINS_TYPESYSTEM_CLANG_TYPESYSTEMCLANG_H 1158