CompilerType.h revision 355940
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/Core/ClangForward.h" 17#include "lldb/lldb-private.h" 18#include "llvm/ADT/APSInt.h" 19 20namespace lldb_private { 21 22class DataExtractor; 23 24// A class that can carry around a clang ASTContext and a opaque clang 25// QualType. A clang::QualType can be easily reconstructed from an opaque clang 26// type and often the ASTContext is needed when doing various type related 27// tasks, so this class allows both items to travel in a single very 28// lightweight class that can be used. There are many static equivalents of the 29// member functions that allow the ASTContext and the opaque clang QualType to 30// be specified for ease of use and to avoid code duplication. 31class CompilerType { 32public: 33 // Constructors and Destructors 34 CompilerType(TypeSystem *type_system, lldb::opaque_compiler_type_t type); 35 CompilerType(clang::ASTContext *ast_context, clang::QualType qual_type); 36 37 CompilerType(const CompilerType &rhs) 38 : m_type(rhs.m_type), m_type_system(rhs.m_type_system) {} 39 40 CompilerType() : m_type(nullptr), m_type_system(nullptr) {} 41 42 ~CompilerType(); 43 44 // Operators 45 46 const CompilerType &operator=(const CompilerType &rhs) { 47 m_type = rhs.m_type; 48 m_type_system = rhs.m_type_system; 49 return *this; 50 } 51 52 // Tests 53 54 explicit operator bool() const { 55 return m_type != nullptr && m_type_system != nullptr; 56 } 57 58 bool operator<(const CompilerType &rhs) const { 59 if (m_type_system == rhs.m_type_system) 60 return m_type < rhs.m_type; 61 return m_type_system < rhs.m_type_system; 62 } 63 64 bool IsValid() const { return m_type != nullptr && m_type_system != nullptr; } 65 66 bool IsArrayType(CompilerType *element_type, uint64_t *size, 67 bool *is_incomplete) const; 68 69 bool IsVectorType(CompilerType *element_type, uint64_t *size) const; 70 71 bool IsArrayOfScalarType() const; 72 73 bool IsAggregateType() const; 74 75 bool IsAnonymousType() const; 76 77 bool IsBeingDefined() const; 78 79 bool IsCharType() const; 80 81 bool IsCompleteType() const; 82 83 bool IsConst() const; 84 85 bool IsCStringType(uint32_t &length) const; 86 87 bool IsDefined() const; 88 89 bool IsFloatingPointType(uint32_t &count, bool &is_complex) const; 90 91 bool IsFunctionType(bool *is_variadic_ptr = nullptr) const; 92 93 uint32_t IsHomogeneousAggregate(CompilerType *base_type_ptr) const; 94 95 size_t GetNumberOfFunctionArguments() const; 96 97 CompilerType GetFunctionArgumentAtIndex(const size_t index) const; 98 99 bool IsVariadicFunctionType() const; 100 101 bool IsFunctionPointerType() const; 102 103 bool IsBlockPointerType(CompilerType *function_pointer_type_ptr) const; 104 105 bool IsIntegerType(bool &is_signed) const; 106 107 bool IsEnumerationType(bool &is_signed) const; 108 109 bool IsIntegerOrEnumerationType(bool &is_signed) const; 110 111 bool IsPolymorphicClass() const; 112 113 bool 114 IsPossibleCPlusPlusDynamicType(CompilerType *target_type = nullptr) const { 115 return IsPossibleDynamicType(target_type, true, false); 116 } 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 void SetCompilerType(clang::ASTContext *ast, clang::QualType qual_type); 173 174 unsigned GetTypeQualifiers() const; 175 176 // Creating related types 177 178 CompilerType GetArrayElementType(uint64_t *stride = nullptr) const; 179 180 CompilerType GetArrayType(uint64_t size) const; 181 182 CompilerType GetCanonicalType() const; 183 184 CompilerType GetFullyUnqualifiedType() const; 185 186 // Returns -1 if this isn't a function of if the function doesn't have a 187 // prototype Returns a value >= 0 if there is a prototype. 188 int GetFunctionArgumentCount() const; 189 190 CompilerType GetFunctionArgumentTypeAtIndex(size_t idx) const; 191 192 CompilerType GetFunctionReturnType() const; 193 194 size_t GetNumMemberFunctions() const; 195 196 TypeMemberFunctionImpl GetMemberFunctionAtIndex(size_t idx); 197 198 // If this type is a reference to a type (L value or R value reference), 199 // return a new type with the reference removed, else return the current type 200 // itself. 201 CompilerType GetNonReferenceType() const; 202 203 // If this type is a pointer type, return the type that the pointer points 204 // to, else return an invalid type. 205 CompilerType GetPointeeType() const; 206 207 // Return a new CompilerType that is a pointer to this type 208 CompilerType GetPointerType() const; 209 210 // Return a new CompilerType that is a L value reference to this type if this 211 // type is valid and the type system supports L value references, else return 212 // an invalid type. 213 CompilerType GetLValueReferenceType() const; 214 215 // Return a new CompilerType that is a R value reference to this type if this 216 // type is valid and the type system supports R value references, else return 217 // an invalid type. 218 CompilerType GetRValueReferenceType() const; 219 220 // Return a new CompilerType adds a const modifier to this type if this type 221 // is valid and the type system supports const modifiers, else return an 222 // invalid type. 223 CompilerType AddConstModifier() const; 224 225 // Return a new CompilerType adds a volatile modifier to this type if this 226 // type is valid and the type system supports volatile modifiers, else return 227 // an invalid type. 228 CompilerType AddVolatileModifier() const; 229 230 // Return a new CompilerType adds a restrict modifier to this type if this 231 // type is valid and the type system supports restrict modifiers, else return 232 // an invalid type. 233 CompilerType AddRestrictModifier() const; 234 235 // Create a typedef to this type using "name" as the name of the typedef this 236 // type is valid and the type system supports typedefs, else return an 237 // invalid type. 238 CompilerType CreateTypedef(const char *name, 239 const CompilerDeclContext &decl_ctx) const; 240 241 // If the current object represents a typedef type, get the underlying type 242 CompilerType GetTypedefedType() const; 243 244 // Create related types using the current type's AST 245 CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const; 246 247 // Exploring the type 248 249 struct IntegralTemplateArgument; 250 251 /// Return the size of the type in bytes. 252 llvm::Optional<uint64_t> GetByteSize(ExecutionContextScope *exe_scope) const; 253 /// Return the size of the type in bits. 254 llvm::Optional<uint64_t> GetBitSize(ExecutionContextScope *exe_scope) const; 255 256 lldb::Encoding GetEncoding(uint64_t &count) const; 257 258 lldb::Format GetFormat() const; 259 260 size_t GetTypeBitAlign() const; 261 262 uint32_t GetNumChildren(bool omit_empty_base_classes, 263 const ExecutionContext *exe_ctx) const; 264 265 lldb::BasicType GetBasicTypeEnumeration() const; 266 267 static lldb::BasicType GetBasicTypeEnumeration(ConstString name); 268 269 // If this type is an enumeration, iterate through all of its enumerators 270 // using a callback. If the callback returns true, keep iterating, else abort 271 // the iteration. 272 void ForEachEnumerator( 273 std::function<bool(const CompilerType &integer_type, 274 ConstString name, 275 const llvm::APSInt &value)> const &callback) const; 276 277 uint32_t GetNumFields() const; 278 279 CompilerType GetFieldAtIndex(size_t idx, std::string &name, 280 uint64_t *bit_offset_ptr, 281 uint32_t *bitfield_bit_size_ptr, 282 bool *is_bitfield_ptr) const; 283 284 uint32_t GetNumDirectBaseClasses() const; 285 286 uint32_t GetNumVirtualBaseClasses() const; 287 288 CompilerType GetDirectBaseClassAtIndex(size_t idx, 289 uint32_t *bit_offset_ptr) const; 290 291 CompilerType GetVirtualBaseClassAtIndex(size_t idx, 292 uint32_t *bit_offset_ptr) const; 293 294 uint32_t GetIndexOfFieldWithName(const char *name, 295 CompilerType *field_compiler_type = nullptr, 296 uint64_t *bit_offset_ptr = nullptr, 297 uint32_t *bitfield_bit_size_ptr = nullptr, 298 bool *is_bitfield_ptr = nullptr) const; 299 300 CompilerType GetChildCompilerTypeAtIndex( 301 ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers, 302 bool omit_empty_base_classes, bool ignore_array_bounds, 303 std::string &child_name, uint32_t &child_byte_size, 304 int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size, 305 uint32_t &child_bitfield_bit_offset, bool &child_is_base_class, 306 bool &child_is_deref_of_parent, ValueObject *valobj, 307 uint64_t &language_flags) const; 308 309 // Lookup a child given a name. This function will match base class names and 310 // member member names in "clang_type" only, not descendants. 311 uint32_t GetIndexOfChildWithName(const char *name, 312 bool omit_empty_base_classes) const; 313 314 // Lookup a child member given a name. This function will match member names 315 // only and will descend into "clang_type" children in search for the first 316 // member in this class, or any base class that matches "name". 317 // TODO: Return all matches for a given name by returning a 318 // vector<vector<uint32_t>> 319 // so we catch all names that match a given child name, not just the first. 320 size_t 321 GetIndexOfChildMemberWithName(const char *name, bool omit_empty_base_classes, 322 std::vector<uint32_t> &child_indexes) const; 323 324 size_t GetNumTemplateArguments() const; 325 326 lldb::TemplateArgumentKind GetTemplateArgumentKind(size_t idx) const; 327 CompilerType GetTypeTemplateArgument(size_t idx) const; 328 329 // Returns the value of the template argument and its type. 330 llvm::Optional<IntegralTemplateArgument> 331 GetIntegralTemplateArgument(size_t idx) const; 332 333 CompilerType GetTypeForFormatters() const; 334 335 LazyBool ShouldPrintAsOneLiner(ValueObject *valobj) const; 336 337 bool IsMeaninglessWithoutDynamicResolution() const; 338 339 // Pointers & References 340 341 // Converts "s" to a floating point value and place resulting floating point 342 // bytes in the "dst" buffer. 343 size_t ConvertStringToFloatValue(const char *s, uint8_t *dst, 344 size_t dst_size) const; 345 346 // Dumping types 347 348#ifndef NDEBUG 349 /// Convenience LLVM-style dump method for use in the debugger only. 350 /// Don't call this function from actual code. 351 LLVM_DUMP_METHOD void dump() const; 352#endif 353 354 void DumpValue(ExecutionContext *exe_ctx, Stream *s, lldb::Format format, 355 const DataExtractor &data, lldb::offset_t data_offset, 356 size_t data_byte_size, uint32_t bitfield_bit_size, 357 uint32_t bitfield_bit_offset, bool show_types, 358 bool show_summary, bool verbose, uint32_t depth); 359 360 bool DumpTypeValue(Stream *s, lldb::Format format, const DataExtractor &data, 361 lldb::offset_t data_offset, size_t data_byte_size, 362 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, 363 ExecutionContextScope *exe_scope); 364 365 void DumpSummary(ExecutionContext *exe_ctx, Stream *s, 366 const DataExtractor &data, lldb::offset_t data_offset, 367 size_t data_byte_size); 368 369 void DumpTypeDescription() const; // Dump to stdout 370 371 void DumpTypeDescription(Stream *s) const; 372 373 bool GetValueAsScalar(const DataExtractor &data, lldb::offset_t data_offset, 374 size_t data_byte_size, Scalar &value) const; 375 376 bool SetValueFromScalar(const Scalar &value, Stream &strm); 377 378 bool ReadFromMemory(ExecutionContext *exe_ctx, lldb::addr_t addr, 379 AddressType address_type, DataExtractor &data); 380 381 bool WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t addr, 382 AddressType address_type, StreamString &new_value); 383 384 void Clear() { 385 m_type = nullptr; 386 m_type_system = nullptr; 387 } 388 389private: 390 lldb::opaque_compiler_type_t m_type; 391 TypeSystem *m_type_system; 392}; 393 394bool operator==(const CompilerType &lhs, const CompilerType &rhs); 395bool operator!=(const CompilerType &lhs, const CompilerType &rhs); 396 397struct CompilerType::IntegralTemplateArgument { 398 llvm::APSInt value; 399 CompilerType type; 400}; 401 402} // namespace lldb_private 403 404#endif // liblldb_CompilerType_h_ 405