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