CompilerType.h revision 327952
1//===-- CompilerType.h ------------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_CompilerType_h_
11#define liblldb_CompilerType_h_
12
13// C Includes
14// C++ Includes
15#include <functional>
16#include <string>
17#include <vector>
18
19// Other libraries and framework includes
20// Project includes
21#include "lldb/Core/ClangForward.h"
22#include "lldb/lldb-private.h"
23#include "llvm/ADT/APSInt.h"
24
25namespace lldb_private {
26
27class DataExtractor;
28
29//----------------------------------------------------------------------
30// A class that can carry around a clang ASTContext and a opaque clang
31// QualType. A clang::QualType can be easily reconstructed from an
32// opaque clang type and often the ASTContext is needed when doing
33// various type related tasks, so this class allows both items to travel
34// in a single very lightweight class that can be used. There are many
35// static equivalents of the member functions that allow the ASTContext
36// and the opaque clang QualType to be specified for ease of use and
37// to avoid code duplication.
38//----------------------------------------------------------------------
39class CompilerType {
40public:
41  //----------------------------------------------------------------------
42  // Constructors and Destructors
43  //----------------------------------------------------------------------
44  CompilerType(TypeSystem *type_system, lldb::opaque_compiler_type_t type);
45  CompilerType(clang::ASTContext *ast_context, clang::QualType qual_type);
46
47  CompilerType(const CompilerType &rhs)
48      : m_type(rhs.m_type), m_type_system(rhs.m_type_system) {}
49
50  CompilerType() : m_type(nullptr), m_type_system(nullptr) {}
51
52  ~CompilerType();
53
54  //----------------------------------------------------------------------
55  // Operators
56  //----------------------------------------------------------------------
57
58  const CompilerType &operator=(const CompilerType &rhs) {
59    m_type = rhs.m_type;
60    m_type_system = rhs.m_type_system;
61    return *this;
62  }
63
64  //----------------------------------------------------------------------
65  // Tests
66  //----------------------------------------------------------------------
67
68  explicit operator bool() const {
69    return m_type != nullptr && m_type_system != nullptr;
70  }
71
72  bool operator<(const CompilerType &rhs) const {
73    if (m_type_system == rhs.m_type_system)
74      return m_type < rhs.m_type;
75    return m_type_system < rhs.m_type_system;
76  }
77
78  bool IsValid() const { return m_type != nullptr && m_type_system != nullptr; }
79
80  bool IsArrayType(CompilerType *element_type, uint64_t *size,
81                   bool *is_incomplete) const;
82
83  bool IsVectorType(CompilerType *element_type, uint64_t *size) const;
84
85  bool IsArrayOfScalarType() const;
86
87  bool IsAggregateType() const;
88
89  bool IsAnonymousType() const;
90
91  bool IsBeingDefined() const;
92
93  bool IsCharType() const;
94
95  bool IsCompleteType() const;
96
97  bool IsConst() const;
98
99  bool IsCStringType(uint32_t &length) const;
100
101  bool IsDefined() const;
102
103  bool IsFloatingPointType(uint32_t &count, bool &is_complex) const;
104
105  bool IsFunctionType(bool *is_variadic_ptr = nullptr) const;
106
107  uint32_t IsHomogeneousAggregate(CompilerType *base_type_ptr) const;
108
109  size_t GetNumberOfFunctionArguments() const;
110
111  CompilerType GetFunctionArgumentAtIndex(const size_t index) const;
112
113  bool IsVariadicFunctionType() const;
114
115  bool IsFunctionPointerType() const;
116
117  bool IsBlockPointerType(CompilerType *function_pointer_type_ptr) const;
118
119  bool IsIntegerType(bool &is_signed) const;
120
121  bool IsEnumerationType(bool &is_signed) const;
122
123  bool IsIntegerOrEnumerationType(bool &is_signed) const;
124
125  bool IsPolymorphicClass() const;
126
127  bool
128  IsPossibleCPlusPlusDynamicType(CompilerType *target_type = nullptr) const {
129    return IsPossibleDynamicType(target_type, true, false);
130  }
131
132  bool IsPossibleDynamicType(CompilerType *target_type, // Can pass nullptr
133                             bool check_cplusplus, bool check_objc) const;
134
135  bool IsPointerToScalarType() const;
136
137  bool IsRuntimeGeneratedType() const;
138
139  bool IsPointerType(CompilerType *pointee_type = nullptr) const;
140
141  bool IsPointerOrReferenceType(CompilerType *pointee_type = nullptr) const;
142
143  bool IsReferenceType(CompilerType *pointee_type = nullptr,
144                       bool *is_rvalue = nullptr) const;
145
146  bool ShouldTreatScalarValueAsAddress() const;
147
148  bool IsScalarType() const;
149
150  bool IsTypedefType() const;
151
152  bool IsVoidType() const;
153
154  //----------------------------------------------------------------------
155  // Type Completion
156  //----------------------------------------------------------------------
157
158  bool GetCompleteType() const;
159
160  //----------------------------------------------------------------------
161  // AST related queries
162  //----------------------------------------------------------------------
163
164  size_t GetPointerByteSize() const;
165
166  //----------------------------------------------------------------------
167  // Accessors
168  //----------------------------------------------------------------------
169
170  TypeSystem *GetTypeSystem() const { return m_type_system; }
171
172  ConstString GetConstQualifiedTypeName() const;
173
174  ConstString GetConstTypeName() const;
175
176  ConstString GetTypeName() const;
177
178  ConstString GetDisplayTypeName() const;
179
180  uint32_t
181  GetTypeInfo(CompilerType *pointee_or_element_compiler_type = nullptr) const;
182
183  lldb::LanguageType GetMinimumLanguage();
184
185  lldb::opaque_compiler_type_t GetOpaqueQualType() const { return m_type; }
186
187  lldb::TypeClass GetTypeClass() const;
188
189  void SetCompilerType(TypeSystem *type_system,
190                       lldb::opaque_compiler_type_t type);
191
192  void SetCompilerType(clang::ASTContext *ast, clang::QualType qual_type);
193
194  unsigned GetTypeQualifiers() const;
195
196  //----------------------------------------------------------------------
197  // Creating related types
198  //----------------------------------------------------------------------
199
200  CompilerType GetArrayElementType(uint64_t *stride = nullptr) const;
201
202  CompilerType GetArrayType(uint64_t size) const;
203
204  CompilerType GetCanonicalType() const;
205
206  CompilerType GetFullyUnqualifiedType() const;
207
208  // Returns -1 if this isn't a function of if the function doesn't have a
209  // prototype
210  // Returns a value >= 0 if there is a prototype.
211  int GetFunctionArgumentCount() const;
212
213  CompilerType GetFunctionArgumentTypeAtIndex(size_t idx) const;
214
215  CompilerType GetFunctionReturnType() const;
216
217  size_t GetNumMemberFunctions() const;
218
219  TypeMemberFunctionImpl GetMemberFunctionAtIndex(size_t idx);
220
221  //----------------------------------------------------------------------
222  // If this type is a reference to a type (L value or R value reference),
223  // return a new type with the reference removed, else return the current
224  // type itself.
225  //----------------------------------------------------------------------
226  CompilerType GetNonReferenceType() const;
227
228  //----------------------------------------------------------------------
229  // If this type is a pointer type, return the type that the pointer
230  // points to, else return an invalid type.
231  //----------------------------------------------------------------------
232  CompilerType GetPointeeType() const;
233
234  //----------------------------------------------------------------------
235  // Return a new CompilerType that is a pointer to this type
236  //----------------------------------------------------------------------
237  CompilerType GetPointerType() const;
238
239  //----------------------------------------------------------------------
240  // Return a new CompilerType that is a L value reference to this type if
241  // this type is valid and the type system supports L value references,
242  // else return an invalid type.
243  //----------------------------------------------------------------------
244  CompilerType GetLValueReferenceType() const;
245
246  //----------------------------------------------------------------------
247  // Return a new CompilerType that is a R value reference to this type if
248  // this type is valid and the type system supports R value references,
249  // else return an invalid type.
250  //----------------------------------------------------------------------
251  CompilerType GetRValueReferenceType() const;
252
253  //----------------------------------------------------------------------
254  // Return a new CompilerType adds a const modifier to this type if
255  // this type is valid and the type system supports const modifiers,
256  // else return an invalid type.
257  //----------------------------------------------------------------------
258  CompilerType AddConstModifier() const;
259
260  //----------------------------------------------------------------------
261  // Return a new CompilerType adds a volatile modifier to this type if
262  // this type is valid and the type system supports volatile modifiers,
263  // else return an invalid type.
264  //----------------------------------------------------------------------
265  CompilerType AddVolatileModifier() const;
266
267  //----------------------------------------------------------------------
268  // Return a new CompilerType adds a restrict modifier to this type if
269  // this type is valid and the type system supports restrict modifiers,
270  // else return an invalid type.
271  //----------------------------------------------------------------------
272  CompilerType AddRestrictModifier() const;
273
274  //----------------------------------------------------------------------
275  // Create a typedef to this type using "name" as the name of the typedef
276  // this type is valid and the type system supports typedefs, else return
277  // an invalid type.
278  //----------------------------------------------------------------------
279  CompilerType CreateTypedef(const char *name,
280                             const CompilerDeclContext &decl_ctx) const;
281
282  // If the current object represents a typedef type, get the underlying type
283  CompilerType GetTypedefedType() const;
284
285  //----------------------------------------------------------------------
286  // Create related types using the current type's AST
287  //----------------------------------------------------------------------
288  CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const;
289
290  //----------------------------------------------------------------------
291  // Exploring the type
292  //----------------------------------------------------------------------
293
294  struct IntegralTemplateArgument;
295
296  uint64_t GetByteSize(ExecutionContextScope *exe_scope) const;
297
298  uint64_t GetBitSize(ExecutionContextScope *exe_scope) const;
299
300  lldb::Encoding GetEncoding(uint64_t &count) const;
301
302  lldb::Format GetFormat() const;
303
304  size_t GetTypeBitAlign() const;
305
306  uint32_t GetNumChildren(bool omit_empty_base_classes) const;
307
308  lldb::BasicType GetBasicTypeEnumeration() const;
309
310  static lldb::BasicType GetBasicTypeEnumeration(const ConstString &name);
311
312  //----------------------------------------------------------------------
313  // If this type is an enumeration, iterate through all of its enumerators
314  // using a callback. If the callback returns true, keep iterating, else
315  // abort the iteration.
316  //----------------------------------------------------------------------
317  void ForEachEnumerator(
318      std::function<bool(const CompilerType &integer_type,
319                         const ConstString &name,
320                         const llvm::APSInt &value)> const &callback) const;
321
322  uint32_t GetNumFields() const;
323
324  CompilerType GetFieldAtIndex(size_t idx, std::string &name,
325                               uint64_t *bit_offset_ptr,
326                               uint32_t *bitfield_bit_size_ptr,
327                               bool *is_bitfield_ptr) const;
328
329  uint32_t GetNumDirectBaseClasses() const;
330
331  uint32_t GetNumVirtualBaseClasses() const;
332
333  CompilerType GetDirectBaseClassAtIndex(size_t idx,
334                                         uint32_t *bit_offset_ptr) const;
335
336  CompilerType GetVirtualBaseClassAtIndex(size_t idx,
337                                          uint32_t *bit_offset_ptr) const;
338
339  uint32_t GetIndexOfFieldWithName(const char *name,
340                                   CompilerType *field_compiler_type = nullptr,
341                                   uint64_t *bit_offset_ptr = nullptr,
342                                   uint32_t *bitfield_bit_size_ptr = nullptr,
343                                   bool *is_bitfield_ptr = nullptr) const;
344
345  CompilerType GetChildCompilerTypeAtIndex(
346      ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
347      bool omit_empty_base_classes, bool ignore_array_bounds,
348      std::string &child_name, uint32_t &child_byte_size,
349      int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size,
350      uint32_t &child_bitfield_bit_offset, bool &child_is_base_class,
351      bool &child_is_deref_of_parent, ValueObject *valobj,
352      uint64_t &language_flags) const;
353
354  // Lookup a child given a name. This function will match base class names
355  // and member member names in "clang_type" only, not descendants.
356  uint32_t GetIndexOfChildWithName(const char *name,
357                                   bool omit_empty_base_classes) const;
358
359  // Lookup a child member given a name. This function will match member names
360  // only and will descend into "clang_type" children in search for the first
361  // member in this class, or any base class that matches "name".
362  // TODO: Return all matches for a given name by returning a
363  // vector<vector<uint32_t>>
364  // so we catch all names that match a given child name, not just the first.
365  size_t
366  GetIndexOfChildMemberWithName(const char *name, bool omit_empty_base_classes,
367                                std::vector<uint32_t> &child_indexes) const;
368
369  size_t GetNumTemplateArguments() const;
370
371  lldb::TemplateArgumentKind GetTemplateArgumentKind(size_t idx) const;
372  CompilerType GetTypeTemplateArgument(size_t idx) const;
373
374  // Returns the value of the template argument and its type.
375  llvm::Optional<IntegralTemplateArgument>
376  GetIntegralTemplateArgument(size_t idx) const;
377
378  CompilerType GetTypeForFormatters() const;
379
380  LazyBool ShouldPrintAsOneLiner(ValueObject *valobj) const;
381
382  bool IsMeaninglessWithoutDynamicResolution() const;
383
384  //------------------------------------------------------------------
385  // Pointers & References
386  //------------------------------------------------------------------
387
388  // Converts "s" to a floating point value and place resulting floating
389  // point bytes in the "dst" buffer.
390  size_t ConvertStringToFloatValue(const char *s, uint8_t *dst,
391                                   size_t dst_size) const;
392
393  //----------------------------------------------------------------------
394  // Dumping types
395  //----------------------------------------------------------------------
396  void DumpValue(ExecutionContext *exe_ctx, Stream *s, lldb::Format format,
397                 const DataExtractor &data, lldb::offset_t data_offset,
398                 size_t data_byte_size, uint32_t bitfield_bit_size,
399                 uint32_t bitfield_bit_offset, bool show_types,
400                 bool show_summary, bool verbose, uint32_t depth);
401
402  bool DumpTypeValue(Stream *s, lldb::Format format, const DataExtractor &data,
403                     lldb::offset_t data_offset, size_t data_byte_size,
404                     uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
405                     ExecutionContextScope *exe_scope);
406
407  void DumpSummary(ExecutionContext *exe_ctx, Stream *s,
408                   const DataExtractor &data, lldb::offset_t data_offset,
409                   size_t data_byte_size);
410
411  void DumpTypeDescription() const; // Dump to stdout
412
413  void DumpTypeDescription(Stream *s) const;
414
415  bool GetValueAsScalar(const DataExtractor &data, lldb::offset_t data_offset,
416                        size_t data_byte_size, Scalar &value) const;
417
418  bool SetValueFromScalar(const Scalar &value, Stream &strm);
419
420  bool ReadFromMemory(ExecutionContext *exe_ctx, lldb::addr_t addr,
421                      AddressType address_type, DataExtractor &data);
422
423  bool WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t addr,
424                     AddressType address_type, StreamString &new_value);
425
426  void Clear() {
427    m_type = nullptr;
428    m_type_system = nullptr;
429  }
430
431private:
432  lldb::opaque_compiler_type_t m_type;
433  TypeSystem *m_type_system;
434};
435
436bool operator==(const CompilerType &lhs, const CompilerType &rhs);
437bool operator!=(const CompilerType &lhs, const CompilerType &rhs);
438
439struct CompilerType::IntegralTemplateArgument {
440  llvm::APSInt value;
441  CompilerType type;
442};
443
444} // namespace lldb_private
445
446#endif // liblldb_CompilerType_h_
447