Type.h revision dc0a38c5a727cae5362b218a3180d0f4265a619d
1//===-- Type.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_Type_h_
11#define liblldb_Type_h_
12
13#include "lldb/lldb-private.h"
14#include "lldb/Core/ClangForward.h"
15#include "lldb/Core/ConstString.h"
16#include "lldb/Core/UserID.h"
17#include "lldb/Symbol/ClangASTType.h"
18#include "lldb/Symbol/Declaration.h"
19#include <set>
20
21namespace lldb_private {
22
23class SymbolFileType :
24    public STD_ENABLE_SHARED_FROM_THIS(SymbolFileType),
25    public UserID
26    {
27    public:
28        SymbolFileType (SymbolFile &symbol_file, lldb::user_id_t uid) :
29            UserID (uid),
30            m_symbol_file (symbol_file)
31        {
32        }
33
34        ~SymbolFileType ()
35        {
36        }
37
38        Type *
39        operator->()
40        {
41            return GetType ();
42        }
43
44        Type *
45        GetType ();
46
47    protected:
48        SymbolFile &m_symbol_file;
49        lldb::TypeSP m_type_sp;
50    };
51
52class Type :
53    public STD_ENABLE_SHARED_FROM_THIS(Type),
54    public UserID
55{
56public:
57    typedef enum EncodingDataTypeTag
58    {
59        eEncodingInvalid,
60        eEncodingIsUID,                 ///< This type is the type whose UID is m_encoding_uid
61        eEncodingIsConstUID,            ///< This type is the type whose UID is m_encoding_uid with the const qualifier added
62        eEncodingIsRestrictUID,         ///< This type is the type whose UID is m_encoding_uid with the restrict qualifier added
63        eEncodingIsVolatileUID,         ///< This type is the type whose UID is m_encoding_uid with the volatile qualifier added
64        eEncodingIsTypedefUID,          ///< This type is pointer to a type whose UID is m_encoding_uid
65        eEncodingIsPointerUID,          ///< This type is pointer to a type whose UID is m_encoding_uid
66        eEncodingIsLValueReferenceUID,  ///< This type is L value reference to a type whose UID is m_encoding_uid
67        eEncodingIsRValueReferenceUID,  ///< This type is R value reference to a type whose UID is m_encoding_uid
68        eEncodingIsSyntheticUID
69    } EncodingDataType;
70
71    typedef enum ResolveStateTag
72    {
73        eResolveStateUnresolved = 0,
74        eResolveStateForward    = 1,
75        eResolveStateLayout     = 2,
76        eResolveStateFull       = 3
77    } ResolveState;
78
79    Type (lldb::user_id_t uid,
80          SymbolFile* symbol_file,
81          const ConstString &name,
82          uint32_t byte_size,
83          SymbolContextScope *context,
84          lldb::user_id_t encoding_uid,
85          EncodingDataType encoding_uid_type,
86          const Declaration& decl,
87          lldb::clang_type_t clang_qual_type,
88          ResolveState clang_type_resolve_state);
89
90    // This makes an invalid type.  Used for functions that return a Type when they
91    // get an error.
92    Type();
93
94    Type (const Type &rhs);
95
96    const Type&
97    operator= (const Type& rhs);
98
99    void
100    Dump(Stream *s, bool show_context);
101
102    void
103    DumpTypeName(Stream *s);
104
105
106    void
107    GetDescription (Stream *s, lldb::DescriptionLevel level, bool show_name);
108
109    SymbolFile *
110    GetSymbolFile()
111    {
112        return m_symbol_file;
113    }
114    const SymbolFile *
115    GetSymbolFile() const
116    {
117        return m_symbol_file;
118    }
119
120    TypeList*
121    GetTypeList();
122
123    const ConstString&
124    GetName();
125
126    uint32_t
127    GetByteSize();
128
129    uint32_t
130    GetNumChildren (bool omit_empty_base_classes);
131
132    bool
133    IsAggregateType ();
134
135    bool
136    IsValidType ()
137    {
138        return m_encoding_uid_type != eEncodingInvalid;
139    }
140
141    void
142    SetByteSize(uint32_t byte_size);
143
144    const ConstString &
145    GetName () const
146    {
147        return m_name;
148    }
149
150    ConstString
151    GetQualifiedName ();
152
153    void
154    DumpValue(ExecutionContext *exe_ctx,
155              Stream *s,
156              const DataExtractor &data,
157              uint32_t data_offset,
158              bool show_type,
159              bool show_summary,
160              bool verbose,
161              lldb::Format format = lldb::eFormatDefault);
162
163    bool
164    DumpValueInMemory(ExecutionContext *exe_ctx,
165                      Stream *s,
166                      lldb::addr_t address,
167                      AddressType address_type,
168                      bool show_types,
169                      bool show_summary,
170                      bool verbose);
171
172    bool
173    ReadFromMemory (ExecutionContext *exe_ctx,
174                    lldb::addr_t address,
175                    AddressType address_type,
176                    DataExtractor &data);
177
178    bool
179    WriteToMemory (ExecutionContext *exe_ctx,
180                   lldb::addr_t address,
181                   AddressType address_type,
182                   DataExtractor &data);
183
184    bool
185    GetIsDeclaration() const;
186
187    void
188    SetIsDeclaration(bool b);
189
190    bool
191    GetIsExternal() const;
192
193    void
194    SetIsExternal(bool b);
195
196    lldb::Format
197    GetFormat ();
198
199    lldb::Encoding
200    GetEncoding (uint32_t &count);
201
202    SymbolContextScope *
203    GetSymbolContextScope()
204    {
205        return m_context;
206    }
207    const SymbolContextScope *
208    GetSymbolContextScope() const
209    {
210        return m_context;
211    }
212    void
213    SetSymbolContextScope(SymbolContextScope *context)
214    {
215        m_context = context;
216    }
217
218    const lldb_private::Declaration &
219    GetDeclaration () const;
220
221    // Get the clang type, and resolve definitions for any
222    // class/struct/union/enum types completely.
223    lldb::clang_type_t
224    GetClangFullType ();
225
226    // Get the clang type, and resolve definitions enough so that the type could
227    // have layout performed. This allows ptrs and refs to class/struct/union/enum
228    // types remain forward declarations.
229    lldb::clang_type_t
230    GetClangLayoutType ();
231
232    // Get the clang type and leave class/struct/union/enum types as forward
233    // declarations if they haven't already been fully defined.
234    lldb::clang_type_t
235    GetClangForwardType ();
236
237    clang::ASTContext *
238    GetClangAST ();
239
240    ClangASTContext &
241    GetClangASTContext ();
242
243    static int
244    Compare(const Type &a, const Type &b);
245
246    // From a fully qualified typename, split the type into the type basename
247    // and the remaining type scope (namespaces/classes).
248    static bool
249    GetTypeScopeAndBasename (const char* name_cstr,
250                             std::string &scope,
251                             std::string &basename);
252    void
253    SetEncodingType (Type *encoding_type)
254    {
255        m_encoding_type = encoding_type;
256    }
257
258    uint32_t
259    GetEncodingMask ();
260
261    void *
262    CreateClangPointerType (Type *type);
263
264    void *
265    CreateClangTypedefType (Type *typedef_type, Type *base_type);
266
267    // For C++98 references (&)
268    void *
269    CreateClangLValueReferenceType (Type *type);
270
271    // For C++0x references (&&)
272    void *
273    CreateClangRValueReferenceType (Type *type);
274
275    bool
276    IsRealObjCClass();
277
278    bool
279    IsCompleteObjCClass()
280    {
281        return m_flags.is_complete_objc_class;
282    }
283
284    void
285    SetIsCompleteObjCClass(bool is_complete_objc_class)
286    {
287        m_flags.is_complete_objc_class = is_complete_objc_class;
288    }
289
290protected:
291    ConstString m_name;
292    SymbolFile *m_symbol_file;
293    SymbolContextScope *m_context; // The symbol context in which this type is defined
294    Type *m_encoding_type;
295    uint32_t m_encoding_uid;
296    EncodingDataType m_encoding_uid_type;
297    uint32_t m_byte_size;
298    Declaration m_decl;
299    lldb::clang_type_t m_clang_type;
300
301    struct Flags {
302        ResolveState    clang_type_resolve_state : 2;
303        bool            is_complete_objc_class   : 1;
304    } m_flags;
305
306    Type *
307    GetEncodingType ();
308
309    bool
310    ResolveClangType (ResolveState clang_type_resolve_state);
311};
312
313
314///
315/// Sometimes you can find the name of the type corresponding to an object, but we don't have debug
316/// information for it.  If that is the case, you can return one of these objects, and then if it
317/// has a full type, you can use that, but if not at least you can print the name for informational
318/// purposes.
319///
320
321class TypeAndOrName
322{
323public:
324    TypeAndOrName ();
325    TypeAndOrName (lldb::TypeSP &type_sp);
326    TypeAndOrName (const char *type_str);
327    TypeAndOrName (const TypeAndOrName &rhs);
328    TypeAndOrName (ConstString &type_const_string);
329
330    TypeAndOrName &
331    operator= (const TypeAndOrName &rhs);
332
333    ConstString GetName () const;
334
335    lldb::TypeSP
336    GetTypeSP () const
337    {
338        return m_type_sp;
339    }
340
341    void
342    SetName (ConstString &type_name_const_str);
343
344    void
345    SetName (const char *type_name_str);
346
347    void
348    SetTypeSP (lldb::TypeSP type_sp);
349
350    bool
351    IsEmpty ();
352
353private:
354    lldb::TypeSP m_type_sp;
355    ConstString m_type_name;
356};
357
358// the two classes here are used by the public API as a backend to
359// the SBType and SBTypeList classes
360
361class TypeImpl
362{
363public:
364
365    TypeImpl() :
366        m_clang_ast_type(),
367        m_type_sp()
368    {
369    }
370
371    TypeImpl(const TypeImpl& rhs) :
372        m_clang_ast_type(rhs.m_clang_ast_type),
373        m_type_sp(rhs.m_type_sp)
374    {
375    }
376
377    TypeImpl(const lldb_private::ClangASTType& type);
378
379    TypeImpl(const lldb::TypeSP& type);
380
381    TypeImpl&
382    operator = (const TypeImpl& rhs);
383
384    bool
385    operator == (const TypeImpl& rhs)
386    {
387        return m_clang_ast_type == rhs.m_clang_ast_type && m_type_sp.get() == rhs.m_type_sp.get();
388    }
389
390    bool
391    operator != (const TypeImpl& rhs)
392    {
393        return m_clang_ast_type != rhs.m_clang_ast_type || m_type_sp.get() != rhs.m_type_sp.get();
394    }
395
396    bool
397    IsValid()
398    {
399        return m_type_sp.get() != NULL || m_clang_ast_type.IsValid();
400    }
401
402    const lldb_private::ClangASTType &
403    GetClangASTType() const
404    {
405        return m_clang_ast_type;
406    }
407
408    clang::ASTContext*
409    GetASTContext();
410
411    lldb::clang_type_t
412    GetOpaqueQualType();
413
414    lldb::TypeSP
415    GetTypeSP ()
416    {
417        return m_type_sp;
418    }
419
420    bool
421    GetDescription (lldb_private::Stream &strm,
422                    lldb::DescriptionLevel description_level);
423
424    void
425    SetType (const lldb::TypeSP &type_sp);
426
427private:
428    ClangASTType m_clang_ast_type;
429    lldb::TypeSP m_type_sp;
430};
431
432class TypeListImpl
433{
434public:
435    TypeListImpl() :
436        m_content()
437    {
438    }
439
440    void
441    Append (const lldb::TypeImplSP& type)
442    {
443        m_content.push_back(type);
444    }
445
446    lldb::TypeImplSP
447    GetTypeAtIndex(size_t idx)
448    {
449        lldb::TypeImplSP type_sp;
450        if (idx < GetSize())
451            type_sp = m_content[idx];
452        return type_sp;
453    }
454
455    size_t
456    GetSize()
457    {
458        return m_content.size();
459    }
460
461private:
462    std::vector<lldb::TypeImplSP> m_content;
463};
464
465class TypeMemberImpl
466{
467public:
468    TypeMemberImpl () :
469        m_type_impl_sp (),
470        m_bit_offset (0),
471        m_name ()
472    {
473    }
474
475    TypeMemberImpl (const lldb::TypeImplSP &type_impl_sp,
476                    uint64_t bit_offset,
477                    const ConstString &name) :
478        m_type_impl_sp (type_impl_sp),
479        m_bit_offset (bit_offset),
480        m_name (name)
481    {
482    }
483
484    TypeMemberImpl (const lldb::TypeImplSP &type_impl_sp,
485                    uint64_t bit_offset):
486        m_type_impl_sp (type_impl_sp),
487        m_bit_offset (bit_offset),
488        m_name ()
489    {
490    }
491
492    const lldb::TypeImplSP &
493    GetTypeImpl ()
494    {
495        return m_type_impl_sp;
496    }
497
498    const ConstString &
499    GetName () const
500    {
501        return m_name;
502    }
503
504    uint64_t
505    GetBitOffset () const
506    {
507        return m_bit_offset;
508    }
509
510protected:
511    lldb::TypeImplSP m_type_impl_sp;
512    uint64_t m_bit_offset;
513    ConstString m_name;
514};
515
516
517} // namespace lldb_private
518
519#endif  // liblldb_Type_h_
520
521