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