CompilerType.h revision 341825
12490Sjkh//===-- CompilerType.h ------------------------------------------*- C++ -*-===//
22490Sjkh//
32490Sjkh//                     The LLVM Compiler Infrastructure
42490Sjkh//
52490Sjkh// This file is distributed under the University of Illinois Open Source
62490Sjkh// License. See LICENSE.TXT for details.
72490Sjkh//
82490Sjkh//===----------------------------------------------------------------------===//
92490Sjkh
102490Sjkh#ifndef liblldb_CompilerType_h_
112490Sjkh#define liblldb_CompilerType_h_
122490Sjkh
132490Sjkh// C Includes
142490Sjkh// C++ Includes
152490Sjkh#include <functional>
16203926Suqs#include <string>
172490Sjkh#include <vector>
182490Sjkh
192490Sjkh// Other libraries and framework includes
202490Sjkh// Project includes
212490Sjkh#include "lldb/Core/ClangForward.h"
222490Sjkh#include "lldb/lldb-private.h"
232490Sjkh#include "llvm/ADT/APSInt.h"
242490Sjkh
252490Sjkhnamespace lldb_private {
262490Sjkh
272490Sjkhclass DataExtractor;
282490Sjkh
292490Sjkh//----------------------------------------------------------------------
302490Sjkh// A class that can carry around a clang ASTContext and a opaque clang
312490Sjkh// QualType. A clang::QualType can be easily reconstructed from an opaque clang
322490Sjkh// type and often the ASTContext is needed when doing various type related
33114725Sobrien// tasks, so this class allows both items to travel in a single very
342490Sjkh// lightweight class that can be used. There are many static equivalents of the
3515944Sache// member functions that allow the ASTContext and the opaque clang QualType to
362490Sjkh// be specified for ease of use and to avoid code duplication.
372490Sjkh//----------------------------------------------------------------------
382490Sjkhclass CompilerType {
392490Sjkhpublic:
402490Sjkh  //----------------------------------------------------------------------
4115944Sache  // Constructors and Destructors
42114725Sobrien  //----------------------------------------------------------------------
4351287Speter  CompilerType(TypeSystem *type_system, lldb::opaque_compiler_type_t type);
44114725Sobrien  CompilerType(clang::ASTContext *ast_context, clang::QualType qual_type);
45114725Sobrien
462490Sjkh  CompilerType(const CompilerType &rhs)
472490Sjkh      : m_type(rhs.m_type), m_type_system(rhs.m_type_system) {}
482490Sjkh
492490Sjkh  CompilerType() : m_type(nullptr), m_type_system(nullptr) {}
502490Sjkh
512490Sjkh  ~CompilerType();
522490Sjkh
532490Sjkh  //----------------------------------------------------------------------
542490Sjkh  // Operators
552490Sjkh  //----------------------------------------------------------------------
562490Sjkh
572490Sjkh  const CompilerType &operator=(const CompilerType &rhs) {
582490Sjkh    m_type = rhs.m_type;
592490Sjkh    m_type_system = rhs.m_type_system;
60203926Suqs    return *this;
61203926Suqs  }
62203926Suqs
63203926Suqs  //----------------------------------------------------------------------
64203926Suqs  // Tests
65203926Suqs  //----------------------------------------------------------------------
66203926Suqs
672490Sjkh  explicit operator bool() const {
68203926Suqs    return m_type != nullptr && m_type_system != nullptr;
69203926Suqs  }
70249828Seadler
71249828Seadler  bool operator<(const CompilerType &rhs) const {
72249828Seadler    if (m_type_system == rhs.m_type_system)
732490Sjkh      return m_type < rhs.m_type;
74249828Seadler    return m_type_system < rhs.m_type_system;
752490Sjkh  }
76249828Seadler
772490Sjkh  bool IsValid() const { return m_type != nullptr && m_type_system != nullptr; }
782490Sjkh
79201175Sed  bool IsArrayType(CompilerType *element_type, uint64_t *size,
80203926Suqs                   bool *is_incomplete) const;
812490Sjkh
82203926Suqs  bool IsVectorType(CompilerType *element_type, uint64_t *size) const;
832490Sjkh
84203926Suqs  bool IsArrayOfScalarType() const;
85203926Suqs
862490Sjkh  bool IsAggregateType() const;
872490Sjkh
88203926Suqs  bool IsAnonymousType() const;
89203926Suqs
90203926Suqs  bool IsBeingDefined() const;
91142328Sru
92142328Sru  bool IsCharType() const;
93142328Sru
94142328Sru  bool IsCompleteType() const;
95203926Suqs
96142022Sru  bool IsConst() const;
97142022Sru
98142022Sru  bool IsCStringType(uint32_t &length) const;
99142022Sru
100142022Sru  bool IsDefined() const;
101142328Sru
102142328Sru  bool IsFloatingPointType(uint32_t &count, bool &is_complex) const;
1032490Sjkh
1042490Sjkh  bool IsFunctionType(bool *is_variadic_ptr = nullptr) const;
105203926Suqs
106203926Suqs  uint32_t IsHomogeneousAggregate(CompilerType *base_type_ptr) const;
1072490Sjkh
1082490Sjkh  size_t GetNumberOfFunctionArguments() const;
1092490Sjkh
110249828Seadler  CompilerType GetFunctionArgumentAtIndex(const size_t index) const;
111201175Sed
1122490Sjkh  bool IsVariadicFunctionType() const;
113142022Sru
114203926Suqs  bool IsFunctionPointerType() const;
115203926Suqs
116203926Suqs  bool IsBlockPointerType(CompilerType *function_pointer_type_ptr) const;
1172490Sjkh
1182490Sjkh  bool IsIntegerType(bool &is_signed) const;
119203926Suqs
120203926Suqs  bool IsEnumerationType(bool &is_signed) const;
1212490Sjkh
122203926Suqs  bool IsIntegerOrEnumerationType(bool &is_signed) const;
1232490Sjkh
124203926Suqs  bool IsPolymorphicClass() const;
1252490Sjkh
1262490Sjkh  bool
1272490Sjkh  IsPossibleCPlusPlusDynamicType(CompilerType *target_type = nullptr) const {
1282490Sjkh    return IsPossibleDynamicType(target_type, true, false);
1292490Sjkh  }
1302490Sjkh
1312490Sjkh  bool IsPossibleDynamicType(CompilerType *target_type, // Can pass nullptr
132                             bool check_cplusplus, bool check_objc) const;
133
134  bool IsPointerToScalarType() const;
135
136  bool IsRuntimeGeneratedType() const;
137
138  bool IsPointerType(CompilerType *pointee_type = nullptr) const;
139
140  bool IsPointerOrReferenceType(CompilerType *pointee_type = nullptr) const;
141
142  bool IsReferenceType(CompilerType *pointee_type = nullptr,
143                       bool *is_rvalue = nullptr) const;
144
145  bool ShouldTreatScalarValueAsAddress() const;
146
147  bool IsScalarType() const;
148
149  bool IsTypedefType() const;
150
151  bool IsVoidType() const;
152
153  //----------------------------------------------------------------------
154  // Type Completion
155  //----------------------------------------------------------------------
156
157  bool GetCompleteType() const;
158
159  //----------------------------------------------------------------------
160  // AST related queries
161  //----------------------------------------------------------------------
162
163  size_t GetPointerByteSize() const;
164
165  //----------------------------------------------------------------------
166  // Accessors
167  //----------------------------------------------------------------------
168
169  TypeSystem *GetTypeSystem() const { return m_type_system; }
170
171  ConstString GetConstQualifiedTypeName() const;
172
173  ConstString GetConstTypeName() const;
174
175  ConstString GetTypeName() const;
176
177  ConstString GetDisplayTypeName() const;
178
179  uint32_t
180  GetTypeInfo(CompilerType *pointee_or_element_compiler_type = nullptr) const;
181
182  lldb::LanguageType GetMinimumLanguage();
183
184  lldb::opaque_compiler_type_t GetOpaqueQualType() const { return m_type; }
185
186  lldb::TypeClass GetTypeClass() const;
187
188  void SetCompilerType(TypeSystem *type_system,
189                       lldb::opaque_compiler_type_t type);
190
191  void SetCompilerType(clang::ASTContext *ast, clang::QualType qual_type);
192
193  unsigned GetTypeQualifiers() const;
194
195  //----------------------------------------------------------------------
196  // Creating related types
197  //----------------------------------------------------------------------
198
199  CompilerType GetArrayElementType(uint64_t *stride = nullptr) const;
200
201  CompilerType GetArrayType(uint64_t size) const;
202
203  CompilerType GetCanonicalType() const;
204
205  CompilerType GetFullyUnqualifiedType() const;
206
207  // Returns -1 if this isn't a function of if the function doesn't have a
208  // prototype Returns a value >= 0 if there is a prototype.
209  int GetFunctionArgumentCount() const;
210
211  CompilerType GetFunctionArgumentTypeAtIndex(size_t idx) const;
212
213  CompilerType GetFunctionReturnType() const;
214
215  size_t GetNumMemberFunctions() const;
216
217  TypeMemberFunctionImpl GetMemberFunctionAtIndex(size_t idx);
218
219  //----------------------------------------------------------------------
220  // If this type is a reference to a type (L value or R value reference),
221  // return a new type with the reference removed, else return the current type
222  // itself.
223  //----------------------------------------------------------------------
224  CompilerType GetNonReferenceType() const;
225
226  //----------------------------------------------------------------------
227  // If this type is a pointer type, return the type that the pointer points
228  // to, else return an invalid type.
229  //----------------------------------------------------------------------
230  CompilerType GetPointeeType() const;
231
232  //----------------------------------------------------------------------
233  // Return a new CompilerType that is a pointer to this type
234  //----------------------------------------------------------------------
235  CompilerType GetPointerType() const;
236
237  //----------------------------------------------------------------------
238  // Return a new CompilerType that is a L value reference to this type if this
239  // type is valid and the type system supports L value references, else return
240  // an invalid type.
241  //----------------------------------------------------------------------
242  CompilerType GetLValueReferenceType() const;
243
244  //----------------------------------------------------------------------
245  // Return a new CompilerType that is a R value reference to this type if this
246  // type is valid and the type system supports R value references, else return
247  // an invalid type.
248  //----------------------------------------------------------------------
249  CompilerType GetRValueReferenceType() const;
250
251  //----------------------------------------------------------------------
252  // Return a new CompilerType adds a const modifier to this type if this type
253  // is valid and the type system supports const modifiers, else return an
254  // invalid type.
255  //----------------------------------------------------------------------
256  CompilerType AddConstModifier() const;
257
258  //----------------------------------------------------------------------
259  // Return a new CompilerType adds a volatile modifier to this type if this
260  // type is valid and the type system supports volatile modifiers, else return
261  // an invalid type.
262  //----------------------------------------------------------------------
263  CompilerType AddVolatileModifier() const;
264
265  //----------------------------------------------------------------------
266  // Return a new CompilerType adds a restrict modifier to this type if this
267  // type is valid and the type system supports restrict modifiers, else return
268  // an invalid type.
269  //----------------------------------------------------------------------
270  CompilerType AddRestrictModifier() const;
271
272  //----------------------------------------------------------------------
273  // Create a typedef to this type using "name" as the name of the typedef this
274  // type is valid and the type system supports typedefs, else return an
275  // invalid type.
276  //----------------------------------------------------------------------
277  CompilerType CreateTypedef(const char *name,
278                             const CompilerDeclContext &decl_ctx) const;
279
280  // If the current object represents a typedef type, get the underlying type
281  CompilerType GetTypedefedType() const;
282
283  //----------------------------------------------------------------------
284  // Create related types using the current type's AST
285  //----------------------------------------------------------------------
286  CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const;
287
288  //----------------------------------------------------------------------
289  // Exploring the type
290  //----------------------------------------------------------------------
291
292  struct IntegralTemplateArgument;
293
294  uint64_t GetByteSize(ExecutionContextScope *exe_scope) const;
295
296  uint64_t GetBitSize(ExecutionContextScope *exe_scope) const;
297
298  lldb::Encoding GetEncoding(uint64_t &count) const;
299
300  lldb::Format GetFormat() const;
301
302  size_t GetTypeBitAlign() const;
303
304  uint32_t GetNumChildren(bool omit_empty_base_classes) const;
305
306  lldb::BasicType GetBasicTypeEnumeration() const;
307
308  static lldb::BasicType GetBasicTypeEnumeration(const ConstString &name);
309
310  //----------------------------------------------------------------------
311  // If this type is an enumeration, iterate through all of its enumerators
312  // using a callback. If the callback returns true, keep iterating, else abort
313  // the iteration.
314  //----------------------------------------------------------------------
315  void ForEachEnumerator(
316      std::function<bool(const CompilerType &integer_type,
317                         const ConstString &name,
318                         const llvm::APSInt &value)> const &callback) const;
319
320  uint32_t GetNumFields() const;
321
322  CompilerType GetFieldAtIndex(size_t idx, std::string &name,
323                               uint64_t *bit_offset_ptr,
324                               uint32_t *bitfield_bit_size_ptr,
325                               bool *is_bitfield_ptr) const;
326
327  uint32_t GetNumDirectBaseClasses() const;
328
329  uint32_t GetNumVirtualBaseClasses() const;
330
331  CompilerType GetDirectBaseClassAtIndex(size_t idx,
332                                         uint32_t *bit_offset_ptr) const;
333
334  CompilerType GetVirtualBaseClassAtIndex(size_t idx,
335                                          uint32_t *bit_offset_ptr) const;
336
337  uint32_t GetIndexOfFieldWithName(const char *name,
338                                   CompilerType *field_compiler_type = nullptr,
339                                   uint64_t *bit_offset_ptr = nullptr,
340                                   uint32_t *bitfield_bit_size_ptr = nullptr,
341                                   bool *is_bitfield_ptr = nullptr) const;
342
343  CompilerType GetChildCompilerTypeAtIndex(
344      ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
345      bool omit_empty_base_classes, bool ignore_array_bounds,
346      std::string &child_name, uint32_t &child_byte_size,
347      int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size,
348      uint32_t &child_bitfield_bit_offset, bool &child_is_base_class,
349      bool &child_is_deref_of_parent, ValueObject *valobj,
350      uint64_t &language_flags) const;
351
352  // Lookup a child given a name. This function will match base class names and
353  // member member names in "clang_type" only, not descendants.
354  uint32_t GetIndexOfChildWithName(const char *name,
355                                   bool omit_empty_base_classes) const;
356
357  // Lookup a child member given a name. This function will match member names
358  // only and will descend into "clang_type" children in search for the first
359  // member in this class, or any base class that matches "name".
360  // TODO: Return all matches for a given name by returning a
361  // vector<vector<uint32_t>>
362  // so we catch all names that match a given child name, not just the first.
363  size_t
364  GetIndexOfChildMemberWithName(const char *name, bool omit_empty_base_classes,
365                                std::vector<uint32_t> &child_indexes) const;
366
367  size_t GetNumTemplateArguments() const;
368
369  lldb::TemplateArgumentKind GetTemplateArgumentKind(size_t idx) const;
370  CompilerType GetTypeTemplateArgument(size_t idx) const;
371
372  // Returns the value of the template argument and its type.
373  llvm::Optional<IntegralTemplateArgument>
374  GetIntegralTemplateArgument(size_t idx) const;
375
376  CompilerType GetTypeForFormatters() const;
377
378  LazyBool ShouldPrintAsOneLiner(ValueObject *valobj) const;
379
380  bool IsMeaninglessWithoutDynamicResolution() const;
381
382  //------------------------------------------------------------------
383  // Pointers & References
384  //------------------------------------------------------------------
385
386  // Converts "s" to a floating point value and place resulting floating point
387  // bytes in the "dst" buffer.
388  size_t ConvertStringToFloatValue(const char *s, uint8_t *dst,
389                                   size_t dst_size) const;
390
391  //----------------------------------------------------------------------
392  // Dumping types
393  //----------------------------------------------------------------------
394  void DumpValue(ExecutionContext *exe_ctx, Stream *s, lldb::Format format,
395                 const DataExtractor &data, lldb::offset_t data_offset,
396                 size_t data_byte_size, uint32_t bitfield_bit_size,
397                 uint32_t bitfield_bit_offset, bool show_types,
398                 bool show_summary, bool verbose, uint32_t depth);
399
400  bool DumpTypeValue(Stream *s, lldb::Format format, const DataExtractor &data,
401                     lldb::offset_t data_offset, size_t data_byte_size,
402                     uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
403                     ExecutionContextScope *exe_scope);
404
405  void DumpSummary(ExecutionContext *exe_ctx, Stream *s,
406                   const DataExtractor &data, lldb::offset_t data_offset,
407                   size_t data_byte_size);
408
409  void DumpTypeDescription() const; // Dump to stdout
410
411  void DumpTypeDescription(Stream *s) const;
412
413  bool GetValueAsScalar(const DataExtractor &data, lldb::offset_t data_offset,
414                        size_t data_byte_size, Scalar &value) const;
415
416  bool SetValueFromScalar(const Scalar &value, Stream &strm);
417
418  bool ReadFromMemory(ExecutionContext *exe_ctx, lldb::addr_t addr,
419                      AddressType address_type, DataExtractor &data);
420
421  bool WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t addr,
422                     AddressType address_type, StreamString &new_value);
423
424  void Clear() {
425    m_type = nullptr;
426    m_type_system = nullptr;
427  }
428
429private:
430  lldb::opaque_compiler_type_t m_type;
431  TypeSystem *m_type_system;
432};
433
434bool operator==(const CompilerType &lhs, const CompilerType &rhs);
435bool operator!=(const CompilerType &lhs, const CompilerType &rhs);
436
437struct CompilerType::IntegralTemplateArgument {
438  llvm::APSInt value;
439  CompilerType type;
440};
441
442} // namespace lldb_private
443
444#endif // liblldb_CompilerType_h_
445