1 //===-- TypeSystem.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_SYMBOL_TYPESYSTEM_H 10 #define LLDB_SYMBOL_TYPESYSTEM_H 11 12 #include <functional> 13 #include <map> 14 #include <mutex> 15 #include <string> 16 17 #include "llvm/ADT/APFloat.h" 18 #include "llvm/ADT/APSInt.h" 19 #include "llvm/ADT/SmallBitVector.h" 20 #include "llvm/Support/Casting.h" 21 #include "llvm/Support/Error.h" 22 23 #include "lldb/Core/PluginInterface.h" 24 #include "lldb/Expression/Expression.h" 25 #include "lldb/Symbol/CompilerDecl.h" 26 #include "lldb/Symbol/CompilerDeclContext.h" 27 #include "lldb/lldb-private.h" 28 29 class DWARFDIE; 30 class DWARFASTParser; 31 class PDBASTParser; 32 33 namespace lldb_private { 34 35 /// A SmallBitVector that represents a set of source languages (\p 36 /// lldb::LanguageType). Each lldb::LanguageType is represented by 37 /// the bit with the position of its enumerator. The largest 38 /// LanguageType is < 64, so this is space-efficient and on 64-bit 39 /// architectures a LanguageSet can be completely stack-allocated. 40 struct LanguageSet { 41 llvm::SmallBitVector bitvector; 42 LanguageSet(); 43 44 /// If the set contains a single language only, return it. 45 llvm::Optional<lldb::LanguageType> GetSingularLanguage(); 46 void Insert(lldb::LanguageType language); 47 bool Empty() const; 48 size_t Size() const; 49 bool operator[](unsigned i) const; 50 }; 51 52 /// Interface for representing a type system. 53 /// 54 /// Implemented by language plugins to define the type system for a given 55 /// language. 56 /// 57 /// This interface extensively used opaque pointers to prevent that generic 58 /// LLDB code has dependencies on language plugins. The type and semantics of 59 /// these opaque pointers are defined by the TypeSystem implementation inside 60 /// the respective language plugin. Opaque pointers from one TypeSystem 61 /// instance should never be passed to a different TypeSystem instance (even 62 /// when the language plugin for both TypeSystem instances is the same). 63 /// 64 /// Most of the functions in this class should not be called directly but only 65 /// called by their respective counterparts in CompilerType, CompilerDecl and 66 /// CompilerDeclContext. 67 /// 68 /// \see lldb_private::CompilerType 69 /// \see lldb_private::CompilerDecl 70 /// \see lldb_private::CompilerDeclContext 71 class TypeSystem : public PluginInterface { 72 public: 73 // Constructors and Destructors 74 ~TypeSystem() override; 75 76 // LLVM RTTI support 77 virtual bool isA(const void *ClassID) const = 0; 78 79 static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language, 80 Module *module); 81 82 static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language, 83 Target *target); 84 85 // Free up any resources associated with this TypeSystem. Done before 86 // removing all the TypeSystems from the TypeSystemMap. Finalize()87 virtual void Finalize() {} 88 GetDWARFParser()89 virtual DWARFASTParser *GetDWARFParser() { return nullptr; } GetPDBParser()90 virtual PDBASTParser *GetPDBParser() { return nullptr; } 91 GetSymbolFile()92 virtual SymbolFile *GetSymbolFile() const { return m_sym_file; } 93 94 // Returns true if the symbol file changed during the set accessor. SetSymbolFile(SymbolFile * sym_file)95 virtual void SetSymbolFile(SymbolFile *sym_file) { m_sym_file = sym_file; } 96 97 // CompilerDecl functions 98 virtual ConstString DeclGetName(void *opaque_decl) = 0; 99 100 virtual ConstString DeclGetMangledName(void *opaque_decl); 101 102 virtual CompilerDeclContext DeclGetDeclContext(void *opaque_decl); 103 104 virtual CompilerType DeclGetFunctionReturnType(void *opaque_decl); 105 106 virtual size_t DeclGetFunctionNumArguments(void *opaque_decl); 107 108 virtual CompilerType DeclGetFunctionArgumentType(void *opaque_decl, 109 size_t arg_idx); 110 111 virtual CompilerType GetTypeForDecl(void *opaque_decl) = 0; 112 113 // CompilerDeclContext functions 114 115 virtual std::vector<CompilerDecl> 116 DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name, 117 const bool ignore_imported_decls); 118 119 virtual ConstString DeclContextGetName(void *opaque_decl_ctx) = 0; 120 121 virtual ConstString 122 DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) = 0; 123 124 virtual bool DeclContextIsClassMethod( 125 void *opaque_decl_ctx, lldb::LanguageType *language_ptr, 126 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) = 0; 127 128 virtual bool DeclContextIsContainedInLookup(void *opaque_decl_ctx, 129 void *other_opaque_decl_ctx) = 0; 130 131 // Tests 132 #ifndef NDEBUG 133 /// Verify the integrity of the type to catch CompilerTypes that mix 134 /// and match invalid TypeSystem/Opaque type pairs. 135 virtual bool Verify(lldb::opaque_compiler_type_t type) = 0; 136 #endif 137 138 virtual bool IsArrayType(lldb::opaque_compiler_type_t type, 139 CompilerType *element_type, uint64_t *size, 140 bool *is_incomplete) = 0; 141 142 virtual bool IsAggregateType(lldb::opaque_compiler_type_t type) = 0; 143 144 virtual bool IsAnonymousType(lldb::opaque_compiler_type_t type); 145 146 virtual bool IsCharType(lldb::opaque_compiler_type_t type) = 0; 147 148 virtual bool IsCompleteType(lldb::opaque_compiler_type_t type) = 0; 149 150 virtual bool IsDefined(lldb::opaque_compiler_type_t type) = 0; 151 152 virtual bool IsFloatingPointType(lldb::opaque_compiler_type_t type, 153 uint32_t &count, bool &is_complex) = 0; 154 155 virtual bool IsFunctionType(lldb::opaque_compiler_type_t type) = 0; 156 157 virtual size_t 158 GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) = 0; 159 160 virtual CompilerType 161 GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type, 162 const size_t index) = 0; 163 164 virtual bool IsFunctionPointerType(lldb::opaque_compiler_type_t type) = 0; 165 166 virtual bool IsBlockPointerType(lldb::opaque_compiler_type_t type, 167 CompilerType *function_pointer_type_ptr) = 0; 168 169 virtual bool IsIntegerType(lldb::opaque_compiler_type_t type, 170 bool &is_signed) = 0; 171 IsEnumerationType(lldb::opaque_compiler_type_t type,bool & is_signed)172 virtual bool IsEnumerationType(lldb::opaque_compiler_type_t type, 173 bool &is_signed) { 174 is_signed = false; 175 return false; 176 } 177 178 virtual bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type, 179 CompilerType *target_type, // Can pass NULL 180 bool check_cplusplus, bool check_objc) = 0; 181 182 virtual bool IsPointerType(lldb::opaque_compiler_type_t type, 183 CompilerType *pointee_type) = 0; 184 185 virtual bool IsScalarType(lldb::opaque_compiler_type_t type) = 0; 186 187 virtual bool IsVoidType(lldb::opaque_compiler_type_t type) = 0; 188 189 virtual bool CanPassInRegisters(const CompilerType &type) = 0; 190 191 // TypeSystems can support more than one language 192 virtual bool SupportsLanguage(lldb::LanguageType language) = 0; 193 194 // Type Completion 195 196 virtual bool GetCompleteType(lldb::opaque_compiler_type_t type) = 0; 197 198 // AST related queries 199 200 virtual uint32_t GetPointerByteSize() = 0; 201 202 // Accessors 203 204 virtual ConstString GetTypeName(lldb::opaque_compiler_type_t type) = 0; 205 206 virtual ConstString GetDisplayTypeName(lldb::opaque_compiler_type_t type) = 0; 207 208 virtual uint32_t 209 GetTypeInfo(lldb::opaque_compiler_type_t type, 210 CompilerType *pointee_or_element_compiler_type) = 0; 211 212 virtual lldb::LanguageType 213 GetMinimumLanguage(lldb::opaque_compiler_type_t type) = 0; 214 215 virtual lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) = 0; 216 217 // Creating related types 218 219 virtual CompilerType 220 GetArrayElementType(lldb::opaque_compiler_type_t type, 221 ExecutionContextScope *exe_scope) = 0; 222 223 virtual CompilerType GetArrayType(lldb::opaque_compiler_type_t type, 224 uint64_t size); 225 226 virtual CompilerType GetCanonicalType(lldb::opaque_compiler_type_t type) = 0; 227 228 // Returns -1 if this isn't a function of if the function doesn't have a 229 // prototype Returns a value >= 0 if there is a prototype. 230 virtual int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) = 0; 231 232 virtual CompilerType 233 GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type, 234 size_t idx) = 0; 235 236 virtual CompilerType 237 GetFunctionReturnType(lldb::opaque_compiler_type_t type) = 0; 238 239 virtual size_t GetNumMemberFunctions(lldb::opaque_compiler_type_t type) = 0; 240 241 virtual TypeMemberFunctionImpl 242 GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, size_t idx) = 0; 243 244 virtual CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) = 0; 245 246 virtual CompilerType GetPointerType(lldb::opaque_compiler_type_t type) = 0; 247 248 virtual CompilerType 249 GetLValueReferenceType(lldb::opaque_compiler_type_t type); 250 251 virtual CompilerType 252 GetRValueReferenceType(lldb::opaque_compiler_type_t type); 253 254 virtual CompilerType GetAtomicType(lldb::opaque_compiler_type_t type); 255 256 virtual CompilerType AddConstModifier(lldb::opaque_compiler_type_t type); 257 258 virtual CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type); 259 260 virtual CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type); 261 262 /// \param opaque_payload The m_payload field of Type, which may 263 /// carry TypeSystem-specific extra information. 264 virtual CompilerType CreateTypedef(lldb::opaque_compiler_type_t type, 265 const char *name, 266 const CompilerDeclContext &decl_ctx, 267 uint32_t opaque_payload); 268 269 // Exploring the type 270 271 virtual const llvm::fltSemantics &GetFloatTypeSemantics(size_t byte_size) = 0; 272 273 virtual llvm::Optional<uint64_t> 274 GetBitSize(lldb::opaque_compiler_type_t type, 275 ExecutionContextScope *exe_scope) = 0; 276 277 virtual lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type, 278 uint64_t &count) = 0; 279 280 virtual lldb::Format GetFormat(lldb::opaque_compiler_type_t type) = 0; 281 282 virtual uint32_t GetNumChildren(lldb::opaque_compiler_type_t type, 283 bool omit_empty_base_classes, 284 const ExecutionContext *exe_ctx) = 0; 285 286 virtual CompilerType GetBuiltinTypeByName(ConstString name); 287 288 virtual lldb::BasicType 289 GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) = 0; 290 ForEachEnumerator(lldb::opaque_compiler_type_t type,std::function<bool (const CompilerType & integer_type,ConstString name,const llvm::APSInt & value)> const & callback)291 virtual void ForEachEnumerator( 292 lldb::opaque_compiler_type_t type, 293 std::function<bool(const CompilerType &integer_type, 294 ConstString name, 295 const llvm::APSInt &value)> const &callback) {} 296 297 virtual uint32_t GetNumFields(lldb::opaque_compiler_type_t type) = 0; 298 299 virtual CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type, 300 size_t idx, std::string &name, 301 uint64_t *bit_offset_ptr, 302 uint32_t *bitfield_bit_size_ptr, 303 bool *is_bitfield_ptr) = 0; 304 305 virtual uint32_t 306 GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) = 0; 307 308 virtual uint32_t 309 GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) = 0; 310 311 virtual CompilerType 312 GetDirectBaseClassAtIndex(lldb::opaque_compiler_type_t type, size_t idx, 313 uint32_t *bit_offset_ptr) = 0; 314 315 virtual CompilerType 316 GetVirtualBaseClassAtIndex(lldb::opaque_compiler_type_t type, size_t idx, 317 uint32_t *bit_offset_ptr) = 0; 318 319 virtual CompilerType GetChildCompilerTypeAtIndex( 320 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx, 321 bool transparent_pointers, bool omit_empty_base_classes, 322 bool ignore_array_bounds, std::string &child_name, 323 uint32_t &child_byte_size, int32_t &child_byte_offset, 324 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset, 325 bool &child_is_base_class, bool &child_is_deref_of_parent, 326 ValueObject *valobj, uint64_t &language_flags) = 0; 327 328 // Lookup a child given a name. This function will match base class names and 329 // member member names in "clang_type" only, not descendants. 330 virtual uint32_t GetIndexOfChildWithName(lldb::opaque_compiler_type_t type, 331 const char *name, 332 bool omit_empty_base_classes) = 0; 333 334 // Lookup a child member given a name. This function will match member names 335 // only and will descend into "clang_type" children in search for the first 336 // member in this class, or any base class that matches "name". 337 // TODO: Return all matches for a given name by returning a 338 // vector<vector<uint32_t>> 339 // so we catch all names that match a given child name, not just the first. 340 virtual size_t 341 GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type, 342 const char *name, bool omit_empty_base_classes, 343 std::vector<uint32_t> &child_indexes) = 0; 344 345 virtual size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type); 346 347 virtual lldb::TemplateArgumentKind 348 GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, size_t idx); 349 virtual CompilerType GetTypeTemplateArgument(lldb::opaque_compiler_type_t type, 350 size_t idx); 351 virtual llvm::Optional<CompilerType::IntegralTemplateArgument> 352 GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx); 353 354 // Dumping types 355 356 #ifndef NDEBUG 357 /// Convenience LLVM-style dump method for use in the debugger only. 358 LLVM_DUMP_METHOD virtual void 359 dump(lldb::opaque_compiler_type_t type) const = 0; 360 #endif 361 362 virtual void DumpValue(lldb::opaque_compiler_type_t type, 363 ExecutionContext *exe_ctx, Stream *s, 364 lldb::Format format, const DataExtractor &data, 365 lldb::offset_t data_offset, size_t data_byte_size, 366 uint32_t bitfield_bit_size, 367 uint32_t bitfield_bit_offset, bool show_types, 368 bool show_summary, bool verbose, uint32_t depth) = 0; 369 370 virtual bool DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s, 371 lldb::Format format, const DataExtractor &data, 372 lldb::offset_t data_offset, size_t data_byte_size, 373 uint32_t bitfield_bit_size, 374 uint32_t bitfield_bit_offset, 375 ExecutionContextScope *exe_scope) = 0; 376 377 /// Dump the type to stdout. 378 virtual void DumpTypeDescription( 379 lldb::opaque_compiler_type_t type, 380 lldb::DescriptionLevel level = lldb::eDescriptionLevelFull) = 0; 381 382 /// Print a description of the type to a stream. The exact implementation 383 /// varies, but the expectation is that eDescriptionLevelFull returns a 384 /// source-like representation of the type, whereas eDescriptionLevelVerbose 385 /// does a dump of the underlying AST if applicable. 386 virtual void DumpTypeDescription( 387 lldb::opaque_compiler_type_t type, Stream *s, 388 lldb::DescriptionLevel level = lldb::eDescriptionLevelFull) = 0; 389 390 // TODO: These methods appear unused. Should they be removed? 391 392 virtual bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) = 0; 393 394 virtual void DumpSummary(lldb::opaque_compiler_type_t type, 395 ExecutionContext *exe_ctx, Stream *s, 396 const DataExtractor &data, 397 lldb::offset_t data_offset, 398 size_t data_byte_size) = 0; 399 400 // TODO: Determine if these methods should move to TypeSystemClang. 401 402 virtual bool IsPointerOrReferenceType(lldb::opaque_compiler_type_t type, 403 CompilerType *pointee_type) = 0; 404 405 virtual unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) = 0; 406 407 virtual bool IsCStringType(lldb::opaque_compiler_type_t type, 408 uint32_t &length) = 0; 409 410 virtual llvm::Optional<size_t> 411 GetTypeBitAlign(lldb::opaque_compiler_type_t type, 412 ExecutionContextScope *exe_scope) = 0; 413 414 virtual CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) = 0; 415 416 virtual CompilerType 417 GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding, 418 size_t bit_size) = 0; 419 420 virtual bool IsBeingDefined(lldb::opaque_compiler_type_t type) = 0; 421 422 virtual bool IsConst(lldb::opaque_compiler_type_t type) = 0; 423 424 virtual uint32_t IsHomogeneousAggregate(lldb::opaque_compiler_type_t type, 425 CompilerType *base_type_ptr) = 0; 426 427 virtual bool IsPolymorphicClass(lldb::opaque_compiler_type_t type) = 0; 428 429 virtual bool IsTypedefType(lldb::opaque_compiler_type_t type) = 0; 430 431 // If the current object represents a typedef type, get the underlying type 432 virtual CompilerType GetTypedefedType(lldb::opaque_compiler_type_t type) = 0; 433 434 virtual bool IsVectorType(lldb::opaque_compiler_type_t type, 435 CompilerType *element_type, uint64_t *size) = 0; 436 437 virtual CompilerType 438 GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) = 0; 439 440 virtual CompilerType 441 GetNonReferenceType(lldb::opaque_compiler_type_t type) = 0; 442 443 virtual bool IsReferenceType(lldb::opaque_compiler_type_t type, 444 CompilerType *pointee_type, bool *is_rvalue) = 0; 445 446 virtual bool ShouldTreatScalarValueAsAddress(lldb::opaque_compiler_type_t type)447 ShouldTreatScalarValueAsAddress(lldb::opaque_compiler_type_t type) { 448 return IsPointerOrReferenceType(type, nullptr); 449 } 450 451 virtual UserExpression * GetUserExpression(llvm::StringRef expr,llvm::StringRef prefix,lldb::LanguageType language,Expression::ResultType desired_type,const EvaluateExpressionOptions & options,ValueObject * ctx_obj)452 GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix, 453 lldb::LanguageType language, 454 Expression::ResultType desired_type, 455 const EvaluateExpressionOptions &options, 456 ValueObject *ctx_obj) { 457 return nullptr; 458 } 459 GetFunctionCaller(const CompilerType & return_type,const Address & function_address,const ValueList & arg_value_list,const char * name)460 virtual FunctionCaller *GetFunctionCaller(const CompilerType &return_type, 461 const Address &function_address, 462 const ValueList &arg_value_list, 463 const char *name) { 464 return nullptr; 465 } 466 467 virtual std::unique_ptr<UtilityFunction> 468 CreateUtilityFunction(std::string text, std::string name); 469 GetPersistentExpressionState()470 virtual PersistentExpressionState *GetPersistentExpressionState() { 471 return nullptr; 472 } 473 474 virtual CompilerType GetTypeForFormatters(void *type); 475 476 virtual LazyBool ShouldPrintAsOneLiner(void *type, ValueObject *valobj); 477 478 // Type systems can have types that are placeholder types, which are meant to 479 // indicate the presence of a type, but offer no actual information about 480 // said types, and leave the burden of actually figuring type information out 481 // to dynamic type resolution. For instance a language with a generics 482 // system, can use placeholder types to indicate "type argument goes here", 483 // without promising uniqueness of the placeholder, nor attaching any 484 // actually idenfiable information to said placeholder. This API allows type 485 // systems to tell LLDB when such a type has been encountered In response, 486 // the debugger can react by not using this type as a cache entry in any 487 // type-specific way For instance, LLDB will currently not cache any 488 // formatters that are discovered on such a type as attributable to the 489 // meaningless type itself, instead preferring to use the dynamic type 490 virtual bool IsMeaninglessWithoutDynamicResolution(void *type); 491 492 protected: 493 SymbolFile *m_sym_file = nullptr; 494 }; 495 496 class TypeSystemMap { 497 public: 498 TypeSystemMap(); 499 ~TypeSystemMap(); 500 501 // Clear calls Finalize on all the TypeSystems managed by this map, and then 502 // empties the map. 503 void Clear(); 504 505 // Iterate through all of the type systems that are created. Return true from 506 // callback to keep iterating, false to stop iterating. 507 void ForEach(std::function<bool(TypeSystem *)> const &callback); 508 509 llvm::Expected<TypeSystem &> 510 GetTypeSystemForLanguage(lldb::LanguageType language, Module *module, 511 bool can_create); 512 513 llvm::Expected<TypeSystem &> 514 GetTypeSystemForLanguage(lldb::LanguageType language, Target *target, 515 bool can_create); 516 517 protected: 518 typedef std::map<lldb::LanguageType, lldb::TypeSystemSP> collection; 519 mutable std::mutex m_mutex; ///< A mutex to keep this object happy in 520 ///multi-threaded environments. 521 collection m_map; 522 bool m_clear_in_progress; 523 524 private: 525 typedef llvm::function_ref<lldb::TypeSystemSP()> CreateCallback; 526 /// Finds the type system for the given language. If no type system could be 527 /// found for a language and a CreateCallback was provided, the value returned 528 /// by the callback will be treated as the TypeSystem for the language. 529 /// 530 /// \param language The language for which the type system should be found. 531 /// \param create_callback A callback that will be called if no previously 532 /// created TypeSystem that fits the given language 533 /// could found. Can be omitted if a non-existent 534 /// type system should be treated as an error instead. 535 /// \return The found type system or an error. 536 llvm::Expected<TypeSystem &> GetTypeSystemForLanguage( 537 lldb::LanguageType language, 538 llvm::Optional<CreateCallback> create_callback = llvm::None); 539 }; 540 541 } // namespace lldb_private 542 543 #endif // LLDB_SYMBOL_TYPESYSTEM_H 544