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