Type.h revision 1a469c75c0597abc2a9abdf86b624b2e71ea8650
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    bool
142    IsTypedef ()
143    {
144        return m_encoding_uid_type == eEncodingIsTypedefUID;
145    }
146
147    lldb::TypeSP
148    GetTypedefType();
149
150    void
151    SetByteSize(uint32_t byte_size);
152
153    const ConstString &
154    GetName () const
155    {
156        return m_name;
157    }
158
159    ConstString
160    GetQualifiedName ();
161
162    void
163    DumpValue(ExecutionContext *exe_ctx,
164              Stream *s,
165              const DataExtractor &data,
166              uint32_t data_offset,
167              bool show_type,
168              bool show_summary,
169              bool verbose,
170              lldb::Format format = lldb::eFormatDefault);
171
172    bool
173    DumpValueInMemory(ExecutionContext *exe_ctx,
174                      Stream *s,
175                      lldb::addr_t address,
176                      AddressType address_type,
177                      bool show_types,
178                      bool show_summary,
179                      bool verbose);
180
181    bool
182    ReadFromMemory (ExecutionContext *exe_ctx,
183                    lldb::addr_t address,
184                    AddressType address_type,
185                    DataExtractor &data);
186
187    bool
188    WriteToMemory (ExecutionContext *exe_ctx,
189                   lldb::addr_t address,
190                   AddressType address_type,
191                   DataExtractor &data);
192
193    bool
194    GetIsDeclaration() const;
195
196    void
197    SetIsDeclaration(bool b);
198
199    bool
200    GetIsExternal() const;
201
202    void
203    SetIsExternal(bool b);
204
205    lldb::Format
206    GetFormat ();
207
208    lldb::Encoding
209    GetEncoding (uint32_t &count);
210
211    SymbolContextScope *
212    GetSymbolContextScope()
213    {
214        return m_context;
215    }
216    const SymbolContextScope *
217    GetSymbolContextScope() const
218    {
219        return m_context;
220    }
221    void
222    SetSymbolContextScope(SymbolContextScope *context)
223    {
224        m_context = context;
225    }
226
227    const lldb_private::Declaration &
228    GetDeclaration () const;
229
230    // Get the clang type, and resolve definitions for any
231    // class/struct/union/enum types completely.
232    lldb::clang_type_t
233    GetClangFullType ();
234
235    // Get the clang type, and resolve definitions enough so that the type could
236    // have layout performed. This allows ptrs and refs to class/struct/union/enum
237    // types remain forward declarations.
238    lldb::clang_type_t
239    GetClangLayoutType ();
240
241    // Get the clang type and leave class/struct/union/enum types as forward
242    // declarations if they haven't already been fully defined.
243    lldb::clang_type_t
244    GetClangForwardType ();
245
246    clang::ASTContext *
247    GetClangAST ();
248
249    ClangASTContext &
250    GetClangASTContext ();
251
252    static int
253    Compare(const Type &a, const Type &b);
254
255    // From a fully qualified typename, split the type into the type basename
256    // and the remaining type scope (namespaces/classes).
257    static bool
258    GetTypeScopeAndBasename (const char* &name_cstr,
259                             std::string &scope,
260                             std::string &basename,
261                             lldb::TypeClass &type_class);
262    void
263    SetEncodingType (Type *encoding_type)
264    {
265        m_encoding_type = encoding_type;
266    }
267
268    uint32_t
269    GetEncodingMask ();
270
271    void *
272    CreateClangPointerType (Type *type);
273
274    void *
275    CreateClangTypedefType (Type *typedef_type, Type *base_type);
276
277    // For C++98 references (&)
278    void *
279    CreateClangLValueReferenceType (Type *type);
280
281    // For C++0x references (&&)
282    void *
283    CreateClangRValueReferenceType (Type *type);
284
285    bool
286    IsRealObjCClass();
287
288    bool
289    IsCompleteObjCClass()
290    {
291        return m_flags.is_complete_objc_class;
292    }
293
294    void
295    SetIsCompleteObjCClass(bool is_complete_objc_class)
296    {
297        m_flags.is_complete_objc_class = is_complete_objc_class;
298    }
299
300protected:
301    ConstString m_name;
302    SymbolFile *m_symbol_file;
303    SymbolContextScope *m_context; // The symbol context in which this type is defined
304    Type *m_encoding_type;
305    lldb::user_id_t m_encoding_uid;
306    EncodingDataType m_encoding_uid_type;
307    uint32_t m_byte_size;
308    Declaration m_decl;
309    lldb::clang_type_t m_clang_type;
310
311    struct Flags {
312        ResolveState    clang_type_resolve_state : 2;
313        bool            is_complete_objc_class   : 1;
314    } m_flags;
315
316    Type *
317    GetEncodingType ();
318
319    bool
320    ResolveClangType (ResolveState clang_type_resolve_state);
321};
322
323
324///
325/// Sometimes you can find the name of the type corresponding to an object, but we don't have debug
326/// information for it.  If that is the case, you can return one of these objects, and then if it
327/// has a full type, you can use that, but if not at least you can print the name for informational
328/// purposes.
329///
330
331class TypeAndOrName
332{
333public:
334    TypeAndOrName ();
335    TypeAndOrName (lldb::TypeSP &type_sp);
336    TypeAndOrName (const char *type_str);
337    TypeAndOrName (const TypeAndOrName &rhs);
338    TypeAndOrName (ConstString &type_const_string);
339
340    TypeAndOrName &
341    operator= (const TypeAndOrName &rhs);
342
343    bool
344    operator==(const TypeAndOrName &other) const;
345
346    bool
347    operator!=(const TypeAndOrName &other) const;
348
349    ConstString GetName () const;
350
351    lldb::TypeSP
352    GetTypeSP () const
353    {
354        return m_type_sp;
355    }
356
357    void
358    SetName (const ConstString &type_name);
359
360    void
361    SetName (const char *type_name_cstr);
362
363    void
364    SetTypeSP (lldb::TypeSP type_sp);
365
366    bool
367    IsEmpty ();
368
369    bool
370    HasName ();
371
372    bool
373    HasTypeSP ();
374
375    void
376    Clear ();
377
378    operator
379    bool ()
380    {
381        return !IsEmpty();
382    }
383
384private:
385    lldb::TypeSP m_type_sp;
386    ConstString m_type_name;
387};
388
389// the two classes here are used by the public API as a backend to
390// the SBType and SBTypeList classes
391
392class TypeImpl
393{
394public:
395
396    TypeImpl() :
397        m_clang_ast_type(),
398        m_type_sp()
399    {
400    }
401
402    TypeImpl(const TypeImpl& rhs) :
403        m_clang_ast_type(rhs.m_clang_ast_type),
404        m_type_sp(rhs.m_type_sp)
405    {
406    }
407
408    TypeImpl(const lldb_private::ClangASTType& type);
409
410    TypeImpl(const lldb::TypeSP& type);
411
412    TypeImpl&
413    operator = (const TypeImpl& rhs);
414
415    bool
416    operator == (const TypeImpl& rhs)
417    {
418        return m_clang_ast_type == rhs.m_clang_ast_type && m_type_sp.get() == rhs.m_type_sp.get();
419    }
420
421    bool
422    operator != (const TypeImpl& rhs)
423    {
424        return m_clang_ast_type != rhs.m_clang_ast_type || m_type_sp.get() != rhs.m_type_sp.get();
425    }
426
427    bool
428    IsValid()
429    {
430        return m_type_sp.get() != NULL || m_clang_ast_type.IsValid();
431    }
432
433    const lldb_private::ClangASTType &
434    GetClangASTType() const
435    {
436        return m_clang_ast_type;
437    }
438
439    clang::ASTContext*
440    GetASTContext();
441
442    lldb::clang_type_t
443    GetOpaqueQualType();
444
445    lldb::TypeSP
446    GetTypeSP ()
447    {
448        return m_type_sp;
449    }
450
451    bool
452    GetDescription (lldb_private::Stream &strm,
453                    lldb::DescriptionLevel description_level);
454
455    void
456    SetType (const lldb::TypeSP &type_sp);
457
458private:
459    ClangASTType m_clang_ast_type;
460    lldb::TypeSP m_type_sp;
461};
462
463class TypeListImpl
464{
465public:
466    TypeListImpl() :
467        m_content()
468    {
469    }
470
471    void
472    Append (const lldb::TypeImplSP& type)
473    {
474        m_content.push_back(type);
475    }
476
477    lldb::TypeImplSP
478    GetTypeAtIndex(size_t idx)
479    {
480        lldb::TypeImplSP type_sp;
481        if (idx < GetSize())
482            type_sp = m_content[idx];
483        return type_sp;
484    }
485
486    size_t
487    GetSize()
488    {
489        return m_content.size();
490    }
491
492private:
493    std::vector<lldb::TypeImplSP> m_content;
494};
495
496class TypeMemberImpl
497{
498public:
499    TypeMemberImpl () :
500        m_type_impl_sp (),
501        m_bit_offset (0),
502        m_name (),
503        m_bitfield_bit_size (0),
504        m_is_bitfield (false)
505
506    {
507    }
508
509    TypeMemberImpl (const lldb::TypeImplSP &type_impl_sp,
510                    uint64_t bit_offset,
511                    const ConstString &name,
512                    uint32_t bitfield_bit_size = 0,
513                    bool is_bitfield = false) :
514        m_type_impl_sp (type_impl_sp),
515        m_bit_offset (bit_offset),
516        m_name (name),
517        m_bitfield_bit_size (bitfield_bit_size),
518        m_is_bitfield (is_bitfield)
519    {
520    }
521
522    TypeMemberImpl (const lldb::TypeImplSP &type_impl_sp,
523                    uint64_t bit_offset):
524        m_type_impl_sp (type_impl_sp),
525        m_bit_offset (bit_offset),
526        m_name (),
527        m_bitfield_bit_size (0),
528        m_is_bitfield (false)
529    {
530    }
531
532    const lldb::TypeImplSP &
533    GetTypeImpl ()
534    {
535        return m_type_impl_sp;
536    }
537
538    const ConstString &
539    GetName () const
540    {
541        return m_name;
542    }
543
544    uint64_t
545    GetBitOffset () const
546    {
547        return m_bit_offset;
548    }
549
550    uint32_t
551    GetBitfieldBitSize () const
552    {
553        return m_bitfield_bit_size;
554    }
555
556    void
557    SetBitfieldBitSize (uint32_t bitfield_bit_size)
558    {
559        m_bitfield_bit_size = bitfield_bit_size;
560    }
561
562    bool
563    GetIsBitfield () const
564    {
565        return m_is_bitfield;
566    }
567
568    void
569    SetIsBitfield (bool is_bitfield)
570    {
571        m_is_bitfield = is_bitfield;
572    }
573
574protected:
575    lldb::TypeImplSP m_type_impl_sp;
576    uint64_t m_bit_offset;
577    ConstString m_name;
578    uint32_t m_bitfield_bit_size; // Bit size for bitfield members only
579    bool m_is_bitfield;
580};
581
582
583} // namespace lldb_private
584
585#endif  // liblldb_Type_h_
586
587