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