1 //===-- GoASTContext.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
28 #include "llvm/Support/Threading.h"
30 #include "Plugins/ExpressionParser/Go/GoUserExpression.h"
31 #include "Plugins/SymbolFile/DWARF/DWARFASTParserGo.h"
35 namespace lldb_private {
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
73 GoType(int kind, const ConstString &name)
74 : m_kind(kind & KIND_MASK), m_name(name) {
75 if (m_kind == KIND_FUNC)
80 int GetGoKind() const { return m_kind; }
81 const ConstString &GetName() const { return m_name; }
82 virtual CompilerType GetElementType() const { return CompilerType(); }
84 bool IsTypedef() const {
96 GoFunction *GetFunction();
97 GoStruct *GetStruct();
102 GoType(const GoType &) = delete;
103 const GoType &operator=(const GoType &) = delete;
106 class GoElem : public GoType {
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; }
113 // TODO: should we store this differently?
116 GoElem(const GoElem &) = delete;
117 const GoElem &operator=(const GoElem &) = delete;
120 class GoArray : public GoElem {
122 GoArray(const ConstString &name, uint64_t length, const CompilerType &elem)
123 : GoElem(KIND_ARRAY, name, elem), m_length(length) {}
125 uint64_t GetLength() const { return m_length; }
129 GoArray(const GoArray &) = delete;
130 const GoArray &operator=(const GoArray &) = delete;
133 class GoFunction : public GoType {
135 GoFunction(const ConstString &name, bool is_variadic)
136 : GoType(KIND_FUNC, name), m_is_variadic(is_variadic) {}
138 bool IsVariadic() const { return m_is_variadic; }
142 GoFunction(const GoFunction &) = delete;
143 const GoFunction &operator=(const GoFunction &) = delete;
146 class GoStruct : public GoType {
149 Field(const ConstString &name, const CompilerType &type, uint64_t offset)
150 : m_name(name), m_type(type), m_byte_offset(offset) {}
153 uint64_t m_byte_offset;
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) {}
160 uint32_t GetNumFields() const { return m_fields.size(); }
162 const Field *GetField(uint32_t i) const {
163 if (i < m_fields.size())
168 void AddField(const ConstString &name, const CompilerType &type,
170 m_fields.push_back(Field(name, type, offset));
173 bool IsComplete() const { return m_is_complete; }
175 void SetComplete() { m_is_complete = true; }
177 int64_t GetByteSize() const { return m_byte_size; }
182 std::vector<Field> m_fields;
184 GoStruct(const GoStruct &) = delete;
185 const GoStruct &operator=(const GoStruct &) = delete;
188 GoArray *GoType::GetArray() {
189 if (m_kind == KIND_ARRAY) {
190 return static_cast<GoArray *>(this);
195 GoFunction *GoType::GetFunction() {
196 if (m_kind == KIND_FUNC) {
197 return static_cast<GoFunction *>(this);
202 GoStruct *GoType::GetStruct() {
207 return static_cast<GoStruct *>(this);
211 } // namespace lldb_private
212 using namespace lldb_private;
214 GoASTContext::GoASTContext()
215 : TypeSystem(eKindGo), m_pointer_byte_size(0), m_int_byte_size(0),
216 m_types(new TypeMap) {}
217 GoASTContext::~GoASTContext() {}
219 //------------------------------------------------------------------
220 // PluginInterface functions
221 //------------------------------------------------------------------
223 ConstString GoASTContext::GetPluginNameStatic() { return ConstString("go"); }
225 ConstString GoASTContext::GetPluginName() {
226 return GoASTContext::GetPluginNameStatic();
229 uint32_t GoASTContext::GetPluginVersion() { return 1; }
231 lldb::TypeSystemSP GoASTContext::CreateInstance(lldb::LanguageType language,
234 if (language == eLanguageTypeGo) {
236 std::shared_ptr<GoASTContext> go_ast_sp;
238 arch = module->GetArchitecture();
239 go_ast_sp = std::shared_ptr<GoASTContext>(new GoASTContext);
241 arch = target->GetArchitecture();
242 go_ast_sp = std::shared_ptr<GoASTContextForExpr>(
243 new GoASTContextForExpr(target->shared_from_this()));
246 if (arch.IsValid()) {
247 go_ast_sp->SetAddressByteSize(arch.GetAddressByteSize());
251 return lldb::TypeSystemSP();
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});
260 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
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());
270 void GoASTContext::Initialize() {
271 PluginManager::RegisterPlugin(GetPluginNameStatic(), "AST context plug-in",
272 CreateInstance, EnumerateSupportedLanguages);
275 void GoASTContext::Terminate() {
276 PluginManager::UnregisterPlugin(CreateInstance);
279 //----------------------------------------------------------------------
281 //----------------------------------------------------------------------
283 bool GoASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
284 CompilerType *element_type, uint64_t *size,
285 bool *is_incomplete) {
287 element_type->Clear();
291 *is_incomplete = false;
292 GoArray *array = static_cast<GoType *>(type)->GetArray();
295 *size = array->GetLength();
297 *element_type = array->GetElementType();
303 bool GoASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
304 CompilerType *element_type, uint64_t *size) {
306 element_type->Clear();
312 bool GoASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
313 int kind = static_cast<GoType *>(type)->GetGoKind();
314 if (kind < GoType::KIND_ARRAY)
316 if (kind == GoType::KIND_PTR)
318 if (kind == GoType::KIND_CHAN)
320 if (kind == GoType::KIND_MAP)
322 if (kind == GoType::KIND_STRING)
324 if (kind == GoType::KIND_UNSAFEPOINTER)
329 bool GoASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
333 bool GoASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
334 // Go's DWARF doesn't distinguish between rune and int32.
338 bool GoASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
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();
349 bool GoASTContext::IsConst(lldb::opaque_compiler_type_t type) { return false; }
351 bool GoASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
356 bool GoASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
357 return type != nullptr;
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) {
378 bool GoASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
379 bool *is_variadic_ptr) {
380 GoFunction *func = static_cast<GoType *>(type)->GetFunction();
383 *is_variadic_ptr = func->IsVariadic();
387 *is_variadic_ptr = false;
391 uint32_t GoASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
392 CompilerType *base_type_ptr) {
397 GoASTContext::GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) {
402 GoASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
403 const size_t index) {
404 return CompilerType();
407 bool GoASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
408 return IsFunctionType(type);
411 bool GoASTContext::IsBlockPointerType(lldb::opaque_compiler_type_t type,
412 CompilerType *function_pointer_type_ptr) {
416 bool GoASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
419 // TODO: Is bool an integer?
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);
430 bool GoASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
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) {
439 target_type->Clear();
441 return static_cast<GoType *>(type)->GetGoKind() == GoType::KIND_INTERFACE;
445 bool GoASTContext::IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) {
449 bool GoASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
450 CompilerType *pointee_type) {
453 GoType *t = static_cast<GoType *>(type);
455 *pointee_type = t->GetElementType();
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?
469 bool GoASTContext::IsPointerOrReferenceType(lldb::opaque_compiler_type_t type,
470 CompilerType *pointee_type) {
471 return IsPointerType(type, pointee_type);
474 bool GoASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
475 CompilerType *pointee_type,
480 bool GoASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
481 return !IsAggregateType(type);
484 bool GoASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
486 return static_cast<GoType *>(type)->IsTypedef();
490 bool GoASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
493 return static_cast<GoType *>(type)->GetGoKind() == GoType::KIND_LLDB_VOID;
496 bool GoASTContext::SupportsLanguage(lldb::LanguageType language) {
497 return language == eLanguageTypeGo;
500 //----------------------------------------------------------------------
502 //----------------------------------------------------------------------
504 bool GoASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
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()) {
513 CompilerType compiler_type(this, s);
514 SymbolFile *symbols = GetSymbolFile();
515 return symbols && symbols->CompleteType(compiler_type);
520 //----------------------------------------------------------------------
521 // AST related queries
522 //----------------------------------------------------------------------
524 uint32_t GoASTContext::GetPointerByteSize() { return m_pointer_byte_size; }
526 //----------------------------------------------------------------------
528 //----------------------------------------------------------------------
530 ConstString GoASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
532 return static_cast<GoType *>(type)->GetName();
533 return ConstString();
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();
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;
556 builtin_type_flags |= eTypeIsFloat;
557 if (kind < GoType::KIND_COMPLEX64)
558 builtin_type_flags |= eTypeIsComplex;
560 builtin_type_flags |= eTypeIsScalar;
562 return builtin_type_flags;
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)
572 return eTypeHasChildren | eTypeIsStructUnion;
575 lldb::TypeClass GoASTContext::GetTypeClass(lldb::opaque_compiler_type_t 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;
593 GoASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
594 ConstString name = GetTypeName(type);
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, []() {
601 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
603 g_type_map.Append(ConstString("int"), eBasicTypeInt);
604 g_type_map.Append(ConstString("uint"), eBasicTypeUnsignedInt);
607 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
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);
626 return g_type_map.Find(name, eBasicTypeInvalid);
628 return eBasicTypeInvalid;
632 GoASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
633 return lldb::eLanguageTypeGo;
636 unsigned GoASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
640 //----------------------------------------------------------------------
641 // Creating related types
642 //----------------------------------------------------------------------
645 GoASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
647 GoArray *array = static_cast<GoType *>(type)->GetArray();
650 *stride = array->GetElementType().GetByteSize(nullptr);
652 return array->GetElementType();
654 return CompilerType();
657 CompilerType GoASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
658 GoType *t = static_cast<GoType *>(type);
660 return t->GetElementType();
661 return CompilerType(this, type);
665 GoASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
666 return CompilerType(this, type);
669 // Returns -1 if this isn't a function of if the function doesn't have a
671 // Returns a value >= 0 if there is a prototype.
672 int GoASTContext::GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) {
673 return GetNumberOfFunctionArguments(type);
677 GoASTContext::GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type,
679 return GetFunctionArgumentAtIndex(type, idx);
683 GoASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
686 GoType *t = static_cast<GoType *>(type);
687 if (t->GetGoKind() == GoType::KIND_FUNC)
688 result = t->GetElementType();
693 size_t GoASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
697 TypeMemberFunctionImpl
698 GoASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
700 return TypeMemberFunctionImpl();
704 GoASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
705 return CompilerType(this, type);
708 CompilerType GoASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
710 return CompilerType();
711 return static_cast<GoType *>(type)->GetElementType();
714 CompilerType GoASTContext::GetPointerType(lldb::opaque_compiler_type_t 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) {
722 new GoElem(GoType::KIND_PTR, pointer_name, CompilerType(this, type));
723 (*m_types)[pointer_name].reset(pointer);
725 return CompilerType(this, pointer);
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();
735 //----------------------------------------------------------------------
736 // Create related types using the current type's AST
737 //----------------------------------------------------------------------
738 CompilerType GoASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
739 return CompilerType();
743 GoASTContext::GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding,
745 return CompilerType();
748 //----------------------------------------------------------------------
749 // Exploring the type
750 //----------------------------------------------------------------------
752 uint64_t GoASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
753 ExecutionContextScope *exe_scope) {
756 if (!GetCompleteType(type))
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:
765 case GoType::KIND_INT16:
766 case GoType::KIND_UINT16:
768 case GoType::KIND_INT32:
769 case GoType::KIND_UINT32:
770 case GoType::KIND_FLOAT32:
772 case GoType::KIND_INT64:
773 case GoType::KIND_UINT64:
774 case GoType::KIND_FLOAT64:
775 case GoType::KIND_COMPLEX64:
777 case GoType::KIND_COMPLEX128:
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;
804 lldb::Encoding GoASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
808 if (IsIntegerType(type, is_signed))
809 return is_signed ? lldb::eEncodingSint : eEncodingUint;
811 uint32_t complex_count;
812 if (IsFloatingPointType(type, complex_count, is_complex)) {
813 count = complex_count;
814 return eEncodingIEEE754;
816 if (IsPointerType(type))
817 return eEncodingUint;
818 return eEncodingInvalid;
821 lldb::Format GoASTContext::GetFormat(lldb::opaque_compiler_type_t 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:
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:
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:
858 // Don't know how to display this.
863 size_t GoASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
867 uint32_t GoASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
868 bool omit_empty_base_classes) {
869 if (!type || !GetCompleteType(type))
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);
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);
883 return GetNumFields(type);
886 uint32_t GoASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
887 if (!type || !GetCompleteType(type))
889 GoType *t = static_cast<GoType *>(type);
891 return t->GetElementType().GetNumFields();
892 GoStruct *s = t->GetStruct();
894 return s->GetNumFields();
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) {
905 if (bitfield_bit_size_ptr)
906 *bitfield_bit_size_ptr = 0;
908 *is_bitfield_ptr = false;
910 if (!type || !GetCompleteType(type))
911 return CompilerType();
913 GoType *t = static_cast<GoType *>(type);
915 return t->GetElementType().GetFieldAtIndex(
916 idx, name, bit_offset_ptr, bitfield_bit_size_ptr, is_bitfield_ptr);
918 GoStruct *s = t->GetStruct();
920 const auto *field = s->GetField(idx);
922 name = field->m_name.GetStringRef();
924 *bit_offset_ptr = field->m_byte_offset * 8;
925 return field->m_type;
928 return CompilerType();
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) {
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;
948 if (!type || !GetCompleteType(type))
949 return CompilerType();
951 GoType *t = static_cast<GoType *>(type);
952 if (t->GetStruct()) {
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;
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,
973 child_is_deref_of_parent = true;
974 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
976 child_name.assign(1, '*');
977 child_name += parent_name;
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;
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;
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);
1008 return CompilerType();
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.
1014 GoASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
1016 bool omit_empty_base_classes) {
1017 if (!type || !GetCompleteType(type))
1020 GoType *t = static_cast<GoType *>(type);
1021 GoStruct *s = t->GetStruct();
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)
1028 } else if (t->GetGoKind() == GoType::KIND_PTR || t->IsTypedef()) {
1029 return t->GetElementType().GetIndexOfChildWithName(name,
1030 omit_empty_base_classes);
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)
1047 child_indexes.push_back(index);
1051 // Converts "s" to a floating point value and place resulting floating
1052 // point bytes in the "dst" buffer.
1054 GoASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
1055 const char *s, uint8_t *dst,
1060 //----------------------------------------------------------------------
1062 //----------------------------------------------------------------------
1063 #define DEPTH_INCREMENT 2
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();
1076 GoType *t = static_cast<GoType *>(type);
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;
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.
1092 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
1094 // Print the member type if requested
1096 ConstString field_type_name = field->m_type.GetTypeName();
1097 s->Printf("(%s) ", field_type_name.AsCString());
1099 // Print the member name and equal sign
1100 s->Printf("%s = ", field->m_name.AsCString());
1102 // Dump the value of the member
1103 CompilerType field_type = field->m_type;
1104 field_type.DumpValue(
1106 s, // Stream to dump to
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
1115 0, // Bitfield bit size
1116 0, // Bitfield bit offset
1117 show_types, // Boolean indicating if we should show the variable
1119 show_summary, // Boolean indicating if we should show a summary for
1121 verbose, // Verbose output?
1122 depth + DEPTH_INCREMENT); // Scope depth for any types that have
1126 // Indent the trailing squiggly bracket
1128 s->Printf("\n%*s}", depth, "");
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());
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)
1148 // Indent and print the index
1149 s->Printf("\n%*s[%" PRIu64 "] ", depth + DEPTH_INCREMENT, "",
1152 // Figure out the field offset within the current struct/union/class type
1153 uint64_t element_offset = element_idx * element_byte_size;
1155 // Dump the value of the member
1156 element_clang_type.DumpValue(
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
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
1169 verbose, // Verbose output?
1171 DEPTH_INCREMENT); // Scope depth for any types that have children
1174 // Indent the trailing squiggly bracket
1175 if (element_idx > 0)
1176 s->Printf("\n%*s}", depth, "");
1180 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
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) {
1191 if (IsAggregateType(type)) {
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);
1201 return typedef_compiler_type.DumpTypeValue(
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
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...
1219 case eFormatBoolean:
1221 case eFormatComplex:
1222 case eFormatCString: // NULL terminated C strings
1223 case eFormatDecimal:
1226 case eFormatHexUppercase:
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:
1247 case eFormatCharPrintable:
1248 case eFormatCharArray:
1250 case eFormatBytesWithASCII:
1251 item_count = byte_size;
1255 case eFormatUnicode16:
1256 item_count = byte_size / 2;
1260 case eFormatUnicode32:
1261 item_count = byte_size / 4;
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);
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
1282 void GoASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
1284 StreamFile s(stdout, false);
1285 DumpTypeDescription(type, &s);
1288 void GoASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
1292 ConstString name = GetTypeName(type);
1293 GoType *t = static_cast<GoType *>(type);
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) {
1305 uint32_t field_idx = 0;
1306 for (auto *field = st->GetField(field_idx); field != nullptr;
1310 s->Printf("%s %s", field->m_name.AsCString(),
1311 field->m_type.GetTypeName().AsCString());
1320 s->PutCString(name.AsCString());
1323 CompilerType GoASTContext::CreateArrayType(const ConstString &name,
1324 const CompilerType &element_type,
1326 GoType *type = new GoArray(name, length, element_type);
1327 (*m_types)[name].reset(type);
1328 return CompilerType(this, type);
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);
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);
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);
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);
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) {
1370 llvm::dyn_cast_or_null<GoASTContext>(struct_type.GetTypeSystem());
1373 GoType *type = static_cast<GoType *>(struct_type.GetOpaqueQualType());
1374 if (GoStruct *s = type->GetStruct())
1375 s->AddField(name, field_type, byte_offset);
1378 void GoASTContext::CompleteStructType(
1379 const lldb_private::CompilerType &struct_type) {
1383 llvm::dyn_cast_or_null<GoASTContext>(struct_type.GetTypeSystem());
1386 GoType *type = static_cast<GoType *>(struct_type.GetOpaqueQualType());
1387 if (GoStruct *s = type->GetStruct())
1392 GoASTContext::CreateFunctionType(const lldb_private::ConstString &name,
1393 CompilerType *params, size_t params_count,
1395 GoType *type = new GoFunction(name, is_variadic);
1396 (*m_types)[name].reset(type);
1397 return CompilerType(this, type);
1400 bool GoASTContext::IsGoString(const lldb_private::CompilerType &type) {
1401 if (!type.IsValid() ||
1402 !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1404 return GoType::KIND_STRING ==
1405 static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1408 bool GoASTContext::IsGoSlice(const lldb_private::CompilerType &type) {
1409 if (!type.IsValid() ||
1410 !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1412 return GoType::KIND_SLICE ==
1413 static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1416 bool GoASTContext::IsGoInterface(const lldb_private::CompilerType &type) {
1417 if (!type.IsValid() ||
1418 !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1420 return GoType::KIND_INTERFACE ==
1421 static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1424 bool GoASTContext::IsPointerKind(uint8_t kind) {
1425 return (kind & GoType::KIND_MASK) == GoType::KIND_PTR;
1428 bool GoASTContext::IsDirectIface(uint8_t kind) {
1429 return (kind & GoType::KIND_DIRECT_IFACE) == GoType::KIND_DIRECT_IFACE;
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();
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();
1444 return new GoUserExpression(*target, expr, prefix, language, desired_type,