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 // these classes are used to back the SBType* objects
308 
309 class TypePair {
310 private:
311     ClangASTType clang_type;
312     lldb::TypeSP type_sp;
313 
314 public:
TypePair()315     TypePair () : clang_type(), type_sp() {}
TypePair(ClangASTType type)316     TypePair (ClangASTType type) :
317     clang_type(type),
318     type_sp()
319     {
320     }
321 
TypePair(lldb::TypeSP type)322     TypePair (lldb::TypeSP type) :
323     clang_type(),
324     type_sp(type)
325     {
326         clang_type = type_sp->GetClangForwardType();
327     }
328 
329     bool
IsValid()330     IsValid () const
331     {
332         return clang_type.IsValid() || (type_sp.get() != nullptr);
333     }
334 
335     explicit operator bool () const
336     {
337         return IsValid();
338     }
339 
340     bool
341     operator == (const TypePair& rhs) const
342     {
343         return clang_type == rhs.clang_type &&
344         type_sp.get() == rhs.type_sp.get();
345     }
346 
347     bool
348     operator != (const TypePair& rhs) const
349     {
350         return clang_type != rhs.clang_type ||
351         type_sp.get() != rhs.type_sp.get();
352     }
353 
354     void
Clear()355     Clear ()
356     {
357         clang_type.Clear();
358         type_sp.reset();
359     }
360 
361     ConstString
GetName()362     GetName () const
363     {
364         if (type_sp)
365             return type_sp->GetName();
366         if (clang_type)
367             return clang_type.GetTypeName();
368         return ConstString ();
369     }
370 
371     void
SetType(ClangASTType type)372     SetType (ClangASTType type)
373     {
374         type_sp.reset();
375         clang_type = type;
376     }
377 
378     void
SetType(lldb::TypeSP type)379     SetType (lldb::TypeSP type)
380     {
381         type_sp = type;
382         clang_type = type_sp->GetClangForwardType();
383     }
384 
385     lldb::TypeSP
GetTypeSP()386     GetTypeSP () const
387     {
388         return type_sp;
389     }
390 
391     ClangASTType
GetClangASTType()392     GetClangASTType () const
393     {
394         return clang_type;
395     }
396 
397     ClangASTType
GetPointerType()398     GetPointerType () const
399     {
400         if (type_sp)
401             return type_sp->GetClangLayoutType().GetPointerType();
402         return clang_type.GetPointerType();
403     }
404 
405     ClangASTType
GetPointeeType()406     GetPointeeType () const
407     {
408         if (type_sp)
409             return type_sp->GetClangFullType().GetPointeeType();
410         return clang_type.GetPointeeType();
411     }
412 
413     ClangASTType
GetReferenceType()414     GetReferenceType () const
415     {
416         if (type_sp)
417             return type_sp->GetClangLayoutType().GetLValueReferenceType();
418         return clang_type.GetLValueReferenceType();
419     }
420 
421     ClangASTType
GetTypedefedType()422     GetTypedefedType () const
423     {
424         if (type_sp)
425             return type_sp->GetClangFullType().GetTypedefedType();
426         return clang_type.GetTypedefedType();
427     }
428 
429     ClangASTType
GetDereferencedType()430     GetDereferencedType () const
431     {
432         if (type_sp)
433             return type_sp->GetClangFullType().GetNonReferenceType();
434         return clang_type.GetNonReferenceType();
435     }
436 
437     ClangASTType
GetUnqualifiedType()438     GetUnqualifiedType () const
439     {
440         if (type_sp)
441             return type_sp->GetClangLayoutType().GetFullyUnqualifiedType();
442         return clang_type.GetFullyUnqualifiedType();
443     }
444 
445     ClangASTType
GetCanonicalType()446     GetCanonicalType () const
447     {
448         if (type_sp)
449             return type_sp->GetClangFullType().GetCanonicalType();
450         return clang_type.GetCanonicalType();
451     }
452 
453     clang::ASTContext *
GetClangASTContext()454     GetClangASTContext () const
455     {
456         return clang_type.GetASTContext();
457     }
458 };
459 
460 class TypeImpl
461 {
462 public:
463 
464     TypeImpl();
465 
~TypeImpl()466     ~TypeImpl () {}
467 
468     TypeImpl(const TypeImpl& rhs);
469 
470     TypeImpl (lldb::TypeSP type_sp);
471 
472     TypeImpl (ClangASTType clang_type);
473 
474     TypeImpl (lldb::TypeSP type_sp, ClangASTType dynamic);
475 
476     TypeImpl (ClangASTType clang_type, ClangASTType dynamic);
477 
478     TypeImpl (TypePair pair, ClangASTType dynamic);
479 
480     void
481     SetType (lldb::TypeSP type_sp);
482 
483     void
484     SetType (ClangASTType clang_type);
485 
486     void
487     SetType (lldb::TypeSP type_sp, ClangASTType dynamic);
488 
489     void
490     SetType (ClangASTType clang_type, ClangASTType dynamic);
491 
492     void
493     SetType (TypePair pair, ClangASTType dynamic);
494 
495     TypeImpl&
496     operator = (const TypeImpl& rhs);
497 
498     bool
499     operator == (const TypeImpl& rhs) const;
500 
501     bool
502     operator != (const TypeImpl& rhs) const;
503 
504     bool
505     IsValid() const;
506 
507     explicit operator bool () const;
508 
509     void Clear();
510 
511     ConstString
512     GetName ()  const;
513 
514     TypeImpl
515     GetPointerType () const;
516 
517     TypeImpl
518     GetPointeeType () const;
519 
520     TypeImpl
521     GetReferenceType () const;
522 
523     TypeImpl
524     GetTypedefedType () const;
525 
526     TypeImpl
527     GetDereferencedType () const;
528 
529     TypeImpl
530     GetUnqualifiedType() const;
531 
532     TypeImpl
533     GetCanonicalType() const;
534 
535     ClangASTType
536     GetClangASTType (bool prefer_dynamic);
537 
538     clang::ASTContext *
539     GetClangASTContext (bool prefer_dynamic);
540 
541     bool
542     GetDescription (lldb_private::Stream &strm,
543                     lldb::DescriptionLevel description_level);
544 
545 private:
546     TypePair m_static_type;
547     ClangASTType m_dynamic_type;
548 };
549 
550 class TypeListImpl
551 {
552 public:
TypeListImpl()553     TypeListImpl() :
554         m_content()
555     {
556     }
557 
558     void
Append(const lldb::TypeImplSP & type)559     Append (const lldb::TypeImplSP& type)
560     {
561         m_content.push_back(type);
562     }
563 
564     class AppendVisitor
565     {
566     public:
AppendVisitor(TypeListImpl & type_list)567         AppendVisitor(TypeListImpl &type_list) :
568             m_type_list(type_list)
569         {
570         }
571 
572         void
operator()573         operator() (const lldb::TypeImplSP& type)
574         {
575             m_type_list.Append(type);
576         }
577 
578     private:
579         TypeListImpl &m_type_list;
580     };
581 
582     void
583     Append (const lldb_private::TypeList &type_list);
584 
585     lldb::TypeImplSP
GetTypeAtIndex(size_t idx)586     GetTypeAtIndex(size_t idx)
587     {
588         lldb::TypeImplSP type_sp;
589         if (idx < GetSize())
590             type_sp = m_content[idx];
591         return type_sp;
592     }
593 
594     size_t
GetSize()595     GetSize()
596     {
597         return m_content.size();
598     }
599 
600 private:
601     std::vector<lldb::TypeImplSP> m_content;
602 };
603 
604 class TypeMemberImpl
605 {
606 public:
TypeMemberImpl()607     TypeMemberImpl () :
608         m_type_impl_sp (),
609         m_bit_offset (0),
610         m_name (),
611         m_bitfield_bit_size (0),
612         m_is_bitfield (false)
613 
614     {
615     }
616 
617     TypeMemberImpl (const lldb::TypeImplSP &type_impl_sp,
618                     uint64_t bit_offset,
619                     const ConstString &name,
620                     uint32_t bitfield_bit_size = 0,
621                     bool is_bitfield = false) :
m_type_impl_sp(type_impl_sp)622         m_type_impl_sp (type_impl_sp),
623         m_bit_offset (bit_offset),
624         m_name (name),
625         m_bitfield_bit_size (bitfield_bit_size),
626         m_is_bitfield (is_bitfield)
627     {
628     }
629 
TypeMemberImpl(const lldb::TypeImplSP & type_impl_sp,uint64_t bit_offset)630     TypeMemberImpl (const lldb::TypeImplSP &type_impl_sp,
631                     uint64_t bit_offset):
632         m_type_impl_sp (type_impl_sp),
633         m_bit_offset (bit_offset),
634         m_name (),
635         m_bitfield_bit_size (0),
636         m_is_bitfield (false)
637     {
638         if (m_type_impl_sp)
639             m_name = m_type_impl_sp->GetName();
640     }
641 
642     const lldb::TypeImplSP &
GetTypeImpl()643     GetTypeImpl ()
644     {
645         return m_type_impl_sp;
646     }
647 
648     const ConstString &
GetName()649     GetName () const
650     {
651         return m_name;
652     }
653 
654     uint64_t
GetBitOffset()655     GetBitOffset () const
656     {
657         return m_bit_offset;
658     }
659 
660     uint32_t
GetBitfieldBitSize()661     GetBitfieldBitSize () const
662     {
663         return m_bitfield_bit_size;
664     }
665 
666     void
SetBitfieldBitSize(uint32_t bitfield_bit_size)667     SetBitfieldBitSize (uint32_t bitfield_bit_size)
668     {
669         m_bitfield_bit_size = bitfield_bit_size;
670     }
671 
672     bool
GetIsBitfield()673     GetIsBitfield () const
674     {
675         return m_is_bitfield;
676     }
677 
678     void
SetIsBitfield(bool is_bitfield)679     SetIsBitfield (bool is_bitfield)
680     {
681         m_is_bitfield = is_bitfield;
682     }
683 
684 protected:
685     lldb::TypeImplSP m_type_impl_sp;
686     uint64_t m_bit_offset;
687     ConstString m_name;
688     uint32_t m_bitfield_bit_size; // Bit size for bitfield members only
689     bool m_is_bitfield;
690 };
691 
692 
693 ///
694 /// Sometimes you can find the name of the type corresponding to an object, but we don't have debug
695 /// information for it.  If that is the case, you can return one of these objects, and then if it
696 /// has a full type, you can use that, but if not at least you can print the name for informational
697 /// purposes.
698 ///
699 
700 class TypeAndOrName
701 {
702 public:
703     TypeAndOrName ();
704     TypeAndOrName (lldb::TypeSP &type_sp);
705     TypeAndOrName (const ClangASTType &clang_type);
706     TypeAndOrName (const char *type_str);
707     TypeAndOrName (const TypeAndOrName &rhs);
708     TypeAndOrName (ConstString &type_const_string);
709 
710     TypeAndOrName &
711     operator= (const TypeAndOrName &rhs);
712 
713     bool
714     operator==(const TypeAndOrName &other) const;
715 
716     bool
717     operator!=(const TypeAndOrName &other) const;
718 
719     ConstString GetName () const;
720 
721     lldb::TypeSP
GetTypeSP()722     GetTypeSP () const
723     {
724         return m_type_pair.GetTypeSP();
725     }
726 
727     ClangASTType
GetClangASTType()728     GetClangASTType () const
729     {
730         return m_type_pair.GetClangASTType();
731     }
732 
733     void
734     SetName (const ConstString &type_name);
735 
736     void
737     SetName (const char *type_name_cstr);
738 
739     void
740     SetTypeSP (lldb::TypeSP type_sp);
741 
742     void
743     SetClangASTType (ClangASTType clang_type);
744 
745     bool
746     IsEmpty () const;
747 
748     bool
749     HasName () const;
750 
751     bool
752     HasTypeSP () const;
753 
754     bool
755     HasClangASTType () const;
756 
757     bool
HasType()758     HasType () const
759     {
760         return HasTypeSP() || HasClangASTType();
761     }
762 
763     void
764     Clear ();
765 
766     explicit operator bool ()
767     {
768         return !IsEmpty();
769     }
770 
771 private:
772     TypePair m_type_pair;
773     ConstString m_type_name;
774 };
775 
776 } // namespace lldb_private
777 
778 #endif  // liblldb_Type_h_
779 
780