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