TypeSystem.h revision 341825
152419Sjulian//===-- TypeSystem.h ------------------------------------------*- C++ -*-===// 252419Sjulian// 352419Sjulian// The LLVM Compiler Infrastructure 452419Sjulian// 552419Sjulian// This file is distributed under the University of Illinois Open Source 652419Sjulian// License. See LICENSE.TXT for details. 752419Sjulian// 852419Sjulian//===----------------------------------------------------------------------===// 952419Sjulian 1052419Sjulian#ifndef liblldb_TypeSystem_h_ 1152419Sjulian#define liblldb_TypeSystem_h_ 1252419Sjulian 1352419Sjulian// C Includes 1452419Sjulian// C++ Includes 1552419Sjulian#include <functional> 1652419Sjulian#include <map> 1752419Sjulian#include <mutex> 1852419Sjulian#include <string> 1952419Sjulian 2052419Sjulian// Other libraries and framework includes 2152419Sjulian#include "llvm/ADT/APSInt.h" 2252419Sjulian#include "llvm/Support/Casting.h" 2352419Sjulian 2452419Sjulian// Project includes 2552419Sjulian#include "lldb/Core/PluginInterface.h" 2652419Sjulian#include "lldb/Expression/Expression.h" 2752419Sjulian#include "lldb/Symbol/CompilerDecl.h" 2852419Sjulian#include "lldb/Symbol/CompilerDeclContext.h" 2952419Sjulian#include "lldb/lldb-private.h" 3052419Sjulian 3152419Sjulianclass DWARFDIE; 3252419Sjulianclass DWARFASTParser; 3352419Sjulian 3452419Sjuliannamespace lldb_private { 3552419Sjulian 3652419Sjulian//---------------------------------------------------------------------- 3752419Sjulian// Interface for representing the Type Systems in different languages. 3852419Sjulian//---------------------------------------------------------------------- 3952419Sjulianclass TypeSystem : public PluginInterface { 40158882Sglebiuspublic: 41158882Sglebius //---------------------------------------------------------------------- 42158882Sglebius // Intrusive type system that allows us to use llvm casting. 43158882Sglebius // 44158882Sglebius // To add a new type system: 4552419Sjulian // 4652419Sjulian // 1 - Add a new enumeration for llvm casting below for your TypeSystem 4752419Sjulian // subclass, here we will use eKindFoo 4852419Sjulian // 4952419Sjulian // 2 - Your TypeSystem subclass will inherit from TypeSystem and needs 5052419Sjulian // to implement a static classof() function that returns your 5152419Sjulian // enumeration: 5252419Sjulian // 5352419Sjulian // class Foo : public lldb_private::TypeSystem 5452419Sjulian // { 55125011Sru // static bool classof(const TypeSystem *ts) 56160423Sstefanf // { 5752419Sjulian // return ts->getKind() == TypeSystem::eKindFoo; 5852419Sjulian // } 5952419Sjulian // }; 6052419Sjulian // 6152419Sjulian // 3 - Contruct your TypeSystem subclass with the enumeration from below 6252419Sjulian // 6352419Sjulian // Foo() : 6452419Sjulian // TypeSystem(TypeSystem::eKindFoo), 6552419Sjulian // ... 6652419Sjulian // { 6752419Sjulian // } 6852419Sjulian // 6952419Sjulian // Then you can use the llvm casting on any "TypeSystem *" to get an instance 7052419Sjulian // of your subclass. 7152419Sjulian //---------------------------------------------------------------------- 7252419Sjulian enum LLVMCastKind { 7352419Sjulian eKindClang, 74160002Sglebius eKindSwift, 7552419Sjulian eKindGo, 7652419Sjulian eKindJava, 7752419Sjulian eKindOCaml, 7852419Sjulian kNumKinds 7952419Sjulian }; 8052419Sjulian 8152419Sjulian //---------------------------------------------------------------------- 8252419Sjulian // Constructors and Destructors 8352419Sjulian //---------------------------------------------------------------------- 8452419Sjulian TypeSystem(LLVMCastKind kind); 8552419Sjulian 86160002Sglebius ~TypeSystem() override; 8752419Sjulian 8852419Sjulian LLVMCastKind getKind() const { return m_kind; } 8952419Sjulian 9052419Sjulian static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language, 9152419Sjulian Module *module); 9252419Sjulian 9352419Sjulian static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language, 9452419Sjulian Target *target); 9552419Sjulian 9652419Sjulian // Free up any resources associated with this TypeSystem. Done before 9752419Sjulian // removing all the TypeSystems from the TypeSystemMap. 9852419Sjulian virtual void Finalize() {} 99160002Sglebius 10052419Sjulian virtual DWARFASTParser *GetDWARFParser() { return nullptr; } 10152419Sjulian 102 virtual SymbolFile *GetSymbolFile() const { return m_sym_file; } 103 104 // Returns true if the symbol file changed during the set accessor. 105 virtual void SetSymbolFile(SymbolFile *sym_file) { m_sym_file = sym_file; } 106 107 //---------------------------------------------------------------------- 108 // CompilerDecl functions 109 //---------------------------------------------------------------------- 110 virtual ConstString DeclGetName(void *opaque_decl) = 0; 111 112 virtual ConstString DeclGetMangledName(void *opaque_decl); 113 114 virtual CompilerDeclContext DeclGetDeclContext(void *opaque_decl); 115 116 virtual CompilerType DeclGetFunctionReturnType(void *opaque_decl); 117 118 virtual size_t DeclGetFunctionNumArguments(void *opaque_decl); 119 120 virtual CompilerType DeclGetFunctionArgumentType(void *opaque_decl, 121 size_t arg_idx); 122 123 //---------------------------------------------------------------------- 124 // CompilerDeclContext functions 125 //---------------------------------------------------------------------- 126 127 virtual std::vector<CompilerDecl> 128 DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name, 129 const bool ignore_imported_decls); 130 131 virtual bool DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) = 0; 132 133 virtual ConstString DeclContextGetName(void *opaque_decl_ctx) = 0; 134 135 virtual ConstString 136 DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) = 0; 137 138 virtual bool DeclContextIsClassMethod( 139 void *opaque_decl_ctx, lldb::LanguageType *language_ptr, 140 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) = 0; 141 142 //---------------------------------------------------------------------- 143 // Tests 144 //---------------------------------------------------------------------- 145 146 virtual bool IsArrayType(lldb::opaque_compiler_type_t type, 147 CompilerType *element_type, uint64_t *size, 148 bool *is_incomplete) = 0; 149 150 virtual bool IsAggregateType(lldb::opaque_compiler_type_t type) = 0; 151 152 virtual bool IsAnonymousType(lldb::opaque_compiler_type_t type); 153 154 virtual bool IsCharType(lldb::opaque_compiler_type_t type) = 0; 155 156 virtual bool IsCompleteType(lldb::opaque_compiler_type_t type) = 0; 157 158 virtual bool IsDefined(lldb::opaque_compiler_type_t type) = 0; 159 160 virtual bool IsFloatingPointType(lldb::opaque_compiler_type_t type, 161 uint32_t &count, bool &is_complex) = 0; 162 163 virtual bool IsFunctionType(lldb::opaque_compiler_type_t type, 164 bool *is_variadic_ptr) = 0; 165 166 virtual size_t 167 GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) = 0; 168 169 virtual CompilerType 170 GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type, 171 const size_t index) = 0; 172 173 virtual bool IsFunctionPointerType(lldb::opaque_compiler_type_t type) = 0; 174 175 virtual bool IsBlockPointerType(lldb::opaque_compiler_type_t type, 176 CompilerType *function_pointer_type_ptr) = 0; 177 178 virtual bool IsIntegerType(lldb::opaque_compiler_type_t type, 179 bool &is_signed) = 0; 180 181 virtual bool IsEnumerationType(lldb::opaque_compiler_type_t type, 182 bool &is_signed) { 183 is_signed = false; 184 return false; 185 } 186 187 virtual bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type, 188 CompilerType *target_type, // Can pass NULL 189 bool check_cplusplus, bool check_objc) = 0; 190 191 virtual bool IsPointerType(lldb::opaque_compiler_type_t type, 192 CompilerType *pointee_type) = 0; 193 194 virtual bool IsScalarType(lldb::opaque_compiler_type_t type) = 0; 195 196 virtual bool IsVoidType(lldb::opaque_compiler_type_t type) = 0; 197 198 // TypeSystems can support more than one language 199 virtual bool SupportsLanguage(lldb::LanguageType language) = 0; 200 201 //---------------------------------------------------------------------- 202 // Type Completion 203 //---------------------------------------------------------------------- 204 205 virtual bool GetCompleteType(lldb::opaque_compiler_type_t type) = 0; 206 207 //---------------------------------------------------------------------- 208 // AST related queries 209 //---------------------------------------------------------------------- 210 211 virtual uint32_t GetPointerByteSize() = 0; 212 213 //---------------------------------------------------------------------- 214 // Accessors 215 //---------------------------------------------------------------------- 216 217 virtual ConstString GetTypeName(lldb::opaque_compiler_type_t type) = 0; 218 219 virtual uint32_t 220 GetTypeInfo(lldb::opaque_compiler_type_t type, 221 CompilerType *pointee_or_element_compiler_type) = 0; 222 223 virtual lldb::LanguageType 224 GetMinimumLanguage(lldb::opaque_compiler_type_t type) = 0; 225 226 virtual lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) = 0; 227 228 //---------------------------------------------------------------------- 229 // Creating related types 230 //---------------------------------------------------------------------- 231 232 virtual CompilerType GetArrayElementType(lldb::opaque_compiler_type_t type, 233 uint64_t *stride) = 0; 234 235 virtual CompilerType GetArrayType(lldb::opaque_compiler_type_t type, 236 uint64_t size); 237 238 virtual CompilerType GetCanonicalType(lldb::opaque_compiler_type_t type) = 0; 239 240 // Returns -1 if this isn't a function of if the function doesn't have a 241 // prototype Returns a value >= 0 if there is a prototype. 242 virtual int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) = 0; 243 244 virtual CompilerType 245 GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type, 246 size_t idx) = 0; 247 248 virtual CompilerType 249 GetFunctionReturnType(lldb::opaque_compiler_type_t type) = 0; 250 251 virtual size_t GetNumMemberFunctions(lldb::opaque_compiler_type_t type) = 0; 252 253 virtual TypeMemberFunctionImpl 254 GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, size_t idx) = 0; 255 256 virtual CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) = 0; 257 258 virtual CompilerType GetPointerType(lldb::opaque_compiler_type_t type) = 0; 259 260 virtual CompilerType 261 GetLValueReferenceType(lldb::opaque_compiler_type_t type); 262 263 virtual CompilerType 264 GetRValueReferenceType(lldb::opaque_compiler_type_t type); 265 266 virtual CompilerType AddConstModifier(lldb::opaque_compiler_type_t type); 267 268 virtual CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type); 269 270 virtual CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type); 271 272 virtual CompilerType CreateTypedef(lldb::opaque_compiler_type_t type, 273 const char *name, 274 const CompilerDeclContext &decl_ctx); 275 276 //---------------------------------------------------------------------- 277 // Exploring the type 278 //---------------------------------------------------------------------- 279 280 virtual uint64_t GetBitSize(lldb::opaque_compiler_type_t type, 281 ExecutionContextScope *exe_scope) = 0; 282 283 virtual lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type, 284 uint64_t &count) = 0; 285 286 virtual lldb::Format GetFormat(lldb::opaque_compiler_type_t type) = 0; 287 288 virtual uint32_t GetNumChildren(lldb::opaque_compiler_type_t type, 289 bool omit_empty_base_classes) = 0; 290 291 virtual CompilerType GetBuiltinTypeByName(const ConstString &name); 292 293 virtual lldb::BasicType 294 GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) = 0; 295 296 virtual void ForEachEnumerator( 297 lldb::opaque_compiler_type_t type, 298 std::function<bool(const CompilerType &integer_type, 299 const ConstString &name, 300 const llvm::APSInt &value)> const &callback) {} 301 302 virtual uint32_t GetNumFields(lldb::opaque_compiler_type_t type) = 0; 303 304 virtual CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type, 305 size_t idx, std::string &name, 306 uint64_t *bit_offset_ptr, 307 uint32_t *bitfield_bit_size_ptr, 308 bool *is_bitfield_ptr) = 0; 309 310 virtual uint32_t 311 GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) = 0; 312 313 virtual uint32_t 314 GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) = 0; 315 316 virtual CompilerType 317 GetDirectBaseClassAtIndex(lldb::opaque_compiler_type_t type, size_t idx, 318 uint32_t *bit_offset_ptr) = 0; 319 320 virtual CompilerType 321 GetVirtualBaseClassAtIndex(lldb::opaque_compiler_type_t type, size_t idx, 322 uint32_t *bit_offset_ptr) = 0; 323 324 virtual CompilerType GetChildCompilerTypeAtIndex( 325 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx, 326 bool transparent_pointers, bool omit_empty_base_classes, 327 bool ignore_array_bounds, std::string &child_name, 328 uint32_t &child_byte_size, int32_t &child_byte_offset, 329 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset, 330 bool &child_is_base_class, bool &child_is_deref_of_parent, 331 ValueObject *valobj, uint64_t &language_flags) = 0; 332 333 // Lookup a child given a name. This function will match base class names and 334 // member member names in "clang_type" only, not descendants. 335 virtual uint32_t GetIndexOfChildWithName(lldb::opaque_compiler_type_t type, 336 const char *name, 337 bool omit_empty_base_classes) = 0; 338 339 // Lookup a child member given a name. This function will match member names 340 // only and will descend into "clang_type" children in search for the first 341 // member in this class, or any base class that matches "name". 342 // TODO: Return all matches for a given name by returning a 343 // vector<vector<uint32_t>> 344 // so we catch all names that match a given child name, not just the first. 345 virtual size_t 346 GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type, 347 const char *name, bool omit_empty_base_classes, 348 std::vector<uint32_t> &child_indexes) = 0; 349 350 virtual size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type) = 0; 351 352 virtual lldb::TemplateArgumentKind 353 GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, size_t idx); 354 virtual CompilerType GetTypeTemplateArgument(lldb::opaque_compiler_type_t type, 355 size_t idx); 356 virtual llvm::Optional<CompilerType::IntegralTemplateArgument> 357 GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx); 358 359 //---------------------------------------------------------------------- 360 // Dumping types 361 //---------------------------------------------------------------------- 362 363 virtual void DumpValue(lldb::opaque_compiler_type_t type, 364 ExecutionContext *exe_ctx, Stream *s, 365 lldb::Format format, const DataExtractor &data, 366 lldb::offset_t data_offset, size_t data_byte_size, 367 uint32_t bitfield_bit_size, 368 uint32_t bitfield_bit_offset, bool show_types, 369 bool show_summary, bool verbose, uint32_t depth) = 0; 370 371 virtual bool DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s, 372 lldb::Format format, const DataExtractor &data, 373 lldb::offset_t data_offset, size_t data_byte_size, 374 uint32_t bitfield_bit_size, 375 uint32_t bitfield_bit_offset, 376 ExecutionContextScope *exe_scope) = 0; 377 378 virtual void 379 DumpTypeDescription(lldb::opaque_compiler_type_t type) = 0; // Dump to stdout 380 381 virtual void DumpTypeDescription(lldb::opaque_compiler_type_t type, 382 Stream *s) = 0; 383 384 //---------------------------------------------------------------------- 385 // TODO: These methods appear unused. Should they be removed? 386 //---------------------------------------------------------------------- 387 388 virtual bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) = 0; 389 390 virtual void DumpSummary(lldb::opaque_compiler_type_t type, 391 ExecutionContext *exe_ctx, Stream *s, 392 const DataExtractor &data, 393 lldb::offset_t data_offset, 394 size_t data_byte_size) = 0; 395 396 // Converts "s" to a floating point value and place resulting floating point 397 // bytes in the "dst" buffer. 398 virtual size_t ConvertStringToFloatValue(lldb::opaque_compiler_type_t type, 399 const char *s, uint8_t *dst, 400 size_t dst_size) = 0; 401 402 //---------------------------------------------------------------------- 403 // TODO: Determine if these methods should move to ClangASTContext. 404 //---------------------------------------------------------------------- 405 406 virtual bool IsPointerOrReferenceType(lldb::opaque_compiler_type_t type, 407 CompilerType *pointee_type) = 0; 408 409 virtual unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) = 0; 410 411 virtual bool IsCStringType(lldb::opaque_compiler_type_t type, 412 uint32_t &length) = 0; 413 414 virtual size_t GetTypeBitAlign(lldb::opaque_compiler_type_t type) = 0; 415 416 virtual CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) = 0; 417 418 virtual CompilerType 419 GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding, 420 size_t bit_size) = 0; 421 422 virtual bool IsBeingDefined(lldb::opaque_compiler_type_t type) = 0; 423 424 virtual bool IsConst(lldb::opaque_compiler_type_t type) = 0; 425 426 virtual uint32_t IsHomogeneousAggregate(lldb::opaque_compiler_type_t type, 427 CompilerType *base_type_ptr) = 0; 428 429 virtual bool IsPolymorphicClass(lldb::opaque_compiler_type_t type) = 0; 430 431 virtual bool IsTypedefType(lldb::opaque_compiler_type_t type) = 0; 432 433 // If the current object represents a typedef type, get the underlying type 434 virtual CompilerType GetTypedefedType(lldb::opaque_compiler_type_t type) = 0; 435 436 virtual bool IsVectorType(lldb::opaque_compiler_type_t type, 437 CompilerType *element_type, uint64_t *size) = 0; 438 439 virtual CompilerType 440 GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) = 0; 441 442 virtual CompilerType 443 GetNonReferenceType(lldb::opaque_compiler_type_t type) = 0; 444 445 virtual bool IsReferenceType(lldb::opaque_compiler_type_t type, 446 CompilerType *pointee_type, bool *is_rvalue) = 0; 447 448 virtual bool 449 ShouldTreatScalarValueAsAddress(lldb::opaque_compiler_type_t type) { 450 return IsPointerOrReferenceType(type, nullptr); 451 } 452 453 virtual UserExpression * 454 GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix, 455 lldb::LanguageType language, 456 Expression::ResultType desired_type, 457 const EvaluateExpressionOptions &options) { 458 return nullptr; 459 } 460 461 virtual FunctionCaller *GetFunctionCaller(const CompilerType &return_type, 462 const Address &function_address, 463 const ValueList &arg_value_list, 464 const char *name) { 465 return nullptr; 466 } 467 468 virtual UtilityFunction *GetUtilityFunction(const char *text, 469 const char *name) { 470 return nullptr; 471 } 472 473 virtual PersistentExpressionState *GetPersistentExpressionState() { 474 return nullptr; 475 } 476 477 virtual CompilerType GetTypeForFormatters(void *type); 478 479 virtual LazyBool ShouldPrintAsOneLiner(void *type, ValueObject *valobj); 480 481 // Type systems can have types that are placeholder types, which are meant to 482 // indicate the presence of a type, but offer no actual information about 483 // said types, and leave the burden of actually figuring type information out 484 // to dynamic type resolution. For instance a language with a generics 485 // system, can use placeholder types to indicate "type argument goes here", 486 // without promising uniqueness of the placeholder, nor attaching any 487 // actually idenfiable information to said placeholder. This API allows type 488 // systems to tell LLDB when such a type has been encountered In response, 489 // the debugger can react by not using this type as a cache entry in any 490 // type-specific way For instance, LLDB will currently not cache any 491 // formatters that are discovered on such a type as attributable to the 492 // meaningless type itself, instead preferring to use the dynamic type 493 virtual bool IsMeaninglessWithoutDynamicResolution(void *type); 494 495protected: 496 const LLVMCastKind m_kind; // Support for llvm casting 497 SymbolFile *m_sym_file; 498}; 499 500class TypeSystemMap { 501public: 502 TypeSystemMap(); 503 ~TypeSystemMap(); 504 505 // Clear calls Finalize on all the TypeSystems managed by this map, and then 506 // empties the map. 507 void Clear(); 508 509 // Iterate through all of the type systems that are created. Return true from 510 // callback to keep iterating, false to stop iterating. 511 void ForEach(std::function<bool(TypeSystem *)> const &callback); 512 513 TypeSystem *GetTypeSystemForLanguage(lldb::LanguageType language, 514 Module *module, bool can_create); 515 516 TypeSystem *GetTypeSystemForLanguage(lldb::LanguageType language, 517 Target *target, bool can_create); 518 519protected: 520 // This function does not take the map mutex, and should only be called from 521 // functions that do take the mutex. 522 void AddToMap(lldb::LanguageType language, 523 lldb::TypeSystemSP const &type_system_sp); 524 525 typedef std::map<lldb::LanguageType, lldb::TypeSystemSP> collection; 526 mutable std::mutex m_mutex; ///< A mutex to keep this object happy in 527 ///multi-threaded environments. 528 collection m_map; 529 bool m_clear_in_progress; 530}; 531 532} // namespace lldb_private 533 534#endif // liblldb_TypeSystem_h_ 535