CompilerType.h revision 309124
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/lldb-private.h"
22#include "lldb/Core/ClangForward.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
37{
38public:
39    //----------------------------------------------------------------------
40    // Constructors and Destructors
41    //----------------------------------------------------------------------
42    CompilerType (TypeSystem *type_system, lldb::opaque_compiler_type_t type);
43    CompilerType (clang::ASTContext *ast_context, clang::QualType qual_type);
44
45    CompilerType (const CompilerType &rhs) :
46        m_type (rhs.m_type),
47        m_type_system  (rhs.m_type_system)
48    {
49    }
50
51    CompilerType () :
52        m_type (nullptr),
53        m_type_system (nullptr)
54    {
55    }
56
57    ~CompilerType();
58
59    //----------------------------------------------------------------------
60    // Operators
61    //----------------------------------------------------------------------
62
63    const CompilerType &
64    operator= (const CompilerType &rhs)
65    {
66        m_type = rhs.m_type;
67        m_type_system = rhs.m_type_system;
68        return *this;
69    }
70
71    //----------------------------------------------------------------------
72    // Tests
73    //----------------------------------------------------------------------
74
75    explicit operator bool () const
76    {
77        return m_type != nullptr && m_type_system != nullptr;
78    }
79
80    bool
81    operator < (const CompilerType &rhs) const
82    {
83        if (m_type_system == rhs.m_type_system)
84            return m_type < rhs.m_type;
85        return m_type_system < rhs.m_type_system;
86    }
87
88    bool
89    IsValid () const
90    {
91        return m_type != nullptr && m_type_system != nullptr;
92    }
93
94    bool
95    IsArrayType (CompilerType *element_type,
96                 uint64_t *size,
97                 bool *is_incomplete) const;
98
99    bool
100    IsVectorType (CompilerType *element_type,
101                  uint64_t *size) const;
102
103    bool
104    IsArrayOfScalarType () const;
105
106    bool
107    IsAggregateType () const;
108
109    bool
110    IsAnonymousType () const;
111
112    bool
113    IsBeingDefined () const;
114
115    bool
116    IsCharType () const;
117
118    bool
119    IsCompleteType () const;
120
121    bool
122    IsConst() const;
123
124    bool
125    IsCStringType (uint32_t &length) const;
126
127    bool
128    IsDefined() const;
129
130    bool
131    IsFloatingPointType (uint32_t &count, bool &is_complex) const;
132
133    bool
134    IsFunctionType(bool *is_variadic_ptr = nullptr) const;
135
136    uint32_t
137    IsHomogeneousAggregate (CompilerType* base_type_ptr) const;
138
139    size_t
140    GetNumberOfFunctionArguments () const;
141
142    CompilerType
143    GetFunctionArgumentAtIndex (const size_t index) const;
144
145    bool
146    IsVariadicFunctionType () const;
147
148    bool
149    IsFunctionPointerType () const;
150
151    bool
152    IsBlockPointerType (CompilerType *function_pointer_type_ptr) const;
153
154    bool
155    IsIntegerType (bool &is_signed) const;
156
157    bool
158    IsEnumerationType (bool &is_signed) const;
159
160    bool
161    IsIntegerOrEnumerationType (bool &is_signed) const;
162
163    bool
164    IsPolymorphicClass () const;
165
166    bool
167    IsPossibleCPlusPlusDynamicType(CompilerType *target_type = nullptr) const
168    {
169        return IsPossibleDynamicType (target_type, true, false);
170    }
171
172    bool
173    IsPossibleDynamicType(CompilerType *target_type, // Can pass nullptr
174                          bool check_cplusplus,
175                          bool check_objc) const;
176
177    bool
178    IsPointerToScalarType () const;
179
180    bool
181    IsRuntimeGeneratedType () const;
182
183    bool
184    IsPointerType(CompilerType *pointee_type = nullptr) const;
185
186    bool
187    IsPointerOrReferenceType(CompilerType *pointee_type = nullptr) const;
188
189    bool
190    IsReferenceType(CompilerType *pointee_type = nullptr, bool* is_rvalue = nullptr) const;
191
192    bool
193    ShouldTreatScalarValueAsAddress () const;
194
195    bool
196    IsScalarType () const;
197
198    bool
199    IsTypedefType () const;
200
201    bool
202    IsVoidType () const;
203
204    //----------------------------------------------------------------------
205    // Type Completion
206    //----------------------------------------------------------------------
207
208    bool
209    GetCompleteType () const;
210
211    //----------------------------------------------------------------------
212    // AST related queries
213    //----------------------------------------------------------------------
214
215    size_t
216    GetPointerByteSize () const;
217
218    //----------------------------------------------------------------------
219    // Accessors
220    //----------------------------------------------------------------------
221
222    TypeSystem *
223    GetTypeSystem() const
224    {
225        return m_type_system;
226    }
227
228    ConstString
229    GetConstQualifiedTypeName () const;
230
231    ConstString
232    GetConstTypeName () const;
233
234    ConstString
235    GetTypeName () const;
236
237    ConstString
238    GetDisplayTypeName () const;
239
240    uint32_t
241    GetTypeInfo(CompilerType *pointee_or_element_compiler_type = nullptr) const;
242
243    lldb::LanguageType
244    GetMinimumLanguage ();
245
246    lldb::opaque_compiler_type_t
247    GetOpaqueQualType() const
248    {
249        return m_type;
250    }
251
252    lldb::TypeClass
253    GetTypeClass () const;
254
255    void
256    SetCompilerType (TypeSystem* type_system, lldb::opaque_compiler_type_t type);
257
258    void
259    SetCompilerType (clang::ASTContext *ast, clang::QualType qual_type);
260
261    unsigned
262    GetTypeQualifiers() const;
263
264    //----------------------------------------------------------------------
265    // Creating related types
266    //----------------------------------------------------------------------
267
268    CompilerType
269    GetArrayElementType(uint64_t *stride = nullptr) const;
270
271    CompilerType
272    GetCanonicalType () const;
273
274    CompilerType
275    GetFullyUnqualifiedType () const;
276
277    // Returns -1 if this isn't a function of if the function doesn't have a prototype
278    // Returns a value >= 0 if there is a prototype.
279    int
280    GetFunctionArgumentCount () const;
281
282    CompilerType
283    GetFunctionArgumentTypeAtIndex (size_t idx) const;
284
285    CompilerType
286    GetFunctionReturnType () const;
287
288    size_t
289    GetNumMemberFunctions () const;
290
291    TypeMemberFunctionImpl
292    GetMemberFunctionAtIndex (size_t idx);
293
294    //----------------------------------------------------------------------
295    // If this type is a reference to a type (L value or R value reference),
296    // return a new type with the reference removed, else return the current
297    // type itself.
298    //----------------------------------------------------------------------
299    CompilerType
300    GetNonReferenceType () const;
301
302    //----------------------------------------------------------------------
303    // If this type is a pointer type, return the type that the pointer
304    // points to, else return an invalid type.
305    //----------------------------------------------------------------------
306    CompilerType
307    GetPointeeType () const;
308
309    //----------------------------------------------------------------------
310    // Return a new CompilerType that is a pointer to this type
311    //----------------------------------------------------------------------
312    CompilerType
313    GetPointerType () const;
314
315    //----------------------------------------------------------------------
316    // Return a new CompilerType that is a L value reference to this type if
317    // this type is valid and the type system supports L value references,
318    // else return an invalid type.
319    //----------------------------------------------------------------------
320    CompilerType
321    GetLValueReferenceType () const;
322
323    //----------------------------------------------------------------------
324    // Return a new CompilerType that is a R value reference to this type if
325    // this type is valid and the type system supports R value references,
326    // else return an invalid type.
327    //----------------------------------------------------------------------
328    CompilerType
329    GetRValueReferenceType () const;
330
331    //----------------------------------------------------------------------
332    // Return a new CompilerType adds a const modifier to this type if
333    // this type is valid and the type system supports const modifiers,
334    // else return an invalid type.
335    //----------------------------------------------------------------------
336    CompilerType
337    AddConstModifier () const;
338
339    //----------------------------------------------------------------------
340    // Return a new CompilerType adds a volatile modifier to this type if
341    // this type is valid and the type system supports volatile modifiers,
342    // else return an invalid type.
343    //----------------------------------------------------------------------
344    CompilerType
345    AddVolatileModifier () const;
346
347    //----------------------------------------------------------------------
348    // Return a new CompilerType adds a restrict modifier to this type if
349    // this type is valid and the type system supports restrict modifiers,
350    // else return an invalid type.
351    //----------------------------------------------------------------------
352    CompilerType
353    AddRestrictModifier () const;
354
355    //----------------------------------------------------------------------
356    // Create a typedef to this type using "name" as the name of the typedef
357    // this type is valid and the type system supports typedefs, else return
358    // an invalid type.
359    //----------------------------------------------------------------------
360    CompilerType
361    CreateTypedef (const char *name, const CompilerDeclContext &decl_ctx) const;
362
363    // If the current object represents a typedef type, get the underlying type
364    CompilerType
365    GetTypedefedType () const;
366
367    //----------------------------------------------------------------------
368    // Create related types using the current type's AST
369    //----------------------------------------------------------------------
370    CompilerType
371    GetBasicTypeFromAST (lldb::BasicType basic_type) const;
372
373    //----------------------------------------------------------------------
374    // Exploring the type
375    //----------------------------------------------------------------------
376
377    uint64_t
378    GetByteSize (ExecutionContextScope *exe_scope) const;
379
380    uint64_t
381    GetBitSize (ExecutionContextScope *exe_scope) const;
382
383    lldb::Encoding
384    GetEncoding (uint64_t &count) const;
385
386    lldb::Format
387    GetFormat () const;
388
389    size_t
390    GetTypeBitAlign () const;
391
392    uint32_t
393    GetNumChildren (bool omit_empty_base_classes) const;
394
395    lldb::BasicType
396    GetBasicTypeEnumeration () const;
397
398    static lldb::BasicType
399    GetBasicTypeEnumeration (const ConstString &name);
400
401    //----------------------------------------------------------------------
402    // If this type is an enumeration, iterate through all of its enumerators
403    // using a callback. If the callback returns true, keep iterating, else
404    // abort the iteration.
405    //----------------------------------------------------------------------
406    void
407    ForEachEnumerator (std::function <bool (const CompilerType &integer_type,
408                                            const ConstString &name,
409                                            const llvm::APSInt &value)> const &callback) const;
410
411    uint32_t
412    GetNumFields () const;
413
414    CompilerType
415    GetFieldAtIndex (size_t idx,
416                     std::string& name,
417                     uint64_t *bit_offset_ptr,
418                     uint32_t *bitfield_bit_size_ptr,
419                     bool *is_bitfield_ptr) const;
420
421    uint32_t
422    GetNumDirectBaseClasses () const;
423
424    uint32_t
425    GetNumVirtualBaseClasses () const;
426
427    CompilerType
428    GetDirectBaseClassAtIndex (size_t idx,
429                               uint32_t *bit_offset_ptr) const;
430
431    CompilerType
432    GetVirtualBaseClassAtIndex (size_t idx,
433                                uint32_t *bit_offset_ptr) const;
434
435    uint32_t
436    GetIndexOfFieldWithName(const char* name,
437                            CompilerType* field_compiler_type = nullptr,
438                            uint64_t *bit_offset_ptr = nullptr,
439                            uint32_t *bitfield_bit_size_ptr = nullptr,
440                            bool *is_bitfield_ptr = nullptr) const;
441
442    CompilerType
443    GetChildCompilerTypeAtIndex (ExecutionContext *exe_ctx,
444                                 size_t idx,
445                                 bool transparent_pointers,
446                                 bool omit_empty_base_classes,
447                                 bool ignore_array_bounds,
448                                 std::string& child_name,
449                                 uint32_t &child_byte_size,
450                                 int32_t &child_byte_offset,
451                                 uint32_t &child_bitfield_bit_size,
452                                 uint32_t &child_bitfield_bit_offset,
453                                 bool &child_is_base_class,
454                                 bool &child_is_deref_of_parent,
455                                 ValueObject *valobj,
456                                 uint64_t &language_flags) const;
457
458    // Lookup a child given a name. This function will match base class names
459    // and member member names in "clang_type" only, not descendants.
460    uint32_t
461    GetIndexOfChildWithName (const char *name,
462                             bool omit_empty_base_classes) const;
463
464    // Lookup a child member given a name. This function will match member names
465    // only and will descend into "clang_type" children in search for the first
466    // member in this class, or any base class that matches "name".
467    // TODO: Return all matches for a given name by returning a vector<vector<uint32_t>>
468    // so we catch all names that match a given child name, not just the first.
469    size_t
470    GetIndexOfChildMemberWithName (const char *name,
471                                   bool omit_empty_base_classes,
472                                   std::vector<uint32_t>& child_indexes) const;
473
474    size_t
475    GetNumTemplateArguments () const;
476
477    CompilerType
478    GetTemplateArgument (size_t idx,
479                         lldb::TemplateArgumentKind &kind) const;
480
481    CompilerType
482    GetTypeForFormatters () const;
483
484    LazyBool
485    ShouldPrintAsOneLiner (ValueObject* valobj) const;
486
487    bool
488    IsMeaninglessWithoutDynamicResolution () const;
489
490    //------------------------------------------------------------------
491    // Pointers & References
492    //------------------------------------------------------------------
493
494    // Converts "s" to a floating point value and place resulting floating
495    // point bytes in the "dst" buffer.
496    size_t
497    ConvertStringToFloatValue (const char *s,
498                               uint8_t *dst,
499                               size_t dst_size) const;
500
501    //----------------------------------------------------------------------
502    // Dumping types
503    //----------------------------------------------------------------------
504    void
505    DumpValue (ExecutionContext *exe_ctx,
506               Stream *s,
507               lldb::Format format,
508               const DataExtractor &data,
509               lldb::offset_t data_offset,
510               size_t data_byte_size,
511               uint32_t bitfield_bit_size,
512               uint32_t bitfield_bit_offset,
513               bool show_types,
514               bool show_summary,
515               bool verbose,
516               uint32_t depth);
517
518    bool
519    DumpTypeValue (Stream *s,
520                   lldb::Format format,
521                   const DataExtractor &data,
522                   lldb::offset_t data_offset,
523                   size_t data_byte_size,
524                   uint32_t bitfield_bit_size,
525                   uint32_t bitfield_bit_offset,
526                   ExecutionContextScope *exe_scope);
527
528    void
529    DumpSummary (ExecutionContext *exe_ctx,
530                 Stream *s,
531                 const DataExtractor &data,
532                 lldb::offset_t data_offset,
533                 size_t data_byte_size);
534
535    void
536    DumpTypeDescription () const; // Dump to stdout
537
538    void
539    DumpTypeDescription (Stream *s) const;
540
541    bool
542    GetValueAsScalar (const DataExtractor &data,
543                      lldb::offset_t data_offset,
544                      size_t data_byte_size,
545                      Scalar &value) const;
546
547    bool
548    SetValueFromScalar (const Scalar &value,
549                        Stream &strm);
550
551    bool
552    ReadFromMemory (ExecutionContext *exe_ctx,
553                    lldb::addr_t addr,
554                    AddressType address_type,
555                    DataExtractor &data);
556
557    bool
558    WriteToMemory (ExecutionContext *exe_ctx,
559                   lldb::addr_t addr,
560                   AddressType address_type,
561                   StreamString &new_value);
562
563    void
564    Clear()
565    {
566        m_type = nullptr;
567        m_type_system = nullptr;
568    }
569
570private:
571    lldb::opaque_compiler_type_t m_type;
572    TypeSystem *m_type_system;
573};
574
575bool operator == (const CompilerType &lhs, const CompilerType &rhs);
576bool operator != (const CompilerType &lhs, const CompilerType &rhs);
577
578} // namespace lldb_private
579
580#endif // liblldb_CompilerType_h_
581