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