]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Symbol/Type.cpp
Update LLDB snapshot to upstream r225923 (git 2b588ecd)
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Symbol / Type.cpp
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/ClangASTType.h"
19 #include "lldb/Symbol/ClangASTContext.h"
20 #include "lldb/Symbol/ObjectFile.h"
21 #include "lldb/Symbol/SymbolContextScope.h"
22 #include "lldb/Symbol/SymbolFile.h"
23 #include "lldb/Symbol/SymbolVendor.h"
24 #include "lldb/Symbol/Type.h"
25 #include "lldb/Symbol/TypeList.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 ClangASTType &clang_type,
90     ResolveState clang_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_clang_type (clang_type)
103 {
104     m_flags.clang_type_resolve_state = (clang_type ? clang_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_clang_type ()
120 {
121     m_flags.clang_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_clang_type (rhs.m_clang_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_clang_type.IsValid())
179     {
180         *s << ", clang_type = \"";
181         GetClangForwardType().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_clang_type.IsValid())
227     {
228         *s << ", clang_type = " << m_clang_type.GetOpaqueQualType() << ' ';
229         GetClangForwardType().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 = GetClangForwardType().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         GetClangForwardType().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 = GetClangLayoutType().GetByteSize();
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             m_byte_size = m_symbol_file->GetClangASTContext().GetPointerByteSize();
348             break;
349         }
350     }
351     return m_byte_size;
352 }
353
354
355 uint32_t
356 Type::GetNumChildren (bool omit_empty_base_classes)
357 {
358     return GetClangForwardType().GetNumChildren(omit_empty_base_classes);
359 }
360
361 bool
362 Type::IsAggregateType ()
363 {
364     return GetClangForwardType().IsAggregateType();
365 }
366
367 lldb::TypeSP
368 Type::GetTypedefType()
369 {
370     lldb::TypeSP type_sp;
371     if (IsTypedef())
372     {
373         Type *typedef_type = m_symbol_file->ResolveTypeUID(m_encoding_uid);
374         if (typedef_type)
375             type_sp = typedef_type->shared_from_this();
376     }
377     return type_sp;
378 }
379
380
381
382 lldb::Format
383 Type::GetFormat ()
384 {
385     return GetClangForwardType().GetFormat();
386 }
387
388
389
390 lldb::Encoding
391 Type::GetEncoding (uint64_t &count)
392 {
393     // Make sure we resolve our type if it already hasn't been.
394     return GetClangForwardType().GetEncoding(count);
395 }
396
397 bool
398 Type::DumpValueInMemory
399 (
400     ExecutionContext *exe_ctx,
401     Stream *s,
402     lldb::addr_t address,
403     AddressType address_type,
404     bool show_types,
405     bool show_summary,
406     bool verbose
407 )
408 {
409     if (address != LLDB_INVALID_ADDRESS)
410     {
411         DataExtractor data;
412         Target *target = nullptr;
413         if (exe_ctx)
414             target = exe_ctx->GetTargetPtr();
415         if (target)
416             data.SetByteOrder (target->GetArchitecture().GetByteOrder());
417         if (ReadFromMemory (exe_ctx, address, address_type, data))
418         {
419             DumpValue(exe_ctx, s, data, 0, show_types, show_summary, verbose);
420             return true;
421         }
422     }
423     return false;
424 }
425
426
427 bool
428 Type::ReadFromMemory (ExecutionContext *exe_ctx, lldb::addr_t addr, AddressType address_type, DataExtractor &data)
429 {
430     if (address_type == eAddressTypeFile)
431     {
432         // Can't convert a file address to anything valid without more
433         // context (which Module it came from)
434         return false;
435     }
436
437     const uint64_t byte_size = GetByteSize();
438     if (data.GetByteSize() < byte_size)
439     {
440         lldb::DataBufferSP data_sp(new DataBufferHeap (byte_size, '\0'));
441         data.SetData(data_sp);
442     }
443
444     uint8_t* dst = (uint8_t*)data.PeekData(0, byte_size);
445     if (dst != nullptr)
446     {
447         if (address_type == eAddressTypeHost)
448         {
449             // The address is an address in this process, so just copy it
450             if (addr == 0)
451                 return false;
452             memcpy (dst, (uint8_t*)nullptr + addr, byte_size);
453             return true;
454         }
455         else
456         {
457             if (exe_ctx)
458             {
459                 Process *process = exe_ctx->GetProcessPtr();
460                 if (process)
461                 {
462                     Error error;
463                     return exe_ctx->GetProcessPtr()->ReadMemory(addr, dst, byte_size, error) == byte_size;
464                 }
465             }
466         }
467     }
468     return false;
469 }
470
471
472 bool
473 Type::WriteToMemory (ExecutionContext *exe_ctx, lldb::addr_t addr, AddressType address_type, DataExtractor &data)
474 {
475     return false;
476 }
477
478
479 TypeList*
480 Type::GetTypeList()
481 {
482     return GetSymbolFile()->GetTypeList();
483 }
484
485 const Declaration &
486 Type::GetDeclaration () const
487 {
488     return m_decl;
489 }
490
491 bool
492 Type::ResolveClangType (ResolveState clang_type_resolve_state)
493 {
494     Type *encoding_type = nullptr;
495     if (!m_clang_type.IsValid())
496     {
497         encoding_type = GetEncodingType();
498         if (encoding_type)
499         {
500             switch (m_encoding_uid_type)
501             {
502             case eEncodingIsUID:
503                 {
504                     ClangASTType encoding_clang_type = encoding_type->GetClangForwardType();
505                     if (encoding_clang_type.IsValid())
506                     {
507                         m_clang_type = encoding_clang_type;
508                         m_flags.clang_type_resolve_state = encoding_type->m_flags.clang_type_resolve_state;
509                     }
510                 }
511                 break;
512
513             case eEncodingIsConstUID:
514                 m_clang_type = encoding_type->GetClangForwardType().AddConstModifier();
515                 break;
516
517             case eEncodingIsRestrictUID:
518                 m_clang_type = encoding_type->GetClangForwardType().AddRestrictModifier();
519                 break;
520
521             case eEncodingIsVolatileUID:
522                 m_clang_type = encoding_type->GetClangForwardType().AddVolatileModifier();
523                 break;
524
525             case eEncodingIsTypedefUID:
526                 m_clang_type = encoding_type->GetClangForwardType().CreateTypedefType (GetName().AsCString(),
527                                                                                        GetSymbolFile()->GetClangDeclContextContainingTypeUID(GetID()));
528                 m_name.Clear();
529                 break;
530
531             case eEncodingIsPointerUID:
532                 m_clang_type = encoding_type->GetClangForwardType().GetPointerType();
533                 break;
534
535             case eEncodingIsLValueReferenceUID:
536                 m_clang_type = encoding_type->GetClangForwardType().GetLValueReferenceType();
537                 break;
538
539             case eEncodingIsRValueReferenceUID:
540                 m_clang_type = encoding_type->GetClangForwardType().GetRValueReferenceType();
541                 break;
542
543             default:
544                 assert(!"Unhandled encoding_data_type.");
545                 break;
546             }
547         }
548         else
549         {
550             // We have no encoding type, return void?
551             ClangASTType void_clang_type (ClangASTContext::GetBasicType(GetClangASTContext().getASTContext(), eBasicTypeVoid));
552             switch (m_encoding_uid_type)
553             {
554             case eEncodingIsUID:
555                 m_clang_type = void_clang_type;
556                 break;
557
558             case eEncodingIsConstUID:
559                 m_clang_type = void_clang_type.AddConstModifier ();
560                 break;
561
562             case eEncodingIsRestrictUID:
563                 m_clang_type = void_clang_type.AddRestrictModifier ();
564                 break;
565
566             case eEncodingIsVolatileUID:
567                 m_clang_type = void_clang_type.AddVolatileModifier ();
568                 break;
569
570             case eEncodingIsTypedefUID:
571                 m_clang_type = void_clang_type.CreateTypedefType (GetName().AsCString(),
572                                                                   GetSymbolFile()->GetClangDeclContextContainingTypeUID(GetID()));
573                 break;
574
575             case eEncodingIsPointerUID:
576                 m_clang_type = void_clang_type.GetPointerType ();
577                 break;
578
579             case eEncodingIsLValueReferenceUID:
580                 m_clang_type = void_clang_type.GetLValueReferenceType ();
581                 break;
582
583             case eEncodingIsRValueReferenceUID:
584                 m_clang_type = void_clang_type.GetRValueReferenceType ();
585                 break;
586
587             default:
588                 assert(!"Unhandled encoding_data_type.");
589                 break;
590             }
591         }
592     }
593     
594     // Check if we have a forward reference to a class/struct/union/enum?
595     if (m_clang_type.IsValid() && m_flags.clang_type_resolve_state < clang_type_resolve_state)
596     {
597         m_flags.clang_type_resolve_state = eResolveStateFull;
598         if (!m_clang_type.IsDefined ())
599         {
600             // We have a forward declaration, we need to resolve it to a complete definition.
601             m_symbol_file->ResolveClangOpaqueTypeDefinition (m_clang_type);
602         }
603     }
604     
605     // If we have an encoding type, then we need to make sure it is 
606     // resolved appropriately.
607     if (m_encoding_uid != LLDB_INVALID_UID)
608     {
609         if (encoding_type == nullptr)
610             encoding_type = GetEncodingType();
611         if (encoding_type)
612         {
613             ResolveState encoding_clang_type_resolve_state = clang_type_resolve_state;
614             
615             if (clang_type_resolve_state == eResolveStateLayout)
616             {
617                 switch (m_encoding_uid_type)
618                 {
619                 case eEncodingIsPointerUID:
620                 case eEncodingIsLValueReferenceUID:
621                 case eEncodingIsRValueReferenceUID:
622                     encoding_clang_type_resolve_state = eResolveStateForward;
623                     break;
624                 default:
625                     break;
626                 }
627             }
628             encoding_type->ResolveClangType (encoding_clang_type_resolve_state);
629         }
630     }
631     return m_clang_type.IsValid();
632 }
633 uint32_t
634 Type::GetEncodingMask ()
635 {
636     uint32_t encoding_mask = 1u << m_encoding_uid_type;
637     Type *encoding_type = GetEncodingType();
638     assert (encoding_type != this);
639     if (encoding_type)
640         encoding_mask |= encoding_type->GetEncodingMask ();
641     return encoding_mask;
642 }
643
644 ClangASTType
645 Type::GetClangFullType ()
646 {
647     ResolveClangType(eResolveStateFull);
648     return m_clang_type;
649 }
650
651 ClangASTType
652 Type::GetClangLayoutType ()
653 {
654     ResolveClangType(eResolveStateLayout);
655     return m_clang_type;
656 }
657
658 ClangASTType 
659 Type::GetClangForwardType ()
660 {
661     ResolveClangType (eResolveStateForward);
662     return m_clang_type;
663 }
664
665 ClangASTContext &
666 Type::GetClangASTContext ()
667 {
668     return m_symbol_file->GetClangASTContext();
669 }
670
671 int
672 Type::Compare(const Type &a, const Type &b)
673 {
674     // Just compare the UID values for now...
675     lldb::user_id_t a_uid = a.GetID();
676     lldb::user_id_t b_uid = b.GetID();
677     if (a_uid < b_uid)
678         return -1;
679     if (a_uid > b_uid)
680         return 1;
681     return 0;
682 //  if (a.getQualType() == b.getQualType())
683 //      return 0;
684 }
685
686
687 #if 0  // START REMOVE
688 // Move this into ClangASTType
689 void *
690 Type::CreateClangPointerType (Type *type)
691 {
692     assert(type);
693     return GetClangASTContext().CreatePointerType(type->GetClangForwardType());
694 }
695
696 void *
697 Type::CreateClangTypedefType (Type *typedef_type, Type *base_type)
698 {
699     assert(typedef_type && base_type);
700     return GetClangASTContext().CreateTypedefType (typedef_type->GetName().AsCString(), 
701                                                    base_type->GetClangForwardType(), 
702                                                    typedef_type->GetSymbolFile()->GetClangDeclContextContainingTypeUID(typedef_type->GetID()));
703 }
704
705 void *
706 Type::CreateClangLValueReferenceType (Type *type)
707 {
708     assert(type);
709     return GetClangASTContext().CreateLValueReferenceType(type->GetClangForwardType());
710 }
711
712 void *
713 Type::CreateClangRValueReferenceType (Type *type)
714 {
715     assert(type);
716     return GetClangASTContext().CreateRValueReferenceType (type->GetClangForwardType());
717 }
718 #endif // END REMOVE
719
720 bool
721 Type::IsRealObjCClass()
722 {
723     // For now we are just skipping ObjC classes that get made by hand from the runtime, because
724     // those don't have any information.  We could extend this to only return true for "full 
725     // definitions" if we can figure that out.
726     
727     if (m_clang_type.IsObjCObjectOrInterfaceType() && GetByteSize() != 0)
728         return true;
729     else
730         return false;
731 }
732
733 ConstString
734 Type::GetQualifiedName ()
735 {
736     return GetClangForwardType().GetConstTypeName();
737 }
738
739
740 bool
741 Type::GetTypeScopeAndBasename (const char* &name_cstr,
742                                std::string &scope,
743                                std::string &basename,
744                                TypeClass &type_class)
745 {
746     // Protect against null c string.
747     
748     type_class = eTypeClassAny;
749
750     if (name_cstr && name_cstr[0])
751     {
752         llvm::StringRef name_strref(name_cstr);
753         if (name_strref.startswith("struct "))
754         {
755             name_cstr += 7;
756             type_class = eTypeClassStruct;
757         }
758         else if (name_strref.startswith("class "))
759         {
760             name_cstr += 6;
761             type_class = eTypeClassClass;
762         }
763         else if (name_strref.startswith("union "))
764         {
765             name_cstr += 6;
766             type_class = eTypeClassUnion;
767         }
768         else if (name_strref.startswith("enum "))
769         {
770             name_cstr += 5;
771             type_class = eTypeClassEnumeration;
772         }
773         else if (name_strref.startswith("typedef "))
774         {
775             name_cstr += 8;
776             type_class = eTypeClassTypedef;
777         }
778         const char *basename_cstr = name_cstr;
779         const char* namespace_separator = ::strstr (basename_cstr, "::");
780         if (namespace_separator)
781         {
782             const char* template_arg_char = ::strchr (basename_cstr, '<');
783             while (namespace_separator != nullptr)
784             {
785                 if (template_arg_char && namespace_separator > template_arg_char) // but namespace'd template arguments are still good to go
786                     break;
787                 basename_cstr = namespace_separator + 2;
788                 namespace_separator = strstr(basename_cstr, "::");
789             }
790             if (basename_cstr > name_cstr)
791             {
792                 scope.assign (name_cstr, basename_cstr - name_cstr);
793                 basename.assign (basename_cstr);
794                 return true;
795             }
796         }
797     }
798     return false;
799 }
800
801
802 ModuleSP
803 Type::GetModule()
804 {
805     if (m_symbol_file)
806         return m_symbol_file->GetObjectFile()->GetModule();
807     return ModuleSP();
808 }
809
810
811 TypeAndOrName::TypeAndOrName () : m_type_pair(), m_type_name()
812 {
813
814 }
815
816 TypeAndOrName::TypeAndOrName (TypeSP &in_type_sp) : m_type_pair(in_type_sp)
817 {
818     if (in_type_sp)
819         m_type_name = in_type_sp->GetName();
820 }
821
822 TypeAndOrName::TypeAndOrName (const char *in_type_str) : m_type_name(in_type_str)
823 {
824 }
825
826 TypeAndOrName::TypeAndOrName (const TypeAndOrName &rhs) : m_type_pair (rhs.m_type_pair), m_type_name (rhs.m_type_name)
827 {
828
829 }
830
831 TypeAndOrName::TypeAndOrName (ConstString &in_type_const_string) : m_type_name (in_type_const_string)
832 {
833 }
834
835 TypeAndOrName &
836 TypeAndOrName::operator= (const TypeAndOrName &rhs)
837 {
838     if (this != &rhs)
839     {
840         m_type_name = rhs.m_type_name;
841         m_type_pair = rhs.m_type_pair;
842     }
843     return *this;
844 }
845
846 bool
847 TypeAndOrName::operator==(const TypeAndOrName &other) const
848 {
849     if (m_type_pair != other.m_type_pair)
850         return false;
851     if (m_type_name != other.m_type_name)
852         return false;
853     return true;
854 }
855
856 bool
857 TypeAndOrName::operator!=(const TypeAndOrName &other) const
858 {
859     if (m_type_pair != other.m_type_pair)
860         return true;
861     if (m_type_name != other.m_type_name)
862         return true;
863     return false;
864 }
865
866 ConstString
867 TypeAndOrName::GetName () const
868 {
869     if (m_type_name)
870         return m_type_name;
871     if (m_type_pair)
872         return m_type_pair.GetName();
873     return ConstString("<invalid>");
874 }
875
876 void
877 TypeAndOrName::SetName (const ConstString &type_name)
878 {
879     m_type_name = type_name;
880 }
881
882 void
883 TypeAndOrName::SetName (const char *type_name_cstr)
884 {
885     m_type_name.SetCString (type_name_cstr);
886 }
887
888 void
889 TypeAndOrName::SetTypeSP (lldb::TypeSP type_sp)
890 {
891     m_type_pair.SetType(type_sp);
892     if (m_type_pair)
893         m_type_name = m_type_pair.GetName();
894 }
895
896 void
897 TypeAndOrName::SetClangASTType (ClangASTType clang_type)
898 {
899     m_type_pair.SetType(clang_type);
900     if (m_type_pair)
901         m_type_name = m_type_pair.GetName();
902 }
903
904 bool
905 TypeAndOrName::IsEmpty()  const
906 {
907     if ((bool)m_type_name || (bool)m_type_pair)
908         return false;
909     else
910         return true;
911 }
912
913 void
914 TypeAndOrName::Clear ()
915 {
916     m_type_name.Clear();
917     m_type_pair.Clear();
918 }
919
920 bool
921 TypeAndOrName::HasName () const
922 {
923     return (bool)m_type_name;
924 }
925
926 bool
927 TypeAndOrName::HasTypeSP () const
928 {
929     return m_type_pair.GetTypeSP().get() != nullptr;
930 }
931
932 bool
933 TypeAndOrName::HasClangASTType () const
934 {
935     return m_type_pair.GetClangASTType().IsValid();
936 }
937
938
939 TypeImpl::TypeImpl() :
940     m_module_wp(),
941     m_static_type(),
942     m_dynamic_type()
943 {
944 }
945
946 TypeImpl::TypeImpl(const TypeImpl& rhs) :
947     m_module_wp (rhs.m_module_wp),
948     m_static_type(rhs.m_static_type),
949     m_dynamic_type(rhs.m_dynamic_type)
950 {
951 }
952
953 TypeImpl::TypeImpl (const lldb::TypeSP &type_sp) :
954     m_module_wp (),
955     m_static_type(),
956     m_dynamic_type()
957 {
958     SetType (type_sp);
959 }
960
961 TypeImpl::TypeImpl (const ClangASTType &clang_type) :
962     m_module_wp (),
963     m_static_type(),
964     m_dynamic_type()
965 {
966     SetType (clang_type);
967 }
968
969 TypeImpl::TypeImpl (const lldb::TypeSP &type_sp, const ClangASTType &dynamic) :
970     m_module_wp (),
971     m_static_type (type_sp),
972     m_dynamic_type(dynamic)
973 {
974     SetType (type_sp, dynamic);
975 }
976
977 TypeImpl::TypeImpl (const ClangASTType &static_type, const ClangASTType &dynamic_type) :
978     m_module_wp (),
979     m_static_type (),
980     m_dynamic_type()
981 {
982     SetType (static_type, dynamic_type);
983 }
984
985 TypeImpl::TypeImpl (const TypePair &pair, const ClangASTType &dynamic) :
986     m_module_wp (),
987     m_static_type (),
988     m_dynamic_type()
989 {
990     SetType (pair, dynamic);
991 }
992
993 void
994 TypeImpl::SetType (const lldb::TypeSP &type_sp)
995 {
996     m_static_type.SetType(type_sp);
997     if (type_sp)
998         m_module_wp = type_sp->GetModule();
999     else
1000         m_module_wp = lldb::ModuleWP();
1001 }
1002
1003 void
1004 TypeImpl::SetType (const ClangASTType &clang_type)
1005 {
1006     m_module_wp = lldb::ModuleWP();
1007     m_static_type.SetType (clang_type);
1008 }
1009
1010 void
1011 TypeImpl::SetType (const lldb::TypeSP &type_sp, const ClangASTType &dynamic)
1012 {
1013     SetType (type_sp);
1014     m_dynamic_type = dynamic;
1015 }
1016
1017 void
1018 TypeImpl::SetType (const ClangASTType &clang_type, const ClangASTType &dynamic)
1019 {
1020     m_module_wp = lldb::ModuleWP();
1021     m_static_type.SetType (clang_type);
1022     m_dynamic_type = dynamic;
1023 }
1024
1025 void
1026 TypeImpl::SetType (const TypePair &pair, const ClangASTType &dynamic)
1027 {
1028     m_module_wp = pair.GetModule();
1029     m_static_type = pair;
1030     m_dynamic_type = dynamic;
1031 }
1032
1033 TypeImpl&
1034 TypeImpl::operator = (const TypeImpl& rhs)
1035 {
1036     if (rhs != *this)
1037     {
1038         m_module_wp = rhs.m_module_wp;
1039         m_static_type = rhs.m_static_type;
1040         m_dynamic_type = rhs.m_dynamic_type;
1041     }
1042     return *this;
1043 }
1044
1045 bool
1046 TypeImpl::CheckModule (lldb::ModuleSP &module_sp) const
1047 {
1048     // Check if we have a module for this type. If we do and the shared pointer is
1049     // can be successfully initialized with m_module_wp, return true. Else return false
1050     // if we didn't have a module, or if we had a module and it has been deleted. Any
1051     // functions doing anything with a TypeSP in this TypeImpl class should call this
1052     // function and only do anything with the ivars if this function returns true. If
1053     // we have a module, the "module_sp" will be filled in with a strong reference to the
1054     // module so that the module will at least stay around long enough for the type
1055     // query to succeed.
1056     module_sp = m_module_wp.lock();
1057     if (!module_sp)
1058     {
1059         lldb::ModuleWP empty_module_wp;
1060         // If either call to "std::weak_ptr::owner_before(...) value returns true, this
1061         // indicates that m_module_wp once contained (possibly still does) a reference
1062         // to a valid shared pointer. This helps us know if we had a valid reference to
1063         // a section which is now invalid because the module it was in was deleted
1064         if (empty_module_wp.owner_before(m_module_wp) || m_module_wp.owner_before(empty_module_wp))
1065         {
1066             // m_module_wp had a valid reference to a module, but all strong references
1067             // have been released and the module has been deleted
1068             return false;
1069         }
1070     }
1071     // We either successfully locked the module, or didn't have one to begin with
1072     return true;
1073 }
1074
1075 bool
1076 TypeImpl::operator == (const TypeImpl& rhs) const
1077 {
1078     return m_static_type == rhs.m_static_type && m_dynamic_type == rhs.m_dynamic_type;
1079 }
1080
1081 bool
1082 TypeImpl::operator != (const TypeImpl& rhs) const
1083 {
1084     return m_static_type != rhs.m_static_type || m_dynamic_type != rhs.m_dynamic_type;
1085 }
1086
1087 bool
1088 TypeImpl::IsValid() const
1089 {
1090     // just a name is not valid
1091     ModuleSP module_sp;
1092     if (CheckModule (module_sp))
1093         return m_static_type.IsValid() || m_dynamic_type.IsValid();
1094     return false;
1095 }
1096
1097 TypeImpl::operator bool () const
1098 {
1099     return IsValid();
1100 }
1101
1102 void
1103 TypeImpl::Clear()
1104 {
1105     m_module_wp = lldb::ModuleWP();
1106     m_static_type.Clear();
1107     m_dynamic_type.Clear();
1108 }
1109
1110 ConstString
1111 TypeImpl::GetName ()  const
1112 {
1113     ModuleSP module_sp;
1114     if (CheckModule (module_sp))
1115     {
1116         if (m_dynamic_type)
1117             return m_dynamic_type.GetTypeName();
1118         return m_static_type.GetName ();
1119     }
1120     return ConstString();
1121 }
1122
1123 ConstString
1124 TypeImpl::GetDisplayTypeName ()  const
1125 {
1126     ModuleSP module_sp;
1127     if (CheckModule (module_sp))
1128     {
1129         if (m_dynamic_type)
1130             return m_dynamic_type.GetDisplayTypeName();
1131         return m_static_type.GetDisplayTypeName();
1132     }
1133     return ConstString();
1134 }
1135
1136 TypeImpl
1137 TypeImpl::GetPointerType () const
1138 {
1139     ModuleSP module_sp;
1140     if (CheckModule (module_sp))
1141     {
1142         if (m_dynamic_type.IsValid())
1143         {
1144             return TypeImpl(m_static_type, m_dynamic_type.GetPointerType());
1145         }
1146         return TypeImpl(m_static_type.GetPointerType());
1147     }
1148     return TypeImpl();
1149 }
1150
1151 TypeImpl
1152 TypeImpl::GetPointeeType () const
1153 {
1154     ModuleSP module_sp;
1155     if (CheckModule (module_sp))
1156     {
1157         if (m_dynamic_type.IsValid())
1158         {
1159             return TypeImpl(m_static_type, m_dynamic_type.GetPointeeType());
1160         }
1161         return TypeImpl(m_static_type.GetPointeeType());
1162     }
1163     return TypeImpl();
1164 }
1165
1166 TypeImpl
1167 TypeImpl::GetReferenceType () const
1168 {
1169     ModuleSP module_sp;
1170     if (CheckModule (module_sp))
1171     {
1172         if (m_dynamic_type.IsValid())
1173         {
1174             return TypeImpl(m_static_type, m_dynamic_type.GetLValueReferenceType());
1175         }
1176         return TypeImpl(m_static_type.GetReferenceType());
1177     }
1178     return TypeImpl();
1179 }
1180
1181 TypeImpl
1182 TypeImpl::GetTypedefedType () const
1183 {
1184     ModuleSP module_sp;
1185     if (CheckModule (module_sp))
1186     {
1187         if (m_dynamic_type.IsValid())
1188         {
1189             return TypeImpl(m_static_type, m_dynamic_type.GetTypedefedType());
1190         }
1191         return TypeImpl(m_static_type.GetTypedefedType());
1192     }
1193     return TypeImpl();
1194 }
1195
1196 TypeImpl
1197 TypeImpl::GetDereferencedType () const
1198 {
1199     ModuleSP module_sp;
1200     if (CheckModule (module_sp))
1201     {
1202         if (m_dynamic_type.IsValid())
1203         {
1204             return TypeImpl(m_static_type, m_dynamic_type.GetNonReferenceType());
1205         }
1206         return TypeImpl(m_static_type.GetDereferencedType());
1207     }
1208     return TypeImpl();
1209 }
1210
1211 TypeImpl
1212 TypeImpl::GetUnqualifiedType() const
1213 {
1214     ModuleSP module_sp;
1215     if (CheckModule (module_sp))
1216     {
1217         if (m_dynamic_type.IsValid())
1218         {
1219             return TypeImpl(m_static_type, m_dynamic_type.GetFullyUnqualifiedType());
1220         }
1221         return TypeImpl(m_static_type.GetUnqualifiedType());
1222     }
1223     return TypeImpl();
1224 }
1225
1226 TypeImpl
1227 TypeImpl::GetCanonicalType() const
1228 {
1229     ModuleSP module_sp;
1230     if (CheckModule (module_sp))
1231     {
1232         if (m_dynamic_type.IsValid())
1233         {
1234             return TypeImpl(m_static_type, m_dynamic_type.GetCanonicalType());
1235         }
1236         return TypeImpl(m_static_type.GetCanonicalType());
1237     }
1238     return TypeImpl();
1239 }
1240
1241 ClangASTType
1242 TypeImpl::GetClangASTType (bool prefer_dynamic)
1243 {
1244     ModuleSP module_sp;
1245     if (CheckModule (module_sp))
1246     {
1247         if (prefer_dynamic)
1248         {
1249             if (m_dynamic_type.IsValid())
1250                 return m_dynamic_type;
1251         }
1252         return m_static_type.GetClangASTType();
1253     }
1254     return ClangASTType();
1255 }
1256
1257 clang::ASTContext *
1258 TypeImpl::GetClangASTContext (bool prefer_dynamic)
1259 {
1260     ModuleSP module_sp;
1261     if (CheckModule (module_sp))
1262     {
1263         if (prefer_dynamic)
1264         {
1265             if (m_dynamic_type.IsValid())
1266                 return m_dynamic_type.GetASTContext();
1267         }
1268         return m_static_type.GetClangASTContext();
1269     }
1270     return NULL;
1271 }
1272
1273 bool
1274 TypeImpl::GetDescription (lldb_private::Stream &strm,
1275                           lldb::DescriptionLevel description_level)
1276 {
1277     ModuleSP module_sp;
1278     if (CheckModule (module_sp))
1279     {
1280         if (m_dynamic_type.IsValid())
1281         {
1282             strm.Printf("Dynamic:\n");
1283             m_dynamic_type.DumpTypeDescription(&strm);
1284             strm.Printf("\nStatic:\n");
1285         }
1286         m_static_type.GetClangASTType().DumpTypeDescription(&strm);
1287     }
1288     else
1289     {
1290         strm.PutCString("Invalid TypeImpl module for type has been deleted\n");
1291     }
1292     return true;
1293 }
1294
1295 TypeMemberFunctionImpl&
1296 TypeMemberFunctionImpl::operator = (const TypeMemberFunctionImpl& rhs)
1297 {
1298     if (this != &rhs)
1299     {
1300         m_type = rhs.m_type;
1301         m_objc_method_decl = rhs.m_objc_method_decl;
1302         m_name = rhs.m_name;
1303         m_kind = rhs.m_kind;
1304     }
1305     return *this;
1306 }
1307
1308 bool
1309 TypeMemberFunctionImpl::IsValid ()
1310 {
1311     return m_type.IsValid() && m_kind != lldb::eMemberFunctionKindUnknown;
1312 }
1313
1314 ConstString
1315 TypeMemberFunctionImpl::GetName () const
1316 {
1317     return m_name;
1318 }
1319
1320 ClangASTType
1321 TypeMemberFunctionImpl::GetType () const
1322 {
1323     return m_type;
1324 }
1325
1326 lldb::MemberFunctionKind
1327 TypeMemberFunctionImpl::GetKind () const
1328 {
1329     return m_kind;
1330 }
1331
1332 std::string
1333 TypeMemberFunctionImpl::GetPrintableTypeName ()
1334 {
1335     if (m_type)
1336         return m_type.GetTypeName().AsCString("<unknown>");
1337     if (m_objc_method_decl)
1338     {
1339         if (m_objc_method_decl->getClassInterface())
1340         {
1341             return m_objc_method_decl->getClassInterface()->getName();
1342         }
1343     }
1344     return "<unknown>";
1345 }
1346
1347 bool
1348 TypeMemberFunctionImpl::GetDescription (Stream& stream)
1349 {
1350     switch (m_kind) {
1351         case lldb::eMemberFunctionKindUnknown:
1352             return false;
1353         case lldb::eMemberFunctionKindConstructor:
1354             stream.Printf("constructor for %s", GetPrintableTypeName().c_str());
1355             break;
1356         case lldb::eMemberFunctionKindDestructor:
1357             stream.Printf("destructor for %s",  GetPrintableTypeName().c_str());
1358             break;
1359         case lldb::eMemberFunctionKindInstanceMethod:
1360             stream.Printf("instance method %s of type %s",
1361                           m_name.AsCString(),
1362                           GetPrintableTypeName().c_str());
1363             break;
1364         case lldb::eMemberFunctionKindStaticMethod:
1365             stream.Printf("static method %s of type %s",
1366                           m_name.AsCString(),
1367                           GetPrintableTypeName().c_str());
1368             break;
1369     }
1370     return true;
1371 }
1372
1373 ClangASTType
1374 TypeMemberFunctionImpl::GetReturnType () const
1375 {
1376     if (m_type)
1377         return m_type.GetFunctionReturnType();
1378     if (m_objc_method_decl)
1379         return ClangASTType(&m_objc_method_decl->getASTContext(),m_objc_method_decl->getReturnType().getAsOpaquePtr());
1380     return ClangASTType();
1381 }
1382
1383 size_t
1384 TypeMemberFunctionImpl::GetNumArguments () const
1385 {
1386     if (m_type)
1387         return m_type.GetNumberOfFunctionArguments();
1388     if (m_objc_method_decl)
1389         return m_objc_method_decl->param_size();
1390     return 0;
1391 }
1392
1393 ClangASTType
1394 TypeMemberFunctionImpl::GetArgumentAtIndex (size_t idx) const
1395 {
1396     if (m_type)
1397         return m_type.GetFunctionArgumentAtIndex (idx);
1398     if (m_objc_method_decl)
1399     {
1400         if (idx < m_objc_method_decl->param_size())
1401             return ClangASTType(&m_objc_method_decl->getASTContext(), m_objc_method_decl->parameters()[idx]->getOriginalType().getAsOpaquePtr());
1402     }
1403     return ClangASTType();
1404 }
1405
1406 TypeEnumMemberImpl::TypeEnumMemberImpl (const clang::EnumConstantDecl* enum_member_decl,
1407                                         const lldb_private::ClangASTType& integer_type) :
1408     m_integer_type_sp(),
1409     m_name(),
1410     m_value(),
1411     m_valid(false)
1412
1413 {
1414     if (enum_member_decl)
1415     {
1416         m_integer_type_sp.reset(new TypeImpl(integer_type));
1417         m_name = ConstString(enum_member_decl->getNameAsString().c_str());
1418         m_value = enum_member_decl->getInitVal();
1419         m_valid = true;
1420     }
1421 }