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