1 //===-- Type.cpp ------------------------------------------------*- 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 // Other libraries and framework includes
11 
12 #include "lldb/Core/DataExtractor.h"
13 #include "lldb/Core/DataBufferHeap.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/Scalar.h"
16 #include "lldb/Core/StreamString.h"
17 
18 #include "lldb/Symbol/CompilerType.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Symbol/SymbolContextScope.h"
21 #include "lldb/Symbol/SymbolFile.h"
22 #include "lldb/Symbol/SymbolVendor.h"
23 #include "lldb/Symbol/Type.h"
24 #include "lldb/Symbol/TypeList.h"
25 #include "lldb/Symbol/TypeSystem.h"
26 
27 #include "lldb/Target/ExecutionContext.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/Target.h"
30 
31 #include "llvm/ADT/StringRef.h"
32 
33 #include "clang/AST/Decl.h"
34 #include "clang/AST/DeclObjC.h"
35 
36 using namespace lldb;
37 using namespace lldb_private;
38 
39 class TypeAppendVisitor
40 {
41 public:
42     TypeAppendVisitor(TypeListImpl &type_list) :
43         m_type_list(type_list)
44     {
45     }
46 
47     bool
48     operator() (const lldb::TypeSP& type)
49     {
50         m_type_list.Append(TypeImplSP(new TypeImpl(type)));
51         return true;
52     }
53 
54 private:
55     TypeListImpl &m_type_list;
56 };
57 
58 void
59 TypeListImpl::Append (const lldb_private::TypeList &type_list)
60 {
61     TypeAppendVisitor cb(*this);
62     type_list.ForEach(cb);
63 }
64 
65 
66 Type *
67 SymbolFileType::GetType ()
68 {
69     if (!m_type_sp)
70     {
71         Type *resolved_type = m_symbol_file.ResolveTypeUID (GetID());
72         if (resolved_type)
73             m_type_sp = resolved_type->shared_from_this();
74     }
75     return m_type_sp.get();
76 }
77 
78 
79 Type::Type
80 (
81     lldb::user_id_t uid,
82     SymbolFile* symbol_file,
83     const ConstString &name,
84     uint64_t byte_size,
85     SymbolContextScope *context,
86     user_id_t encoding_uid,
87     EncodingDataType encoding_uid_type,
88     const Declaration& decl,
89     const CompilerType &compiler_type,
90     ResolveState compiler_type_resolve_state
91 ) :
92     std::enable_shared_from_this<Type> (),
93     UserID (uid),
94     m_name (name),
95     m_symbol_file (symbol_file),
96     m_context (context),
97     m_encoding_type (nullptr),
98     m_encoding_uid (encoding_uid),
99     m_encoding_uid_type (encoding_uid_type),
100     m_byte_size (byte_size),
101     m_decl (decl),
102     m_compiler_type (compiler_type)
103 {
104     m_flags.compiler_type_resolve_state = (compiler_type ? compiler_type_resolve_state : eResolveStateUnresolved);
105     m_flags.is_complete_objc_class = false;
106 }
107 
108 Type::Type () :
109     std::enable_shared_from_this<Type> (),
110     UserID (0),
111     m_name ("<INVALID TYPE>"),
112     m_symbol_file (nullptr),
113     m_context (nullptr),
114     m_encoding_type (nullptr),
115     m_encoding_uid (LLDB_INVALID_UID),
116     m_encoding_uid_type (eEncodingInvalid),
117     m_byte_size (0),
118     m_decl (),
119     m_compiler_type ()
120 {
121     m_flags.compiler_type_resolve_state = eResolveStateUnresolved;
122     m_flags.is_complete_objc_class = false;
123 }
124 
125 
126 Type::Type (const Type &rhs) :
127     std::enable_shared_from_this<Type> (rhs),
128     UserID (rhs),
129     m_name (rhs.m_name),
130     m_symbol_file (rhs.m_symbol_file),
131     m_context (rhs.m_context),
132     m_encoding_type (rhs.m_encoding_type),
133     m_encoding_uid (rhs.m_encoding_uid),
134     m_encoding_uid_type (rhs.m_encoding_uid_type),
135     m_byte_size (rhs.m_byte_size),
136     m_decl (rhs.m_decl),
137     m_compiler_type (rhs.m_compiler_type),
138     m_flags (rhs.m_flags)
139 {
140 }
141 
142 const Type&
143 Type::operator= (const Type& rhs)
144 {
145     if (this != &rhs)
146     {
147     }
148     return *this;
149 }
150 
151 
152 void
153 Type::GetDescription (Stream *s, lldb::DescriptionLevel level, bool show_name)
154 {
155     *s << "id = " << (const UserID&)*this;
156 
157     // Call the name accessor to make sure we resolve the type name
158     if (show_name)
159     {
160         const ConstString &type_name = GetName();
161         if (type_name)
162         {
163             *s << ", name = \"" << type_name << '"';
164             ConstString qualified_type_name (GetQualifiedName());
165             if (qualified_type_name != type_name)
166             {
167                 *s << ", qualified = \"" << qualified_type_name << '"';
168             }
169         }
170     }
171 
172     // Call the get byte size accesor so we resolve our byte size
173     if (GetByteSize())
174         s->Printf(", byte-size = %" PRIu64, m_byte_size);
175     bool show_fullpaths = (level == lldb::eDescriptionLevelVerbose);
176     m_decl.Dump(s, show_fullpaths);
177 
178     if (m_compiler_type.IsValid())
179     {
180         *s << ", compiler_type = \"";
181         GetForwardCompilerType ().DumpTypeDescription(s);
182         *s << '"';
183     }
184     else if (m_encoding_uid != LLDB_INVALID_UID)
185     {
186         s->Printf(", type_uid = 0x%8.8" PRIx64, m_encoding_uid);
187         switch (m_encoding_uid_type)
188         {
189         case eEncodingInvalid: break;
190         case eEncodingIsUID: s->PutCString(" (unresolved type)"); break;
191         case eEncodingIsConstUID: s->PutCString(" (unresolved const type)"); break;
192         case eEncodingIsRestrictUID: s->PutCString(" (unresolved restrict type)"); break;
193         case eEncodingIsVolatileUID: s->PutCString(" (unresolved volatile type)"); break;
194         case eEncodingIsTypedefUID: s->PutCString(" (unresolved typedef)"); break;
195         case eEncodingIsPointerUID: s->PutCString(" (unresolved pointer)"); break;
196         case eEncodingIsLValueReferenceUID: s->PutCString(" (unresolved L value reference)"); break;
197         case eEncodingIsRValueReferenceUID: s->PutCString(" (unresolved R value reference)"); break;
198         case eEncodingIsSyntheticUID: s->PutCString(" (synthetic type)"); break;
199         }
200     }
201 }
202 
203 
204 void
205 Type::Dump (Stream *s, bool show_context)
206 {
207     s->Printf("%p: ", static_cast<void*>(this));
208     s->Indent();
209     *s << "Type" << static_cast<const UserID&>(*this) << ' ';
210     if (m_name)
211         *s << ", name = \"" << m_name << "\"";
212 
213     if (m_byte_size != 0)
214         s->Printf(", size = %" PRIu64, m_byte_size);
215 
216     if (show_context && m_context != nullptr)
217     {
218         s->PutCString(", context = ( ");
219         m_context->DumpSymbolContext(s);
220         s->PutCString(" )");
221     }
222 
223     bool show_fullpaths = false;
224     m_decl.Dump (s,show_fullpaths);
225 
226     if (m_compiler_type.IsValid())
227     {
228         *s << ", compiler_type = " << m_compiler_type.GetOpaqueQualType() << ' ';
229         GetForwardCompilerType ().DumpTypeDescription (s);
230     }
231     else if (m_encoding_uid != LLDB_INVALID_UID)
232     {
233         *s << ", type_data = " << (uint64_t)m_encoding_uid;
234         switch (m_encoding_uid_type)
235         {
236         case eEncodingInvalid: break;
237         case eEncodingIsUID: s->PutCString(" (unresolved type)"); break;
238         case eEncodingIsConstUID: s->PutCString(" (unresolved const type)"); break;
239         case eEncodingIsRestrictUID: s->PutCString(" (unresolved restrict type)"); break;
240         case eEncodingIsVolatileUID: s->PutCString(" (unresolved volatile type)"); break;
241         case eEncodingIsTypedefUID: s->PutCString(" (unresolved typedef)"); break;
242         case eEncodingIsPointerUID: s->PutCString(" (unresolved pointer)"); break;
243         case eEncodingIsLValueReferenceUID: s->PutCString(" (unresolved L value reference)"); break;
244         case eEncodingIsRValueReferenceUID: s->PutCString(" (unresolved R value reference)"); break;
245         case eEncodingIsSyntheticUID: s->PutCString(" (synthetic type)"); break;
246         }
247     }
248 
249 //
250 //  if (m_access)
251 //      s->Printf(", access = %u", m_access);
252     s->EOL();
253 }
254 
255 const ConstString &
256 Type::GetName()
257 {
258     if (!m_name)
259         m_name = GetForwardCompilerType ().GetConstTypeName();
260     return m_name;
261 }
262 
263 void
264 Type::DumpTypeName(Stream *s)
265 {
266     GetName().Dump(s, "<invalid-type-name>");
267 }
268 
269 
270 void
271 Type::DumpValue
272 (
273     ExecutionContext *exe_ctx,
274     Stream *s,
275     const DataExtractor &data,
276     uint32_t data_byte_offset,
277     bool show_types,
278     bool show_summary,
279     bool verbose,
280     lldb::Format format
281 )
282 {
283     if (ResolveClangType(eResolveStateForward))
284     {
285         if (show_types)
286         {
287             s->PutChar('(');
288             if (verbose)
289                 s->Printf("Type{0x%8.8" PRIx64 "} ", GetID());
290             DumpTypeName (s);
291             s->PutCString(") ");
292         }
293 
294         GetForwardCompilerType ().DumpValue (exe_ctx,
295                                          s,
296                                          format == lldb::eFormatDefault ? GetFormat() : format,
297                                          data,
298                                          data_byte_offset,
299                                          GetByteSize(),
300                                          0, // Bitfield bit size
301                                          0, // Bitfield bit offset
302                                          show_types,
303                                          show_summary,
304                                          verbose,
305                                          0);
306     }
307 }
308 
309 Type *
310 Type::GetEncodingType ()
311 {
312     if (m_encoding_type == nullptr && m_encoding_uid != LLDB_INVALID_UID)
313         m_encoding_type = m_symbol_file->ResolveTypeUID(m_encoding_uid);
314     return m_encoding_type;
315 }
316 
317 
318 
319 uint64_t
320 Type::GetByteSize()
321 {
322     if (m_byte_size == 0)
323     {
324         switch (m_encoding_uid_type)
325         {
326         case eEncodingInvalid:
327         case eEncodingIsSyntheticUID:
328             break;
329         case eEncodingIsUID:
330         case eEncodingIsConstUID:
331         case eEncodingIsRestrictUID:
332         case eEncodingIsVolatileUID:
333         case eEncodingIsTypedefUID:
334             {
335                 Type *encoding_type = GetEncodingType ();
336                 if (encoding_type)
337                     m_byte_size = encoding_type->GetByteSize();
338                 if (m_byte_size == 0)
339                     m_byte_size = GetLayoutCompilerType ().GetByteSize(nullptr);
340             }
341             break;
342 
343         // If we are a pointer or reference, then this is just a pointer size;
344         case eEncodingIsPointerUID:
345         case eEncodingIsLValueReferenceUID:
346         case eEncodingIsRValueReferenceUID:
347             {
348                 ArchSpec arch;
349                 if (m_symbol_file->GetObjectFile()->GetArchitecture(arch))
350                     m_byte_size = arch.GetAddressByteSize();
351             }
352             break;
353         }
354     }
355     return m_byte_size;
356 }
357 
358 
359 uint32_t
360 Type::GetNumChildren (bool omit_empty_base_classes)
361 {
362     return GetForwardCompilerType ().GetNumChildren(omit_empty_base_classes);
363 }
364 
365 bool
366 Type::IsAggregateType ()
367 {
368     return GetForwardCompilerType ().IsAggregateType();
369 }
370 
371 lldb::TypeSP
372 Type::GetTypedefType()
373 {
374     lldb::TypeSP type_sp;
375     if (IsTypedef())
376     {
377         Type *typedef_type = m_symbol_file->ResolveTypeUID(m_encoding_uid);
378         if (typedef_type)
379             type_sp = typedef_type->shared_from_this();
380     }
381     return type_sp;
382 }
383 
384 
385 
386 lldb::Format
387 Type::GetFormat ()
388 {
389     return GetForwardCompilerType ().GetFormat();
390 }
391 
392 
393 
394 lldb::Encoding
395 Type::GetEncoding (uint64_t &count)
396 {
397     // Make sure we resolve our type if it already hasn't been.
398     return GetForwardCompilerType ().GetEncoding(count);
399 }
400 
401 bool
402 Type::DumpValueInMemory
403 (
404     ExecutionContext *exe_ctx,
405     Stream *s,
406     lldb::addr_t address,
407     AddressType address_type,
408     bool show_types,
409     bool show_summary,
410     bool verbose
411 )
412 {
413     if (address != LLDB_INVALID_ADDRESS)
414     {
415         DataExtractor data;
416         Target *target = nullptr;
417         if (exe_ctx)
418             target = exe_ctx->GetTargetPtr();
419         if (target)
420             data.SetByteOrder (target->GetArchitecture().GetByteOrder());
421         if (ReadFromMemory (exe_ctx, address, address_type, data))
422         {
423             DumpValue(exe_ctx, s, data, 0, show_types, show_summary, verbose);
424             return true;
425         }
426     }
427     return false;
428 }
429 
430 
431 bool
432 Type::ReadFromMemory (ExecutionContext *exe_ctx, lldb::addr_t addr, AddressType address_type, DataExtractor &data)
433 {
434     if (address_type == eAddressTypeFile)
435     {
436         // Can't convert a file address to anything valid without more
437         // context (which Module it came from)
438         return false;
439     }
440 
441     const uint64_t byte_size = GetByteSize();
442     if (data.GetByteSize() < byte_size)
443     {
444         lldb::DataBufferSP data_sp(new DataBufferHeap (byte_size, '\0'));
445         data.SetData(data_sp);
446     }
447 
448     uint8_t* dst = (uint8_t*)data.PeekData(0, byte_size);
449     if (dst != nullptr)
450     {
451         if (address_type == eAddressTypeHost)
452         {
453             // The address is an address in this process, so just copy it
454             if (addr == 0)
455                 return false;
456             memcpy (dst, (uint8_t*)nullptr + addr, byte_size);
457             return true;
458         }
459         else
460         {
461             if (exe_ctx)
462             {
463                 Process *process = exe_ctx->GetProcessPtr();
464                 if (process)
465                 {
466                     Error error;
467                     return exe_ctx->GetProcessPtr()->ReadMemory(addr, dst, byte_size, error) == byte_size;
468                 }
469             }
470         }
471     }
472     return false;
473 }
474 
475 
476 bool
477 Type::WriteToMemory (ExecutionContext *exe_ctx, lldb::addr_t addr, AddressType address_type, DataExtractor &data)
478 {
479     return false;
480 }
481 
482 
483 TypeList*
484 Type::GetTypeList()
485 {
486     return GetSymbolFile()->GetTypeList();
487 }
488 
489 const Declaration &
490 Type::GetDeclaration () const
491 {
492     return m_decl;
493 }
494 
495 bool
496 Type::ResolveClangType (ResolveState compiler_type_resolve_state)
497 {
498     // TODO: This needs to consider the correct type system to use.
499     Type *encoding_type = nullptr;
500     if (!m_compiler_type.IsValid())
501     {
502         encoding_type = GetEncodingType();
503         if (encoding_type)
504         {
505             switch (m_encoding_uid_type)
506             {
507             case eEncodingIsUID:
508                 {
509                     CompilerType encoding_compiler_type = encoding_type->GetForwardCompilerType ();
510                     if (encoding_compiler_type.IsValid())
511                     {
512                         m_compiler_type = encoding_compiler_type;
513                         m_flags.compiler_type_resolve_state = encoding_type->m_flags.compiler_type_resolve_state;
514                     }
515                 }
516                 break;
517 
518             case eEncodingIsConstUID:
519                 m_compiler_type = encoding_type->GetForwardCompilerType ().AddConstModifier();
520                 break;
521 
522             case eEncodingIsRestrictUID:
523                 m_compiler_type = encoding_type->GetForwardCompilerType ().AddRestrictModifier();
524                 break;
525 
526             case eEncodingIsVolatileUID:
527                 m_compiler_type = encoding_type->GetForwardCompilerType ().AddVolatileModifier();
528                 break;
529 
530             case eEncodingIsTypedefUID:
531                 m_compiler_type = encoding_type->GetForwardCompilerType ().CreateTypedef(GetName().AsCString(),
532                                                                                       GetSymbolFile()->GetDeclContextContainingUID(GetID()));
533                 m_name.Clear();
534                 break;
535 
536             case eEncodingIsPointerUID:
537                 m_compiler_type = encoding_type->GetForwardCompilerType ().GetPointerType();
538                 break;
539 
540             case eEncodingIsLValueReferenceUID:
541                 m_compiler_type = encoding_type->GetForwardCompilerType ().GetLValueReferenceType();
542                 break;
543 
544             case eEncodingIsRValueReferenceUID:
545                 m_compiler_type = encoding_type->GetForwardCompilerType ().GetRValueReferenceType();
546                 break;
547 
548             default:
549                 assert(!"Unhandled encoding_data_type.");
550                 break;
551             }
552         }
553         else
554         {
555             // We have no encoding type, return void?
556             TypeSystem *type_system = m_symbol_file->GetTypeSystemForLanguage(eLanguageTypeC);
557             CompilerType void_compiler_type = type_system->GetBasicTypeFromAST(eBasicTypeVoid);
558             switch (m_encoding_uid_type)
559             {
560             case eEncodingIsUID:
561                 m_compiler_type = void_compiler_type;
562                 break;
563 
564             case eEncodingIsConstUID:
565                 m_compiler_type = void_compiler_type.AddConstModifier();
566                 break;
567 
568             case eEncodingIsRestrictUID:
569                 m_compiler_type = void_compiler_type.AddRestrictModifier();
570                 break;
571 
572             case eEncodingIsVolatileUID:
573                 m_compiler_type = void_compiler_type.AddVolatileModifier();
574                 break;
575 
576             case eEncodingIsTypedefUID:
577                 m_compiler_type = void_compiler_type.CreateTypedef(GetName().AsCString(),
578                                                              GetSymbolFile()->GetDeclContextContainingUID(GetID()));
579                 break;
580 
581             case eEncodingIsPointerUID:
582                 m_compiler_type = void_compiler_type.GetPointerType ();
583                 break;
584 
585             case eEncodingIsLValueReferenceUID:
586                 m_compiler_type = void_compiler_type.GetLValueReferenceType();
587                 break;
588 
589             case eEncodingIsRValueReferenceUID:
590                 m_compiler_type = void_compiler_type.GetRValueReferenceType();
591                 break;
592 
593             default:
594                 assert(!"Unhandled encoding_data_type.");
595                 break;
596             }
597         }
598 
599         // When we have a EncodingUID, our "m_flags.compiler_type_resolve_state" is set to eResolveStateUnresolved
600         // so we need to update it to say that we now have a forward declaration since that is what we created
601         // above.
602         if (m_compiler_type.IsValid())
603             m_flags.compiler_type_resolve_state = eResolveStateForward;
604 
605     }
606 
607     // Check if we have a forward reference to a class/struct/union/enum?
608     if (compiler_type_resolve_state == eResolveStateLayout || compiler_type_resolve_state == eResolveStateFull)
609     {
610         // Check if we have a forward reference to a class/struct/union/enum?
611         if (m_compiler_type.IsValid() && m_flags.compiler_type_resolve_state < compiler_type_resolve_state)
612         {
613             m_flags.compiler_type_resolve_state = eResolveStateFull;
614             if (!m_compiler_type.IsDefined ())
615             {
616                 // We have a forward declaration, we need to resolve it to a complete definition.
617                 m_symbol_file->CompleteType (m_compiler_type);
618             }
619         }
620     }
621 
622     // If we have an encoding type, then we need to make sure it is
623     // resolved appropriately.
624     if (m_encoding_uid != LLDB_INVALID_UID)
625     {
626         if (encoding_type == nullptr)
627             encoding_type = GetEncodingType();
628         if (encoding_type)
629         {
630             ResolveState encoding_compiler_type_resolve_state = compiler_type_resolve_state;
631 
632             if (compiler_type_resolve_state == eResolveStateLayout)
633             {
634                 switch (m_encoding_uid_type)
635                 {
636                 case eEncodingIsPointerUID:
637                 case eEncodingIsLValueReferenceUID:
638                 case eEncodingIsRValueReferenceUID:
639                     encoding_compiler_type_resolve_state = eResolveStateForward;
640                     break;
641                 default:
642                     break;
643                 }
644             }
645             encoding_type->ResolveClangType (encoding_compiler_type_resolve_state);
646         }
647     }
648     return m_compiler_type.IsValid();
649 }
650 uint32_t
651 Type::GetEncodingMask ()
652 {
653     uint32_t encoding_mask = 1u << m_encoding_uid_type;
654     Type *encoding_type = GetEncodingType();
655     assert (encoding_type != this);
656     if (encoding_type)
657         encoding_mask |= encoding_type->GetEncodingMask ();
658     return encoding_mask;
659 }
660 
661 CompilerType
662 Type::GetFullCompilerType ()
663 {
664     ResolveClangType(eResolveStateFull);
665     return m_compiler_type;
666 }
667 
668 CompilerType
669 Type::GetLayoutCompilerType ()
670 {
671     ResolveClangType(eResolveStateLayout);
672     return m_compiler_type;
673 }
674 
675 CompilerType
676 Type::GetForwardCompilerType ()
677 {
678     ResolveClangType (eResolveStateForward);
679     return m_compiler_type;
680 }
681 
682 int
683 Type::Compare(const Type &a, const Type &b)
684 {
685     // Just compare the UID values for now...
686     lldb::user_id_t a_uid = a.GetID();
687     lldb::user_id_t b_uid = b.GetID();
688     if (a_uid < b_uid)
689         return -1;
690     if (a_uid > b_uid)
691         return 1;
692     return 0;
693 }
694 
695 ConstString
696 Type::GetQualifiedName ()
697 {
698     return GetForwardCompilerType ().GetConstTypeName();
699 }
700 
701 bool
702 Type::GetTypeScopeAndBasename (const char* &name_cstr,
703                                std::string &scope,
704                                std::string &basename,
705                                TypeClass &type_class)
706 {
707     // Protect against null c string.
708 
709     type_class = eTypeClassAny;
710 
711     if (name_cstr && name_cstr[0])
712     {
713         llvm::StringRef name_strref(name_cstr);
714         if (name_strref.startswith("struct "))
715         {
716             name_cstr += 7;
717             type_class = eTypeClassStruct;
718         }
719         else if (name_strref.startswith("class "))
720         {
721             name_cstr += 6;
722             type_class = eTypeClassClass;
723         }
724         else if (name_strref.startswith("union "))
725         {
726             name_cstr += 6;
727             type_class = eTypeClassUnion;
728         }
729         else if (name_strref.startswith("enum "))
730         {
731             name_cstr += 5;
732             type_class = eTypeClassEnumeration;
733         }
734         else if (name_strref.startswith("typedef "))
735         {
736             name_cstr += 8;
737             type_class = eTypeClassTypedef;
738         }
739         const char *basename_cstr = name_cstr;
740         const char* namespace_separator = ::strstr (basename_cstr, "::");
741         if (namespace_separator)
742         {
743             const char* template_arg_char = ::strchr (basename_cstr, '<');
744             while (namespace_separator != nullptr)
745             {
746                 if (template_arg_char && namespace_separator > template_arg_char) // but namespace'd template arguments are still good to go
747                     break;
748                 basename_cstr = namespace_separator + 2;
749                 namespace_separator = strstr(basename_cstr, "::");
750             }
751             if (basename_cstr > name_cstr)
752             {
753                 scope.assign (name_cstr, basename_cstr - name_cstr);
754                 basename.assign (basename_cstr);
755                 return true;
756             }
757         }
758     }
759     return false;
760 }
761 
762 
763 ModuleSP
764 Type::GetModule()
765 {
766     if (m_symbol_file)
767         return m_symbol_file->GetObjectFile()->GetModule();
768     return ModuleSP();
769 }
770 
771 
772 TypeAndOrName::TypeAndOrName () : m_type_pair(), m_type_name()
773 {
774 
775 }
776 
777 TypeAndOrName::TypeAndOrName (TypeSP &in_type_sp) : m_type_pair(in_type_sp)
778 {
779     if (in_type_sp)
780         m_type_name = in_type_sp->GetName();
781 }
782 
783 TypeAndOrName::TypeAndOrName (const char *in_type_str) : m_type_name(in_type_str)
784 {
785 }
786 
787 TypeAndOrName::TypeAndOrName (const TypeAndOrName &rhs) : m_type_pair (rhs.m_type_pair), m_type_name (rhs.m_type_name)
788 {
789 
790 }
791 
792 TypeAndOrName::TypeAndOrName (ConstString &in_type_const_string) : m_type_name (in_type_const_string)
793 {
794 }
795 
796 TypeAndOrName &
797 TypeAndOrName::operator= (const TypeAndOrName &rhs)
798 {
799     if (this != &rhs)
800     {
801         m_type_name = rhs.m_type_name;
802         m_type_pair = rhs.m_type_pair;
803     }
804     return *this;
805 }
806 
807 bool
808 TypeAndOrName::operator==(const TypeAndOrName &other) const
809 {
810     if (m_type_pair != other.m_type_pair)
811         return false;
812     if (m_type_name != other.m_type_name)
813         return false;
814     return true;
815 }
816 
817 bool
818 TypeAndOrName::operator!=(const TypeAndOrName &other) const
819 {
820     if (m_type_pair != other.m_type_pair)
821         return true;
822     if (m_type_name != other.m_type_name)
823         return true;
824     return false;
825 }
826 
827 ConstString
828 TypeAndOrName::GetName () const
829 {
830     if (m_type_name)
831         return m_type_name;
832     if (m_type_pair)
833         return m_type_pair.GetName();
834     return ConstString("<invalid>");
835 }
836 
837 void
838 TypeAndOrName::SetName (const ConstString &type_name)
839 {
840     m_type_name = type_name;
841 }
842 
843 void
844 TypeAndOrName::SetName (const char *type_name_cstr)
845 {
846     m_type_name.SetCString (type_name_cstr);
847 }
848 
849 void
850 TypeAndOrName::SetTypeSP (lldb::TypeSP type_sp)
851 {
852     m_type_pair.SetType(type_sp);
853     if (m_type_pair)
854         m_type_name = m_type_pair.GetName();
855 }
856 
857 void
858 TypeAndOrName::SetCompilerType (CompilerType compiler_type)
859 {
860     m_type_pair.SetType(compiler_type);
861     if (m_type_pair)
862         m_type_name = m_type_pair.GetName();
863 }
864 
865 bool
866 TypeAndOrName::IsEmpty()  const
867 {
868     if ((bool)m_type_name || (bool)m_type_pair)
869         return false;
870     else
871         return true;
872 }
873 
874 void
875 TypeAndOrName::Clear ()
876 {
877     m_type_name.Clear();
878     m_type_pair.Clear();
879 }
880 
881 bool
882 TypeAndOrName::HasName () const
883 {
884     return (bool)m_type_name;
885 }
886 
887 bool
888 TypeAndOrName::HasTypeSP () const
889 {
890     return m_type_pair.GetTypeSP().get() != nullptr;
891 }
892 
893 bool
894 TypeAndOrName::HasCompilerType () const
895 {
896     return m_type_pair.GetCompilerType().IsValid();
897 }
898 
899 
900 TypeImpl::TypeImpl() :
901     m_module_wp(),
902     m_static_type(),
903     m_dynamic_type()
904 {
905 }
906 
907 TypeImpl::TypeImpl(const TypeImpl& rhs) :
908     m_module_wp (rhs.m_module_wp),
909     m_static_type(rhs.m_static_type),
910     m_dynamic_type(rhs.m_dynamic_type)
911 {
912 }
913 
914 TypeImpl::TypeImpl (const lldb::TypeSP &type_sp) :
915     m_module_wp (),
916     m_static_type(),
917     m_dynamic_type()
918 {
919     SetType (type_sp);
920 }
921 
922 TypeImpl::TypeImpl (const CompilerType &compiler_type) :
923     m_module_wp (),
924     m_static_type(),
925     m_dynamic_type()
926 {
927     SetType (compiler_type);
928 }
929 
930 TypeImpl::TypeImpl (const lldb::TypeSP &type_sp, const CompilerType &dynamic) :
931     m_module_wp (),
932     m_static_type (type_sp),
933     m_dynamic_type(dynamic)
934 {
935     SetType (type_sp, dynamic);
936 }
937 
938 TypeImpl::TypeImpl (const CompilerType &static_type, const CompilerType &dynamic_type) :
939     m_module_wp (),
940     m_static_type (),
941     m_dynamic_type()
942 {
943     SetType (static_type, dynamic_type);
944 }
945 
946 TypeImpl::TypeImpl (const TypePair &pair, const CompilerType &dynamic) :
947     m_module_wp (),
948     m_static_type (),
949     m_dynamic_type()
950 {
951     SetType (pair, dynamic);
952 }
953 
954 void
955 TypeImpl::SetType (const lldb::TypeSP &type_sp)
956 {
957     m_static_type.SetType(type_sp);
958     if (type_sp)
959         m_module_wp = type_sp->GetModule();
960     else
961         m_module_wp = lldb::ModuleWP();
962 }
963 
964 void
965 TypeImpl::SetType (const CompilerType &compiler_type)
966 {
967     m_module_wp = lldb::ModuleWP();
968     m_static_type.SetType (compiler_type);
969 }
970 
971 void
972 TypeImpl::SetType (const lldb::TypeSP &type_sp, const CompilerType &dynamic)
973 {
974     SetType (type_sp);
975     m_dynamic_type = dynamic;
976 }
977 
978 void
979 TypeImpl::SetType (const CompilerType &compiler_type, const CompilerType &dynamic)
980 {
981     m_module_wp = lldb::ModuleWP();
982     m_static_type.SetType (compiler_type);
983     m_dynamic_type = dynamic;
984 }
985 
986 void
987 TypeImpl::SetType (const TypePair &pair, const CompilerType &dynamic)
988 {
989     m_module_wp = pair.GetModule();
990     m_static_type = pair;
991     m_dynamic_type = dynamic;
992 }
993 
994 TypeImpl&
995 TypeImpl::operator = (const TypeImpl& rhs)
996 {
997     if (rhs != *this)
998     {
999         m_module_wp = rhs.m_module_wp;
1000         m_static_type = rhs.m_static_type;
1001         m_dynamic_type = rhs.m_dynamic_type;
1002     }
1003     return *this;
1004 }
1005 
1006 bool
1007 TypeImpl::CheckModule (lldb::ModuleSP &module_sp) const
1008 {
1009     // Check if we have a module for this type. If we do and the shared pointer is
1010     // can be successfully initialized with m_module_wp, return true. Else return false
1011     // if we didn't have a module, or if we had a module and it has been deleted. Any
1012     // functions doing anything with a TypeSP in this TypeImpl class should call this
1013     // function and only do anything with the ivars if this function returns true. If
1014     // we have a module, the "module_sp" will be filled in with a strong reference to the
1015     // module so that the module will at least stay around long enough for the type
1016     // query to succeed.
1017     module_sp = m_module_wp.lock();
1018     if (!module_sp)
1019     {
1020         lldb::ModuleWP empty_module_wp;
1021         // If either call to "std::weak_ptr::owner_before(...) value returns true, this
1022         // indicates that m_module_wp once contained (possibly still does) a reference
1023         // to a valid shared pointer. This helps us know if we had a valid reference to
1024         // a section which is now invalid because the module it was in was deleted
1025         if (empty_module_wp.owner_before(m_module_wp) || m_module_wp.owner_before(empty_module_wp))
1026         {
1027             // m_module_wp had a valid reference to a module, but all strong references
1028             // have been released and the module has been deleted
1029             return false;
1030         }
1031     }
1032     // We either successfully locked the module, or didn't have one to begin with
1033     return true;
1034 }
1035 
1036 bool
1037 TypeImpl::operator == (const TypeImpl& rhs) const
1038 {
1039     return m_static_type == rhs.m_static_type && m_dynamic_type == rhs.m_dynamic_type;
1040 }
1041 
1042 bool
1043 TypeImpl::operator != (const TypeImpl& rhs) const
1044 {
1045     return m_static_type != rhs.m_static_type || m_dynamic_type != rhs.m_dynamic_type;
1046 }
1047 
1048 bool
1049 TypeImpl::IsValid() const
1050 {
1051     // just a name is not valid
1052     ModuleSP module_sp;
1053     if (CheckModule (module_sp))
1054         return m_static_type.IsValid() || m_dynamic_type.IsValid();
1055     return false;
1056 }
1057 
1058 TypeImpl::operator bool () const
1059 {
1060     return IsValid();
1061 }
1062 
1063 void
1064 TypeImpl::Clear()
1065 {
1066     m_module_wp = lldb::ModuleWP();
1067     m_static_type.Clear();
1068     m_dynamic_type.Clear();
1069 }
1070 
1071 ConstString
1072 TypeImpl::GetName ()  const
1073 {
1074     ModuleSP module_sp;
1075     if (CheckModule (module_sp))
1076     {
1077         if (m_dynamic_type)
1078             return m_dynamic_type.GetTypeName();
1079         return m_static_type.GetName ();
1080     }
1081     return ConstString();
1082 }
1083 
1084 ConstString
1085 TypeImpl::GetDisplayTypeName ()  const
1086 {
1087     ModuleSP module_sp;
1088     if (CheckModule (module_sp))
1089     {
1090         if (m_dynamic_type)
1091             return m_dynamic_type.GetDisplayTypeName();
1092         return m_static_type.GetDisplayTypeName();
1093     }
1094     return ConstString();
1095 }
1096 
1097 TypeImpl
1098 TypeImpl::GetPointerType () const
1099 {
1100     ModuleSP module_sp;
1101     if (CheckModule (module_sp))
1102     {
1103         if (m_dynamic_type.IsValid())
1104         {
1105             return TypeImpl(m_static_type.GetPointerType(), m_dynamic_type.GetPointerType());
1106         }
1107         return TypeImpl(m_static_type.GetPointerType());
1108     }
1109     return TypeImpl();
1110 }
1111 
1112 TypeImpl
1113 TypeImpl::GetPointeeType () const
1114 {
1115     ModuleSP module_sp;
1116     if (CheckModule (module_sp))
1117     {
1118         if (m_dynamic_type.IsValid())
1119         {
1120             return TypeImpl(m_static_type.GetPointeeType(), m_dynamic_type.GetPointeeType());
1121         }
1122         return TypeImpl(m_static_type.GetPointeeType());
1123     }
1124     return TypeImpl();
1125 }
1126 
1127 TypeImpl
1128 TypeImpl::GetReferenceType () const
1129 {
1130     ModuleSP module_sp;
1131     if (CheckModule (module_sp))
1132     {
1133         if (m_dynamic_type.IsValid())
1134         {
1135             return TypeImpl(m_static_type.GetReferenceType(), m_dynamic_type.GetLValueReferenceType());
1136         }
1137         return TypeImpl(m_static_type.GetReferenceType());
1138     }
1139     return TypeImpl();
1140 }
1141 
1142 TypeImpl
1143 TypeImpl::GetTypedefedType () const
1144 {
1145     ModuleSP module_sp;
1146     if (CheckModule (module_sp))
1147     {
1148         if (m_dynamic_type.IsValid())
1149         {
1150             return TypeImpl(m_static_type.GetTypedefedType(), m_dynamic_type.GetTypedefedType());
1151         }
1152         return TypeImpl(m_static_type.GetTypedefedType());
1153     }
1154     return TypeImpl();
1155 }
1156 
1157 TypeImpl
1158 TypeImpl::GetDereferencedType () const
1159 {
1160     ModuleSP module_sp;
1161     if (CheckModule (module_sp))
1162     {
1163         if (m_dynamic_type.IsValid())
1164         {
1165             return TypeImpl(m_static_type.GetDereferencedType(), m_dynamic_type.GetNonReferenceType());
1166         }
1167         return TypeImpl(m_static_type.GetDereferencedType());
1168     }
1169     return TypeImpl();
1170 }
1171 
1172 TypeImpl
1173 TypeImpl::GetUnqualifiedType() const
1174 {
1175     ModuleSP module_sp;
1176     if (CheckModule (module_sp))
1177     {
1178         if (m_dynamic_type.IsValid())
1179         {
1180             return TypeImpl(m_static_type.GetUnqualifiedType(), m_dynamic_type.GetFullyUnqualifiedType());
1181         }
1182         return TypeImpl(m_static_type.GetUnqualifiedType());
1183     }
1184     return TypeImpl();
1185 }
1186 
1187 TypeImpl
1188 TypeImpl::GetCanonicalType() const
1189 {
1190     ModuleSP module_sp;
1191     if (CheckModule (module_sp))
1192     {
1193         if (m_dynamic_type.IsValid())
1194         {
1195             return TypeImpl(m_static_type.GetCanonicalType(), m_dynamic_type.GetCanonicalType());
1196         }
1197         return TypeImpl(m_static_type.GetCanonicalType());
1198     }
1199     return TypeImpl();
1200 }
1201 
1202 CompilerType
1203 TypeImpl::GetCompilerType (bool prefer_dynamic)
1204 {
1205     ModuleSP module_sp;
1206     if (CheckModule (module_sp))
1207     {
1208         if (prefer_dynamic)
1209         {
1210             if (m_dynamic_type.IsValid())
1211                 return m_dynamic_type;
1212         }
1213         return m_static_type.GetCompilerType();
1214     }
1215     return CompilerType();
1216 }
1217 
1218 TypeSystem *
1219 TypeImpl::GetTypeSystem (bool prefer_dynamic)
1220 {
1221     ModuleSP module_sp;
1222     if (CheckModule (module_sp))
1223     {
1224         if (prefer_dynamic)
1225         {
1226             if (m_dynamic_type.IsValid())
1227                 return m_dynamic_type.GetTypeSystem();
1228         }
1229         return m_static_type.GetCompilerType().GetTypeSystem();
1230     }
1231     return NULL;
1232 }
1233 
1234 bool
1235 TypeImpl::GetDescription (lldb_private::Stream &strm,
1236                           lldb::DescriptionLevel description_level)
1237 {
1238     ModuleSP module_sp;
1239     if (CheckModule (module_sp))
1240     {
1241         if (m_dynamic_type.IsValid())
1242         {
1243             strm.Printf("Dynamic:\n");
1244             m_dynamic_type.DumpTypeDescription(&strm);
1245             strm.Printf("\nStatic:\n");
1246         }
1247         m_static_type.GetCompilerType().DumpTypeDescription(&strm);
1248     }
1249     else
1250     {
1251         strm.PutCString("Invalid TypeImpl module for type has been deleted\n");
1252     }
1253     return true;
1254 }
1255 
1256 TypeMemberFunctionImpl&
1257 TypeMemberFunctionImpl::operator = (const TypeMemberFunctionImpl& rhs)
1258 {
1259     if (this != &rhs)
1260     {
1261         m_type = rhs.m_type;
1262         m_objc_method_decl = rhs.m_objc_method_decl;
1263         m_name = rhs.m_name;
1264         m_kind = rhs.m_kind;
1265     }
1266     return *this;
1267 }
1268 
1269 bool
1270 TypeMemberFunctionImpl::IsValid ()
1271 {
1272     return m_type.IsValid() && m_kind != lldb::eMemberFunctionKindUnknown;
1273 }
1274 
1275 ConstString
1276 TypeMemberFunctionImpl::GetName () const
1277 {
1278     return m_name;
1279 }
1280 
1281 CompilerType
1282 TypeMemberFunctionImpl::GetType () const
1283 {
1284     return m_type;
1285 }
1286 
1287 lldb::MemberFunctionKind
1288 TypeMemberFunctionImpl::GetKind () const
1289 {
1290     return m_kind;
1291 }
1292 
1293 std::string
1294 TypeMemberFunctionImpl::GetPrintableTypeName ()
1295 {
1296     if (m_type)
1297         return m_type.GetTypeName().AsCString("<unknown>");
1298     if (m_objc_method_decl)
1299     {
1300         if (m_objc_method_decl->getClassInterface())
1301         {
1302             return m_objc_method_decl->getClassInterface()->getName();
1303         }
1304     }
1305     return "<unknown>";
1306 }
1307 
1308 bool
1309 TypeMemberFunctionImpl::GetDescription (Stream& stream)
1310 {
1311     switch (m_kind) {
1312         case lldb::eMemberFunctionKindUnknown:
1313             return false;
1314         case lldb::eMemberFunctionKindConstructor:
1315             stream.Printf("constructor for %s", GetPrintableTypeName().c_str());
1316             break;
1317         case lldb::eMemberFunctionKindDestructor:
1318             stream.Printf("destructor for %s",  GetPrintableTypeName().c_str());
1319             break;
1320         case lldb::eMemberFunctionKindInstanceMethod:
1321             stream.Printf("instance method %s of type %s",
1322                           m_name.AsCString(),
1323                           GetPrintableTypeName().c_str());
1324             break;
1325         case lldb::eMemberFunctionKindStaticMethod:
1326             stream.Printf("static method %s of type %s",
1327                           m_name.AsCString(),
1328                           GetPrintableTypeName().c_str());
1329             break;
1330     }
1331     return true;
1332 }
1333 
1334 CompilerType
1335 TypeMemberFunctionImpl::GetReturnType () const
1336 {
1337     if (m_type)
1338         return m_type.GetFunctionReturnType();
1339     if (m_objc_method_decl)
1340         return CompilerType(&m_objc_method_decl->getASTContext(), m_objc_method_decl->getReturnType());
1341     return CompilerType();
1342 }
1343 
1344 size_t
1345 TypeMemberFunctionImpl::GetNumArguments () const
1346 {
1347     if (m_type)
1348         return m_type.GetNumberOfFunctionArguments();
1349     if (m_objc_method_decl)
1350         return m_objc_method_decl->param_size();
1351     return 0;
1352 }
1353 
1354 CompilerType
1355 TypeMemberFunctionImpl::GetArgumentAtIndex (size_t idx) const
1356 {
1357     if (m_type)
1358         return m_type.GetFunctionArgumentAtIndex (idx);
1359     if (m_objc_method_decl)
1360     {
1361         if (idx < m_objc_method_decl->param_size())
1362             return CompilerType(&m_objc_method_decl->getASTContext(), m_objc_method_decl->parameters()[idx]->getOriginalType());
1363     }
1364     return CompilerType();
1365 }
1366 
1367 TypeEnumMemberImpl::TypeEnumMemberImpl (const lldb::TypeImplSP &integer_type_sp,
1368                                         const ConstString &name,
1369                                         const llvm::APSInt &value) :
1370     m_integer_type_sp(integer_type_sp),
1371     m_name(name),
1372     m_value(value),
1373     m_valid((bool)name && (bool)integer_type_sp)
1374 
1375 {
1376 }
1377