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