]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Symbol/Type.cpp
Merge clang 7.0.1 and several follow-up changes
[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 context
391     // (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, reinterpret_cast<uint8_t *>(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 so we need to update it to say that we
537     // now have a forward declaration since that is what we created above.
538     if (m_compiler_type.IsValid())
539       m_flags.compiler_type_resolve_state = eResolveStateForward;
540   }
541
542   // Check if we have a forward reference to a class/struct/union/enum?
543   if (compiler_type_resolve_state == eResolveStateLayout ||
544       compiler_type_resolve_state == eResolveStateFull) {
545     // Check if we have a forward reference to a class/struct/union/enum?
546     if (m_compiler_type.IsValid() &&
547         m_flags.compiler_type_resolve_state < compiler_type_resolve_state) {
548       m_flags.compiler_type_resolve_state = eResolveStateFull;
549       if (!m_compiler_type.IsDefined()) {
550         // We have a forward declaration, we need to resolve it to a complete
551         // definition.
552         m_symbol_file->CompleteType(m_compiler_type);
553       }
554     }
555   }
556
557   // If we have an encoding type, then we need to make sure it is resolved
558   // appropriately.
559   if (m_encoding_uid != LLDB_INVALID_UID) {
560     if (encoding_type == nullptr)
561       encoding_type = GetEncodingType();
562     if (encoding_type) {
563       ResolveState encoding_compiler_type_resolve_state =
564           compiler_type_resolve_state;
565
566       if (compiler_type_resolve_state == eResolveStateLayout) {
567         switch (m_encoding_uid_type) {
568         case eEncodingIsPointerUID:
569         case eEncodingIsLValueReferenceUID:
570         case eEncodingIsRValueReferenceUID:
571           encoding_compiler_type_resolve_state = eResolveStateForward;
572           break;
573         default:
574           break;
575         }
576       }
577       encoding_type->ResolveClangType(encoding_compiler_type_resolve_state);
578     }
579   }
580   return m_compiler_type.IsValid();
581 }
582 uint32_t Type::GetEncodingMask() {
583   uint32_t encoding_mask = 1u << m_encoding_uid_type;
584   Type *encoding_type = GetEncodingType();
585   assert(encoding_type != this);
586   if (encoding_type)
587     encoding_mask |= encoding_type->GetEncodingMask();
588   return encoding_mask;
589 }
590
591 CompilerType Type::GetFullCompilerType() {
592   ResolveClangType(eResolveStateFull);
593   return m_compiler_type;
594 }
595
596 CompilerType Type::GetLayoutCompilerType() {
597   ResolveClangType(eResolveStateLayout);
598   return m_compiler_type;
599 }
600
601 CompilerType Type::GetForwardCompilerType() {
602   ResolveClangType(eResolveStateForward);
603   return m_compiler_type;
604 }
605
606 int Type::Compare(const Type &a, const Type &b) {
607   // Just compare the UID values for now...
608   lldb::user_id_t a_uid = a.GetID();
609   lldb::user_id_t b_uid = b.GetID();
610   if (a_uid < b_uid)
611     return -1;
612   if (a_uid > b_uid)
613     return 1;
614   return 0;
615 }
616
617 ConstString Type::GetQualifiedName() {
618   return GetForwardCompilerType().GetConstTypeName();
619 }
620
621 bool Type::GetTypeScopeAndBasename(const llvm::StringRef& name,
622                                    llvm::StringRef &scope,
623                                    llvm::StringRef &basename,
624                                    TypeClass &type_class) {
625   type_class = eTypeClassAny;
626
627   if (name.empty())
628     return false;
629
630   basename = name;
631   if (basename.consume_front("struct "))
632     type_class = eTypeClassStruct;
633   else if (basename.consume_front("class "))
634     type_class = eTypeClassClass;
635   else if (basename.consume_front("union "))
636     type_class = eTypeClassUnion;
637   else if (basename.consume_front("enum "))
638     type_class = eTypeClassEnumeration;
639   else if (basename.consume_front("typedef "))
640     type_class = eTypeClassTypedef;
641
642   size_t namespace_separator = basename.find("::");
643   if (namespace_separator == llvm::StringRef::npos)
644     return false;
645
646   size_t template_begin = basename.find('<');
647   while (namespace_separator != llvm::StringRef::npos) {
648     if (template_begin != llvm::StringRef::npos &&
649         namespace_separator > template_begin) {
650       size_t template_depth = 1;
651       llvm::StringRef template_arg =
652           basename.drop_front(template_begin + 1);
653       while (template_depth > 0 && !template_arg.empty()) {
654         if (template_arg.front() == '<')
655           template_depth++;
656         else if (template_arg.front() == '>')
657           template_depth--;
658         template_arg = template_arg.drop_front(1);
659       }
660       if (template_depth != 0)
661         return false; // We have an invalid type name. Bail out.
662       if (template_arg.empty())
663         break; // The template ends at the end of the full name.
664       basename = template_arg;
665     } else {
666       basename = basename.drop_front(namespace_separator + 2);
667     }
668     template_begin = basename.find('<');
669     namespace_separator = basename.find("::");
670   }
671   if (basename.size() < name.size()) {
672     scope = name.take_front(name.size() - basename.size());
673     return true;
674   }
675   return false;
676 }
677
678 ModuleSP Type::GetModule() {
679   if (m_symbol_file)
680     return m_symbol_file->GetObjectFile()->GetModule();
681   return ModuleSP();
682 }
683
684 TypeAndOrName::TypeAndOrName() : m_type_pair(), m_type_name() {}
685
686 TypeAndOrName::TypeAndOrName(TypeSP &in_type_sp) : m_type_pair(in_type_sp) {
687   if (in_type_sp)
688     m_type_name = in_type_sp->GetName();
689 }
690
691 TypeAndOrName::TypeAndOrName(const char *in_type_str)
692     : m_type_name(in_type_str) {}
693
694 TypeAndOrName::TypeAndOrName(const TypeAndOrName &rhs)
695     : m_type_pair(rhs.m_type_pair), m_type_name(rhs.m_type_name) {}
696
697 TypeAndOrName::TypeAndOrName(ConstString &in_type_const_string)
698     : m_type_name(in_type_const_string) {}
699
700 TypeAndOrName &TypeAndOrName::operator=(const TypeAndOrName &rhs) {
701   if (this != &rhs) {
702     m_type_name = rhs.m_type_name;
703     m_type_pair = rhs.m_type_pair;
704   }
705   return *this;
706 }
707
708 bool TypeAndOrName::operator==(const TypeAndOrName &other) const {
709   if (m_type_pair != other.m_type_pair)
710     return false;
711   if (m_type_name != other.m_type_name)
712     return false;
713   return true;
714 }
715
716 bool TypeAndOrName::operator!=(const TypeAndOrName &other) const {
717   if (m_type_pair != other.m_type_pair)
718     return true;
719   if (m_type_name != other.m_type_name)
720     return true;
721   return false;
722 }
723
724 ConstString TypeAndOrName::GetName() const {
725   if (m_type_name)
726     return m_type_name;
727   if (m_type_pair)
728     return m_type_pair.GetName();
729   return ConstString("<invalid>");
730 }
731
732 void TypeAndOrName::SetName(const ConstString &type_name) {
733   m_type_name = type_name;
734 }
735
736 void TypeAndOrName::SetName(const char *type_name_cstr) {
737   m_type_name.SetCString(type_name_cstr);
738 }
739
740 void TypeAndOrName::SetTypeSP(lldb::TypeSP type_sp) {
741   m_type_pair.SetType(type_sp);
742   if (m_type_pair)
743     m_type_name = m_type_pair.GetName();
744 }
745
746 void TypeAndOrName::SetCompilerType(CompilerType compiler_type) {
747   m_type_pair.SetType(compiler_type);
748   if (m_type_pair)
749     m_type_name = m_type_pair.GetName();
750 }
751
752 bool TypeAndOrName::IsEmpty() const {
753   if ((bool)m_type_name || (bool)m_type_pair)
754     return false;
755   else
756     return true;
757 }
758
759 void TypeAndOrName::Clear() {
760   m_type_name.Clear();
761   m_type_pair.Clear();
762 }
763
764 bool TypeAndOrName::HasName() const { return (bool)m_type_name; }
765
766 bool TypeAndOrName::HasTypeSP() const {
767   return m_type_pair.GetTypeSP().get() != nullptr;
768 }
769
770 bool TypeAndOrName::HasCompilerType() const {
771   return m_type_pair.GetCompilerType().IsValid();
772 }
773
774 TypeImpl::TypeImpl() : m_module_wp(), m_static_type(), m_dynamic_type() {}
775
776 TypeImpl::TypeImpl(const TypeImpl &rhs)
777     : m_module_wp(rhs.m_module_wp), m_static_type(rhs.m_static_type),
778       m_dynamic_type(rhs.m_dynamic_type) {}
779
780 TypeImpl::TypeImpl(const lldb::TypeSP &type_sp)
781     : m_module_wp(), m_static_type(), m_dynamic_type() {
782   SetType(type_sp);
783 }
784
785 TypeImpl::TypeImpl(const CompilerType &compiler_type)
786     : m_module_wp(), m_static_type(), m_dynamic_type() {
787   SetType(compiler_type);
788 }
789
790 TypeImpl::TypeImpl(const lldb::TypeSP &type_sp, const CompilerType &dynamic)
791     : m_module_wp(), m_static_type(type_sp), m_dynamic_type(dynamic) {
792   SetType(type_sp, dynamic);
793 }
794
795 TypeImpl::TypeImpl(const CompilerType &static_type,
796                    const CompilerType &dynamic_type)
797     : m_module_wp(), m_static_type(), m_dynamic_type() {
798   SetType(static_type, dynamic_type);
799 }
800
801 TypeImpl::TypeImpl(const TypePair &pair, const CompilerType &dynamic)
802     : m_module_wp(), m_static_type(), m_dynamic_type() {
803   SetType(pair, dynamic);
804 }
805
806 void TypeImpl::SetType(const lldb::TypeSP &type_sp) {
807   m_static_type.SetType(type_sp);
808   if (type_sp)
809     m_module_wp = type_sp->GetModule();
810   else
811     m_module_wp = lldb::ModuleWP();
812 }
813
814 void TypeImpl::SetType(const CompilerType &compiler_type) {
815   m_module_wp = lldb::ModuleWP();
816   m_static_type.SetType(compiler_type);
817 }
818
819 void TypeImpl::SetType(const lldb::TypeSP &type_sp,
820                        const CompilerType &dynamic) {
821   SetType(type_sp);
822   m_dynamic_type = dynamic;
823 }
824
825 void TypeImpl::SetType(const CompilerType &compiler_type,
826                        const CompilerType &dynamic) {
827   m_module_wp = lldb::ModuleWP();
828   m_static_type.SetType(compiler_type);
829   m_dynamic_type = dynamic;
830 }
831
832 void TypeImpl::SetType(const TypePair &pair, const CompilerType &dynamic) {
833   m_module_wp = pair.GetModule();
834   m_static_type = pair;
835   m_dynamic_type = dynamic;
836 }
837
838 TypeImpl &TypeImpl::operator=(const TypeImpl &rhs) {
839   if (rhs != *this) {
840     m_module_wp = rhs.m_module_wp;
841     m_static_type = rhs.m_static_type;
842     m_dynamic_type = rhs.m_dynamic_type;
843   }
844   return *this;
845 }
846
847 bool TypeImpl::CheckModule(lldb::ModuleSP &module_sp) const {
848   // Check if we have a module for this type. If we do and the shared pointer
849   // is can be successfully initialized with m_module_wp, return true. Else
850   // return false if we didn't have a module, or if we had a module and it has
851   // been deleted. Any functions doing anything with a TypeSP in this TypeImpl
852   // class should call this function and only do anything with the ivars if
853   // this function returns true. If we have a module, the "module_sp" will be
854   // filled in with a strong reference to the module so that the module will at
855   // least stay around long enough for the type query to succeed.
856   module_sp = m_module_wp.lock();
857   if (!module_sp) {
858     lldb::ModuleWP empty_module_wp;
859     // If either call to "std::weak_ptr::owner_before(...) value returns true,
860     // this indicates that m_module_wp once contained (possibly still does) a
861     // reference to a valid shared pointer. This helps us know if we had a
862     // valid reference to a section which is now invalid because the module it
863     // was in was deleted
864     if (empty_module_wp.owner_before(m_module_wp) ||
865         m_module_wp.owner_before(empty_module_wp)) {
866       // m_module_wp had a valid reference to a module, but all strong
867       // references have been released and the module has been deleted
868       return false;
869     }
870   }
871   // We either successfully locked the module, or didn't have one to begin with
872   return true;
873 }
874
875 bool TypeImpl::operator==(const TypeImpl &rhs) const {
876   return m_static_type == rhs.m_static_type &&
877          m_dynamic_type == rhs.m_dynamic_type;
878 }
879
880 bool TypeImpl::operator!=(const TypeImpl &rhs) const {
881   return m_static_type != rhs.m_static_type ||
882          m_dynamic_type != rhs.m_dynamic_type;
883 }
884
885 bool TypeImpl::IsValid() const {
886   // just a name is not valid
887   ModuleSP module_sp;
888   if (CheckModule(module_sp))
889     return m_static_type.IsValid() || m_dynamic_type.IsValid();
890   return false;
891 }
892
893 TypeImpl::operator bool() const { return IsValid(); }
894
895 void TypeImpl::Clear() {
896   m_module_wp = lldb::ModuleWP();
897   m_static_type.Clear();
898   m_dynamic_type.Clear();
899 }
900
901 ConstString TypeImpl::GetName() const {
902   ModuleSP module_sp;
903   if (CheckModule(module_sp)) {
904     if (m_dynamic_type)
905       return m_dynamic_type.GetTypeName();
906     return m_static_type.GetName();
907   }
908   return ConstString();
909 }
910
911 ConstString TypeImpl::GetDisplayTypeName() const {
912   ModuleSP module_sp;
913   if (CheckModule(module_sp)) {
914     if (m_dynamic_type)
915       return m_dynamic_type.GetDisplayTypeName();
916     return m_static_type.GetDisplayTypeName();
917   }
918   return ConstString();
919 }
920
921 TypeImpl TypeImpl::GetPointerType() const {
922   ModuleSP module_sp;
923   if (CheckModule(module_sp)) {
924     if (m_dynamic_type.IsValid()) {
925       return TypeImpl(m_static_type.GetPointerType(),
926                       m_dynamic_type.GetPointerType());
927     }
928     return TypeImpl(m_static_type.GetPointerType());
929   }
930   return TypeImpl();
931 }
932
933 TypeImpl TypeImpl::GetPointeeType() const {
934   ModuleSP module_sp;
935   if (CheckModule(module_sp)) {
936     if (m_dynamic_type.IsValid()) {
937       return TypeImpl(m_static_type.GetPointeeType(),
938                       m_dynamic_type.GetPointeeType());
939     }
940     return TypeImpl(m_static_type.GetPointeeType());
941   }
942   return TypeImpl();
943 }
944
945 TypeImpl TypeImpl::GetReferenceType() const {
946   ModuleSP module_sp;
947   if (CheckModule(module_sp)) {
948     if (m_dynamic_type.IsValid()) {
949       return TypeImpl(m_static_type.GetReferenceType(),
950                       m_dynamic_type.GetLValueReferenceType());
951     }
952     return TypeImpl(m_static_type.GetReferenceType());
953   }
954   return TypeImpl();
955 }
956
957 TypeImpl TypeImpl::GetTypedefedType() const {
958   ModuleSP module_sp;
959   if (CheckModule(module_sp)) {
960     if (m_dynamic_type.IsValid()) {
961       return TypeImpl(m_static_type.GetTypedefedType(),
962                       m_dynamic_type.GetTypedefedType());
963     }
964     return TypeImpl(m_static_type.GetTypedefedType());
965   }
966   return TypeImpl();
967 }
968
969 TypeImpl TypeImpl::GetDereferencedType() const {
970   ModuleSP module_sp;
971   if (CheckModule(module_sp)) {
972     if (m_dynamic_type.IsValid()) {
973       return TypeImpl(m_static_type.GetDereferencedType(),
974                       m_dynamic_type.GetNonReferenceType());
975     }
976     return TypeImpl(m_static_type.GetDereferencedType());
977   }
978   return TypeImpl();
979 }
980
981 TypeImpl TypeImpl::GetUnqualifiedType() const {
982   ModuleSP module_sp;
983   if (CheckModule(module_sp)) {
984     if (m_dynamic_type.IsValid()) {
985       return TypeImpl(m_static_type.GetUnqualifiedType(),
986                       m_dynamic_type.GetFullyUnqualifiedType());
987     }
988     return TypeImpl(m_static_type.GetUnqualifiedType());
989   }
990   return TypeImpl();
991 }
992
993 TypeImpl TypeImpl::GetCanonicalType() const {
994   ModuleSP module_sp;
995   if (CheckModule(module_sp)) {
996     if (m_dynamic_type.IsValid()) {
997       return TypeImpl(m_static_type.GetCanonicalType(),
998                       m_dynamic_type.GetCanonicalType());
999     }
1000     return TypeImpl(m_static_type.GetCanonicalType());
1001   }
1002   return TypeImpl();
1003 }
1004
1005 CompilerType TypeImpl::GetCompilerType(bool prefer_dynamic) {
1006   ModuleSP module_sp;
1007   if (CheckModule(module_sp)) {
1008     if (prefer_dynamic) {
1009       if (m_dynamic_type.IsValid())
1010         return m_dynamic_type;
1011     }
1012     return m_static_type.GetCompilerType();
1013   }
1014   return CompilerType();
1015 }
1016
1017 TypeSystem *TypeImpl::GetTypeSystem(bool prefer_dynamic) {
1018   ModuleSP module_sp;
1019   if (CheckModule(module_sp)) {
1020     if (prefer_dynamic) {
1021       if (m_dynamic_type.IsValid())
1022         return m_dynamic_type.GetTypeSystem();
1023     }
1024     return m_static_type.GetCompilerType().GetTypeSystem();
1025   }
1026   return NULL;
1027 }
1028
1029 bool TypeImpl::GetDescription(lldb_private::Stream &strm,
1030                               lldb::DescriptionLevel description_level) {
1031   ModuleSP module_sp;
1032   if (CheckModule(module_sp)) {
1033     if (m_dynamic_type.IsValid()) {
1034       strm.Printf("Dynamic:\n");
1035       m_dynamic_type.DumpTypeDescription(&strm);
1036       strm.Printf("\nStatic:\n");
1037     }
1038     m_static_type.GetCompilerType().DumpTypeDescription(&strm);
1039   } else {
1040     strm.PutCString("Invalid TypeImpl module for type has been deleted\n");
1041   }
1042   return true;
1043 }
1044
1045 bool TypeMemberFunctionImpl::IsValid() {
1046   return m_type.IsValid() && m_kind != lldb::eMemberFunctionKindUnknown;
1047 }
1048
1049 ConstString TypeMemberFunctionImpl::GetName() const { return m_name; }
1050
1051 ConstString TypeMemberFunctionImpl::GetMangledName() const {
1052   return m_decl.GetMangledName();
1053 }
1054
1055 CompilerType TypeMemberFunctionImpl::GetType() const { return m_type; }
1056
1057 lldb::MemberFunctionKind TypeMemberFunctionImpl::GetKind() const {
1058   return m_kind;
1059 }
1060
1061 bool TypeMemberFunctionImpl::GetDescription(Stream &stream) {
1062   switch (m_kind) {
1063   case lldb::eMemberFunctionKindUnknown:
1064     return false;
1065   case lldb::eMemberFunctionKindConstructor:
1066     stream.Printf("constructor for %s",
1067                   m_type.GetTypeName().AsCString("<unknown>"));
1068     break;
1069   case lldb::eMemberFunctionKindDestructor:
1070     stream.Printf("destructor for %s",
1071                   m_type.GetTypeName().AsCString("<unknown>"));
1072     break;
1073   case lldb::eMemberFunctionKindInstanceMethod:
1074     stream.Printf("instance method %s of type %s", m_name.AsCString(),
1075                   m_decl.GetDeclContext().GetName().AsCString());
1076     break;
1077   case lldb::eMemberFunctionKindStaticMethod:
1078     stream.Printf("static method %s of type %s", m_name.AsCString(),
1079                   m_decl.GetDeclContext().GetName().AsCString());
1080     break;
1081   }
1082   return true;
1083 }
1084
1085 CompilerType TypeMemberFunctionImpl::GetReturnType() const {
1086   if (m_type)
1087     return m_type.GetFunctionReturnType();
1088   return m_decl.GetFunctionReturnType();
1089 }
1090
1091 size_t TypeMemberFunctionImpl::GetNumArguments() const {
1092   if (m_type)
1093     return m_type.GetNumberOfFunctionArguments();
1094   else
1095     return m_decl.GetNumFunctionArguments();
1096 }
1097
1098 CompilerType TypeMemberFunctionImpl::GetArgumentAtIndex(size_t idx) const {
1099   if (m_type)
1100     return m_type.GetFunctionArgumentAtIndex(idx);
1101   else
1102     return m_decl.GetFunctionArgumentType(idx);
1103 }
1104
1105 TypeEnumMemberImpl::TypeEnumMemberImpl(const lldb::TypeImplSP &integer_type_sp,
1106                                        const ConstString &name,
1107                                        const llvm::APSInt &value)
1108     : m_integer_type_sp(integer_type_sp), m_name(name), m_value(value),
1109       m_valid((bool)name && (bool)integer_type_sp)
1110
1111 {}