]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Symbol/CompilerType.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Symbol / CompilerType.h
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 #include <functional>
14 #include <string>
15 #include <vector>
16
17 #include "lldb/Core/ClangForward.h"
18 #include "lldb/lldb-private.h"
19 #include "llvm/ADT/APSInt.h"
20
21 namespace lldb_private {
22
23 class DataExtractor;
24
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 //----------------------------------------------------------------------
34 class CompilerType {
35 public:
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);
41
42   CompilerType(const CompilerType &rhs)
43       : m_type(rhs.m_type), m_type_system(rhs.m_type_system) {}
44
45   CompilerType() : m_type(nullptr), m_type_system(nullptr) {}
46
47   ~CompilerType();
48
49   //----------------------------------------------------------------------
50   // Operators
51   //----------------------------------------------------------------------
52
53   const CompilerType &operator=(const CompilerType &rhs) {
54     m_type = rhs.m_type;
55     m_type_system = rhs.m_type_system;
56     return *this;
57   }
58
59   //----------------------------------------------------------------------
60   // Tests
61   //----------------------------------------------------------------------
62
63   explicit operator bool() const {
64     return m_type != nullptr && m_type_system != nullptr;
65   }
66
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;
71   }
72
73   bool IsValid() const { return m_type != nullptr && m_type_system != nullptr; }
74
75   bool IsArrayType(CompilerType *element_type, uint64_t *size,
76                    bool *is_incomplete) const;
77
78   bool IsVectorType(CompilerType *element_type, uint64_t *size) const;
79
80   bool IsArrayOfScalarType() const;
81
82   bool IsAggregateType() const;
83
84   bool IsAnonymousType() const;
85
86   bool IsBeingDefined() const;
87
88   bool IsCharType() const;
89
90   bool IsCompleteType() const;
91
92   bool IsConst() const;
93
94   bool IsCStringType(uint32_t &length) const;
95
96   bool IsDefined() const;
97
98   bool IsFloatingPointType(uint32_t &count, bool &is_complex) const;
99
100   bool IsFunctionType(bool *is_variadic_ptr = nullptr) const;
101
102   uint32_t IsHomogeneousAggregate(CompilerType *base_type_ptr) const;
103
104   size_t GetNumberOfFunctionArguments() const;
105
106   CompilerType GetFunctionArgumentAtIndex(const size_t index) const;
107
108   bool IsVariadicFunctionType() const;
109
110   bool IsFunctionPointerType() const;
111
112   bool IsBlockPointerType(CompilerType *function_pointer_type_ptr) const;
113
114   bool IsIntegerType(bool &is_signed) const;
115
116   bool IsEnumerationType(bool &is_signed) const;
117
118   bool IsIntegerOrEnumerationType(bool &is_signed) const;
119
120   bool IsPolymorphicClass() const;
121
122   bool
123   IsPossibleCPlusPlusDynamicType(CompilerType *target_type = nullptr) const {
124     return IsPossibleDynamicType(target_type, true, false);
125   }
126
127   bool IsPossibleDynamicType(CompilerType *target_type, // Can pass nullptr
128                              bool check_cplusplus, bool check_objc) const;
129
130   bool IsPointerToScalarType() const;
131
132   bool IsRuntimeGeneratedType() const;
133
134   bool IsPointerType(CompilerType *pointee_type = nullptr) const;
135
136   bool IsPointerOrReferenceType(CompilerType *pointee_type = nullptr) const;
137
138   bool IsReferenceType(CompilerType *pointee_type = nullptr,
139                        bool *is_rvalue = nullptr) const;
140
141   bool ShouldTreatScalarValueAsAddress() const;
142
143   bool IsScalarType() const;
144
145   bool IsTypedefType() const;
146
147   bool IsVoidType() const;
148
149   //----------------------------------------------------------------------
150   // Type Completion
151   //----------------------------------------------------------------------
152
153   bool GetCompleteType() const;
154
155   //----------------------------------------------------------------------
156   // AST related queries
157   //----------------------------------------------------------------------
158
159   size_t GetPointerByteSize() const;
160
161   //----------------------------------------------------------------------
162   // Accessors
163   //----------------------------------------------------------------------
164
165   TypeSystem *GetTypeSystem() const { return m_type_system; }
166
167   ConstString GetConstQualifiedTypeName() const;
168
169   ConstString GetConstTypeName() const;
170
171   ConstString GetTypeName() const;
172
173   ConstString GetDisplayTypeName() const;
174
175   uint32_t
176   GetTypeInfo(CompilerType *pointee_or_element_compiler_type = nullptr) const;
177
178   lldb::LanguageType GetMinimumLanguage();
179
180   lldb::opaque_compiler_type_t GetOpaqueQualType() const { return m_type; }
181
182   lldb::TypeClass GetTypeClass() const;
183
184   void SetCompilerType(TypeSystem *type_system,
185                        lldb::opaque_compiler_type_t type);
186
187   void SetCompilerType(clang::ASTContext *ast, clang::QualType qual_type);
188
189   unsigned GetTypeQualifiers() const;
190
191   //----------------------------------------------------------------------
192   // Creating related types
193   //----------------------------------------------------------------------
194
195   CompilerType GetArrayElementType(uint64_t *stride = nullptr) const;
196
197   CompilerType GetArrayType(uint64_t size) const;
198
199   CompilerType GetCanonicalType() const;
200
201   CompilerType GetFullyUnqualifiedType() const;
202
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;
206
207   CompilerType GetFunctionArgumentTypeAtIndex(size_t idx) const;
208
209   CompilerType GetFunctionReturnType() const;
210
211   size_t GetNumMemberFunctions() const;
212
213   TypeMemberFunctionImpl GetMemberFunctionAtIndex(size_t idx);
214
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
218   // itself.
219   //----------------------------------------------------------------------
220   CompilerType GetNonReferenceType() const;
221
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;
227
228   //----------------------------------------------------------------------
229   // Return a new CompilerType that is a pointer to this type
230   //----------------------------------------------------------------------
231   CompilerType GetPointerType() const;
232
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
236   // an invalid type.
237   //----------------------------------------------------------------------
238   CompilerType GetLValueReferenceType() const;
239
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
243   // an invalid type.
244   //----------------------------------------------------------------------
245   CompilerType GetRValueReferenceType() const;
246
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
250   // invalid type.
251   //----------------------------------------------------------------------
252   CompilerType AddConstModifier() const;
253
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
257   // an invalid type.
258   //----------------------------------------------------------------------
259   CompilerType AddVolatileModifier() const;
260
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
264   // an invalid type.
265   //----------------------------------------------------------------------
266   CompilerType AddRestrictModifier() const;
267
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
271   // invalid type.
272   //----------------------------------------------------------------------
273   CompilerType CreateTypedef(const char *name,
274                              const CompilerDeclContext &decl_ctx) const;
275
276   // If the current object represents a typedef type, get the underlying type
277   CompilerType GetTypedefedType() const;
278
279   //----------------------------------------------------------------------
280   // Create related types using the current type's AST
281   //----------------------------------------------------------------------
282   CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const;
283
284   //----------------------------------------------------------------------
285   // Exploring the type
286   //----------------------------------------------------------------------
287
288   struct IntegralTemplateArgument;
289
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;
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,
302                           const ExecutionContext *exe_ctx) const;
303
304   lldb::BasicType GetBasicTypeEnumeration() const;
305
306   static lldb::BasicType GetBasicTypeEnumeration(const ConstString &name);
307
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
311   // the iteration.
312   //----------------------------------------------------------------------
313   void ForEachEnumerator(
314       std::function<bool(const CompilerType &integer_type,
315                          const ConstString &name,
316                          const llvm::APSInt &value)> const &callback) const;
317
318   uint32_t GetNumFields() const;
319
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;
324
325   uint32_t GetNumDirectBaseClasses() const;
326
327   uint32_t GetNumVirtualBaseClasses() const;
328
329   CompilerType GetDirectBaseClassAtIndex(size_t idx,
330                                          uint32_t *bit_offset_ptr) const;
331
332   CompilerType GetVirtualBaseClassAtIndex(size_t idx,
333                                           uint32_t *bit_offset_ptr) const;
334
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;
340
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;
349
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;
354
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.
361   size_t
362   GetIndexOfChildMemberWithName(const char *name, bool omit_empty_base_classes,
363                                 std::vector<uint32_t> &child_indexes) const;
364
365   size_t GetNumTemplateArguments() const;
366
367   lldb::TemplateArgumentKind GetTemplateArgumentKind(size_t idx) const;
368   CompilerType GetTypeTemplateArgument(size_t idx) const;
369
370   // Returns the value of the template argument and its type.
371   llvm::Optional<IntegralTemplateArgument>
372   GetIntegralTemplateArgument(size_t idx) const;
373
374   CompilerType GetTypeForFormatters() const;
375
376   LazyBool ShouldPrintAsOneLiner(ValueObject *valobj) const;
377
378   bool IsMeaninglessWithoutDynamicResolution() const;
379
380   //------------------------------------------------------------------
381   // Pointers & References
382   //------------------------------------------------------------------
383
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;
388
389   //----------------------------------------------------------------------
390   // Dumping types
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);
397
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);
402
403   void DumpSummary(ExecutionContext *exe_ctx, Stream *s,
404                    const DataExtractor &data, lldb::offset_t data_offset,
405                    size_t data_byte_size);
406
407   void DumpTypeDescription() const; // Dump to stdout
408
409   void DumpTypeDescription(Stream *s) const;
410
411   bool GetValueAsScalar(const DataExtractor &data, lldb::offset_t data_offset,
412                         size_t data_byte_size, Scalar &value) const;
413
414   bool SetValueFromScalar(const Scalar &value, Stream &strm);
415
416   bool ReadFromMemory(ExecutionContext *exe_ctx, lldb::addr_t addr,
417                       AddressType address_type, DataExtractor &data);
418
419   bool WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t addr,
420                      AddressType address_type, StreamString &new_value);
421
422   void Clear() {
423     m_type = nullptr;
424     m_type_system = nullptr;
425   }
426
427 private:
428   lldb::opaque_compiler_type_t m_type;
429   TypeSystem *m_type_system;
430 };
431
432 bool operator==(const CompilerType &lhs, const CompilerType &rhs);
433 bool operator!=(const CompilerType &lhs, const CompilerType &rhs);
434
435 struct CompilerType::IntegralTemplateArgument {
436   llvm::APSInt value;
437   CompilerType type;
438 };
439
440 } // namespace lldb_private
441
442 #endif // liblldb_CompilerType_h_