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/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"
28 #include "Plugins/ExpressionParser/Go/GoUserExpression.h"
29 #include "Plugins/SymbolFile/DWARF/DWARFASTParserGo.h"
33 namespace lldb_private
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
74 GoType(int kind, const ConstString &name)
75 : m_kind(kind & KIND_MASK)
78 if (m_kind == KIND_FUNC)
94 GetElementType() const
96 return CompilerType();
114 GoFunction *GetFunction();
115 GoStruct *GetStruct();
120 GoType(const GoType &) = delete;
121 const GoType &operator=(const GoType &) = delete;
124 class GoElem : public GoType
127 GoElem(int kind, const ConstString &name, const CompilerType &elem)
133 GetElementType() const
139 // TODO: should we store this differently?
142 GoElem(const GoElem &) = delete;
143 const GoElem &operator=(const GoElem &) = delete;
146 class GoArray : public GoElem
149 GoArray(const ConstString &name, uint64_t length, const CompilerType &elem)
150 : GoElem(KIND_ARRAY, name, elem)
163 GoArray(const GoArray &) = delete;
164 const GoArray &operator=(const GoArray &) = delete;
167 class GoFunction : public GoType
170 GoFunction(const ConstString &name, bool is_variadic)
171 : GoType(KIND_FUNC, name)
172 , m_is_variadic(is_variadic)
179 return m_is_variadic;
184 GoFunction(const GoFunction &) = delete;
185 const GoFunction &operator=(const GoFunction &) = delete;
188 class GoStruct : public GoType
193 Field(const ConstString &name, const CompilerType &type, uint64_t offset)
196 , m_byte_offset(offset)
201 uint64_t m_byte_offset;
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)
212 return m_fields.size();
216 GetField(uint32_t i) const
218 if (i < m_fields.size())
224 AddField(const ConstString &name, const CompilerType &type, uint64_t offset)
226 m_fields.push_back(Field(name, type, offset));
232 return m_is_complete;
238 m_is_complete = true;
250 std::vector<Field> m_fields;
252 GoStruct(const GoStruct &) = delete;
253 const GoStruct &operator=(const GoStruct &) = delete;
259 if (m_kind == KIND_ARRAY)
261 return static_cast<GoArray *>(this);
267 GoType::GetFunction()
269 if (m_kind == KIND_FUNC)
271 return static_cast<GoFunction *>(this);
284 return static_cast<GoStruct *>(this);
288 } // namespace lldb_private
289 using namespace lldb_private;
291 GoASTContext::GoASTContext()
292 : TypeSystem(eKindGo)
293 , m_pointer_byte_size(0)
295 , m_types(new TypeMap)
298 GoASTContext::~GoASTContext()
302 //------------------------------------------------------------------
303 // PluginInterface functions
304 //------------------------------------------------------------------
307 GoASTContext::GetPluginNameStatic()
309 return ConstString("go");
313 GoASTContext::GetPluginName()
315 return GoASTContext::GetPluginNameStatic();
319 GoASTContext::GetPluginVersion()
325 GoASTContext::CreateInstance (lldb::LanguageType language, Module *module, Target *target)
327 if (language == eLanguageTypeGo)
330 std::shared_ptr<GoASTContext> go_ast_sp;
333 arch = module->GetArchitecture();
334 go_ast_sp = std::shared_ptr<GoASTContext>(new GoASTContext);
338 arch = target->GetArchitecture();
339 go_ast_sp = std::shared_ptr<GoASTContextForExpr>(new GoASTContextForExpr(target->shared_from_this()));
344 go_ast_sp->SetAddressByteSize(arch.GetAddressByteSize());
348 return lldb::TypeSystemSP();
352 GoASTContext::EnumerateSupportedLanguages(std::set<lldb::LanguageType> &languages_for_types, std::set<lldb::LanguageType> &languages_for_expressions)
354 static std::vector<lldb::LanguageType> s_supported_languages_for_types({
355 lldb::eLanguageTypeGo});
357 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions({});
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());
365 GoASTContext::Initialize()
367 PluginManager::RegisterPlugin (GetPluginNameStatic(),
368 "AST context plug-in",
370 EnumerateSupportedLanguages);
374 GoASTContext::Terminate()
376 PluginManager::UnregisterPlugin (CreateInstance);
380 //----------------------------------------------------------------------
382 //----------------------------------------------------------------------
385 GoASTContext::IsArrayType(lldb::opaque_compiler_type_t type, CompilerType *element_type, uint64_t *size, bool *is_incomplete)
388 element_type->Clear();
392 *is_incomplete = false;
393 GoArray *array = static_cast<GoType *>(type)->GetArray();
397 *size = array->GetLength();
399 *element_type = array->GetElementType();
406 GoASTContext::IsVectorType(lldb::opaque_compiler_type_t type, CompilerType *element_type, uint64_t *size)
409 element_type->Clear();
416 GoASTContext::IsAggregateType(lldb::opaque_compiler_type_t type)
418 int kind = static_cast<GoType *>(type)->GetGoKind();
419 if (kind < GoType::KIND_ARRAY)
421 if (kind == GoType::KIND_PTR)
423 if (kind == GoType::KIND_CHAN)
425 if (kind == GoType::KIND_MAP)
427 if (kind == GoType::KIND_STRING)
429 if (kind == GoType::KIND_UNSAFEPOINTER)
435 GoASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type)
441 GoASTContext::IsCharType(lldb::opaque_compiler_type_t type)
443 // Go's DWARF doesn't distinguish between rune and int32.
448 GoASTContext::IsCompleteType(lldb::opaque_compiler_type_t type)
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();
461 GoASTContext::IsConst(lldb::opaque_compiler_type_t type)
467 GoASTContext::IsCStringType(lldb::opaque_compiler_type_t type, uint32_t &length)
473 GoASTContext::IsDefined(lldb::opaque_compiler_type_t type)
475 return type != nullptr;
479 GoASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type, uint32_t &count, bool &is_complex)
481 int kind = static_cast<GoType *>(type)->GetGoKind();
482 if (kind >= GoType::KIND_FLOAT32 && kind <= GoType::KIND_COMPLEX128)
484 if (kind >= GoType::KIND_COMPLEX64)
502 GoASTContext::IsFunctionType(lldb::opaque_compiler_type_t type, bool *is_variadic_ptr)
504 GoFunction *func = static_cast<GoType *>(type)->GetFunction();
508 *is_variadic_ptr = func->IsVariadic();
512 *is_variadic_ptr = false;
517 GoASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type, CompilerType *base_type_ptr)
523 GoASTContext::GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type)
529 GoASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type, const size_t index)
531 return CompilerType();
535 GoASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type)
537 return IsFunctionType(type);
541 GoASTContext::IsIntegerType(lldb::opaque_compiler_type_t type, bool &is_signed)
544 // TODO: Is bool an integer?
547 int kind = static_cast<GoType *>(type)->GetGoKind();
548 if (kind <= GoType::KIND_UINTPTR)
550 is_signed = (kind != GoType::KIND_BOOL) & (kind <= GoType::KIND_INT64);
558 GoASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type)
564 GoASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
565 CompilerType *target_type, // Can pass NULL
566 bool check_cplusplus, bool check_objc)
569 target_type->Clear();
571 return static_cast<GoType *>(type)->GetGoKind() == GoType::KIND_INTERFACE;
576 GoASTContext::IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type)
582 GoASTContext::IsPointerType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type)
586 GoType *t = static_cast<GoType *>(type);
589 *pointee_type = t->GetElementType();
591 switch (t->GetGoKind())
593 case GoType::KIND_PTR:
594 case GoType::KIND_UNSAFEPOINTER:
595 case GoType::KIND_CHAN:
596 case GoType::KIND_MAP:
597 // TODO: is function a pointer?
605 GoASTContext::IsPointerOrReferenceType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type)
607 return IsPointerType(type, pointee_type);
611 GoASTContext::IsReferenceType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type, bool *is_rvalue)
617 GoASTContext::IsScalarType(lldb::opaque_compiler_type_t type)
619 return !IsAggregateType(type);
623 GoASTContext::IsTypedefType(lldb::opaque_compiler_type_t type)
626 return static_cast<GoType *>(type)->IsTypedef();
631 GoASTContext::IsVoidType(lldb::opaque_compiler_type_t type)
635 return static_cast<GoType *>(type)->GetGoKind() == GoType::KIND_LLDB_VOID;
639 GoASTContext::SupportsLanguage (lldb::LanguageType language)
641 return language == eLanguageTypeGo;
644 //----------------------------------------------------------------------
646 //----------------------------------------------------------------------
649 GoASTContext::GetCompleteType(lldb::opaque_compiler_type_t type)
653 GoType *t = static_cast<GoType *>(type);
654 if (t->IsTypedef() || t->GetGoKind() == GoType::KIND_PTR || t->GetArray())
655 return t->GetElementType().GetCompleteType();
656 if (GoStruct *s = t->GetStruct())
660 CompilerType compiler_type(this, s);
661 SymbolFile *symbols = GetSymbolFile();
662 return symbols && symbols->CompleteType(compiler_type);
667 //----------------------------------------------------------------------
668 // AST related queries
669 //----------------------------------------------------------------------
672 GoASTContext::GetPointerByteSize()
674 return m_pointer_byte_size;
677 //----------------------------------------------------------------------
679 //----------------------------------------------------------------------
682 GoASTContext::GetTypeName(lldb::opaque_compiler_type_t type)
685 return static_cast<GoType *>(type)->GetName();
686 return ConstString();
690 GoASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type, CompilerType *pointee_or_element_compiler_type)
692 if (pointee_or_element_compiler_type)
693 pointee_or_element_compiler_type->Clear();
696 GoType *t = static_cast<GoType *>(type);
697 if (pointee_or_element_compiler_type)
698 *pointee_or_element_compiler_type = t->GetElementType();
699 int kind = t->GetGoKind();
700 if (kind == GoType::KIND_ARRAY)
701 return eTypeHasChildren | eTypeIsArray;
702 if (kind < GoType::KIND_ARRAY)
704 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
705 if (kind < GoType::KIND_FLOAT32)
707 builtin_type_flags |= eTypeIsInteger | eTypeIsScalar;
708 if (kind >= GoType::KIND_INT && kind <= GoType::KIND_INT64)
709 builtin_type_flags |= eTypeIsSigned;
713 builtin_type_flags |= eTypeIsFloat;
714 if (kind < GoType::KIND_COMPLEX64)
715 builtin_type_flags |= eTypeIsComplex;
717 builtin_type_flags |= eTypeIsScalar;
719 return builtin_type_flags;
721 if (kind == GoType::KIND_STRING)
722 return eTypeHasValue | eTypeIsBuiltIn;
723 if (kind == GoType::KIND_FUNC)
724 return eTypeIsFuncPrototype | eTypeHasValue;
725 if (IsPointerType(type))
726 return eTypeIsPointer | eTypeHasValue | eTypeHasChildren;
727 if (kind == GoType::KIND_LLDB_VOID)
729 return eTypeHasChildren | eTypeIsStructUnion;
733 GoASTContext::GetTypeClass(lldb::opaque_compiler_type_t type)
736 return eTypeClassInvalid;
737 int kind = static_cast<GoType *>(type)->GetGoKind();
738 if (kind == GoType::KIND_FUNC)
739 return eTypeClassFunction;
740 if (IsPointerType(type))
741 return eTypeClassPointer;
742 if (kind < GoType::KIND_COMPLEX64)
743 return eTypeClassBuiltin;
744 if (kind <= GoType::KIND_COMPLEX128)
745 return eTypeClassComplexFloat;
746 if (kind == GoType::KIND_LLDB_VOID)
747 return eTypeClassInvalid;
748 return eTypeClassStruct;
752 GoASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type)
754 ConstString name = GetTypeName(type);
757 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
758 static TypeNameToBasicTypeMap g_type_map;
759 static std::once_flag g_once_flag;
760 std::call_once(g_once_flag, [](){
762 g_type_map.Append(ConstString("void").GetCString(), eBasicTypeVoid);
764 g_type_map.Append(ConstString("int").GetCString(), eBasicTypeInt);
765 g_type_map.Append(ConstString("uint").GetCString(), eBasicTypeUnsignedInt);
768 g_type_map.Append(ConstString("bool").GetCString(), eBasicTypeBool);
770 // Others. Should these map to C types?
771 g_type_map.Append(ConstString("byte").GetCString(), eBasicTypeOther);
772 g_type_map.Append(ConstString("uint8").GetCString(), eBasicTypeOther);
773 g_type_map.Append(ConstString("uint16").GetCString(), eBasicTypeOther);
774 g_type_map.Append(ConstString("uint32").GetCString(), eBasicTypeOther);
775 g_type_map.Append(ConstString("uint64").GetCString(), eBasicTypeOther);
776 g_type_map.Append(ConstString("int8").GetCString(), eBasicTypeOther);
777 g_type_map.Append(ConstString("int16").GetCString(), eBasicTypeOther);
778 g_type_map.Append(ConstString("int32").GetCString(), eBasicTypeOther);
779 g_type_map.Append(ConstString("int64").GetCString(), eBasicTypeOther);
780 g_type_map.Append(ConstString("float32").GetCString(), eBasicTypeOther);
781 g_type_map.Append(ConstString("float64").GetCString(), eBasicTypeOther);
782 g_type_map.Append(ConstString("uintptr").GetCString(), eBasicTypeOther);
787 return g_type_map.Find(name.GetCString(), eBasicTypeInvalid);
789 return eBasicTypeInvalid;
793 GoASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type)
795 return lldb::eLanguageTypeGo;
799 GoASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type)
804 //----------------------------------------------------------------------
805 // Creating related types
806 //----------------------------------------------------------------------
809 GoASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type, uint64_t *stride)
811 GoArray *array = static_cast<GoType *>(type)->GetArray();
816 *stride = array->GetElementType().GetByteSize(nullptr);
818 return array->GetElementType();
820 return CompilerType();
824 GoASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type)
826 GoType *t = static_cast<GoType *>(type);
828 return t->GetElementType();
829 return CompilerType(this, type);
833 GoASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type)
835 return CompilerType(this, type);
838 // Returns -1 if this isn't a function of if the function doesn't have a prototype
839 // Returns a value >= 0 if there is a prototype.
841 GoASTContext::GetFunctionArgumentCount(lldb::opaque_compiler_type_t type)
843 return GetNumberOfFunctionArguments(type);
847 GoASTContext::GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type, size_t idx)
849 return GetFunctionArgumentAtIndex(type, idx);
853 GoASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type)
858 GoType *t = static_cast<GoType *>(type);
859 if (t->GetGoKind() == GoType::KIND_FUNC)
860 result = t->GetElementType();
866 GoASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type)
871 TypeMemberFunctionImpl
872 GoASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, size_t idx)
874 return TypeMemberFunctionImpl();
878 GoASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type)
880 return CompilerType(this, type);
884 GoASTContext::GetPointeeType(lldb::opaque_compiler_type_t type)
887 return CompilerType();
888 return static_cast<GoType *>(type)->GetElementType();
892 GoASTContext::GetPointerType(lldb::opaque_compiler_type_t type)
895 return CompilerType();
896 ConstString type_name = GetTypeName(type);
897 ConstString pointer_name(std::string("*") + type_name.GetCString());
898 GoType *pointer = (*m_types)[pointer_name].get();
899 if (pointer == nullptr)
901 pointer = new GoElem(GoType::KIND_PTR, pointer_name, CompilerType(this, type));
902 (*m_types)[pointer_name].reset(pointer);
904 return CompilerType(this, pointer);
907 // If the current object represents a typedef type, get the underlying type
909 GoASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type)
911 if (IsTypedefType(type))
912 return static_cast<GoType *>(type)->GetElementType();
913 return CompilerType();
916 //----------------------------------------------------------------------
917 // Create related types using the current type's AST
918 //----------------------------------------------------------------------
920 GoASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type)
922 return CompilerType();
926 GoASTContext::GetBuiltinTypeForEncodingAndBitSize (lldb::Encoding encoding,
929 return CompilerType();
933 //----------------------------------------------------------------------
934 // Exploring the type
935 //----------------------------------------------------------------------
938 GoASTContext::GetBitSize(lldb::opaque_compiler_type_t type, ExecutionContextScope *exe_scope)
942 if (!GetCompleteType(type))
944 GoType *t = static_cast<GoType *>(type);
945 GoArray *array = nullptr;
946 switch (t->GetGoKind())
948 case GoType::KIND_BOOL:
949 case GoType::KIND_INT8:
950 case GoType::KIND_UINT8:
952 case GoType::KIND_INT16:
953 case GoType::KIND_UINT16:
955 case GoType::KIND_INT32:
956 case GoType::KIND_UINT32:
957 case GoType::KIND_FLOAT32:
959 case GoType::KIND_INT64:
960 case GoType::KIND_UINT64:
961 case GoType::KIND_FLOAT64:
962 case GoType::KIND_COMPLEX64:
964 case GoType::KIND_COMPLEX128:
966 case GoType::KIND_INT:
967 case GoType::KIND_UINT:
968 return m_int_byte_size * 8;
969 case GoType::KIND_UINTPTR:
970 case GoType::KIND_FUNC: // I assume this is a pointer?
971 case GoType::KIND_CHAN:
972 case GoType::KIND_PTR:
973 case GoType::KIND_UNSAFEPOINTER:
974 case GoType::KIND_MAP:
975 return m_pointer_byte_size * 8;
976 case GoType::KIND_ARRAY:
977 array = t->GetArray();
978 return array->GetLength() * array->GetElementType().GetBitSize(exe_scope);
979 case GoType::KIND_INTERFACE:
980 return t->GetElementType().GetBitSize(exe_scope);
981 case GoType::KIND_SLICE:
982 case GoType::KIND_STRING:
983 case GoType::KIND_STRUCT:
984 return t->GetStruct()->GetByteSize() * 8;
992 GoASTContext::GetEncoding(lldb::opaque_compiler_type_t type, uint64_t &count)
996 if (IsIntegerType(type, is_signed))
997 return is_signed ? lldb::eEncodingSint : eEncodingUint;
999 uint32_t complex_count;
1000 if (IsFloatingPointType(type, complex_count, is_complex))
1002 count = complex_count;
1003 return eEncodingIEEE754;
1005 if (IsPointerType(type))
1006 return eEncodingUint;
1007 return eEncodingInvalid;
1011 GoASTContext::GetFormat(lldb::opaque_compiler_type_t type)
1014 return eFormatDefault;
1015 switch (static_cast<GoType *>(type)->GetGoKind())
1017 case GoType::KIND_BOOL:
1018 return eFormatBoolean;
1019 case GoType::KIND_INT:
1020 case GoType::KIND_INT8:
1021 case GoType::KIND_INT16:
1022 case GoType::KIND_INT32:
1023 case GoType::KIND_INT64:
1024 return eFormatDecimal;
1025 case GoType::KIND_UINT:
1026 case GoType::KIND_UINT8:
1027 case GoType::KIND_UINT16:
1028 case GoType::KIND_UINT32:
1029 case GoType::KIND_UINT64:
1030 return eFormatUnsigned;
1031 case GoType::KIND_FLOAT32:
1032 case GoType::KIND_FLOAT64:
1033 return eFormatFloat;
1034 case GoType::KIND_COMPLEX64:
1035 case GoType::KIND_COMPLEX128:
1036 return eFormatComplexFloat;
1037 case GoType::KIND_UINTPTR:
1038 case GoType::KIND_CHAN:
1039 case GoType::KIND_PTR:
1040 case GoType::KIND_MAP:
1041 case GoType::KIND_UNSAFEPOINTER:
1043 case GoType::KIND_STRING:
1044 return eFormatCString;
1045 case GoType::KIND_ARRAY:
1046 case GoType::KIND_INTERFACE:
1047 case GoType::KIND_SLICE:
1048 case GoType::KIND_STRUCT:
1050 // Don't know how to display this.
1051 return eFormatBytes;
1056 GoASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type)
1062 GoASTContext::GetNumChildren(lldb::opaque_compiler_type_t type, bool omit_empty_base_classes)
1064 if (!type || !GetCompleteType(type))
1066 GoType *t = static_cast<GoType *>(type);
1067 if (t->GetGoKind() == GoType::KIND_PTR)
1069 CompilerType elem = t->GetElementType();
1070 if (elem.IsAggregateType())
1071 return elem.GetNumChildren(omit_empty_base_classes);
1074 else if (GoArray *array = t->GetArray())
1076 return array->GetLength();
1078 else if (t->IsTypedef())
1080 return t->GetElementType().GetNumChildren(omit_empty_base_classes);
1083 return GetNumFields(type);
1087 GoASTContext::GetNumFields(lldb::opaque_compiler_type_t type)
1089 if (!type || !GetCompleteType(type))
1091 GoType *t = static_cast<GoType *>(type);
1093 return t->GetElementType().GetNumFields();
1094 GoStruct *s = t->GetStruct();
1096 return s->GetNumFields();
1101 GoASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type, size_t idx, std::string &name, uint64_t *bit_offset_ptr,
1102 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr)
1105 *bit_offset_ptr = 0;
1106 if (bitfield_bit_size_ptr)
1107 *bitfield_bit_size_ptr = 0;
1108 if (is_bitfield_ptr)
1109 *is_bitfield_ptr = false;
1111 if (!type || !GetCompleteType(type))
1112 return CompilerType();
1114 GoType *t = static_cast<GoType *>(type);
1116 return t->GetElementType().GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, is_bitfield_ptr);
1118 GoStruct *s = t->GetStruct();
1121 const auto *field = s->GetField(idx);
1124 name = field->m_name.GetStringRef();
1126 *bit_offset_ptr = field->m_byte_offset * 8;
1127 return field->m_type;
1130 return CompilerType();
1134 GoASTContext::GetChildCompilerTypeAtIndex(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
1135 bool omit_empty_base_classes, bool ignore_array_bounds, std::string &child_name,
1136 uint32_t &child_byte_size, int32_t &child_byte_offset,
1137 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
1138 bool &child_is_base_class, bool &child_is_deref_of_parent, ValueObject *valobj, uint64_t &language_flags)
1141 child_byte_size = 0;
1142 child_byte_offset = 0;
1143 child_bitfield_bit_size = 0;
1144 child_bitfield_bit_offset = 0;
1145 child_is_base_class = false;
1146 child_is_deref_of_parent = false;
1149 if (!type || !GetCompleteType(type))
1150 return CompilerType();
1152 GoType *t = static_cast<GoType *>(type);
1155 uint64_t bit_offset;
1156 CompilerType ret = GetFieldAtIndex(type, idx, child_name, &bit_offset, nullptr, nullptr);
1157 child_byte_size = ret.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr);
1158 child_byte_offset = bit_offset / 8;
1161 else if (t->GetGoKind() == GoType::KIND_PTR)
1163 CompilerType pointee = t->GetElementType();
1164 if (!pointee.IsValid() || pointee.IsVoidType())
1165 return CompilerType();
1166 if (transparent_pointers && pointee.IsAggregateType())
1168 bool tmp_child_is_deref_of_parent = false;
1169 return pointee.GetChildCompilerTypeAtIndex(exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
1170 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
1171 child_bitfield_bit_size, child_bitfield_bit_offset,
1172 child_is_base_class, tmp_child_is_deref_of_parent, valobj, language_flags);
1176 child_is_deref_of_parent = true;
1177 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
1180 child_name.assign(1, '*');
1181 child_name += parent_name;
1184 // We have a pointer to an simple type
1185 if (idx == 0 && pointee.GetCompleteType())
1187 child_byte_size = pointee.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
1188 child_byte_offset = 0;
1193 else if (GoArray *a = t->GetArray())
1195 if (ignore_array_bounds || idx < a->GetLength())
1197 CompilerType element_type = a->GetElementType();
1198 if (element_type.GetCompleteType())
1200 char element_name[64];
1201 ::snprintf(element_name, sizeof(element_name), "[%zu]", idx);
1202 child_name.assign(element_name);
1203 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
1204 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
1205 return element_type;
1209 else if (t->IsTypedef())
1211 return t->GetElementType().GetChildCompilerTypeAtIndex(
1212 exe_ctx, idx, transparent_pointers, omit_empty_base_classes, ignore_array_bounds, child_name,
1213 child_byte_size, child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
1214 child_is_deref_of_parent, valobj, language_flags);
1216 return CompilerType();
1219 // Lookup a child given a name. This function will match base class names
1220 // and member member names in "clang_type" only, not descendants.
1222 GoASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type, const char *name, bool omit_empty_base_classes)
1224 if (!type || !GetCompleteType(type))
1227 GoType *t = static_cast<GoType *>(type);
1228 GoStruct *s = t->GetStruct();
1231 for (uint32_t i = 0; i < s->GetNumFields(); ++i)
1233 const GoStruct::Field *f = s->GetField(i);
1234 if (f->m_name.GetStringRef() == name)
1238 else if (t->GetGoKind() == GoType::KIND_PTR || t->IsTypedef())
1240 return t->GetElementType().GetIndexOfChildWithName(name, omit_empty_base_classes);
1245 // Lookup a child member given a name. This function will match member names
1246 // only and will descend into "clang_type" children in search for the first
1247 // member in this class, or any base class that matches "name".
1248 // TODO: Return all matches for a given name by returning a vector<vector<uint32_t>>
1249 // so we catch all names that match a given child name, not just the first.
1251 GoASTContext::GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type, const char *name, bool omit_empty_base_classes,
1252 std::vector<uint32_t> &child_indexes)
1254 uint32_t index = GetIndexOfChildWithName(type, name, omit_empty_base_classes);
1255 if (index == UINT_MAX)
1257 child_indexes.push_back(index);
1261 // Converts "s" to a floating point value and place resulting floating
1262 // point bytes in the "dst" buffer.
1264 GoASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type, const char *s, uint8_t *dst, size_t dst_size)
1269 //----------------------------------------------------------------------
1271 //----------------------------------------------------------------------
1272 #define DEPTH_INCREMENT 2
1275 GoASTContext::DumpValue(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s, lldb::Format format,
1276 const DataExtractor &data, lldb::offset_t data_byte_offset, size_t data_byte_size,
1277 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types, bool show_summary,
1278 bool verbose, uint32_t depth)
1280 if (IsTypedefType(type))
1281 type = GetTypedefedType(type).GetOpaqueQualType();
1284 GoType *t = static_cast<GoType *>(type);
1286 if (GoStruct *st = t->GetStruct())
1288 if (GetCompleteType(type))
1290 uint32_t field_idx = 0;
1291 for (auto* field = st->GetField(field_idx); field != nullptr; field_idx++)
1293 // Print the starting squiggly bracket (if this is the
1294 // first member) or comma (for member 2 and beyond) for
1295 // the struct/union/class member.
1302 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
1304 // Print the member type if requested
1307 ConstString field_type_name = field->m_type.GetTypeName();
1308 s->Printf("(%s) ", field_type_name.AsCString());
1310 // Print the member name and equal sign
1311 s->Printf("%s = ", field->m_name.AsCString());
1314 // Dump the value of the member
1315 CompilerType field_type = field->m_type;
1316 field_type.DumpValue (exe_ctx,
1317 s, // Stream to dump to
1318 field_type.GetFormat(), // The format with which to display the member
1319 data, // Data buffer containing all bytes for this type
1320 data_byte_offset + field->m_byte_offset,// Offset into "data" where to grab value from
1321 field->m_type.GetByteSize(exe_ctx->GetBestExecutionContextScope()), // Size of this type in bytes
1322 0, // Bitfield bit size
1323 0, // Bitfield bit offset
1324 show_types, // Boolean indicating if we should show the variable types
1325 show_summary, // Boolean indicating if we should show a summary for the current type
1326 verbose, // Verbose output?
1327 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
1330 // Indent the trailing squiggly bracket
1332 s->Printf("\n%*s}", depth, "");
1337 if (GoArray *a = t->GetArray()) {
1338 CompilerType element_clang_type = a->GetElementType();
1339 lldb::Format element_format = element_clang_type.GetFormat();
1340 uint32_t element_byte_size = element_clang_type.GetByteSize(exe_ctx->GetBestExecutionContextScope());
1342 uint64_t element_idx;
1343 for (element_idx = 0; element_idx < a->GetLength(); ++element_idx)
1345 // Print the starting squiggly bracket (if this is the
1346 // first member) or comman (for member 2 and beyong) for
1347 // the struct/union/class member.
1348 if (element_idx == 0)
1353 // Indent and print the index
1354 s->Printf("\n%*s[%" PRIu64 "] ", depth + DEPTH_INCREMENT, "", element_idx);
1356 // Figure out the field offset within the current struct/union/class type
1357 uint64_t element_offset = element_idx * element_byte_size;
1359 // Dump the value of the member
1360 element_clang_type.DumpValue (exe_ctx,
1361 s, // Stream to dump to
1362 element_format, // The format with which to display the element
1363 data, // Data buffer containing all bytes for this type
1364 data_byte_offset + element_offset,// Offset into "data" where to grab value from
1365 element_byte_size, // Size of this type in bytes
1366 0, // Bitfield bit size
1367 0, // Bitfield bit offset
1368 show_types, // Boolean indicating if we should show the variable types
1369 show_summary, // Boolean indicating if we should show a summary for the current type
1370 verbose, // Verbose output?
1371 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
1374 // Indent the trailing squiggly bracket
1375 if (element_idx > 0)
1376 s->Printf("\n%*s}", depth, "");
1380 DumpSummary (type, exe_ctx, s, data, data_byte_offset, data_byte_size);
1384 GoASTContext::DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format, const DataExtractor &data,
1385 lldb::offset_t byte_offset, size_t byte_size, uint32_t bitfield_bit_size,
1386 uint32_t bitfield_bit_offset, ExecutionContextScope *exe_scope)
1390 if (IsAggregateType(type))
1396 GoType *t = static_cast<GoType *>(type);
1399 CompilerType typedef_compiler_type = t->GetElementType();
1400 if (format == eFormatDefault)
1401 format = typedef_compiler_type.GetFormat();
1402 uint64_t typedef_byte_size = typedef_compiler_type.GetByteSize(exe_scope);
1404 return typedef_compiler_type.DumpTypeValue(
1406 format, // The format with which to display the element
1407 data, // Data buffer containing all bytes for this type
1408 byte_offset, // Offset into "data" where to grab value from
1409 typedef_byte_size, // Size of this type in bytes
1410 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't treat as a bitfield
1411 bitfield_bit_offset, // Offset in bits of a bitfield value if bitfield_bit_size != 0
1415 uint32_t item_count = 1;
1416 // A few formats, we might need to modify our size and count for depending
1417 // on how we are trying to display the value...
1421 case eFormatBoolean:
1423 case eFormatComplex:
1424 case eFormatCString: // NULL terminated C strings
1425 case eFormatDecimal:
1428 case eFormatHexUppercase:
1432 case eFormatUnsigned:
1433 case eFormatPointer:
1434 case eFormatVectorOfChar:
1435 case eFormatVectorOfSInt8:
1436 case eFormatVectorOfUInt8:
1437 case eFormatVectorOfSInt16:
1438 case eFormatVectorOfUInt16:
1439 case eFormatVectorOfSInt32:
1440 case eFormatVectorOfUInt32:
1441 case eFormatVectorOfSInt64:
1442 case eFormatVectorOfUInt64:
1443 case eFormatVectorOfFloat32:
1444 case eFormatVectorOfFloat64:
1445 case eFormatVectorOfUInt128:
1449 case eFormatCharPrintable:
1450 case eFormatCharArray:
1452 case eFormatBytesWithASCII:
1453 item_count = byte_size;
1457 case eFormatUnicode16:
1458 item_count = byte_size / 2;
1462 case eFormatUnicode32:
1463 item_count = byte_size / 4;
1467 return data.Dump(s, byte_offset, format, byte_size, item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
1468 bitfield_bit_size, bitfield_bit_offset, exe_scope);
1474 GoASTContext::DumpSummary(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s, const DataExtractor &data,
1475 lldb::offset_t data_offset, size_t data_byte_size)
1477 if (type && GoType::KIND_STRING == static_cast<GoType *>(type)->GetGoKind())
1479 // TODO(ribrdb): read length and data
1484 GoASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type)
1487 StreamFile s (stdout, false);
1488 DumpTypeDescription (type, &s);
1492 GoASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type, Stream *s)
1496 ConstString name = GetTypeName(type);
1497 GoType *t = static_cast<GoType *>(type);
1499 if (GoStruct *st = t->GetStruct())
1501 if (GetCompleteType(type))
1503 if (NULL == strchr(name.AsCString(), '{'))
1504 s->Printf("type %s ", name.AsCString());
1505 s->PutCString("struct {");
1506 if (st->GetNumFields() == 0) {
1511 uint32_t field_idx = 0;
1512 for (auto* field = st->GetField(field_idx); field != nullptr; field_idx++)
1516 s->Printf("%s %s", field->m_name.AsCString(), field->m_type.GetTypeName().AsCString());
1525 s->PutCString(name.AsCString());
1529 GoASTContext::CreateArrayType(const ConstString &name, const CompilerType &element_type, uint64_t length)
1531 GoType *type = new GoArray(name, length, element_type);
1532 (*m_types)[name].reset(type);
1533 return CompilerType(this, type);
1537 GoASTContext::CreateBaseType(int go_kind, const lldb_private::ConstString &name, uint64_t byte_size)
1539 if (go_kind == GoType::KIND_UINT || go_kind == GoType::KIND_INT)
1540 m_int_byte_size = byte_size;
1541 GoType *type = new GoType(go_kind, name);
1542 (*m_types)[name].reset(type);
1543 return CompilerType(this, type);
1547 GoASTContext::CreateTypedefType(int kind, const ConstString &name, CompilerType impl)
1549 GoType *type = new GoElem(kind, name, impl);
1550 (*m_types)[name].reset(type);
1551 return CompilerType(this, type);
1555 GoASTContext::CreateVoidType(const lldb_private::ConstString &name)
1557 GoType *type = new GoType(GoType::KIND_LLDB_VOID, name);
1558 (*m_types)[name].reset(type);
1559 return CompilerType(this, type);
1563 GoASTContext::CreateStructType(int kind, const lldb_private::ConstString &name, uint32_t byte_size)
1565 GoType *type = new GoStruct(kind, name, byte_size);
1566 (*m_types)[name].reset(type);
1567 return CompilerType(this, type);
1571 GoASTContext::AddFieldToStruct(const lldb_private::CompilerType &struct_type, const lldb_private::ConstString &name,
1572 const lldb_private::CompilerType &field_type, uint32_t byte_offset)
1576 GoASTContext *ast = llvm::dyn_cast_or_null<GoASTContext>(struct_type.GetTypeSystem());
1579 GoType *type = static_cast<GoType *>(struct_type.GetOpaqueQualType());
1580 if (GoStruct *s = type->GetStruct())
1581 s->AddField(name, field_type, byte_offset);
1585 GoASTContext::CompleteStructType(const lldb_private::CompilerType &struct_type)
1589 GoASTContext *ast = llvm::dyn_cast_or_null<GoASTContext>(struct_type.GetTypeSystem());
1592 GoType *type = static_cast<GoType *>(struct_type.GetOpaqueQualType());
1593 if (GoStruct *s = type->GetStruct())
1598 GoASTContext::CreateFunctionType(const lldb_private::ConstString &name, CompilerType *params, size_t params_count,
1601 GoType *type = new GoFunction(name, is_variadic);
1602 (*m_types)[name].reset(type);
1603 return CompilerType(this, type);
1607 GoASTContext::IsGoString(const lldb_private::CompilerType &type)
1609 if (!type.IsValid() || !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1611 return GoType::KIND_STRING == static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1615 GoASTContext::IsGoSlice(const lldb_private::CompilerType &type)
1617 if (!type.IsValid() || !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1619 return GoType::KIND_SLICE == static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1623 GoASTContext::IsGoInterface(const lldb_private::CompilerType &type)
1625 if (!type.IsValid() || !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1627 return GoType::KIND_INTERFACE == static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1631 GoASTContext::IsPointerKind(uint8_t kind)
1633 return (kind & GoType::KIND_MASK) == GoType::KIND_PTR;
1637 GoASTContext::IsDirectIface(uint8_t kind)
1639 return (kind & GoType::KIND_DIRECT_IFACE) == GoType::KIND_DIRECT_IFACE;
1643 GoASTContext::GetDWARFParser()
1645 if (!m_dwarf_ast_parser_ap)
1646 m_dwarf_ast_parser_ap.reset(new DWARFASTParserGo(*this));
1647 return m_dwarf_ast_parser_ap.get();
1651 GoASTContextForExpr::GetUserExpression(const char *expr, const char *expr_prefix, lldb::LanguageType language,
1652 Expression::ResultType desired_type, const EvaluateExpressionOptions &options)
1654 TargetSP target = m_target_wp.lock();
1656 return new GoUserExpression(*target, expr, expr_prefix, language, desired_type, options);