]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Symbol/GoASTContext.cpp
MFV r324714:
[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
671 // Returns a value >= 0 if there is a prototype.
672 int GoASTContext::GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) {
673   return GetNumberOfFunctionArguments(type);
674 }
675
676 CompilerType
677 GoASTContext::GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type,
678                                              size_t idx) {
679   return GetFunctionArgumentAtIndex(type, idx);
680 }
681
682 CompilerType
683 GoASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
684   CompilerType result;
685   if (type) {
686     GoType *t = static_cast<GoType *>(type);
687     if (t->GetGoKind() == GoType::KIND_FUNC)
688       result = t->GetElementType();
689   }
690   return result;
691 }
692
693 size_t GoASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
694   return 0;
695 }
696
697 TypeMemberFunctionImpl
698 GoASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
699                                        size_t idx) {
700   return TypeMemberFunctionImpl();
701 }
702
703 CompilerType
704 GoASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
705   return CompilerType(this, type);
706 }
707
708 CompilerType GoASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
709   if (!type)
710     return CompilerType();
711   return static_cast<GoType *>(type)->GetElementType();
712 }
713
714 CompilerType GoASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
715   if (!type)
716     return CompilerType();
717   ConstString type_name = GetTypeName(type);
718   ConstString pointer_name(std::string("*") + type_name.GetCString());
719   GoType *pointer = (*m_types)[pointer_name].get();
720   if (pointer == nullptr) {
721     pointer =
722         new GoElem(GoType::KIND_PTR, pointer_name, CompilerType(this, type));
723     (*m_types)[pointer_name].reset(pointer);
724   }
725   return CompilerType(this, pointer);
726 }
727
728 // If the current object represents a typedef type, get the underlying type
729 CompilerType GoASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
730   if (IsTypedefType(type))
731     return static_cast<GoType *>(type)->GetElementType();
732   return CompilerType();
733 }
734
735 //----------------------------------------------------------------------
736 // Create related types using the current type's AST
737 //----------------------------------------------------------------------
738 CompilerType GoASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
739   return CompilerType();
740 }
741
742 CompilerType
743 GoASTContext::GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding,
744                                                   size_t bit_size) {
745   return CompilerType();
746 }
747
748 //----------------------------------------------------------------------
749 // Exploring the type
750 //----------------------------------------------------------------------
751
752 uint64_t GoASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
753                                   ExecutionContextScope *exe_scope) {
754   if (!type)
755     return 0;
756   if (!GetCompleteType(type))
757     return 0;
758   GoType *t = static_cast<GoType *>(type);
759   GoArray *array = nullptr;
760   switch (t->GetGoKind()) {
761   case GoType::KIND_BOOL:
762   case GoType::KIND_INT8:
763   case GoType::KIND_UINT8:
764     return 8;
765   case GoType::KIND_INT16:
766   case GoType::KIND_UINT16:
767     return 16;
768   case GoType::KIND_INT32:
769   case GoType::KIND_UINT32:
770   case GoType::KIND_FLOAT32:
771     return 32;
772   case GoType::KIND_INT64:
773   case GoType::KIND_UINT64:
774   case GoType::KIND_FLOAT64:
775   case GoType::KIND_COMPLEX64:
776     return 64;
777   case GoType::KIND_COMPLEX128:
778     return 128;
779   case GoType::KIND_INT:
780   case GoType::KIND_UINT:
781     return m_int_byte_size * 8;
782   case GoType::KIND_UINTPTR:
783   case GoType::KIND_FUNC: // I assume this is a pointer?
784   case GoType::KIND_CHAN:
785   case GoType::KIND_PTR:
786   case GoType::KIND_UNSAFEPOINTER:
787   case GoType::KIND_MAP:
788     return m_pointer_byte_size * 8;
789   case GoType::KIND_ARRAY:
790     array = t->GetArray();
791     return array->GetLength() * array->GetElementType().GetBitSize(exe_scope);
792   case GoType::KIND_INTERFACE:
793     return t->GetElementType().GetBitSize(exe_scope);
794   case GoType::KIND_SLICE:
795   case GoType::KIND_STRING:
796   case GoType::KIND_STRUCT:
797     return t->GetStruct()->GetByteSize() * 8;
798   default:
799     assert(false);
800   }
801   return 0;
802 }
803
804 lldb::Encoding GoASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
805                                          uint64_t &count) {
806   count = 1;
807   bool is_signed;
808   if (IsIntegerType(type, is_signed))
809     return is_signed ? lldb::eEncodingSint : eEncodingUint;
810   bool is_complex;
811   uint32_t complex_count;
812   if (IsFloatingPointType(type, complex_count, is_complex)) {
813     count = complex_count;
814     return eEncodingIEEE754;
815   }
816   if (IsPointerType(type))
817     return eEncodingUint;
818   return eEncodingInvalid;
819 }
820
821 lldb::Format GoASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
822   if (!type)
823     return eFormatDefault;
824   switch (static_cast<GoType *>(type)->GetGoKind()) {
825   case GoType::KIND_BOOL:
826     return eFormatBoolean;
827   case GoType::KIND_INT:
828   case GoType::KIND_INT8:
829   case GoType::KIND_INT16:
830   case GoType::KIND_INT32:
831   case GoType::KIND_INT64:
832     return eFormatDecimal;
833   case GoType::KIND_UINT:
834   case GoType::KIND_UINT8:
835   case GoType::KIND_UINT16:
836   case GoType::KIND_UINT32:
837   case GoType::KIND_UINT64:
838     return eFormatUnsigned;
839   case GoType::KIND_FLOAT32:
840   case GoType::KIND_FLOAT64:
841     return eFormatFloat;
842   case GoType::KIND_COMPLEX64:
843   case GoType::KIND_COMPLEX128:
844     return eFormatComplexFloat;
845   case GoType::KIND_UINTPTR:
846   case GoType::KIND_CHAN:
847   case GoType::KIND_PTR:
848   case GoType::KIND_MAP:
849   case GoType::KIND_UNSAFEPOINTER:
850     return eFormatHex;
851   case GoType::KIND_STRING:
852     return eFormatCString;
853   case GoType::KIND_ARRAY:
854   case GoType::KIND_INTERFACE:
855   case GoType::KIND_SLICE:
856   case GoType::KIND_STRUCT:
857   default:
858     // Don't know how to display this.
859     return eFormatBytes;
860   }
861 }
862
863 size_t GoASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
864   return 0;
865 }
866
867 uint32_t GoASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
868                                       bool omit_empty_base_classes) {
869   if (!type || !GetCompleteType(type))
870     return 0;
871   GoType *t = static_cast<GoType *>(type);
872   if (t->GetGoKind() == GoType::KIND_PTR) {
873     CompilerType elem = t->GetElementType();
874     if (elem.IsAggregateType())
875       return elem.GetNumChildren(omit_empty_base_classes);
876     return 1;
877   } else if (GoArray *array = t->GetArray()) {
878     return array->GetLength();
879   } else if (t->IsTypedef()) {
880     return t->GetElementType().GetNumChildren(omit_empty_base_classes);
881   }
882
883   return GetNumFields(type);
884 }
885
886 uint32_t GoASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
887   if (!type || !GetCompleteType(type))
888     return 0;
889   GoType *t = static_cast<GoType *>(type);
890   if (t->IsTypedef())
891     return t->GetElementType().GetNumFields();
892   GoStruct *s = t->GetStruct();
893   if (s)
894     return s->GetNumFields();
895   return 0;
896 }
897
898 CompilerType GoASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
899                                            size_t idx, std::string &name,
900                                            uint64_t *bit_offset_ptr,
901                                            uint32_t *bitfield_bit_size_ptr,
902                                            bool *is_bitfield_ptr) {
903   if (bit_offset_ptr)
904     *bit_offset_ptr = 0;
905   if (bitfield_bit_size_ptr)
906     *bitfield_bit_size_ptr = 0;
907   if (is_bitfield_ptr)
908     *is_bitfield_ptr = false;
909
910   if (!type || !GetCompleteType(type))
911     return CompilerType();
912
913   GoType *t = static_cast<GoType *>(type);
914   if (t->IsTypedef())
915     return t->GetElementType().GetFieldAtIndex(
916         idx, name, bit_offset_ptr, bitfield_bit_size_ptr, is_bitfield_ptr);
917
918   GoStruct *s = t->GetStruct();
919   if (s) {
920     const auto *field = s->GetField(idx);
921     if (field) {
922       name = field->m_name.GetStringRef();
923       if (bit_offset_ptr)
924         *bit_offset_ptr = field->m_byte_offset * 8;
925       return field->m_type;
926     }
927   }
928   return CompilerType();
929 }
930
931 CompilerType GoASTContext::GetChildCompilerTypeAtIndex(
932     lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
933     bool transparent_pointers, bool omit_empty_base_classes,
934     bool ignore_array_bounds, std::string &child_name,
935     uint32_t &child_byte_size, int32_t &child_byte_offset,
936     uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
937     bool &child_is_base_class, bool &child_is_deref_of_parent,
938     ValueObject *valobj, uint64_t &language_flags) {
939   child_name.clear();
940   child_byte_size = 0;
941   child_byte_offset = 0;
942   child_bitfield_bit_size = 0;
943   child_bitfield_bit_offset = 0;
944   child_is_base_class = false;
945   child_is_deref_of_parent = false;
946   language_flags = 0;
947
948   if (!type || !GetCompleteType(type))
949     return CompilerType();
950
951   GoType *t = static_cast<GoType *>(type);
952   if (t->GetStruct()) {
953     uint64_t bit_offset;
954     CompilerType ret =
955         GetFieldAtIndex(type, idx, child_name, &bit_offset, nullptr, nullptr);
956     child_byte_size = ret.GetByteSize(
957         exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr);
958     child_byte_offset = bit_offset / 8;
959     return ret;
960   } else if (t->GetGoKind() == GoType::KIND_PTR) {
961     CompilerType pointee = t->GetElementType();
962     if (!pointee.IsValid() || pointee.IsVoidType())
963       return CompilerType();
964     if (transparent_pointers && pointee.IsAggregateType()) {
965       bool tmp_child_is_deref_of_parent = false;
966       return pointee.GetChildCompilerTypeAtIndex(
967           exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
968           ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
969           child_bitfield_bit_size, child_bitfield_bit_offset,
970           child_is_base_class, tmp_child_is_deref_of_parent, valobj,
971           language_flags);
972     } else {
973       child_is_deref_of_parent = true;
974       const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
975       if (parent_name) {
976         child_name.assign(1, '*');
977         child_name += parent_name;
978       }
979
980       // We have a pointer to an simple type
981       if (idx == 0 && pointee.GetCompleteType()) {
982         child_byte_size = pointee.GetByteSize(
983             exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
984         child_byte_offset = 0;
985         return pointee;
986       }
987     }
988   } else if (GoArray *a = t->GetArray()) {
989     if (ignore_array_bounds || idx < a->GetLength()) {
990       CompilerType element_type = a->GetElementType();
991       if (element_type.GetCompleteType()) {
992         char element_name[64];
993         ::snprintf(element_name, sizeof(element_name), "[%zu]", idx);
994         child_name.assign(element_name);
995         child_byte_size = element_type.GetByteSize(
996             exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
997         child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
998         return element_type;
999       }
1000     }
1001   } else if (t->IsTypedef()) {
1002     return t->GetElementType().GetChildCompilerTypeAtIndex(
1003         exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
1004         ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
1005         child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
1006         child_is_deref_of_parent, valobj, language_flags);
1007   }
1008   return CompilerType();
1009 }
1010
1011 // Lookup a child given a name. This function will match base class names
1012 // and member member names in "clang_type" only, not descendants.
1013 uint32_t
1014 GoASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
1015                                       const char *name,
1016                                       bool omit_empty_base_classes) {
1017   if (!type || !GetCompleteType(type))
1018     return UINT_MAX;
1019
1020   GoType *t = static_cast<GoType *>(type);
1021   GoStruct *s = t->GetStruct();
1022   if (s) {
1023     for (uint32_t i = 0; i < s->GetNumFields(); ++i) {
1024       const GoStruct::Field *f = s->GetField(i);
1025       if (f->m_name.GetStringRef() == name)
1026         return i;
1027     }
1028   } else if (t->GetGoKind() == GoType::KIND_PTR || t->IsTypedef()) {
1029     return t->GetElementType().GetIndexOfChildWithName(name,
1030                                                        omit_empty_base_classes);
1031   }
1032   return UINT_MAX;
1033 }
1034
1035 // Lookup a child member given a name. This function will match member names
1036 // only and will descend into "clang_type" children in search for the first
1037 // member in this class, or any base class that matches "name".
1038 // TODO: Return all matches for a given name by returning a
1039 // vector<vector<uint32_t>>
1040 // so we catch all names that match a given child name, not just the first.
1041 size_t GoASTContext::GetIndexOfChildMemberWithName(
1042     lldb::opaque_compiler_type_t type, const char *name,
1043     bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
1044   uint32_t index = GetIndexOfChildWithName(type, name, omit_empty_base_classes);
1045   if (index == UINT_MAX)
1046     return 0;
1047   child_indexes.push_back(index);
1048   return 1;
1049 }
1050
1051 // Converts "s" to a floating point value and place resulting floating
1052 // point bytes in the "dst" buffer.
1053 size_t
1054 GoASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
1055                                         const char *s, uint8_t *dst,
1056                                         size_t dst_size) {
1057   assert(false);
1058   return 0;
1059 }
1060 //----------------------------------------------------------------------
1061 // Dumping types
1062 //----------------------------------------------------------------------
1063 #define DEPTH_INCREMENT 2
1064
1065 void GoASTContext::DumpValue(lldb::opaque_compiler_type_t type,
1066                              ExecutionContext *exe_ctx, Stream *s,
1067                              lldb::Format format, const DataExtractor &data,
1068                              lldb::offset_t data_byte_offset,
1069                              size_t data_byte_size, uint32_t bitfield_bit_size,
1070                              uint32_t bitfield_bit_offset, bool show_types,
1071                              bool show_summary, bool verbose, uint32_t depth) {
1072   if (IsTypedefType(type))
1073     type = GetTypedefedType(type).GetOpaqueQualType();
1074   if (!type)
1075     return;
1076   GoType *t = static_cast<GoType *>(type);
1077
1078   if (GoStruct *st = t->GetStruct()) {
1079     if (GetCompleteType(type)) {
1080       uint32_t field_idx = 0;
1081       for (auto *field = st->GetField(field_idx); field != nullptr;
1082            field_idx++) {
1083         // Print the starting squiggly bracket (if this is the
1084         // first member) or comma (for member 2 and beyond) for
1085         // the struct/union/class member.
1086         if (field_idx == 0)
1087           s->PutChar('{');
1088         else
1089           s->PutChar(',');
1090
1091         // Indent
1092         s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
1093
1094         // Print the member type if requested
1095         if (show_types) {
1096           ConstString field_type_name = field->m_type.GetTypeName();
1097           s->Printf("(%s) ", field_type_name.AsCString());
1098         }
1099         // Print the member name and equal sign
1100         s->Printf("%s = ", field->m_name.AsCString());
1101
1102         // Dump the value of the member
1103         CompilerType field_type = field->m_type;
1104         field_type.DumpValue(
1105             exe_ctx,
1106             s, // Stream to dump to
1107             field_type
1108                 .GetFormat(), // The format with which to display the member
1109             data,             // Data buffer containing all bytes for this type
1110             data_byte_offset + field->m_byte_offset, // Offset into "data" where
1111                                                      // to grab value from
1112             field->m_type.GetByteSize(
1113                 exe_ctx->GetBestExecutionContextScope()), // Size of this type
1114                                                           // in bytes
1115             0,                                            // Bitfield bit size
1116             0,                                            // Bitfield bit offset
1117             show_types,   // Boolean indicating if we should show the variable
1118                           // types
1119             show_summary, // Boolean indicating if we should show a summary for
1120                           // the current type
1121             verbose,      // Verbose output?
1122             depth + DEPTH_INCREMENT); // Scope depth for any types that have
1123                                       // children
1124       }
1125
1126       // Indent the trailing squiggly bracket
1127       if (field_idx > 0)
1128         s->Printf("\n%*s}", depth, "");
1129     }
1130   }
1131
1132   if (GoArray *a = t->GetArray()) {
1133     CompilerType element_clang_type = a->GetElementType();
1134     lldb::Format element_format = element_clang_type.GetFormat();
1135     uint32_t element_byte_size =
1136         element_clang_type.GetByteSize(exe_ctx->GetBestExecutionContextScope());
1137
1138     uint64_t element_idx;
1139     for (element_idx = 0; element_idx < a->GetLength(); ++element_idx) {
1140       // Print the starting squiggly bracket (if this is the
1141       // first member) or comman (for member 2 and beyong) for
1142       // the struct/union/class member.
1143       if (element_idx == 0)
1144         s->PutChar('{');
1145       else
1146         s->PutChar(',');
1147
1148       // Indent and print the index
1149       s->Printf("\n%*s[%" PRIu64 "] ", depth + DEPTH_INCREMENT, "",
1150                 element_idx);
1151
1152       // Figure out the field offset within the current struct/union/class type
1153       uint64_t element_offset = element_idx * element_byte_size;
1154
1155       // Dump the value of the member
1156       element_clang_type.DumpValue(
1157           exe_ctx,
1158           s,              // Stream to dump to
1159           element_format, // The format with which to display the element
1160           data,           // Data buffer containing all bytes for this type
1161           data_byte_offset +
1162               element_offset, // Offset into "data" where to grab value from
1163           element_byte_size,  // Size of this type in bytes
1164           0,                  // Bitfield bit size
1165           0,                  // Bitfield bit offset
1166           show_types, // Boolean indicating if we should show the variable types
1167           show_summary, // Boolean indicating if we should show a summary for
1168                         // the current type
1169           verbose,      // Verbose output?
1170           depth +
1171               DEPTH_INCREMENT); // Scope depth for any types that have children
1172     }
1173
1174     // Indent the trailing squiggly bracket
1175     if (element_idx > 0)
1176       s->Printf("\n%*s}", depth, "");
1177   }
1178
1179   if (show_summary)
1180     DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
1181 }
1182
1183 bool GoASTContext::DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s,
1184                                  lldb::Format format, const DataExtractor &data,
1185                                  lldb::offset_t byte_offset, size_t byte_size,
1186                                  uint32_t bitfield_bit_size,
1187                                  uint32_t bitfield_bit_offset,
1188                                  ExecutionContextScope *exe_scope) {
1189   if (!type)
1190     return false;
1191   if (IsAggregateType(type)) {
1192     return false;
1193   } else {
1194     GoType *t = static_cast<GoType *>(type);
1195     if (t->IsTypedef()) {
1196       CompilerType typedef_compiler_type = t->GetElementType();
1197       if (format == eFormatDefault)
1198         format = typedef_compiler_type.GetFormat();
1199       uint64_t typedef_byte_size = typedef_compiler_type.GetByteSize(exe_scope);
1200
1201       return typedef_compiler_type.DumpTypeValue(
1202           s,
1203           format,            // The format with which to display the element
1204           data,              // Data buffer containing all bytes for this type
1205           byte_offset,       // Offset into "data" where to grab value from
1206           typedef_byte_size, // Size of this type in bytes
1207           bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
1208                              // treat as a bitfield
1209           bitfield_bit_offset, // Offset in bits of a bitfield value if
1210                                // bitfield_bit_size != 0
1211           exe_scope);
1212     }
1213
1214     uint32_t item_count = 1;
1215     // A few formats, we might need to modify our size and count for depending
1216     // on how we are trying to display the value...
1217     switch (format) {
1218     default:
1219     case eFormatBoolean:
1220     case eFormatBinary:
1221     case eFormatComplex:
1222     case eFormatCString: // NULL terminated C strings
1223     case eFormatDecimal:
1224     case eFormatEnum:
1225     case eFormatHex:
1226     case eFormatHexUppercase:
1227     case eFormatFloat:
1228     case eFormatOctal:
1229     case eFormatOSType:
1230     case eFormatUnsigned:
1231     case eFormatPointer:
1232     case eFormatVectorOfChar:
1233     case eFormatVectorOfSInt8:
1234     case eFormatVectorOfUInt8:
1235     case eFormatVectorOfSInt16:
1236     case eFormatVectorOfUInt16:
1237     case eFormatVectorOfSInt32:
1238     case eFormatVectorOfUInt32:
1239     case eFormatVectorOfSInt64:
1240     case eFormatVectorOfUInt64:
1241     case eFormatVectorOfFloat32:
1242     case eFormatVectorOfFloat64:
1243     case eFormatVectorOfUInt128:
1244       break;
1245
1246     case eFormatChar:
1247     case eFormatCharPrintable:
1248     case eFormatCharArray:
1249     case eFormatBytes:
1250     case eFormatBytesWithASCII:
1251       item_count = byte_size;
1252       byte_size = 1;
1253       break;
1254
1255     case eFormatUnicode16:
1256       item_count = byte_size / 2;
1257       byte_size = 2;
1258       break;
1259
1260     case eFormatUnicode32:
1261       item_count = byte_size / 4;
1262       byte_size = 4;
1263       break;
1264     }
1265     return DumpDataExtractor(data, s, byte_offset, format, byte_size,
1266                              item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
1267                              bitfield_bit_size, bitfield_bit_offset, exe_scope);
1268   }
1269   return 0;
1270 }
1271
1272 void GoASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
1273                                ExecutionContext *exe_ctx, Stream *s,
1274                                const DataExtractor &data,
1275                                lldb::offset_t data_offset,
1276                                size_t data_byte_size) {
1277   if (type && GoType::KIND_STRING == static_cast<GoType *>(type)->GetGoKind()) {
1278     // TODO(ribrdb): read length and data
1279   }
1280 }
1281
1282 void GoASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
1283   // Dump to stdout
1284   StreamFile s(stdout, false);
1285   DumpTypeDescription(type, &s);
1286 }
1287
1288 void GoASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
1289                                        Stream *s) {
1290   if (!type)
1291     return;
1292   ConstString name = GetTypeName(type);
1293   GoType *t = static_cast<GoType *>(type);
1294
1295   if (GoStruct *st = t->GetStruct()) {
1296     if (GetCompleteType(type)) {
1297       if (NULL == strchr(name.AsCString(), '{'))
1298         s->Printf("type %s ", name.AsCString());
1299       s->PutCString("struct {");
1300       if (st->GetNumFields() == 0) {
1301         s->PutChar('}');
1302         return;
1303       }
1304       s->IndentMore();
1305       uint32_t field_idx = 0;
1306       for (auto *field = st->GetField(field_idx); field != nullptr;
1307            field_idx++) {
1308         s->PutChar('\n');
1309         s->Indent();
1310         s->Printf("%s %s", field->m_name.AsCString(),
1311                   field->m_type.GetTypeName().AsCString());
1312       }
1313       s->IndentLess();
1314       s->PutChar('\n');
1315       s->Indent("}");
1316       return;
1317     }
1318   }
1319
1320   s->PutCString(name.AsCString());
1321 }
1322
1323 CompilerType GoASTContext::CreateArrayType(const ConstString &name,
1324                                            const CompilerType &element_type,
1325                                            uint64_t length) {
1326   GoType *type = new GoArray(name, length, element_type);
1327   (*m_types)[name].reset(type);
1328   return CompilerType(this, type);
1329 }
1330
1331 CompilerType GoASTContext::CreateBaseType(int go_kind,
1332                                           const lldb_private::ConstString &name,
1333                                           uint64_t byte_size) {
1334   if (go_kind == GoType::KIND_UINT || go_kind == GoType::KIND_INT)
1335     m_int_byte_size = byte_size;
1336   GoType *type = new GoType(go_kind, name);
1337   (*m_types)[name].reset(type);
1338   return CompilerType(this, type);
1339 }
1340
1341 CompilerType GoASTContext::CreateTypedefType(int kind, const ConstString &name,
1342                                              CompilerType impl) {
1343   GoType *type = new GoElem(kind, name, impl);
1344   (*m_types)[name].reset(type);
1345   return CompilerType(this, type);
1346 }
1347
1348 CompilerType
1349 GoASTContext::CreateVoidType(const lldb_private::ConstString &name) {
1350   GoType *type = new GoType(GoType::KIND_LLDB_VOID, name);
1351   (*m_types)[name].reset(type);
1352   return CompilerType(this, type);
1353 }
1354
1355 CompilerType
1356 GoASTContext::CreateStructType(int kind, const lldb_private::ConstString &name,
1357                                uint32_t byte_size) {
1358   GoType *type = new GoStruct(kind, name, byte_size);
1359   (*m_types)[name].reset(type);
1360   return CompilerType(this, type);
1361 }
1362
1363 void GoASTContext::AddFieldToStruct(
1364     const lldb_private::CompilerType &struct_type,
1365     const lldb_private::ConstString &name,
1366     const lldb_private::CompilerType &field_type, uint32_t byte_offset) {
1367   if (!struct_type)
1368     return;
1369   GoASTContext *ast =
1370       llvm::dyn_cast_or_null<GoASTContext>(struct_type.GetTypeSystem());
1371   if (!ast)
1372     return;
1373   GoType *type = static_cast<GoType *>(struct_type.GetOpaqueQualType());
1374   if (GoStruct *s = type->GetStruct())
1375     s->AddField(name, field_type, byte_offset);
1376 }
1377
1378 void GoASTContext::CompleteStructType(
1379     const lldb_private::CompilerType &struct_type) {
1380   if (!struct_type)
1381     return;
1382   GoASTContext *ast =
1383       llvm::dyn_cast_or_null<GoASTContext>(struct_type.GetTypeSystem());
1384   if (!ast)
1385     return;
1386   GoType *type = static_cast<GoType *>(struct_type.GetOpaqueQualType());
1387   if (GoStruct *s = type->GetStruct())
1388     s->SetComplete();
1389 }
1390
1391 CompilerType
1392 GoASTContext::CreateFunctionType(const lldb_private::ConstString &name,
1393                                  CompilerType *params, size_t params_count,
1394                                  bool is_variadic) {
1395   GoType *type = new GoFunction(name, is_variadic);
1396   (*m_types)[name].reset(type);
1397   return CompilerType(this, type);
1398 }
1399
1400 bool GoASTContext::IsGoString(const lldb_private::CompilerType &type) {
1401   if (!type.IsValid() ||
1402       !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1403     return false;
1404   return GoType::KIND_STRING ==
1405          static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1406 }
1407
1408 bool GoASTContext::IsGoSlice(const lldb_private::CompilerType &type) {
1409   if (!type.IsValid() ||
1410       !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1411     return false;
1412   return GoType::KIND_SLICE ==
1413          static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1414 }
1415
1416 bool GoASTContext::IsGoInterface(const lldb_private::CompilerType &type) {
1417   if (!type.IsValid() ||
1418       !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1419     return false;
1420   return GoType::KIND_INTERFACE ==
1421          static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1422 }
1423
1424 bool GoASTContext::IsPointerKind(uint8_t kind) {
1425   return (kind & GoType::KIND_MASK) == GoType::KIND_PTR;
1426 }
1427
1428 bool GoASTContext::IsDirectIface(uint8_t kind) {
1429   return (kind & GoType::KIND_DIRECT_IFACE) == GoType::KIND_DIRECT_IFACE;
1430 }
1431
1432 DWARFASTParser *GoASTContext::GetDWARFParser() {
1433   if (!m_dwarf_ast_parser_ap)
1434     m_dwarf_ast_parser_ap.reset(new DWARFASTParserGo(*this));
1435   return m_dwarf_ast_parser_ap.get();
1436 }
1437
1438 UserExpression *GoASTContextForExpr::GetUserExpression(
1439     llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
1440     Expression::ResultType desired_type,
1441     const EvaluateExpressionOptions &options) {
1442   TargetSP target = m_target_wp.lock();
1443   if (target)
1444     return new GoUserExpression(*target, expr, prefix, language, desired_type,
1445                                 options);
1446   return nullptr;
1447 }