]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Symbol/GoASTContext.cpp
MFC r345703:
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Symbol / GoASTContext.cpp
1 //===-- GoASTContext.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 #include <mutex>
11 #include <utility>
12 #include <vector>
13
14 #include "lldb/Core/DumpDataExtractor.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/PluginManager.h"
17 #include "lldb/Core/StreamFile.h"
18 #include "lldb/Core/UniqueCStringMap.h"
19 #include "lldb/Core/ValueObject.h"
20 #include "lldb/Symbol/CompilerType.h"
21 #include "lldb/Symbol/GoASTContext.h"
22 #include "lldb/Symbol/ObjectFile.h"
23 #include "lldb/Symbol/SymbolFile.h"
24 #include "lldb/Symbol/Type.h"
25 #include "lldb/Target/ExecutionContext.h"
26 #include "lldb/Target/Target.h"
27
28 #include "llvm/Support/Threading.h"
29
30 #include "Plugins/ExpressionParser/Go/GoUserExpression.h"
31 #include "Plugins/SymbolFile/DWARF/DWARFASTParserGo.h"
32
33 using namespace lldb;
34
35 namespace lldb_private {
36 class GoArray;
37 class GoFunction;
38 class GoStruct;
39
40 class GoType {
41 public:
42   enum {
43     KIND_BOOL = 1,
44     KIND_INT = 2,
45     KIND_INT8 = 3,
46     KIND_INT16 = 4,
47     KIND_INT32 = 5,
48     KIND_INT64 = 6,
49     KIND_UINT = 7,
50     KIND_UINT8 = 8,
51     KIND_UINT16 = 9,
52     KIND_UINT32 = 10,
53     KIND_UINT64 = 11,
54     KIND_UINTPTR = 12,
55     KIND_FLOAT32 = 13,
56     KIND_FLOAT64 = 14,
57     KIND_COMPLEX64 = 15,
58     KIND_COMPLEX128 = 16,
59     KIND_ARRAY = 17,
60     KIND_CHAN = 18,
61     KIND_FUNC = 19,
62     KIND_INTERFACE = 20,
63     KIND_MAP = 21,
64     KIND_PTR = 22,
65     KIND_SLICE = 23,
66     KIND_STRING = 24,
67     KIND_STRUCT = 25,
68     KIND_UNSAFEPOINTER = 26,
69     KIND_LLDB_VOID, // Extension for LLDB, not used by go runtime.
70     KIND_MASK = (1 << 5) - 1,
71     KIND_DIRECT_IFACE = 1 << 5
72   };
73   GoType(int kind, const ConstString &name)
74       : m_kind(kind & KIND_MASK), m_name(name) {
75     if (m_kind == KIND_FUNC)
76       m_kind = KIND_FUNC;
77   }
78   virtual ~GoType() {}
79
80   int GetGoKind() const { return m_kind; }
81   const ConstString &GetName() const { return m_name; }
82   virtual CompilerType GetElementType() const { return CompilerType(); }
83
84   bool IsTypedef() const {
85     switch (m_kind) {
86     case KIND_CHAN:
87     case KIND_MAP:
88     case KIND_INTERFACE:
89       return true;
90     default:
91       return false;
92     }
93   }
94
95   GoArray *GetArray();
96   GoFunction *GetFunction();
97   GoStruct *GetStruct();
98
99 private:
100   int m_kind;
101   ConstString m_name;
102   GoType(const GoType &) = delete;
103   const GoType &operator=(const GoType &) = delete;
104 };
105
106 class GoElem : public GoType {
107 public:
108   GoElem(int kind, const ConstString &name, const CompilerType &elem)
109       : GoType(kind, name), m_elem(elem) {}
110   virtual CompilerType GetElementType() const { return m_elem; }
111
112 private:
113   // TODO: should we store this differently?
114   CompilerType m_elem;
115
116   GoElem(const GoElem &) = delete;
117   const GoElem &operator=(const GoElem &) = delete;
118 };
119
120 class GoArray : public GoElem {
121 public:
122   GoArray(const ConstString &name, uint64_t length, const CompilerType &elem)
123       : GoElem(KIND_ARRAY, name, elem), m_length(length) {}
124
125   uint64_t GetLength() const { return m_length; }
126
127 private:
128   uint64_t m_length;
129   GoArray(const GoArray &) = delete;
130   const GoArray &operator=(const GoArray &) = delete;
131 };
132
133 class GoFunction : public GoType {
134 public:
135   GoFunction(const ConstString &name, bool is_variadic)
136       : GoType(KIND_FUNC, name), m_is_variadic(is_variadic) {}
137
138   bool IsVariadic() const { return m_is_variadic; }
139
140 private:
141   bool m_is_variadic;
142   GoFunction(const GoFunction &) = delete;
143   const GoFunction &operator=(const GoFunction &) = delete;
144 };
145
146 class GoStruct : public GoType {
147 public:
148   struct Field {
149     Field(const ConstString &name, const CompilerType &type, uint64_t offset)
150         : m_name(name), m_type(type), m_byte_offset(offset) {}
151     ConstString m_name;
152     CompilerType m_type;
153     uint64_t m_byte_offset;
154   };
155
156   GoStruct(int kind, const ConstString &name, int64_t byte_size)
157       : GoType(kind == 0 ? KIND_STRUCT : kind, name), m_is_complete(false),
158         m_byte_size(byte_size) {}
159
160   uint32_t GetNumFields() const { return m_fields.size(); }
161
162   const Field *GetField(uint32_t i) const {
163     if (i < m_fields.size())
164       return &m_fields[i];
165     return nullptr;
166   }
167
168   void AddField(const ConstString &name, const CompilerType &type,
169                 uint64_t offset) {
170     m_fields.push_back(Field(name, type, offset));
171   }
172
173   bool IsComplete() const { return m_is_complete; }
174
175   void SetComplete() { m_is_complete = true; }
176
177   int64_t GetByteSize() const { return m_byte_size; }
178
179 private:
180   bool m_is_complete;
181   int64_t m_byte_size;
182   std::vector<Field> m_fields;
183
184   GoStruct(const GoStruct &) = delete;
185   const GoStruct &operator=(const GoStruct &) = delete;
186 };
187
188 GoArray *GoType::GetArray() {
189   if (m_kind == KIND_ARRAY) {
190     return static_cast<GoArray *>(this);
191   }
192   return nullptr;
193 }
194
195 GoFunction *GoType::GetFunction() {
196   if (m_kind == KIND_FUNC) {
197     return static_cast<GoFunction *>(this);
198   }
199   return nullptr;
200 }
201
202 GoStruct *GoType::GetStruct() {
203   switch (m_kind) {
204   case KIND_STRING:
205   case KIND_STRUCT:
206   case KIND_SLICE:
207     return static_cast<GoStruct *>(this);
208   }
209   return nullptr;
210 }
211 } // namespace lldb_private
212 using namespace lldb_private;
213
214 GoASTContext::GoASTContext()
215     : TypeSystem(eKindGo), m_pointer_byte_size(0), m_int_byte_size(0),
216       m_types(new TypeMap) {}
217 GoASTContext::~GoASTContext() {}
218
219 //------------------------------------------------------------------
220 // PluginInterface functions
221 //------------------------------------------------------------------
222
223 ConstString GoASTContext::GetPluginNameStatic() { return ConstString("go"); }
224
225 ConstString GoASTContext::GetPluginName() {
226   return GoASTContext::GetPluginNameStatic();
227 }
228
229 uint32_t GoASTContext::GetPluginVersion() { return 1; }
230
231 lldb::TypeSystemSP GoASTContext::CreateInstance(lldb::LanguageType language,
232                                                 Module *module,
233                                                 Target *target) {
234   if (language == eLanguageTypeGo) {
235     ArchSpec arch;
236     std::shared_ptr<GoASTContext> go_ast_sp;
237     if (module) {
238       arch = module->GetArchitecture();
239       go_ast_sp = std::shared_ptr<GoASTContext>(new GoASTContext);
240     } else if (target) {
241       arch = target->GetArchitecture();
242       go_ast_sp = std::shared_ptr<GoASTContextForExpr>(
243           new GoASTContextForExpr(target->shared_from_this()));
244     }
245
246     if (arch.IsValid()) {
247       go_ast_sp->SetAddressByteSize(arch.GetAddressByteSize());
248       return go_ast_sp;
249     }
250   }
251   return lldb::TypeSystemSP();
252 }
253
254 void GoASTContext::EnumerateSupportedLanguages(
255     std::set<lldb::LanguageType> &languages_for_types,
256     std::set<lldb::LanguageType> &languages_for_expressions) {
257   static std::vector<lldb::LanguageType> s_supported_languages_for_types(
258       {lldb::eLanguageTypeGo});
259
260   static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
261       {});
262
263   languages_for_types.insert(s_supported_languages_for_types.begin(),
264                              s_supported_languages_for_types.end());
265   languages_for_expressions.insert(
266       s_supported_languages_for_expressions.begin(),
267       s_supported_languages_for_expressions.end());
268 }
269
270 void GoASTContext::Initialize() {
271   PluginManager::RegisterPlugin(GetPluginNameStatic(), "AST context plug-in",
272                                 CreateInstance, EnumerateSupportedLanguages);
273 }
274
275 void GoASTContext::Terminate() {
276   PluginManager::UnregisterPlugin(CreateInstance);
277 }
278
279 //----------------------------------------------------------------------
280 // Tests
281 //----------------------------------------------------------------------
282
283 bool GoASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
284                                CompilerType *element_type, uint64_t *size,
285                                bool *is_incomplete) {
286   if (element_type)
287     element_type->Clear();
288   if (size)
289     *size = 0;
290   if (is_incomplete)
291     *is_incomplete = false;
292   GoArray *array = static_cast<GoType *>(type)->GetArray();
293   if (array) {
294     if (size)
295       *size = array->GetLength();
296     if (element_type)
297       *element_type = array->GetElementType();
298     return true;
299   }
300   return false;
301 }
302
303 bool GoASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
304                                 CompilerType *element_type, uint64_t *size) {
305   if (element_type)
306     element_type->Clear();
307   if (size)
308     *size = 0;
309   return false;
310 }
311
312 bool GoASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
313   int kind = static_cast<GoType *>(type)->GetGoKind();
314   if (kind < GoType::KIND_ARRAY)
315     return false;
316   if (kind == GoType::KIND_PTR)
317     return false;
318   if (kind == GoType::KIND_CHAN)
319     return false;
320   if (kind == GoType::KIND_MAP)
321     return false;
322   if (kind == GoType::KIND_STRING)
323     return false;
324   if (kind == GoType::KIND_UNSAFEPOINTER)
325     return false;
326   return true;
327 }
328
329 bool GoASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
330   return false;
331 }
332
333 bool GoASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
334   // Go's DWARF doesn't distinguish between rune and int32.
335   return false;
336 }
337
338 bool GoASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
339   if (!type)
340     return false;
341   GoType *t = static_cast<GoType *>(type);
342   if (GoStruct *s = t->GetStruct())
343     return s->IsComplete();
344   if (t->IsTypedef() || t->GetGoKind() == GoType::KIND_PTR)
345     return t->GetElementType().IsCompleteType();
346   return true;
347 }
348
349 bool GoASTContext::IsConst(lldb::opaque_compiler_type_t type) { return false; }
350
351 bool GoASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
352                                  uint32_t &length) {
353   return false;
354 }
355
356 bool GoASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
357   return type != nullptr;
358 }
359
360 bool GoASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
361                                        uint32_t &count, bool &is_complex) {
362   int kind = static_cast<GoType *>(type)->GetGoKind();
363   if (kind >= GoType::KIND_FLOAT32 && kind <= GoType::KIND_COMPLEX128) {
364     if (kind >= GoType::KIND_COMPLEX64) {
365       is_complex = true;
366       count = 2;
367     } else {
368       is_complex = false;
369       count = 1;
370     }
371     return true;
372   }
373   count = 0;
374   is_complex = false;
375   return false;
376 }
377
378 bool GoASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
379                                   bool *is_variadic_ptr) {
380   GoFunction *func = static_cast<GoType *>(type)->GetFunction();
381   if (func) {
382     if (is_variadic_ptr)
383       *is_variadic_ptr = func->IsVariadic();
384     return true;
385   }
386   if (is_variadic_ptr)
387     *is_variadic_ptr = false;
388   return false;
389 }
390
391 uint32_t GoASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
392                                               CompilerType *base_type_ptr) {
393   return false;
394 }
395
396 size_t
397 GoASTContext::GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) {
398   return 0;
399 }
400
401 CompilerType
402 GoASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
403                                          const size_t index) {
404   return CompilerType();
405 }
406
407 bool GoASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
408   return IsFunctionType(type);
409 }
410
411 bool GoASTContext::IsBlockPointerType(lldb::opaque_compiler_type_t type,
412                                       CompilerType *function_pointer_type_ptr) {
413   return false;
414 }
415
416 bool GoASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
417                                  bool &is_signed) {
418   is_signed = false;
419   // TODO: Is bool an integer?
420   if (type) {
421     int kind = static_cast<GoType *>(type)->GetGoKind();
422     if (kind <= GoType::KIND_UINTPTR) {
423       is_signed = (kind != GoType::KIND_BOOL) & (kind <= GoType::KIND_INT64);
424       return true;
425     }
426   }
427   return false;
428 }
429
430 bool GoASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
431   return false;
432 }
433
434 bool GoASTContext::IsPossibleDynamicType(
435     lldb::opaque_compiler_type_t type,
436     CompilerType *target_type, // Can pass NULL
437     bool check_cplusplus, bool check_objc) {
438   if (target_type)
439     target_type->Clear();
440   if (type)
441     return static_cast<GoType *>(type)->GetGoKind() == GoType::KIND_INTERFACE;
442   return false;
443 }
444
445 bool GoASTContext::IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) {
446   return false;
447 }
448
449 bool GoASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
450                                  CompilerType *pointee_type) {
451   if (!type)
452     return false;
453   GoType *t = static_cast<GoType *>(type);
454   if (pointee_type) {
455     *pointee_type = t->GetElementType();
456   }
457   switch (t->GetGoKind()) {
458   case GoType::KIND_PTR:
459   case GoType::KIND_UNSAFEPOINTER:
460   case GoType::KIND_CHAN:
461   case GoType::KIND_MAP:
462     // TODO: is function a pointer?
463     return true;
464   default:
465     return false;
466   }
467 }
468
469 bool GoASTContext::IsPointerOrReferenceType(lldb::opaque_compiler_type_t type,
470                                             CompilerType *pointee_type) {
471   return IsPointerType(type, pointee_type);
472 }
473
474 bool GoASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
475                                    CompilerType *pointee_type,
476                                    bool *is_rvalue) {
477   return false;
478 }
479
480 bool GoASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
481   return !IsAggregateType(type);
482 }
483
484 bool GoASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
485   if (type)
486     return static_cast<GoType *>(type)->IsTypedef();
487   return false;
488 }
489
490 bool GoASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
491   if (!type)
492     return false;
493   return static_cast<GoType *>(type)->GetGoKind() == GoType::KIND_LLDB_VOID;
494 }
495
496 bool GoASTContext::SupportsLanguage(lldb::LanguageType language) {
497   return language == eLanguageTypeGo;
498 }
499
500 //----------------------------------------------------------------------
501 // Type Completion
502 //----------------------------------------------------------------------
503
504 bool GoASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
505   if (!type)
506     return false;
507   GoType *t = static_cast<GoType *>(type);
508   if (t->IsTypedef() || t->GetGoKind() == GoType::KIND_PTR || t->GetArray())
509     return t->GetElementType().GetCompleteType();
510   if (GoStruct *s = t->GetStruct()) {
511     if (s->IsComplete())
512       return true;
513     CompilerType compiler_type(this, s);
514     SymbolFile *symbols = GetSymbolFile();
515     return symbols && symbols->CompleteType(compiler_type);
516   }
517   return true;
518 }
519
520 //----------------------------------------------------------------------
521 // AST related queries
522 //----------------------------------------------------------------------
523
524 uint32_t GoASTContext::GetPointerByteSize() { return m_pointer_byte_size; }
525
526 //----------------------------------------------------------------------
527 // Accessors
528 //----------------------------------------------------------------------
529
530 ConstString GoASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
531   if (type)
532     return static_cast<GoType *>(type)->GetName();
533   return ConstString();
534 }
535
536 uint32_t
537 GoASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
538                           CompilerType *pointee_or_element_compiler_type) {
539   if (pointee_or_element_compiler_type)
540     pointee_or_element_compiler_type->Clear();
541   if (!type)
542     return 0;
543   GoType *t = static_cast<GoType *>(type);
544   if (pointee_or_element_compiler_type)
545     *pointee_or_element_compiler_type = t->GetElementType();
546   int kind = t->GetGoKind();
547   if (kind == GoType::KIND_ARRAY)
548     return eTypeHasChildren | eTypeIsArray;
549   if (kind < GoType::KIND_ARRAY) {
550     uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
551     if (kind < GoType::KIND_FLOAT32) {
552       builtin_type_flags |= eTypeIsInteger | eTypeIsScalar;
553       if (kind >= GoType::KIND_INT && kind <= GoType::KIND_INT64)
554         builtin_type_flags |= eTypeIsSigned;
555     } else {
556       builtin_type_flags |= eTypeIsFloat;
557       if (kind < GoType::KIND_COMPLEX64)
558         builtin_type_flags |= eTypeIsComplex;
559       else
560         builtin_type_flags |= eTypeIsScalar;
561     }
562     return builtin_type_flags;
563   }
564   if (kind == GoType::KIND_STRING)
565     return eTypeHasValue | eTypeIsBuiltIn;
566   if (kind == GoType::KIND_FUNC)
567     return eTypeIsFuncPrototype | eTypeHasValue;
568   if (IsPointerType(type))
569     return eTypeIsPointer | eTypeHasValue | eTypeHasChildren;
570   if (kind == GoType::KIND_LLDB_VOID)
571     return 0;
572   return eTypeHasChildren | eTypeIsStructUnion;
573 }
574
575 lldb::TypeClass GoASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
576   if (!type)
577     return eTypeClassInvalid;
578   int kind = static_cast<GoType *>(type)->GetGoKind();
579   if (kind == GoType::KIND_FUNC)
580     return eTypeClassFunction;
581   if (IsPointerType(type))
582     return eTypeClassPointer;
583   if (kind < GoType::KIND_COMPLEX64)
584     return eTypeClassBuiltin;
585   if (kind <= GoType::KIND_COMPLEX128)
586     return eTypeClassComplexFloat;
587   if (kind == GoType::KIND_LLDB_VOID)
588     return eTypeClassInvalid;
589   return eTypeClassStruct;
590 }
591
592 lldb::BasicType
593 GoASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
594   ConstString name = GetTypeName(type);
595   if (name) {
596     typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
597     static TypeNameToBasicTypeMap g_type_map;
598     static llvm::once_flag g_once_flag;
599     llvm::call_once(g_once_flag, []() {
600       // "void"
601       g_type_map.Append(ConstString("void"), eBasicTypeVoid);
602       // "int"
603       g_type_map.Append(ConstString("int"), eBasicTypeInt);
604       g_type_map.Append(ConstString("uint"), eBasicTypeUnsignedInt);
605
606       // Miscellaneous
607       g_type_map.Append(ConstString("bool"), eBasicTypeBool);
608
609       // Others. Should these map to C types?
610       g_type_map.Append(ConstString("byte"), eBasicTypeOther);
611       g_type_map.Append(ConstString("uint8"), eBasicTypeOther);
612       g_type_map.Append(ConstString("uint16"), eBasicTypeOther);
613       g_type_map.Append(ConstString("uint32"), eBasicTypeOther);
614       g_type_map.Append(ConstString("uint64"), eBasicTypeOther);
615       g_type_map.Append(ConstString("int8"), eBasicTypeOther);
616       g_type_map.Append(ConstString("int16"), eBasicTypeOther);
617       g_type_map.Append(ConstString("int32"), eBasicTypeOther);
618       g_type_map.Append(ConstString("int64"), eBasicTypeOther);
619       g_type_map.Append(ConstString("float32"), eBasicTypeOther);
620       g_type_map.Append(ConstString("float64"), eBasicTypeOther);
621       g_type_map.Append(ConstString("uintptr"), eBasicTypeOther);
622
623       g_type_map.Sort();
624     });
625
626     return g_type_map.Find(name, eBasicTypeInvalid);
627   }
628   return eBasicTypeInvalid;
629 }
630
631 lldb::LanguageType
632 GoASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
633   return lldb::eLanguageTypeGo;
634 }
635
636 unsigned GoASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
637   return 0;
638 }
639
640 //----------------------------------------------------------------------
641 // Creating related types
642 //----------------------------------------------------------------------
643
644 CompilerType
645 GoASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
646                                   uint64_t *stride) {
647   GoArray *array = static_cast<GoType *>(type)->GetArray();
648   if (array) {
649     if (stride) {
650       *stride = array->GetElementType().GetByteSize(nullptr);
651     }
652     return array->GetElementType();
653   }
654   return CompilerType();
655 }
656
657 CompilerType GoASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
658   GoType *t = static_cast<GoType *>(type);
659   if (t->IsTypedef())
660     return t->GetElementType();
661   return CompilerType(this, type);
662 }
663
664 CompilerType
665 GoASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
666   return CompilerType(this, type);
667 }
668
669 // Returns -1 if this isn't a function of if the function doesn't have a
670 // prototype Returns a value >= 0 if there is a prototype.
671 int GoASTContext::GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) {
672   return GetNumberOfFunctionArguments(type);
673 }
674
675 CompilerType
676 GoASTContext::GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type,
677                                              size_t idx) {
678   return GetFunctionArgumentAtIndex(type, idx);
679 }
680
681 CompilerType
682 GoASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
683   CompilerType result;
684   if (type) {
685     GoType *t = static_cast<GoType *>(type);
686     if (t->GetGoKind() == GoType::KIND_FUNC)
687       result = t->GetElementType();
688   }
689   return result;
690 }
691
692 size_t GoASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
693   return 0;
694 }
695
696 TypeMemberFunctionImpl
697 GoASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
698                                        size_t idx) {
699   return TypeMemberFunctionImpl();
700 }
701
702 CompilerType
703 GoASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
704   return CompilerType(this, type);
705 }
706
707 CompilerType GoASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
708   if (!type)
709     return CompilerType();
710   return static_cast<GoType *>(type)->GetElementType();
711 }
712
713 CompilerType GoASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
714   if (!type)
715     return CompilerType();
716   ConstString type_name = GetTypeName(type);
717   ConstString pointer_name(std::string("*") + type_name.GetCString());
718   GoType *pointer = (*m_types)[pointer_name].get();
719   if (pointer == nullptr) {
720     pointer =
721         new GoElem(GoType::KIND_PTR, pointer_name, CompilerType(this, type));
722     (*m_types)[pointer_name].reset(pointer);
723   }
724   return CompilerType(this, pointer);
725 }
726
727 // If the current object represents a typedef type, get the underlying type
728 CompilerType GoASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
729   if (IsTypedefType(type))
730     return static_cast<GoType *>(type)->GetElementType();
731   return CompilerType();
732 }
733
734 //----------------------------------------------------------------------
735 // Create related types using the current type's AST
736 //----------------------------------------------------------------------
737 CompilerType GoASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
738   return CompilerType();
739 }
740
741 CompilerType
742 GoASTContext::GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding,
743                                                   size_t bit_size) {
744   return CompilerType();
745 }
746
747 //----------------------------------------------------------------------
748 // Exploring the type
749 //----------------------------------------------------------------------
750
751 uint64_t GoASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
752                                   ExecutionContextScope *exe_scope) {
753   if (!type)
754     return 0;
755   if (!GetCompleteType(type))
756     return 0;
757   GoType *t = static_cast<GoType *>(type);
758   GoArray *array = nullptr;
759   switch (t->GetGoKind()) {
760   case GoType::KIND_BOOL:
761   case GoType::KIND_INT8:
762   case GoType::KIND_UINT8:
763     return 8;
764   case GoType::KIND_INT16:
765   case GoType::KIND_UINT16:
766     return 16;
767   case GoType::KIND_INT32:
768   case GoType::KIND_UINT32:
769   case GoType::KIND_FLOAT32:
770     return 32;
771   case GoType::KIND_INT64:
772   case GoType::KIND_UINT64:
773   case GoType::KIND_FLOAT64:
774   case GoType::KIND_COMPLEX64:
775     return 64;
776   case GoType::KIND_COMPLEX128:
777     return 128;
778   case GoType::KIND_INT:
779   case GoType::KIND_UINT:
780     return m_int_byte_size * 8;
781   case GoType::KIND_UINTPTR:
782   case GoType::KIND_FUNC: // I assume this is a pointer?
783   case GoType::KIND_CHAN:
784   case GoType::KIND_PTR:
785   case GoType::KIND_UNSAFEPOINTER:
786   case GoType::KIND_MAP:
787     return m_pointer_byte_size * 8;
788   case GoType::KIND_ARRAY:
789     array = t->GetArray();
790     return array->GetLength() * array->GetElementType().GetBitSize(exe_scope);
791   case GoType::KIND_INTERFACE:
792     return t->GetElementType().GetBitSize(exe_scope);
793   case GoType::KIND_SLICE:
794   case GoType::KIND_STRING:
795   case GoType::KIND_STRUCT:
796     return t->GetStruct()->GetByteSize() * 8;
797   default:
798     assert(false);
799   }
800   return 0;
801 }
802
803 lldb::Encoding GoASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
804                                          uint64_t &count) {
805   count = 1;
806   bool is_signed;
807   if (IsIntegerType(type, is_signed))
808     return is_signed ? lldb::eEncodingSint : eEncodingUint;
809   bool is_complex;
810   uint32_t complex_count;
811   if (IsFloatingPointType(type, complex_count, is_complex)) {
812     count = complex_count;
813     return eEncodingIEEE754;
814   }
815   if (IsPointerType(type))
816     return eEncodingUint;
817   return eEncodingInvalid;
818 }
819
820 lldb::Format GoASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
821   if (!type)
822     return eFormatDefault;
823   switch (static_cast<GoType *>(type)->GetGoKind()) {
824   case GoType::KIND_BOOL:
825     return eFormatBoolean;
826   case GoType::KIND_INT:
827   case GoType::KIND_INT8:
828   case GoType::KIND_INT16:
829   case GoType::KIND_INT32:
830   case GoType::KIND_INT64:
831     return eFormatDecimal;
832   case GoType::KIND_UINT:
833   case GoType::KIND_UINT8:
834   case GoType::KIND_UINT16:
835   case GoType::KIND_UINT32:
836   case GoType::KIND_UINT64:
837     return eFormatUnsigned;
838   case GoType::KIND_FLOAT32:
839   case GoType::KIND_FLOAT64:
840     return eFormatFloat;
841   case GoType::KIND_COMPLEX64:
842   case GoType::KIND_COMPLEX128:
843     return eFormatComplexFloat;
844   case GoType::KIND_UINTPTR:
845   case GoType::KIND_CHAN:
846   case GoType::KIND_PTR:
847   case GoType::KIND_MAP:
848   case GoType::KIND_UNSAFEPOINTER:
849     return eFormatHex;
850   case GoType::KIND_STRING:
851     return eFormatCString;
852   case GoType::KIND_ARRAY:
853   case GoType::KIND_INTERFACE:
854   case GoType::KIND_SLICE:
855   case GoType::KIND_STRUCT:
856   default:
857     // Don't know how to display this.
858     return eFormatBytes;
859   }
860 }
861
862 size_t GoASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
863   return 0;
864 }
865
866 uint32_t GoASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
867                                       bool omit_empty_base_classes) {
868   if (!type || !GetCompleteType(type))
869     return 0;
870   GoType *t = static_cast<GoType *>(type);
871   if (t->GetGoKind() == GoType::KIND_PTR) {
872     CompilerType elem = t->GetElementType();
873     if (elem.IsAggregateType())
874       return elem.GetNumChildren(omit_empty_base_classes);
875     return 1;
876   } else if (GoArray *array = t->GetArray()) {
877     return array->GetLength();
878   } else if (t->IsTypedef()) {
879     return t->GetElementType().GetNumChildren(omit_empty_base_classes);
880   }
881
882   return GetNumFields(type);
883 }
884
885 uint32_t GoASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
886   if (!type || !GetCompleteType(type))
887     return 0;
888   GoType *t = static_cast<GoType *>(type);
889   if (t->IsTypedef())
890     return t->GetElementType().GetNumFields();
891   GoStruct *s = t->GetStruct();
892   if (s)
893     return s->GetNumFields();
894   return 0;
895 }
896
897 CompilerType GoASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
898                                            size_t idx, std::string &name,
899                                            uint64_t *bit_offset_ptr,
900                                            uint32_t *bitfield_bit_size_ptr,
901                                            bool *is_bitfield_ptr) {
902   if (bit_offset_ptr)
903     *bit_offset_ptr = 0;
904   if (bitfield_bit_size_ptr)
905     *bitfield_bit_size_ptr = 0;
906   if (is_bitfield_ptr)
907     *is_bitfield_ptr = false;
908
909   if (!type || !GetCompleteType(type))
910     return CompilerType();
911
912   GoType *t = static_cast<GoType *>(type);
913   if (t->IsTypedef())
914     return t->GetElementType().GetFieldAtIndex(
915         idx, name, bit_offset_ptr, bitfield_bit_size_ptr, is_bitfield_ptr);
916
917   GoStruct *s = t->GetStruct();
918   if (s) {
919     const auto *field = s->GetField(idx);
920     if (field) {
921       name = field->m_name.GetStringRef();
922       if (bit_offset_ptr)
923         *bit_offset_ptr = field->m_byte_offset * 8;
924       return field->m_type;
925     }
926   }
927   return CompilerType();
928 }
929
930 CompilerType GoASTContext::GetChildCompilerTypeAtIndex(
931     lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
932     bool transparent_pointers, bool omit_empty_base_classes,
933     bool ignore_array_bounds, std::string &child_name,
934     uint32_t &child_byte_size, int32_t &child_byte_offset,
935     uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
936     bool &child_is_base_class, bool &child_is_deref_of_parent,
937     ValueObject *valobj, uint64_t &language_flags) {
938   child_name.clear();
939   child_byte_size = 0;
940   child_byte_offset = 0;
941   child_bitfield_bit_size = 0;
942   child_bitfield_bit_offset = 0;
943   child_is_base_class = false;
944   child_is_deref_of_parent = false;
945   language_flags = 0;
946
947   if (!type || !GetCompleteType(type))
948     return CompilerType();
949
950   GoType *t = static_cast<GoType *>(type);
951   if (t->GetStruct()) {
952     uint64_t bit_offset;
953     CompilerType ret =
954         GetFieldAtIndex(type, idx, child_name, &bit_offset, nullptr, nullptr);
955     child_byte_size = ret.GetByteSize(
956         exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr);
957     child_byte_offset = bit_offset / 8;
958     return ret;
959   } else if (t->GetGoKind() == GoType::KIND_PTR) {
960     CompilerType pointee = t->GetElementType();
961     if (!pointee.IsValid() || pointee.IsVoidType())
962       return CompilerType();
963     if (transparent_pointers && pointee.IsAggregateType()) {
964       bool tmp_child_is_deref_of_parent = false;
965       return pointee.GetChildCompilerTypeAtIndex(
966           exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
967           ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
968           child_bitfield_bit_size, child_bitfield_bit_offset,
969           child_is_base_class, tmp_child_is_deref_of_parent, valobj,
970           language_flags);
971     } else {
972       child_is_deref_of_parent = true;
973       const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
974       if (parent_name) {
975         child_name.assign(1, '*');
976         child_name += parent_name;
977       }
978
979       // We have a pointer to an simple type
980       if (idx == 0 && pointee.GetCompleteType()) {
981         child_byte_size = pointee.GetByteSize(
982             exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
983         child_byte_offset = 0;
984         return pointee;
985       }
986     }
987   } else if (GoArray *a = t->GetArray()) {
988     if (ignore_array_bounds || idx < a->GetLength()) {
989       CompilerType element_type = a->GetElementType();
990       if (element_type.GetCompleteType()) {
991         char element_name[64];
992         ::snprintf(element_name, sizeof(element_name), "[%zu]", idx);
993         child_name.assign(element_name);
994         child_byte_size = element_type.GetByteSize(
995             exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
996         child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
997         return element_type;
998       }
999     }
1000   } else if (t->IsTypedef()) {
1001     return t->GetElementType().GetChildCompilerTypeAtIndex(
1002         exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
1003         ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
1004         child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
1005         child_is_deref_of_parent, valobj, language_flags);
1006   }
1007   return CompilerType();
1008 }
1009
1010 // Lookup a child given a name. This function will match base class names and
1011 // member member names in "clang_type" only, not descendants.
1012 uint32_t
1013 GoASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
1014                                       const char *name,
1015                                       bool omit_empty_base_classes) {
1016   if (!type || !GetCompleteType(type))
1017     return UINT_MAX;
1018
1019   GoType *t = static_cast<GoType *>(type);
1020   GoStruct *s = t->GetStruct();
1021   if (s) {
1022     for (uint32_t i = 0; i < s->GetNumFields(); ++i) {
1023       const GoStruct::Field *f = s->GetField(i);
1024       if (f->m_name.GetStringRef() == name)
1025         return i;
1026     }
1027   } else if (t->GetGoKind() == GoType::KIND_PTR || t->IsTypedef()) {
1028     return t->GetElementType().GetIndexOfChildWithName(name,
1029                                                        omit_empty_base_classes);
1030   }
1031   return UINT_MAX;
1032 }
1033
1034 // Lookup a child member given a name. This function will match member names
1035 // only and will descend into "clang_type" children in search for the first
1036 // member in this class, or any base class that matches "name".
1037 // TODO: Return all matches for a given name by returning a
1038 // vector<vector<uint32_t>>
1039 // so we catch all names that match a given child name, not just the first.
1040 size_t GoASTContext::GetIndexOfChildMemberWithName(
1041     lldb::opaque_compiler_type_t type, const char *name,
1042     bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
1043   uint32_t index = GetIndexOfChildWithName(type, name, omit_empty_base_classes);
1044   if (index == UINT_MAX)
1045     return 0;
1046   child_indexes.push_back(index);
1047   return 1;
1048 }
1049
1050 // Converts "s" to a floating point value and place resulting floating point
1051 // bytes in the "dst" buffer.
1052 size_t
1053 GoASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
1054                                         const char *s, uint8_t *dst,
1055                                         size_t dst_size) {
1056   assert(false);
1057   return 0;
1058 }
1059 //----------------------------------------------------------------------
1060 // Dumping types
1061 //----------------------------------------------------------------------
1062 #define DEPTH_INCREMENT 2
1063
1064 void GoASTContext::DumpValue(lldb::opaque_compiler_type_t type,
1065                              ExecutionContext *exe_ctx, Stream *s,
1066                              lldb::Format format, const DataExtractor &data,
1067                              lldb::offset_t data_byte_offset,
1068                              size_t data_byte_size, uint32_t bitfield_bit_size,
1069                              uint32_t bitfield_bit_offset, bool show_types,
1070                              bool show_summary, bool verbose, uint32_t depth) {
1071   if (IsTypedefType(type))
1072     type = GetTypedefedType(type).GetOpaqueQualType();
1073   if (!type)
1074     return;
1075   GoType *t = static_cast<GoType *>(type);
1076
1077   if (GoStruct *st = t->GetStruct()) {
1078     if (GetCompleteType(type)) {
1079       uint32_t field_idx = 0;
1080       for (auto *field = st->GetField(field_idx); field != nullptr;
1081            field_idx++) {
1082         // Print the starting squiggly bracket (if this is the first member) or
1083         // comma (for member 2 and beyond) for the struct/union/class member.
1084         if (field_idx == 0)
1085           s->PutChar('{');
1086         else
1087           s->PutChar(',');
1088
1089         // Indent
1090         s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
1091
1092         // Print the member type if requested
1093         if (show_types) {
1094           ConstString field_type_name = field->m_type.GetTypeName();
1095           s->Printf("(%s) ", field_type_name.AsCString());
1096         }
1097         // Print the member name and equal sign
1098         s->Printf("%s = ", field->m_name.AsCString());
1099
1100         // Dump the value of the member
1101         CompilerType field_type = field->m_type;
1102         field_type.DumpValue(
1103             exe_ctx,
1104             s, // Stream to dump to
1105             field_type
1106                 .GetFormat(), // The format with which to display the member
1107             data,             // Data buffer containing all bytes for this type
1108             data_byte_offset + field->m_byte_offset, // Offset into "data" where
1109                                                      // to grab value from
1110             field->m_type.GetByteSize(
1111                 exe_ctx->GetBestExecutionContextScope()), // Size of this type
1112                                                           // in bytes
1113             0,                                            // Bitfield bit size
1114             0,                                            // Bitfield bit offset
1115             show_types,   // Boolean indicating if we should show the variable
1116                           // types
1117             show_summary, // Boolean indicating if we should show a summary for
1118                           // the current type
1119             verbose,      // Verbose output?
1120             depth + DEPTH_INCREMENT); // Scope depth for any types that have
1121                                       // children
1122       }
1123
1124       // Indent the trailing squiggly bracket
1125       if (field_idx > 0)
1126         s->Printf("\n%*s}", depth, "");
1127     }
1128   }
1129
1130   if (GoArray *a = t->GetArray()) {
1131     CompilerType element_clang_type = a->GetElementType();
1132     lldb::Format element_format = element_clang_type.GetFormat();
1133     uint32_t element_byte_size =
1134         element_clang_type.GetByteSize(exe_ctx->GetBestExecutionContextScope());
1135
1136     uint64_t element_idx;
1137     for (element_idx = 0; element_idx < a->GetLength(); ++element_idx) {
1138       // Print the starting squiggly bracket (if this is the first member) or
1139       // comman (for member 2 and beyong) for the struct/union/class member.
1140       if (element_idx == 0)
1141         s->PutChar('{');
1142       else
1143         s->PutChar(',');
1144
1145       // Indent and print the index
1146       s->Printf("\n%*s[%" PRIu64 "] ", depth + DEPTH_INCREMENT, "",
1147                 element_idx);
1148
1149       // Figure out the field offset within the current struct/union/class type
1150       uint64_t element_offset = element_idx * element_byte_size;
1151
1152       // Dump the value of the member
1153       element_clang_type.DumpValue(
1154           exe_ctx,
1155           s,              // Stream to dump to
1156           element_format, // The format with which to display the element
1157           data,           // Data buffer containing all bytes for this type
1158           data_byte_offset +
1159               element_offset, // Offset into "data" where to grab value from
1160           element_byte_size,  // Size of this type in bytes
1161           0,                  // Bitfield bit size
1162           0,                  // Bitfield bit offset
1163           show_types, // Boolean indicating if we should show the variable types
1164           show_summary, // Boolean indicating if we should show a summary for
1165                         // the current type
1166           verbose,      // Verbose output?
1167           depth +
1168               DEPTH_INCREMENT); // Scope depth for any types that have children
1169     }
1170
1171     // Indent the trailing squiggly bracket
1172     if (element_idx > 0)
1173       s->Printf("\n%*s}", depth, "");
1174   }
1175
1176   if (show_summary)
1177     DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
1178 }
1179
1180 bool GoASTContext::DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s,
1181                                  lldb::Format format, const DataExtractor &data,
1182                                  lldb::offset_t byte_offset, size_t byte_size,
1183                                  uint32_t bitfield_bit_size,
1184                                  uint32_t bitfield_bit_offset,
1185                                  ExecutionContextScope *exe_scope) {
1186   if (!type)
1187     return false;
1188   if (IsAggregateType(type)) {
1189     return false;
1190   } else {
1191     GoType *t = static_cast<GoType *>(type);
1192     if (t->IsTypedef()) {
1193       CompilerType typedef_compiler_type = t->GetElementType();
1194       if (format == eFormatDefault)
1195         format = typedef_compiler_type.GetFormat();
1196       uint64_t typedef_byte_size = typedef_compiler_type.GetByteSize(exe_scope);
1197
1198       return typedef_compiler_type.DumpTypeValue(
1199           s,
1200           format,            // The format with which to display the element
1201           data,              // Data buffer containing all bytes for this type
1202           byte_offset,       // Offset into "data" where to grab value from
1203           typedef_byte_size, // Size of this type in bytes
1204           bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
1205                              // treat as a bitfield
1206           bitfield_bit_offset, // Offset in bits of a bitfield value if
1207                                // bitfield_bit_size != 0
1208           exe_scope);
1209     }
1210
1211     uint32_t item_count = 1;
1212     // A few formats, we might need to modify our size and count for depending
1213     // on how we are trying to display the value...
1214     switch (format) {
1215     default:
1216     case eFormatBoolean:
1217     case eFormatBinary:
1218     case eFormatComplex:
1219     case eFormatCString: // NULL terminated C strings
1220     case eFormatDecimal:
1221     case eFormatEnum:
1222     case eFormatHex:
1223     case eFormatHexUppercase:
1224     case eFormatFloat:
1225     case eFormatOctal:
1226     case eFormatOSType:
1227     case eFormatUnsigned:
1228     case eFormatPointer:
1229     case eFormatVectorOfChar:
1230     case eFormatVectorOfSInt8:
1231     case eFormatVectorOfUInt8:
1232     case eFormatVectorOfSInt16:
1233     case eFormatVectorOfUInt16:
1234     case eFormatVectorOfSInt32:
1235     case eFormatVectorOfUInt32:
1236     case eFormatVectorOfSInt64:
1237     case eFormatVectorOfUInt64:
1238     case eFormatVectorOfFloat32:
1239     case eFormatVectorOfFloat64:
1240     case eFormatVectorOfUInt128:
1241       break;
1242
1243     case eFormatChar:
1244     case eFormatCharPrintable:
1245     case eFormatCharArray:
1246     case eFormatBytes:
1247     case eFormatBytesWithASCII:
1248       item_count = byte_size;
1249       byte_size = 1;
1250       break;
1251
1252     case eFormatUnicode16:
1253       item_count = byte_size / 2;
1254       byte_size = 2;
1255       break;
1256
1257     case eFormatUnicode32:
1258       item_count = byte_size / 4;
1259       byte_size = 4;
1260       break;
1261     }
1262     return DumpDataExtractor(data, s, byte_offset, format, byte_size,
1263                              item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
1264                              bitfield_bit_size, bitfield_bit_offset, exe_scope);
1265   }
1266   return 0;
1267 }
1268
1269 void GoASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
1270                                ExecutionContext *exe_ctx, Stream *s,
1271                                const DataExtractor &data,
1272                                lldb::offset_t data_offset,
1273                                size_t data_byte_size) {
1274   if (type && GoType::KIND_STRING == static_cast<GoType *>(type)->GetGoKind()) {
1275     // TODO(ribrdb): read length and data
1276   }
1277 }
1278
1279 void GoASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
1280   // Dump to stdout
1281   StreamFile s(stdout, false);
1282   DumpTypeDescription(type, &s);
1283 }
1284
1285 void GoASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
1286                                        Stream *s) {
1287   if (!type)
1288     return;
1289   ConstString name = GetTypeName(type);
1290   GoType *t = static_cast<GoType *>(type);
1291
1292   if (GoStruct *st = t->GetStruct()) {
1293     if (GetCompleteType(type)) {
1294       if (NULL == strchr(name.AsCString(), '{'))
1295         s->Printf("type %s ", name.AsCString());
1296       s->PutCString("struct {");
1297       if (st->GetNumFields() == 0) {
1298         s->PutChar('}');
1299         return;
1300       }
1301       s->IndentMore();
1302       uint32_t field_idx = 0;
1303       for (auto *field = st->GetField(field_idx); field != nullptr;
1304            field_idx++) {
1305         s->PutChar('\n');
1306         s->Indent();
1307         s->Printf("%s %s", field->m_name.AsCString(),
1308                   field->m_type.GetTypeName().AsCString());
1309       }
1310       s->IndentLess();
1311       s->PutChar('\n');
1312       s->Indent("}");
1313       return;
1314     }
1315   }
1316
1317   s->PutCString(name.AsCString());
1318 }
1319
1320 CompilerType GoASTContext::CreateArrayType(const ConstString &name,
1321                                            const CompilerType &element_type,
1322                                            uint64_t length) {
1323   GoType *type = new GoArray(name, length, element_type);
1324   (*m_types)[name].reset(type);
1325   return CompilerType(this, type);
1326 }
1327
1328 CompilerType GoASTContext::CreateBaseType(int go_kind,
1329                                           const lldb_private::ConstString &name,
1330                                           uint64_t byte_size) {
1331   if (go_kind == GoType::KIND_UINT || go_kind == GoType::KIND_INT)
1332     m_int_byte_size = byte_size;
1333   GoType *type = new GoType(go_kind, name);
1334   (*m_types)[name].reset(type);
1335   return CompilerType(this, type);
1336 }
1337
1338 CompilerType GoASTContext::CreateTypedefType(int kind, const ConstString &name,
1339                                              CompilerType impl) {
1340   GoType *type = new GoElem(kind, name, impl);
1341   (*m_types)[name].reset(type);
1342   return CompilerType(this, type);
1343 }
1344
1345 CompilerType
1346 GoASTContext::CreateVoidType(const lldb_private::ConstString &name) {
1347   GoType *type = new GoType(GoType::KIND_LLDB_VOID, name);
1348   (*m_types)[name].reset(type);
1349   return CompilerType(this, type);
1350 }
1351
1352 CompilerType
1353 GoASTContext::CreateStructType(int kind, const lldb_private::ConstString &name,
1354                                uint32_t byte_size) {
1355   GoType *type = new GoStruct(kind, name, byte_size);
1356   (*m_types)[name].reset(type);
1357   return CompilerType(this, type);
1358 }
1359
1360 void GoASTContext::AddFieldToStruct(
1361     const lldb_private::CompilerType &struct_type,
1362     const lldb_private::ConstString &name,
1363     const lldb_private::CompilerType &field_type, uint32_t byte_offset) {
1364   if (!struct_type)
1365     return;
1366   GoASTContext *ast =
1367       llvm::dyn_cast_or_null<GoASTContext>(struct_type.GetTypeSystem());
1368   if (!ast)
1369     return;
1370   GoType *type = static_cast<GoType *>(struct_type.GetOpaqueQualType());
1371   if (GoStruct *s = type->GetStruct())
1372     s->AddField(name, field_type, byte_offset);
1373 }
1374
1375 void GoASTContext::CompleteStructType(
1376     const lldb_private::CompilerType &struct_type) {
1377   if (!struct_type)
1378     return;
1379   GoASTContext *ast =
1380       llvm::dyn_cast_or_null<GoASTContext>(struct_type.GetTypeSystem());
1381   if (!ast)
1382     return;
1383   GoType *type = static_cast<GoType *>(struct_type.GetOpaqueQualType());
1384   if (GoStruct *s = type->GetStruct())
1385     s->SetComplete();
1386 }
1387
1388 CompilerType
1389 GoASTContext::CreateFunctionType(const lldb_private::ConstString &name,
1390                                  CompilerType *params, size_t params_count,
1391                                  bool is_variadic) {
1392   GoType *type = new GoFunction(name, is_variadic);
1393   (*m_types)[name].reset(type);
1394   return CompilerType(this, type);
1395 }
1396
1397 bool GoASTContext::IsGoString(const lldb_private::CompilerType &type) {
1398   if (!type.IsValid() ||
1399       !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1400     return false;
1401   return GoType::KIND_STRING ==
1402          static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1403 }
1404
1405 bool GoASTContext::IsGoSlice(const lldb_private::CompilerType &type) {
1406   if (!type.IsValid() ||
1407       !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1408     return false;
1409   return GoType::KIND_SLICE ==
1410          static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1411 }
1412
1413 bool GoASTContext::IsGoInterface(const lldb_private::CompilerType &type) {
1414   if (!type.IsValid() ||
1415       !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1416     return false;
1417   return GoType::KIND_INTERFACE ==
1418          static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1419 }
1420
1421 bool GoASTContext::IsPointerKind(uint8_t kind) {
1422   return (kind & GoType::KIND_MASK) == GoType::KIND_PTR;
1423 }
1424
1425 bool GoASTContext::IsDirectIface(uint8_t kind) {
1426   return (kind & GoType::KIND_DIRECT_IFACE) == GoType::KIND_DIRECT_IFACE;
1427 }
1428
1429 DWARFASTParser *GoASTContext::GetDWARFParser() {
1430   if (!m_dwarf_ast_parser_ap)
1431     m_dwarf_ast_parser_ap.reset(new DWARFASTParserGo(*this));
1432   return m_dwarf_ast_parser_ap.get();
1433 }
1434
1435 UserExpression *GoASTContextForExpr::GetUserExpression(
1436     llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
1437     Expression::ResultType desired_type,
1438     const EvaluateExpressionOptions &options) {
1439   TargetSP target = m_target_wp.lock();
1440   if (target)
1441     return new GoUserExpression(*target, expr, prefix, language, desired_type,
1442                                 options);
1443   return nullptr;
1444 }