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