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