1 //===-- CompilerType.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_COMPILERTYPE_H 10 #define LLDB_SYMBOL_COMPILERTYPE_H 11 12 #include <functional> 13 #include <optional> 14 #include <string> 15 #include <vector> 16 17 #include "lldb/lldb-private.h" 18 #include "llvm/ADT/APSInt.h" 19 #include "llvm/Support/Casting.h" 20 21 namespace lldb_private { 22 23 class DataExtractor; 24 class TypeSystem; 25 26 /// Generic representation of a type in a programming language. 27 /// 28 /// This class serves as an abstraction for a type inside one of the TypeSystems 29 /// implemented by the language plugins. It does not have any actual logic in it 30 /// but only stores an opaque pointer and a pointer to the TypeSystem that 31 /// gives meaning to this opaque pointer. All methods of this class should call 32 /// their respective method in the TypeSystem interface and pass the opaque 33 /// pointer along. 34 /// 35 /// \see lldb_private::TypeSystem 36 class CompilerType { 37 public: 38 /// Creates a CompilerType with the given TypeSystem and opaque compiler type. 39 /// 40 /// This constructor should only be called from the respective TypeSystem 41 /// implementation. 42 /// 43 /// \see lldb_private::TypeSystemClang::GetType(clang::QualType) CompilerType(lldb::TypeSystemWP type_system,lldb::opaque_compiler_type_t type)44 CompilerType(lldb::TypeSystemWP type_system, 45 lldb::opaque_compiler_type_t type) 46 : m_type_system(type_system), m_type(type) { 47 assert(Verify() && "verification failed"); 48 } 49 50 /// This is a minimal wrapper of a TypeSystem shared pointer as 51 /// returned by CompilerType which conventien dyn_cast support. 52 class TypeSystemSPWrapper { 53 lldb::TypeSystemSP m_typesystem_sp; 54 55 public: 56 TypeSystemSPWrapper() = default; TypeSystemSPWrapper(lldb::TypeSystemSP typesystem_sp)57 TypeSystemSPWrapper(lldb::TypeSystemSP typesystem_sp) 58 : m_typesystem_sp(typesystem_sp) {} 59 isa_and_nonnull()60 template <class TypeSystemType> bool isa_and_nonnull() { 61 if (auto *ts = m_typesystem_sp.get()) 62 return llvm::isa<TypeSystemType>(ts); 63 return false; 64 } 65 66 /// Return a shared_ptr<TypeSystemType> if dyn_cast succeeds. 67 template <class TypeSystemType> dyn_cast_or_null()68 std::shared_ptr<TypeSystemType> dyn_cast_or_null() { 69 if (isa_and_nonnull<TypeSystemType>()) 70 return std::shared_ptr<TypeSystemType>( 71 m_typesystem_sp, llvm::cast<TypeSystemType>(m_typesystem_sp.get())); 72 return nullptr; 73 } 74 75 explicit operator bool() const { 76 return static_cast<bool>(m_typesystem_sp); 77 } 78 bool operator==(const TypeSystemSPWrapper &other) const; 79 bool operator!=(const TypeSystemSPWrapper &other) const { 80 return !(*this == other); 81 } 82 83 /// Only to be used in a one-off situations like 84 /// if (typesystem && typesystem->method()) 85 /// Do not store this pointer! 86 TypeSystem *operator->() const; 87 GetSharedPointer()88 lldb::TypeSystemSP GetSharedPointer() const { return m_typesystem_sp; } 89 }; 90 CompilerType(TypeSystemSPWrapper type_system,lldb::opaque_compiler_type_t type)91 CompilerType(TypeSystemSPWrapper type_system, lldb::opaque_compiler_type_t type) 92 : m_type_system(type_system.GetSharedPointer()), m_type(type) { 93 assert(Verify() && "verification failed"); 94 } 95 CompilerType(const CompilerType & rhs)96 CompilerType(const CompilerType &rhs) 97 : m_type_system(rhs.m_type_system), m_type(rhs.m_type) {} 98 99 CompilerType() = default; 100 101 /// Operators. 102 /// \{ 103 const CompilerType &operator=(const CompilerType &rhs) { 104 m_type_system = rhs.m_type_system; 105 m_type = rhs.m_type; 106 return *this; 107 } 108 109 bool operator<(const CompilerType &rhs) const { 110 auto lts = m_type_system.lock(); 111 auto rts = rhs.m_type_system.lock(); 112 if (lts.get() == rts.get()) 113 return m_type < rhs.m_type; 114 return lts.get() < rts.get(); 115 } 116 /// \} 117 118 /// Tests. 119 /// \{ 120 explicit operator bool() const { 121 return m_type_system.lock() && m_type; 122 } 123 IsValid()124 bool IsValid() const { return (bool)*this; } 125 126 bool IsArrayType(CompilerType *element_type = nullptr, 127 uint64_t *size = nullptr, 128 bool *is_incomplete = nullptr) const; 129 130 bool IsVectorType(CompilerType *element_type = nullptr, 131 uint64_t *size = nullptr) const; 132 133 bool IsArrayOfScalarType() const; 134 135 bool IsAggregateType() const; 136 137 bool IsAnonymousType() const; 138 139 bool IsScopedEnumerationType() const; 140 141 bool IsBeingDefined() const; 142 143 bool IsCharType() const; 144 145 bool IsCompleteType() const; 146 147 bool IsConst() const; 148 149 bool IsCStringType(uint32_t &length) const; 150 151 bool IsDefined() const; 152 153 bool IsFloatingPointType(uint32_t &count, bool &is_complex) const; 154 155 bool IsFunctionType() const; 156 157 uint32_t IsHomogeneousAggregate(CompilerType *base_type_ptr) const; 158 159 size_t GetNumberOfFunctionArguments() const; 160 161 CompilerType GetFunctionArgumentAtIndex(const size_t index) const; 162 163 bool IsVariadicFunctionType() const; 164 165 bool IsFunctionPointerType() const; 166 167 bool 168 IsBlockPointerType(CompilerType *function_pointer_type_ptr = nullptr) const; 169 170 bool IsIntegerType(bool &is_signed) const; 171 172 bool IsEnumerationType(bool &is_signed) const; 173 174 bool IsIntegerOrEnumerationType(bool &is_signed) const; 175 176 bool IsPolymorphicClass() const; 177 178 /// \param target_type Can pass nullptr. 179 bool IsPossibleDynamicType(CompilerType *target_type, bool check_cplusplus, 180 bool check_objc) const; 181 182 bool IsPointerToScalarType() const; 183 184 bool IsRuntimeGeneratedType() const; 185 186 bool IsPointerType(CompilerType *pointee_type = nullptr) const; 187 188 bool IsPointerOrReferenceType(CompilerType *pointee_type = nullptr) const; 189 190 bool IsReferenceType(CompilerType *pointee_type = nullptr, 191 bool *is_rvalue = nullptr) const; 192 193 bool ShouldTreatScalarValueAsAddress() const; 194 195 bool IsScalarType() const; 196 197 bool IsTemplateType() const; 198 199 bool IsTypedefType() const; 200 201 bool IsVoidType() const; 202 /// \} 203 204 /// Type Completion. 205 /// \{ 206 bool GetCompleteType() const; 207 /// \} 208 209 bool IsForcefullyCompleted() const; 210 211 /// AST related queries. 212 /// \{ 213 size_t GetPointerByteSize() const; 214 /// \} 215 216 /// Accessors. 217 /// \{ 218 219 /// Returns a shared pointer to the type system. The 220 /// TypeSystem::TypeSystemSPWrapper can be compared for equality. 221 TypeSystemSPWrapper GetTypeSystem() const; 222 223 ConstString GetTypeName(bool BaseOnly = false) const; 224 225 ConstString GetDisplayTypeName() const; 226 227 uint32_t 228 GetTypeInfo(CompilerType *pointee_or_element_compiler_type = nullptr) const; 229 230 lldb::LanguageType GetMinimumLanguage(); 231 GetOpaqueQualType()232 lldb::opaque_compiler_type_t GetOpaqueQualType() const { return m_type; } 233 234 lldb::TypeClass GetTypeClass() const; 235 236 void SetCompilerType(lldb::TypeSystemWP type_system, 237 lldb::opaque_compiler_type_t type); 238 void SetCompilerType(TypeSystemSPWrapper type_system, 239 lldb::opaque_compiler_type_t type); 240 241 unsigned GetTypeQualifiers() const; 242 /// \} 243 244 /// Creating related types. 245 /// \{ 246 CompilerType GetArrayElementType(ExecutionContextScope *exe_scope) const; 247 248 CompilerType GetArrayType(uint64_t size) const; 249 250 CompilerType GetCanonicalType() const; 251 252 CompilerType GetFullyUnqualifiedType() const; 253 254 CompilerType GetEnumerationIntegerType() const; 255 256 /// Returns -1 if this isn't a function of if the function doesn't 257 /// have a prototype Returns a value >= 0 if there is a prototype. 258 int GetFunctionArgumentCount() const; 259 260 CompilerType GetFunctionArgumentTypeAtIndex(size_t idx) const; 261 262 CompilerType GetFunctionReturnType() const; 263 264 size_t GetNumMemberFunctions() const; 265 266 TypeMemberFunctionImpl GetMemberFunctionAtIndex(size_t idx); 267 268 /// If this type is a reference to a type (L value or R value reference), 269 /// return a new type with the reference removed, else return the current type 270 /// itself. 271 CompilerType GetNonReferenceType() const; 272 273 /// If this type is a pointer type, return the type that the pointer points 274 /// to, else return an invalid type. 275 CompilerType GetPointeeType() const; 276 277 /// Return a new CompilerType that is a pointer to this type 278 CompilerType GetPointerType() const; 279 280 /// Return a new CompilerType that is a L value reference to this type if this 281 /// type is valid and the type system supports L value references, else return 282 /// an invalid type. 283 CompilerType GetLValueReferenceType() const; 284 285 /// Return a new CompilerType that is a R value reference to this type if this 286 /// type is valid and the type system supports R value references, else return 287 /// an invalid type. 288 CompilerType GetRValueReferenceType() const; 289 290 /// Return a new CompilerType adds a const modifier to this type if this type 291 /// is valid and the type system supports const modifiers, else return an 292 /// invalid type. 293 CompilerType AddConstModifier() const; 294 295 /// Return a new CompilerType adds a volatile modifier to this type if this 296 /// type is valid and the type system supports volatile modifiers, else return 297 /// an invalid type. 298 CompilerType AddVolatileModifier() const; 299 300 /// Return a new CompilerType that is the atomic type of this type. If this 301 /// type is not valid or the type system doesn't support atomic types, this 302 /// returns an invalid type. 303 CompilerType GetAtomicType() const; 304 305 /// Return a new CompilerType adds a restrict modifier to this type if this 306 /// type is valid and the type system supports restrict modifiers, else return 307 /// an invalid type. 308 CompilerType AddRestrictModifier() const; 309 310 /// Create a typedef to this type using "name" as the name of the typedef this 311 /// type is valid and the type system supports typedefs, else return an 312 /// invalid type. 313 /// \param payload The typesystem-specific \p lldb::Type payload. 314 CompilerType CreateTypedef(const char *name, 315 const CompilerDeclContext &decl_ctx, 316 uint32_t payload) const; 317 318 /// If the current object represents a typedef type, get the underlying type 319 CompilerType GetTypedefedType() const; 320 321 /// Create related types using the current type's AST 322 CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const; 323 /// \} 324 325 /// Exploring the type. 326 /// \{ 327 struct IntegralTemplateArgument; 328 329 /// Return the size of the type in bytes. 330 std::optional<uint64_t> GetByteSize(ExecutionContextScope *exe_scope) const; 331 /// Return the size of the type in bits. 332 std::optional<uint64_t> GetBitSize(ExecutionContextScope *exe_scope) const; 333 334 lldb::Encoding GetEncoding(uint64_t &count) const; 335 336 lldb::Format GetFormat() const; 337 338 std::optional<size_t> GetTypeBitAlign(ExecutionContextScope *exe_scope) const; 339 340 uint32_t GetNumChildren(bool omit_empty_base_classes, 341 const ExecutionContext *exe_ctx) const; 342 343 lldb::BasicType GetBasicTypeEnumeration() const; 344 345 static lldb::BasicType GetBasicTypeEnumeration(ConstString name); 346 347 /// If this type is an enumeration, iterate through all of its enumerators 348 /// using a callback. If the callback returns true, keep iterating, else abort 349 /// the iteration. 350 void ForEachEnumerator( 351 std::function<bool(const CompilerType &integer_type, ConstString name, 352 const llvm::APSInt &value)> const &callback) const; 353 354 uint32_t GetNumFields() const; 355 356 CompilerType GetFieldAtIndex(size_t idx, std::string &name, 357 uint64_t *bit_offset_ptr, 358 uint32_t *bitfield_bit_size_ptr, 359 bool *is_bitfield_ptr) const; 360 361 uint32_t GetNumDirectBaseClasses() const; 362 363 uint32_t GetNumVirtualBaseClasses() const; 364 365 CompilerType GetDirectBaseClassAtIndex(size_t idx, 366 uint32_t *bit_offset_ptr) const; 367 368 CompilerType GetVirtualBaseClassAtIndex(size_t idx, 369 uint32_t *bit_offset_ptr) const; 370 371 uint32_t GetIndexOfFieldWithName(const char *name, 372 CompilerType *field_compiler_type = nullptr, 373 uint64_t *bit_offset_ptr = nullptr, 374 uint32_t *bitfield_bit_size_ptr = nullptr, 375 bool *is_bitfield_ptr = nullptr) const; 376 377 CompilerType GetChildCompilerTypeAtIndex( 378 ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers, 379 bool omit_empty_base_classes, bool ignore_array_bounds, 380 std::string &child_name, uint32_t &child_byte_size, 381 int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size, 382 uint32_t &child_bitfield_bit_offset, bool &child_is_base_class, 383 bool &child_is_deref_of_parent, ValueObject *valobj, 384 uint64_t &language_flags) const; 385 386 /// Lookup a child given a name. This function will match base class names and 387 /// member member names in "clang_type" only, not descendants. 388 uint32_t GetIndexOfChildWithName(const char *name, 389 bool omit_empty_base_classes) const; 390 391 /// Lookup a child member given a name. This function will match member names 392 /// only and will descend into "clang_type" children in search for the first 393 /// member in this class, or any base class that matches "name". 394 /// TODO: Return all matches for a given name by returning a 395 /// vector<vector<uint32_t>> 396 /// so we catch all names that match a given child name, not just the first. 397 size_t 398 GetIndexOfChildMemberWithName(const char *name, bool omit_empty_base_classes, 399 std::vector<uint32_t> &child_indexes) const; 400 401 /// Return the number of template arguments the type has. 402 /// If expand_pack is true, then variadic argument packs are automatically 403 /// expanded to their supplied arguments. If it is false an argument pack 404 /// will only count as 1 argument. 405 size_t GetNumTemplateArguments(bool expand_pack = false) const; 406 407 // Return the TemplateArgumentKind of the template argument at index idx. 408 // If expand_pack is true, then variadic argument packs are automatically 409 // expanded to their supplied arguments. With expand_pack set to false, an 410 // arguement pack will count as 1 argument and return a type of Pack. 411 lldb::TemplateArgumentKind 412 GetTemplateArgumentKind(size_t idx, bool expand_pack = false) const; 413 CompilerType GetTypeTemplateArgument(size_t idx, 414 bool expand_pack = false) const; 415 416 /// Returns the value of the template argument and its type. 417 /// If expand_pack is true, then variadic argument packs are automatically 418 /// expanded to their supplied arguments. With expand_pack set to false, an 419 /// arguement pack will count as 1 argument and it is invalid to call this 420 /// method on the pack argument. 421 std::optional<IntegralTemplateArgument> 422 GetIntegralTemplateArgument(size_t idx, bool expand_pack = false) const; 423 424 CompilerType GetTypeForFormatters() const; 425 426 LazyBool ShouldPrintAsOneLiner(ValueObject *valobj) const; 427 428 bool IsMeaninglessWithoutDynamicResolution() const; 429 /// \} 430 431 /// Dumping types. 432 /// \{ 433 #ifndef NDEBUG 434 /// Convenience LLVM-style dump method for use in the debugger only. 435 /// Don't call this function from actual code. 436 LLVM_DUMP_METHOD void dump() const; 437 #endif 438 439 void DumpValue(ExecutionContext *exe_ctx, Stream *s, lldb::Format format, 440 const DataExtractor &data, lldb::offset_t data_offset, 441 size_t data_byte_size, uint32_t bitfield_bit_size, 442 uint32_t bitfield_bit_offset, bool show_types, 443 bool show_summary, bool verbose, uint32_t depth); 444 445 bool DumpTypeValue(Stream *s, lldb::Format format, const DataExtractor &data, 446 lldb::offset_t data_offset, size_t data_byte_size, 447 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, 448 ExecutionContextScope *exe_scope); 449 450 void DumpSummary(ExecutionContext *exe_ctx, Stream *s, 451 const DataExtractor &data, lldb::offset_t data_offset, 452 size_t data_byte_size); 453 454 /// Dump to stdout. 455 void DumpTypeDescription(lldb::DescriptionLevel level = 456 lldb::eDescriptionLevelFull) const; 457 458 /// Print a description of the type to a stream. The exact implementation 459 /// varies, but the expectation is that eDescriptionLevelFull returns a 460 /// source-like representation of the type, whereas eDescriptionLevelVerbose 461 /// does a dump of the underlying AST if applicable. 462 void DumpTypeDescription(Stream *s, lldb::DescriptionLevel level = 463 lldb::eDescriptionLevelFull) const; 464 /// \} 465 466 bool GetValueAsScalar(const DataExtractor &data, lldb::offset_t data_offset, 467 size_t data_byte_size, Scalar &value, 468 ExecutionContextScope *exe_scope) const; Clear()469 void Clear() { 470 m_type_system = {}; 471 m_type = nullptr; 472 } 473 474 private: 475 #ifndef NDEBUG 476 /// If the type is valid, ask the TypeSystem to verify the integrity 477 /// of the type to catch CompilerTypes that mix and match invalid 478 /// TypeSystem/Opaque type pairs. 479 bool Verify() const; 480 #endif 481 482 lldb::TypeSystemWP m_type_system; 483 lldb::opaque_compiler_type_t m_type = nullptr; 484 }; 485 486 bool operator==(const CompilerType &lhs, const CompilerType &rhs); 487 bool operator!=(const CompilerType &lhs, const CompilerType &rhs); 488 489 struct CompilerType::IntegralTemplateArgument { 490 llvm::APSInt value; 491 CompilerType type; 492 }; 493 494 } // namespace lldb_private 495 496 #endif // LLDB_SYMBOL_COMPILERTYPE_H 497