LLDB  mainline
CompilerType.h
Go to the documentation of this file.
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 
20 namespace lldb_private {
21 
22 class 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.
31 class CompilerType {
32 public:
33  // Constructors and Destructors
35  CompilerType(clang::ASTContext *ast_context, clang::QualType qual_type);
36 
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 
153 
155 
156  ConstString GetTypeName() const;
157 
159 
160  uint32_t
161  GetTypeInfo(CompilerType *pointee_or_element_compiler_type = nullptr) const;
162 
164 
166 
167  lldb::TypeClass GetTypeClass() const;
168 
169  void SetCompilerType(TypeSystem *type_system,
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 
183 
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 
191 
193 
194  size_t GetNumMemberFunctions() const;
195 
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.
202 
203  // If this type is a pointer type, return the type that the pointer points
204  // to, else return an invalid type.
206 
207  // Return a new CompilerType that is a pointer to this type
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.
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.
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.
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.
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.
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
243 
244  // Create related types using the current type's AST
246 
247  // Exploring the type
248 
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 
266 
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 
285 
287 
289  uint32_t *bit_offset_ptr) const;
290 
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 
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 
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 
334 
336 
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 
389 private:
391  TypeSystem *m_type_system;
392 };
393 
394 bool operator==(const CompilerType &lhs, const CompilerType &rhs);
395 bool operator!=(const CompilerType &lhs, const CompilerType &rhs);
396 
398  llvm::APSInt value;
400 };
401 
402 } // namespace lldb_private
403 
404 #endif // liblldb_CompilerType_h_
An data extractor class.
Definition: DataExtractor.h:47
bool IsEnumerationType(bool &is_signed) const
CompilerType GetNonReferenceType() const
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
bool IsPointerOrReferenceType(CompilerType *pointee_type=nullptr) const
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
bool IsFunctionPointerType() const
bool ShouldTreatScalarValueAsAddress() const
uint32_t GetIndexOfChildWithName(const char *name, bool omit_empty_base_classes) const
void ForEachEnumerator(std::function< bool(const CompilerType &integer_type, ConstString name, const llvm::APSInt &value)> const &callback) const
void DumpValue(ExecutionContext *exe_ctx, Stream *s, lldb::Format format, const DataExtractor &data, lldb::offset_t data_offset, size_t data_byte_size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types, bool show_summary, bool verbose, uint32_t depth)
lldb::Format GetFormat() const
size_t ConvertStringToFloatValue(const char *s, uint8_t *dst, size_t dst_size) const
bool SetValueFromScalar(const Scalar &value, Stream &strm)
bool IsIntegerOrEnumerationType(bool &is_signed) const
void * opaque_compiler_type_t
Definition: lldb-types.h:90
bool IsPointerToScalarType() const
uint32_t IsHomogeneousAggregate(CompilerType *base_type_ptr) const
TypeMemberFunctionImpl GetMemberFunctionAtIndex(size_t idx)
size_t GetPointerByteSize() const
bool WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t addr, AddressType address_type, StreamString &new_value)
CompilerType GetArrayType(uint64_t size) const
uint32_t GetTypeInfo(CompilerType *pointee_or_element_compiler_type=nullptr) const
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
llvm::Optional< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
uint32_t GetNumChildren(bool omit_empty_base_classes, const ExecutionContext *exe_ctx) const
CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const
CompilerType GetPointeeType() const
uint32_t GetNumFields() const
CompilerType GetDirectBaseClassAtIndex(size_t idx, uint32_t *bit_offset_ptr) const
bool IsPointerType(CompilerType *pointee_type=nullptr) const
CompilerType AddConstModifier() const
bool IsVectorType(CompilerType *element_type, uint64_t *size) const
bool operator==(const Address &lhs, const Address &rhs)
Definition: Address.cpp:973
bool DumpTypeValue(Stream *s, lldb::Format format, const DataExtractor &data, lldb::offset_t data_offset, size_t data_byte_size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, ExecutionContextScope *exe_scope)
lldb::opaque_compiler_type_t GetOpaqueQualType() const
Definition: CompilerType.h:165
bool IsArrayType(CompilerType *element_type, uint64_t *size, bool *is_incomplete) const
bool IsMeaninglessWithoutDynamicResolution() const
bool IsFloatingPointType(uint32_t &count, bool &is_complex) const
unsigned GetTypeQualifiers() const
void DumpSummary(ExecutionContext *exe_ctx, Stream *s, const DataExtractor &data, lldb::offset_t data_offset, size_t data_byte_size)
LanguageType
Programming language type.
const CompilerType & operator=(const CompilerType &rhs)
Definition: CompilerType.h:46
uint32_t GetNumVirtualBaseClasses() const
CompilerType GetFunctionArgumentTypeAtIndex(size_t idx) const
lldb::LanguageType GetMinimumLanguage()
llvm::Optional< IntegralTemplateArgument > GetIntegralTemplateArgument(size_t idx) const
uint64_t offset_t
Definition: lldb-types.h:87
lldb::TypeClass GetTypeClass() const
TypeSystem * GetTypeSystem() const
Definition: CompilerType.h:150
bool IsReferenceType(CompilerType *pointee_type=nullptr, bool *is_rvalue=nullptr) const
bool operator<(const CompilerType &rhs) const
Definition: CompilerType.h:58
void SetCompilerType(TypeSystem *type_system, lldb::opaque_compiler_type_t type)
bool IsRuntimeGeneratedType() const
uint32_t GetIndexOfFieldWithName(const char *name, CompilerType *field_compiler_type=nullptr, uint64_t *bit_offset_ptr=nullptr, uint32_t *bitfield_bit_size_ptr=nullptr, bool *is_bitfield_ptr=nullptr) const
size_t GetNumMemberFunctions() const
size_t GetNumberOfFunctionArguments() const
LLVM_DUMP_METHOD void dump() const
Convenience LLVM-style dump method for use in the debugger only.
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
size_t GetIndexOfChildMemberWithName(const char *name, bool omit_empty_base_classes, std::vector< uint32_t > &child_indexes) const
ConstString GetDisplayTypeName() const
bool IsVariadicFunctionType() const
ConstString GetConstQualifiedTypeName() const
CompilerType GetPointerType() const
CompilerType GetLValueReferenceType() const
bool operator!=(const Address &lhs, const Address &rhs)
Definition: Address.cpp:979
CompilerType GetChildCompilerTypeAtIndex(ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers, bool omit_empty_base_classes, bool ignore_array_bounds, std::string &child_name, uint32_t &child_byte_size, int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset, bool &child_is_base_class, bool &child_is_deref_of_parent, ValueObject *valobj, uint64_t &language_flags) const
CompilerType AddVolatileModifier() const
uint32_t GetNumDirectBaseClasses() const
CompilerType GetCanonicalType() const
bool IsPossibleCPlusPlusDynamicType(CompilerType *target_type=nullptr) const
Definition: CompilerType.h:114
bool IsCStringType(uint32_t &length) const
size_t GetNumTemplateArguments() const
lldb::Encoding GetEncoding(uint64_t &count) const
CompilerType GetArrayElementType(uint64_t *stride=nullptr) const
LazyBool ShouldPrintAsOneLiner(ValueObject *valobj) const
uint64_t addr_t
Definition: lldb-types.h:83
A uniqued constant string class.
Definition: ConstString.h:38
llvm::Optional< uint64_t > GetBitSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bits.
CompilerType CreateTypedef(const char *name, const CompilerDeclContext &decl_ctx) const
CompilerType GetFullyUnqualifiedType() const
CompilerType GetFunctionReturnType() const
lldb::TemplateArgumentKind GetTemplateArgumentKind(size_t idx) const
CompilerType GetFunctionArgumentAtIndex(const size_t index) const
int GetFunctionArgumentCount() const
CompilerType GetTypedefedType() const
CompilerType GetTypeTemplateArgument(size_t idx) const
CompilerType GetRValueReferenceType() const
CompilerType(const CompilerType &rhs)
Definition: CompilerType.h:37
CompilerType GetFieldAtIndex(size_t idx, std::string &name, uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) const
bool IsIntegerType(bool &is_signed) const
bool IsBlockPointerType(CompilerType *function_pointer_type_ptr) const
size_t GetTypeBitAlign() const
CompilerType GetVirtualBaseClassAtIndex(size_t idx, uint32_t *bit_offset_ptr) const
bool IsFunctionType(bool *is_variadic_ptr=nullptr) const
ConstString GetConstTypeName() const
CompilerType GetTypeForFormatters() const
ConstString GetTypeName() const
bool IsPossibleDynamicType(CompilerType *target_type, bool check_cplusplus, bool check_objc) const
lldb::BasicType GetBasicTypeEnumeration() const
CompilerType AddRestrictModifier() const
bool GetValueAsScalar(const DataExtractor &data, lldb::offset_t data_offset, size_t data_byte_size, Scalar &value) const
bool ReadFromMemory(ExecutionContext *exe_ctx, lldb::addr_t addr, AddressType address_type, DataExtractor &data)