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 liblldb_CompilerType_h_ 10 #define liblldb_CompilerType_h_ 11 12 #include <functional> 13 #include <string> 14 #include <vector> 15 16 #include "lldb/lldb-private.h" 17 #include "llvm/ADT/APSInt.h" 18 19 namespace lldb_private { 20 21 class DataExtractor; 22 23 /// Represents a generic type in a programming language. 24 /// 25 /// This class serves as an abstraction for a type inside one of the TypeSystems 26 /// implemented by the language plugins. It does not have any actual logic in it 27 /// but only stores an opaque pointer and a pointer to the TypeSystem that 28 /// gives meaning to this opaque pointer. All methods of this class should call 29 /// their respective method in the TypeSystem interface and pass the opaque 30 /// pointer along. 31 /// 32 /// \see lldb_private::TypeSystem 33 class CompilerType { 34 public: 35 /// Creates a CompilerType with the given TypeSystem and opaque compiler type. 36 /// 37 /// This constructor should only be called from the respective TypeSystem 38 /// implementation. 39 /// 40 /// \see lldb_private::ClangASTContext::GetType(clang::QualType) 41 CompilerType(TypeSystem *type_system, lldb::opaque_compiler_type_t type) 42 : m_type(type), m_type_system(type_system) {} 43 44 CompilerType(const CompilerType &rhs) 45 : m_type(rhs.m_type), m_type_system(rhs.m_type_system) {} 46 47 CompilerType() = default; 48 49 // Operators 50 51 const CompilerType &operator=(const CompilerType &rhs) { 52 m_type = rhs.m_type; 53 m_type_system = rhs.m_type_system; 54 return *this; 55 } 56 57 // Tests 58 59 explicit operator bool() const { 60 return m_type != nullptr && m_type_system != nullptr; 61 } 62 63 bool operator<(const CompilerType &rhs) const { 64 if (m_type_system == rhs.m_type_system) 65 return m_type < rhs.m_type; 66 return m_type_system < rhs.m_type_system; 67 } 68 69 bool IsValid() const { return m_type != nullptr && m_type_system != nullptr; } 70 71 bool IsArrayType(CompilerType *element_type, uint64_t *size, 72 bool *is_incomplete) const; 73 74 bool IsVectorType(CompilerType *element_type, uint64_t *size) const; 75 76 bool IsArrayOfScalarType() const; 77 78 bool IsAggregateType() const; 79 80 bool IsAnonymousType() const; 81 82 bool IsBeingDefined() const; 83 84 bool IsCharType() const; 85 86 bool IsCompleteType() const; 87 88 bool IsConst() const; 89 90 bool IsCStringType(uint32_t &length) const; 91 92 bool IsDefined() const; 93 94 bool IsFloatingPointType(uint32_t &count, bool &is_complex) const; 95 96 bool IsFunctionType(bool *is_variadic_ptr = nullptr) const; 97 98 uint32_t IsHomogeneousAggregate(CompilerType *base_type_ptr) const; 99 100 size_t GetNumberOfFunctionArguments() const; 101 102 CompilerType GetFunctionArgumentAtIndex(const size_t index) const; 103 104 bool IsVariadicFunctionType() const; 105 106 bool IsFunctionPointerType() const; 107 108 bool IsBlockPointerType(CompilerType *function_pointer_type_ptr) const; 109 110 bool IsIntegerType(bool &is_signed) const; 111 112 bool IsEnumerationType(bool &is_signed) const; 113 114 bool IsIntegerOrEnumerationType(bool &is_signed) const; 115 116 bool IsPolymorphicClass() const; 117 118 bool IsPossibleDynamicType(CompilerType *target_type, // Can pass nullptr 119 bool check_cplusplus, bool check_objc) const; 120 121 bool IsPointerToScalarType() const; 122 123 bool IsRuntimeGeneratedType() const; 124 125 bool IsPointerType(CompilerType *pointee_type = nullptr) const; 126 127 bool IsPointerOrReferenceType(CompilerType *pointee_type = nullptr) const; 128 129 bool IsReferenceType(CompilerType *pointee_type = nullptr, 130 bool *is_rvalue = nullptr) const; 131 132 bool ShouldTreatScalarValueAsAddress() const; 133 134 bool IsScalarType() const; 135 136 bool IsTypedefType() const; 137 138 bool IsVoidType() const; 139 140 // Type Completion 141 142 bool GetCompleteType() const; 143 144 // AST related queries 145 146 size_t GetPointerByteSize() const; 147 148 // Accessors 149 150 TypeSystem *GetTypeSystem() const { return m_type_system; } 151 152 ConstString GetConstQualifiedTypeName() const; 153 154 ConstString GetConstTypeName() const; 155 156 ConstString GetTypeName() const; 157 158 ConstString GetDisplayTypeName() const; 159 160 uint32_t 161 GetTypeInfo(CompilerType *pointee_or_element_compiler_type = nullptr) const; 162 163 lldb::LanguageType GetMinimumLanguage(); 164 165 lldb::opaque_compiler_type_t GetOpaqueQualType() const { return m_type; } 166 167 lldb::TypeClass GetTypeClass() const; 168 169 void SetCompilerType(TypeSystem *type_system, 170 lldb::opaque_compiler_type_t type); 171 172 unsigned GetTypeQualifiers() const; 173 174 // Creating related types 175 176 CompilerType GetArrayElementType(uint64_t *stride = nullptr) const; 177 178 CompilerType GetArrayType(uint64_t size) const; 179 180 CompilerType GetCanonicalType() const; 181 182 CompilerType GetFullyUnqualifiedType() const; 183 184 // Returns -1 if this isn't a function of if the function doesn't have a 185 // prototype Returns a value >= 0 if there is a prototype. 186 int GetFunctionArgumentCount() const; 187 188 CompilerType GetFunctionArgumentTypeAtIndex(size_t idx) const; 189 190 CompilerType GetFunctionReturnType() const; 191 192 size_t GetNumMemberFunctions() const; 193 194 TypeMemberFunctionImpl GetMemberFunctionAtIndex(size_t idx); 195 196 // If this type is a reference to a type (L value or R value reference), 197 // return a new type with the reference removed, else return the current type 198 // itself. 199 CompilerType GetNonReferenceType() const; 200 201 // If this type is a pointer type, return the type that the pointer points 202 // to, else return an invalid type. 203 CompilerType GetPointeeType() const; 204 205 // Return a new CompilerType that is a pointer to this type 206 CompilerType GetPointerType() const; 207 208 // Return a new CompilerType that is a L value reference to this type if this 209 // type is valid and the type system supports L value references, else return 210 // an invalid type. 211 CompilerType GetLValueReferenceType() const; 212 213 // Return a new CompilerType that is a R value reference to this type if this 214 // type is valid and the type system supports R value references, else return 215 // an invalid type. 216 CompilerType GetRValueReferenceType() const; 217 218 // Return a new CompilerType adds a const modifier to this type if this type 219 // is valid and the type system supports const modifiers, else return an 220 // invalid type. 221 CompilerType AddConstModifier() const; 222 223 // Return a new CompilerType adds a volatile modifier to this type if this 224 // type is valid and the type system supports volatile modifiers, else return 225 // an invalid type. 226 CompilerType AddVolatileModifier() const; 227 228 // Return a new CompilerType that is the atomic type of this type. If this 229 // type is not valid or the type system doesn't support atomic types, this 230 // returns an invalid type. 231 CompilerType GetAtomicType() const; 232 233 // Return a new CompilerType adds a restrict modifier to this type if this 234 // type is valid and the type system supports restrict modifiers, else return 235 // an invalid type. 236 CompilerType AddRestrictModifier() const; 237 238 // Create a typedef to this type using "name" as the name of the typedef this 239 // type is valid and the type system supports typedefs, else return an 240 // invalid type. 241 CompilerType CreateTypedef(const char *name, 242 const CompilerDeclContext &decl_ctx) const; 243 244 // If the current object represents a typedef type, get the underlying type 245 CompilerType GetTypedefedType() const; 246 247 // Create related types using the current type's AST 248 CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const; 249 250 // Exploring the type 251 252 struct IntegralTemplateArgument; 253 254 /// Return the size of the type in bytes. 255 llvm::Optional<uint64_t> GetByteSize(ExecutionContextScope *exe_scope) const; 256 /// Return the size of the type in bits. 257 llvm::Optional<uint64_t> GetBitSize(ExecutionContextScope *exe_scope) const; 258 259 lldb::Encoding GetEncoding(uint64_t &count) const; 260 261 lldb::Format GetFormat() const; 262 263 llvm::Optional<size_t> GetTypeBitAlign(ExecutionContextScope *exe_scope) const; 264 265 uint32_t GetNumChildren(bool omit_empty_base_classes, 266 const ExecutionContext *exe_ctx) const; 267 268 lldb::BasicType GetBasicTypeEnumeration() const; 269 270 static lldb::BasicType GetBasicTypeEnumeration(ConstString name); 271 272 // If this type is an enumeration, iterate through all of its enumerators 273 // using a callback. If the callback returns true, keep iterating, else abort 274 // the iteration. 275 void ForEachEnumerator( 276 std::function<bool(const CompilerType &integer_type, 277 ConstString name, 278 const llvm::APSInt &value)> const &callback) const; 279 280 uint32_t GetNumFields() const; 281 282 CompilerType GetFieldAtIndex(size_t idx, std::string &name, 283 uint64_t *bit_offset_ptr, 284 uint32_t *bitfield_bit_size_ptr, 285 bool *is_bitfield_ptr) const; 286 287 uint32_t GetNumDirectBaseClasses() const; 288 289 uint32_t GetNumVirtualBaseClasses() const; 290 291 CompilerType GetDirectBaseClassAtIndex(size_t idx, 292 uint32_t *bit_offset_ptr) const; 293 294 CompilerType GetVirtualBaseClassAtIndex(size_t idx, 295 uint32_t *bit_offset_ptr) const; 296 297 uint32_t GetIndexOfFieldWithName(const char *name, 298 CompilerType *field_compiler_type = nullptr, 299 uint64_t *bit_offset_ptr = nullptr, 300 uint32_t *bitfield_bit_size_ptr = nullptr, 301 bool *is_bitfield_ptr = nullptr) const; 302 303 CompilerType GetChildCompilerTypeAtIndex( 304 ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers, 305 bool omit_empty_base_classes, bool ignore_array_bounds, 306 std::string &child_name, uint32_t &child_byte_size, 307 int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size, 308 uint32_t &child_bitfield_bit_offset, bool &child_is_base_class, 309 bool &child_is_deref_of_parent, ValueObject *valobj, 310 uint64_t &language_flags) const; 311 312 // Lookup a child given a name. This function will match base class names and 313 // member member names in "clang_type" only, not descendants. 314 uint32_t GetIndexOfChildWithName(const char *name, 315 bool omit_empty_base_classes) const; 316 317 // Lookup a child member given a name. This function will match member names 318 // only and will descend into "clang_type" children in search for the first 319 // member in this class, or any base class that matches "name". 320 // TODO: Return all matches for a given name by returning a 321 // vector<vector<uint32_t>> 322 // so we catch all names that match a given child name, not just the first. 323 size_t 324 GetIndexOfChildMemberWithName(const char *name, bool omit_empty_base_classes, 325 std::vector<uint32_t> &child_indexes) const; 326 327 size_t GetNumTemplateArguments() const; 328 329 lldb::TemplateArgumentKind GetTemplateArgumentKind(size_t idx) const; 330 CompilerType GetTypeTemplateArgument(size_t idx) const; 331 332 // Returns the value of the template argument and its type. 333 llvm::Optional<IntegralTemplateArgument> 334 GetIntegralTemplateArgument(size_t idx) const; 335 336 CompilerType GetTypeForFormatters() const; 337 338 LazyBool ShouldPrintAsOneLiner(ValueObject *valobj) const; 339 340 bool IsMeaninglessWithoutDynamicResolution() const; 341 342 // Dumping types 343 344 #ifndef NDEBUG 345 /// Convenience LLVM-style dump method for use in the debugger only. 346 /// Don't call this function from actual code. 347 LLVM_DUMP_METHOD void dump() const; 348 #endif 349 350 void DumpValue(ExecutionContext *exe_ctx, Stream *s, lldb::Format format, 351 const DataExtractor &data, lldb::offset_t data_offset, 352 size_t data_byte_size, uint32_t bitfield_bit_size, 353 uint32_t bitfield_bit_offset, bool show_types, 354 bool show_summary, bool verbose, uint32_t depth); 355 356 bool DumpTypeValue(Stream *s, lldb::Format format, const DataExtractor &data, 357 lldb::offset_t data_offset, size_t data_byte_size, 358 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, 359 ExecutionContextScope *exe_scope); 360 361 void DumpSummary(ExecutionContext *exe_ctx, Stream *s, 362 const DataExtractor &data, lldb::offset_t data_offset, 363 size_t data_byte_size); 364 365 void DumpTypeDescription() const; // Dump to stdout 366 367 void DumpTypeDescription(Stream *s) const; 368 369 bool GetValueAsScalar(const DataExtractor &data, lldb::offset_t data_offset, 370 size_t data_byte_size, Scalar &value) const; 371 372 void Clear() { 373 m_type = nullptr; 374 m_type_system = nullptr; 375 } 376 377 private: 378 lldb::opaque_compiler_type_t m_type = nullptr; 379 TypeSystem *m_type_system = nullptr; 380 }; 381 382 bool operator==(const CompilerType &lhs, const CompilerType &rhs); 383 bool operator!=(const CompilerType &lhs, const CompilerType &rhs); 384 385 struct CompilerType::IntegralTemplateArgument { 386 llvm::APSInt value; 387 CompilerType type; 388 }; 389 390 } // namespace lldb_private 391 392 #endif // liblldb_CompilerType_h_ 393