1 //===-- CompilerType.h ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_CompilerType_h_
11 #define liblldb_CompilerType_h_
17 #include "lldb/Core/ClangForward.h"
18 #include "lldb/lldb-private.h"
19 #include "llvm/ADT/APSInt.h"
21 namespace lldb_private {
25 //----------------------------------------------------------------------
26 // A class that can carry around a clang ASTContext and a opaque clang
27 // QualType. A clang::QualType can be easily reconstructed from an opaque clang
28 // type and often the ASTContext is needed when doing various type related
29 // tasks, so this class allows both items to travel in a single very
30 // lightweight class that can be used. There are many static equivalents of the
31 // member functions that allow the ASTContext and the opaque clang QualType to
32 // be specified for ease of use and to avoid code duplication.
33 //----------------------------------------------------------------------
36 //----------------------------------------------------------------------
37 // Constructors and Destructors
38 //----------------------------------------------------------------------
39 CompilerType(TypeSystem *type_system, lldb::opaque_compiler_type_t type);
40 CompilerType(clang::ASTContext *ast_context, clang::QualType qual_type);
42 CompilerType(const CompilerType &rhs)
43 : m_type(rhs.m_type), m_type_system(rhs.m_type_system) {}
45 CompilerType() : m_type(nullptr), m_type_system(nullptr) {}
49 //----------------------------------------------------------------------
51 //----------------------------------------------------------------------
53 const CompilerType &operator=(const CompilerType &rhs) {
55 m_type_system = rhs.m_type_system;
59 //----------------------------------------------------------------------
61 //----------------------------------------------------------------------
63 explicit operator bool() const {
64 return m_type != nullptr && m_type_system != nullptr;
67 bool operator<(const CompilerType &rhs) const {
68 if (m_type_system == rhs.m_type_system)
69 return m_type < rhs.m_type;
70 return m_type_system < rhs.m_type_system;
73 bool IsValid() const { return m_type != nullptr && m_type_system != nullptr; }
75 bool IsArrayType(CompilerType *element_type, uint64_t *size,
76 bool *is_incomplete) const;
78 bool IsVectorType(CompilerType *element_type, uint64_t *size) const;
80 bool IsArrayOfScalarType() const;
82 bool IsAggregateType() const;
84 bool IsAnonymousType() const;
86 bool IsBeingDefined() const;
88 bool IsCharType() const;
90 bool IsCompleteType() const;
94 bool IsCStringType(uint32_t &length) const;
96 bool IsDefined() const;
98 bool IsFloatingPointType(uint32_t &count, bool &is_complex) const;
100 bool IsFunctionType(bool *is_variadic_ptr = nullptr) const;
102 uint32_t IsHomogeneousAggregate(CompilerType *base_type_ptr) const;
104 size_t GetNumberOfFunctionArguments() const;
106 CompilerType GetFunctionArgumentAtIndex(const size_t index) const;
108 bool IsVariadicFunctionType() const;
110 bool IsFunctionPointerType() const;
112 bool IsBlockPointerType(CompilerType *function_pointer_type_ptr) const;
114 bool IsIntegerType(bool &is_signed) const;
116 bool IsEnumerationType(bool &is_signed) const;
118 bool IsIntegerOrEnumerationType(bool &is_signed) const;
120 bool IsPolymorphicClass() const;
123 IsPossibleCPlusPlusDynamicType(CompilerType *target_type = nullptr) const {
124 return IsPossibleDynamicType(target_type, true, false);
127 bool IsPossibleDynamicType(CompilerType *target_type, // Can pass nullptr
128 bool check_cplusplus, bool check_objc) const;
130 bool IsPointerToScalarType() const;
132 bool IsRuntimeGeneratedType() const;
134 bool IsPointerType(CompilerType *pointee_type = nullptr) const;
136 bool IsPointerOrReferenceType(CompilerType *pointee_type = nullptr) const;
138 bool IsReferenceType(CompilerType *pointee_type = nullptr,
139 bool *is_rvalue = nullptr) const;
141 bool ShouldTreatScalarValueAsAddress() const;
143 bool IsScalarType() const;
145 bool IsTypedefType() const;
147 bool IsVoidType() const;
149 //----------------------------------------------------------------------
151 //----------------------------------------------------------------------
153 bool GetCompleteType() const;
155 //----------------------------------------------------------------------
156 // AST related queries
157 //----------------------------------------------------------------------
159 size_t GetPointerByteSize() const;
161 //----------------------------------------------------------------------
163 //----------------------------------------------------------------------
165 TypeSystem *GetTypeSystem() const { return m_type_system; }
167 ConstString GetConstQualifiedTypeName() const;
169 ConstString GetConstTypeName() const;
171 ConstString GetTypeName() const;
173 ConstString GetDisplayTypeName() const;
176 GetTypeInfo(CompilerType *pointee_or_element_compiler_type = nullptr) const;
178 lldb::LanguageType GetMinimumLanguage();
180 lldb::opaque_compiler_type_t GetOpaqueQualType() const { return m_type; }
182 lldb::TypeClass GetTypeClass() const;
184 void SetCompilerType(TypeSystem *type_system,
185 lldb::opaque_compiler_type_t type);
187 void SetCompilerType(clang::ASTContext *ast, clang::QualType qual_type);
189 unsigned GetTypeQualifiers() const;
191 //----------------------------------------------------------------------
192 // Creating related types
193 //----------------------------------------------------------------------
195 CompilerType GetArrayElementType(uint64_t *stride = nullptr) const;
197 CompilerType GetArrayType(uint64_t size) const;
199 CompilerType GetCanonicalType() const;
201 CompilerType GetFullyUnqualifiedType() const;
203 // Returns -1 if this isn't a function of if the function doesn't have a
204 // prototype Returns a value >= 0 if there is a prototype.
205 int GetFunctionArgumentCount() const;
207 CompilerType GetFunctionArgumentTypeAtIndex(size_t idx) const;
209 CompilerType GetFunctionReturnType() const;
211 size_t GetNumMemberFunctions() const;
213 TypeMemberFunctionImpl GetMemberFunctionAtIndex(size_t idx);
215 //----------------------------------------------------------------------
216 // If this type is a reference to a type (L value or R value reference),
217 // return a new type with the reference removed, else return the current type
219 //----------------------------------------------------------------------
220 CompilerType GetNonReferenceType() const;
222 //----------------------------------------------------------------------
223 // If this type is a pointer type, return the type that the pointer points
224 // to, else return an invalid type.
225 //----------------------------------------------------------------------
226 CompilerType GetPointeeType() const;
228 //----------------------------------------------------------------------
229 // Return a new CompilerType that is a pointer to this type
230 //----------------------------------------------------------------------
231 CompilerType GetPointerType() const;
233 //----------------------------------------------------------------------
234 // Return a new CompilerType that is a L value reference to this type if this
235 // type is valid and the type system supports L value references, else return
237 //----------------------------------------------------------------------
238 CompilerType GetLValueReferenceType() const;
240 //----------------------------------------------------------------------
241 // Return a new CompilerType that is a R value reference to this type if this
242 // type is valid and the type system supports R value references, else return
244 //----------------------------------------------------------------------
245 CompilerType GetRValueReferenceType() const;
247 //----------------------------------------------------------------------
248 // Return a new CompilerType adds a const modifier to this type if this type
249 // is valid and the type system supports const modifiers, else return an
251 //----------------------------------------------------------------------
252 CompilerType AddConstModifier() const;
254 //----------------------------------------------------------------------
255 // Return a new CompilerType adds a volatile modifier to this type if this
256 // type is valid and the type system supports volatile modifiers, else return
258 //----------------------------------------------------------------------
259 CompilerType AddVolatileModifier() const;
261 //----------------------------------------------------------------------
262 // Return a new CompilerType adds a restrict modifier to this type if this
263 // type is valid and the type system supports restrict modifiers, else return
265 //----------------------------------------------------------------------
266 CompilerType AddRestrictModifier() const;
268 //----------------------------------------------------------------------
269 // Create a typedef to this type using "name" as the name of the typedef this
270 // type is valid and the type system supports typedefs, else return an
272 //----------------------------------------------------------------------
273 CompilerType CreateTypedef(const char *name,
274 const CompilerDeclContext &decl_ctx) const;
276 // If the current object represents a typedef type, get the underlying type
277 CompilerType GetTypedefedType() const;
279 //----------------------------------------------------------------------
280 // Create related types using the current type's AST
281 //----------------------------------------------------------------------
282 CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const;
284 //----------------------------------------------------------------------
285 // Exploring the type
286 //----------------------------------------------------------------------
288 struct IntegralTemplateArgument;
290 /// Return the size of the type in bytes.
291 llvm::Optional<uint64_t> GetByteSize(ExecutionContextScope *exe_scope) const;
292 /// Return the size of the type in bits.
293 llvm::Optional<uint64_t> GetBitSize(ExecutionContextScope *exe_scope) const;
295 lldb::Encoding GetEncoding(uint64_t &count) const;
297 lldb::Format GetFormat() const;
299 size_t GetTypeBitAlign() const;
301 uint32_t GetNumChildren(bool omit_empty_base_classes,
302 const ExecutionContext *exe_ctx) const;
304 lldb::BasicType GetBasicTypeEnumeration() const;
306 static lldb::BasicType GetBasicTypeEnumeration(const ConstString &name);
308 //----------------------------------------------------------------------
309 // If this type is an enumeration, iterate through all of its enumerators
310 // using a callback. If the callback returns true, keep iterating, else abort
312 //----------------------------------------------------------------------
313 void ForEachEnumerator(
314 std::function<bool(const CompilerType &integer_type,
315 const ConstString &name,
316 const llvm::APSInt &value)> const &callback) const;
318 uint32_t GetNumFields() const;
320 CompilerType GetFieldAtIndex(size_t idx, std::string &name,
321 uint64_t *bit_offset_ptr,
322 uint32_t *bitfield_bit_size_ptr,
323 bool *is_bitfield_ptr) const;
325 uint32_t GetNumDirectBaseClasses() const;
327 uint32_t GetNumVirtualBaseClasses() const;
329 CompilerType GetDirectBaseClassAtIndex(size_t idx,
330 uint32_t *bit_offset_ptr) const;
332 CompilerType GetVirtualBaseClassAtIndex(size_t idx,
333 uint32_t *bit_offset_ptr) const;
335 uint32_t GetIndexOfFieldWithName(const char *name,
336 CompilerType *field_compiler_type = nullptr,
337 uint64_t *bit_offset_ptr = nullptr,
338 uint32_t *bitfield_bit_size_ptr = nullptr,
339 bool *is_bitfield_ptr = nullptr) const;
341 CompilerType GetChildCompilerTypeAtIndex(
342 ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
343 bool omit_empty_base_classes, bool ignore_array_bounds,
344 std::string &child_name, uint32_t &child_byte_size,
345 int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size,
346 uint32_t &child_bitfield_bit_offset, bool &child_is_base_class,
347 bool &child_is_deref_of_parent, ValueObject *valobj,
348 uint64_t &language_flags) const;
350 // Lookup a child given a name. This function will match base class names and
351 // member member names in "clang_type" only, not descendants.
352 uint32_t GetIndexOfChildWithName(const char *name,
353 bool omit_empty_base_classes) const;
355 // Lookup a child member given a name. This function will match member names
356 // only and will descend into "clang_type" children in search for the first
357 // member in this class, or any base class that matches "name".
358 // TODO: Return all matches for a given name by returning a
359 // vector<vector<uint32_t>>
360 // so we catch all names that match a given child name, not just the first.
362 GetIndexOfChildMemberWithName(const char *name, bool omit_empty_base_classes,
363 std::vector<uint32_t> &child_indexes) const;
365 size_t GetNumTemplateArguments() const;
367 lldb::TemplateArgumentKind GetTemplateArgumentKind(size_t idx) const;
368 CompilerType GetTypeTemplateArgument(size_t idx) const;
370 // Returns the value of the template argument and its type.
371 llvm::Optional<IntegralTemplateArgument>
372 GetIntegralTemplateArgument(size_t idx) const;
374 CompilerType GetTypeForFormatters() const;
376 LazyBool ShouldPrintAsOneLiner(ValueObject *valobj) const;
378 bool IsMeaninglessWithoutDynamicResolution() const;
380 //------------------------------------------------------------------
381 // Pointers & References
382 //------------------------------------------------------------------
384 // Converts "s" to a floating point value and place resulting floating point
385 // bytes in the "dst" buffer.
386 size_t ConvertStringToFloatValue(const char *s, uint8_t *dst,
387 size_t dst_size) const;
389 //----------------------------------------------------------------------
391 //----------------------------------------------------------------------
392 void DumpValue(ExecutionContext *exe_ctx, Stream *s, lldb::Format format,
393 const DataExtractor &data, lldb::offset_t data_offset,
394 size_t data_byte_size, uint32_t bitfield_bit_size,
395 uint32_t bitfield_bit_offset, bool show_types,
396 bool show_summary, bool verbose, uint32_t depth);
398 bool DumpTypeValue(Stream *s, lldb::Format format, const DataExtractor &data,
399 lldb::offset_t data_offset, size_t data_byte_size,
400 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
401 ExecutionContextScope *exe_scope);
403 void DumpSummary(ExecutionContext *exe_ctx, Stream *s,
404 const DataExtractor &data, lldb::offset_t data_offset,
405 size_t data_byte_size);
407 void DumpTypeDescription() const; // Dump to stdout
409 void DumpTypeDescription(Stream *s) const;
411 bool GetValueAsScalar(const DataExtractor &data, lldb::offset_t data_offset,
412 size_t data_byte_size, Scalar &value) const;
414 bool SetValueFromScalar(const Scalar &value, Stream &strm);
416 bool ReadFromMemory(ExecutionContext *exe_ctx, lldb::addr_t addr,
417 AddressType address_type, DataExtractor &data);
419 bool WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t addr,
420 AddressType address_type, StreamString &new_value);
424 m_type_system = nullptr;
428 lldb::opaque_compiler_type_t m_type;
429 TypeSystem *m_type_system;
432 bool operator==(const CompilerType &lhs, const CompilerType &rhs);
433 bool operator!=(const CompilerType &lhs, const CompilerType &rhs);
435 struct CompilerType::IntegralTemplateArgument {
440 } // namespace lldb_private
442 #endif // liblldb_CompilerType_h_