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::IsBlockPointerType (lldb::opaque_compiler_type_t type, CompilerType *function_pointer_type_ptr)
547 GoASTContext::IsIntegerType(lldb::opaque_compiler_type_t type, bool &is_signed)
550 // TODO: Is bool an integer?
553 int kind = static_cast<GoType *>(type)->GetGoKind();
554 if (kind <= GoType::KIND_UINTPTR)
556 is_signed = (kind != GoType::KIND_BOOL) & (kind <= GoType::KIND_INT64);
564 GoASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type)
570 GoASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
571 CompilerType *target_type, // Can pass NULL
572 bool check_cplusplus, bool check_objc)
575 target_type->Clear();
577 return static_cast<GoType *>(type)->GetGoKind() == GoType::KIND_INTERFACE;
582 GoASTContext::IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type)
588 GoASTContext::IsPointerType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type)
592 GoType *t = static_cast<GoType *>(type);
595 *pointee_type = t->GetElementType();
597 switch (t->GetGoKind())
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?
611 GoASTContext::IsPointerOrReferenceType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type)
613 return IsPointerType(type, pointee_type);
617 GoASTContext::IsReferenceType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type, bool *is_rvalue)
623 GoASTContext::IsScalarType(lldb::opaque_compiler_type_t type)
625 return !IsAggregateType(type);
629 GoASTContext::IsTypedefType(lldb::opaque_compiler_type_t type)
632 return static_cast<GoType *>(type)->IsTypedef();
637 GoASTContext::IsVoidType(lldb::opaque_compiler_type_t type)
641 return static_cast<GoType *>(type)->GetGoKind() == GoType::KIND_LLDB_VOID;
645 GoASTContext::SupportsLanguage (lldb::LanguageType language)
647 return language == eLanguageTypeGo;
650 //----------------------------------------------------------------------
652 //----------------------------------------------------------------------
655 GoASTContext::GetCompleteType(lldb::opaque_compiler_type_t type)
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())
666 CompilerType compiler_type(this, s);
667 SymbolFile *symbols = GetSymbolFile();
668 return symbols && symbols->CompleteType(compiler_type);
673 //----------------------------------------------------------------------
674 // AST related queries
675 //----------------------------------------------------------------------
678 GoASTContext::GetPointerByteSize()
680 return m_pointer_byte_size;
683 //----------------------------------------------------------------------
685 //----------------------------------------------------------------------
688 GoASTContext::GetTypeName(lldb::opaque_compiler_type_t type)
691 return static_cast<GoType *>(type)->GetName();
692 return ConstString();
696 GoASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type, CompilerType *pointee_or_element_compiler_type)
698 if (pointee_or_element_compiler_type)
699 pointee_or_element_compiler_type->Clear();
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)
710 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
711 if (kind < GoType::KIND_FLOAT32)
713 builtin_type_flags |= eTypeIsInteger | eTypeIsScalar;
714 if (kind >= GoType::KIND_INT && kind <= GoType::KIND_INT64)
715 builtin_type_flags |= eTypeIsSigned;
719 builtin_type_flags |= eTypeIsFloat;
720 if (kind < GoType::KIND_COMPLEX64)
721 builtin_type_flags |= eTypeIsComplex;
723 builtin_type_flags |= eTypeIsScalar;
725 return builtin_type_flags;
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)
735 return eTypeHasChildren | eTypeIsStructUnion;
739 GoASTContext::GetTypeClass(lldb::opaque_compiler_type_t 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;
758 GoASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type)
760 ConstString name = GetTypeName(type);
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, [](){
768 g_type_map.Append(ConstString("void").GetCString(), eBasicTypeVoid);
770 g_type_map.Append(ConstString("int").GetCString(), eBasicTypeInt);
771 g_type_map.Append(ConstString("uint").GetCString(), eBasicTypeUnsignedInt);
774 g_type_map.Append(ConstString("bool").GetCString(), eBasicTypeBool);
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);
793 return g_type_map.Find(name.GetCString(), eBasicTypeInvalid);
795 return eBasicTypeInvalid;
799 GoASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type)
801 return lldb::eLanguageTypeGo;
805 GoASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type)
810 //----------------------------------------------------------------------
811 // Creating related types
812 //----------------------------------------------------------------------
815 GoASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type, uint64_t *stride)
817 GoArray *array = static_cast<GoType *>(type)->GetArray();
822 *stride = array->GetElementType().GetByteSize(nullptr);
824 return array->GetElementType();
826 return CompilerType();
830 GoASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type)
832 GoType *t = static_cast<GoType *>(type);
834 return t->GetElementType();
835 return CompilerType(this, type);
839 GoASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type)
841 return CompilerType(this, type);
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.
847 GoASTContext::GetFunctionArgumentCount(lldb::opaque_compiler_type_t type)
849 return GetNumberOfFunctionArguments(type);
853 GoASTContext::GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type, size_t idx)
855 return GetFunctionArgumentAtIndex(type, idx);
859 GoASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type)
864 GoType *t = static_cast<GoType *>(type);
865 if (t->GetGoKind() == GoType::KIND_FUNC)
866 result = t->GetElementType();
872 GoASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type)
877 TypeMemberFunctionImpl
878 GoASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, size_t idx)
880 return TypeMemberFunctionImpl();
884 GoASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type)
886 return CompilerType(this, type);
890 GoASTContext::GetPointeeType(lldb::opaque_compiler_type_t type)
893 return CompilerType();
894 return static_cast<GoType *>(type)->GetElementType();
898 GoASTContext::GetPointerType(lldb::opaque_compiler_type_t 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)
907 pointer = new GoElem(GoType::KIND_PTR, pointer_name, CompilerType(this, type));
908 (*m_types)[pointer_name].reset(pointer);
910 return CompilerType(this, pointer);
913 // If the current object represents a typedef type, get the underlying type
915 GoASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type)
917 if (IsTypedefType(type))
918 return static_cast<GoType *>(type)->GetElementType();
919 return CompilerType();
922 //----------------------------------------------------------------------
923 // Create related types using the current type's AST
924 //----------------------------------------------------------------------
926 GoASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type)
928 return CompilerType();
932 GoASTContext::GetBuiltinTypeForEncodingAndBitSize (lldb::Encoding encoding,
935 return CompilerType();
939 //----------------------------------------------------------------------
940 // Exploring the type
941 //----------------------------------------------------------------------
944 GoASTContext::GetBitSize(lldb::opaque_compiler_type_t type, ExecutionContextScope *exe_scope)
948 if (!GetCompleteType(type))
950 GoType *t = static_cast<GoType *>(type);
951 GoArray *array = nullptr;
952 switch (t->GetGoKind())
954 case GoType::KIND_BOOL:
955 case GoType::KIND_INT8:
956 case GoType::KIND_UINT8:
958 case GoType::KIND_INT16:
959 case GoType::KIND_UINT16:
961 case GoType::KIND_INT32:
962 case GoType::KIND_UINT32:
963 case GoType::KIND_FLOAT32:
965 case GoType::KIND_INT64:
966 case GoType::KIND_UINT64:
967 case GoType::KIND_FLOAT64:
968 case GoType::KIND_COMPLEX64:
970 case GoType::KIND_COMPLEX128:
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;
998 GoASTContext::GetEncoding(lldb::opaque_compiler_type_t type, uint64_t &count)
1002 if (IsIntegerType(type, is_signed))
1003 return is_signed ? lldb::eEncodingSint : eEncodingUint;
1005 uint32_t complex_count;
1006 if (IsFloatingPointType(type, complex_count, is_complex))
1008 count = complex_count;
1009 return eEncodingIEEE754;
1011 if (IsPointerType(type))
1012 return eEncodingUint;
1013 return eEncodingInvalid;
1017 GoASTContext::GetFormat(lldb::opaque_compiler_type_t type)
1020 return eFormatDefault;
1021 switch (static_cast<GoType *>(type)->GetGoKind())
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:
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:
1056 // Don't know how to display this.
1057 return eFormatBytes;
1062 GoASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type)
1068 GoASTContext::GetNumChildren(lldb::opaque_compiler_type_t type, bool omit_empty_base_classes)
1070 if (!type || !GetCompleteType(type))
1072 GoType *t = static_cast<GoType *>(type);
1073 if (t->GetGoKind() == GoType::KIND_PTR)
1075 CompilerType elem = t->GetElementType();
1076 if (elem.IsAggregateType())
1077 return elem.GetNumChildren(omit_empty_base_classes);
1080 else if (GoArray *array = t->GetArray())
1082 return array->GetLength();
1084 else if (t->IsTypedef())
1086 return t->GetElementType().GetNumChildren(omit_empty_base_classes);
1089 return GetNumFields(type);
1093 GoASTContext::GetNumFields(lldb::opaque_compiler_type_t type)
1095 if (!type || !GetCompleteType(type))
1097 GoType *t = static_cast<GoType *>(type);
1099 return t->GetElementType().GetNumFields();
1100 GoStruct *s = t->GetStruct();
1102 return s->GetNumFields();
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)
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;
1117 if (!type || !GetCompleteType(type))
1118 return CompilerType();
1120 GoType *t = static_cast<GoType *>(type);
1122 return t->GetElementType().GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, is_bitfield_ptr);
1124 GoStruct *s = t->GetStruct();
1127 const auto *field = s->GetField(idx);
1130 name = field->m_name.GetStringRef();
1132 *bit_offset_ptr = field->m_byte_offset * 8;
1133 return field->m_type;
1136 return 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)
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;
1155 if (!type || !GetCompleteType(type))
1156 return CompilerType();
1158 GoType *t = static_cast<GoType *>(type);
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;
1167 else if (t->GetGoKind() == GoType::KIND_PTR)
1169 CompilerType pointee = t->GetElementType();
1170 if (!pointee.IsValid() || pointee.IsVoidType())
1171 return CompilerType();
1172 if (transparent_pointers && pointee.IsAggregateType())
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);
1182 child_is_deref_of_parent = true;
1183 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
1186 child_name.assign(1, '*');
1187 child_name += parent_name;
1190 // We have a pointer to an simple type
1191 if (idx == 0 && pointee.GetCompleteType())
1193 child_byte_size = pointee.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
1194 child_byte_offset = 0;
1199 else if (GoArray *a = t->GetArray())
1201 if (ignore_array_bounds || idx < a->GetLength())
1203 CompilerType element_type = a->GetElementType();
1204 if (element_type.GetCompleteType())
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;
1215 else if (t->IsTypedef())
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);
1222 return CompilerType();
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.
1228 GoASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type, const char *name, bool omit_empty_base_classes)
1230 if (!type || !GetCompleteType(type))
1233 GoType *t = static_cast<GoType *>(type);
1234 GoStruct *s = t->GetStruct();
1237 for (uint32_t i = 0; i < s->GetNumFields(); ++i)
1239 const GoStruct::Field *f = s->GetField(i);
1240 if (f->m_name.GetStringRef() == name)
1244 else if (t->GetGoKind() == GoType::KIND_PTR || t->IsTypedef())
1246 return t->GetElementType().GetIndexOfChildWithName(name, omit_empty_base_classes);
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.
1257 GoASTContext::GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type, const char *name, bool omit_empty_base_classes,
1258 std::vector<uint32_t> &child_indexes)
1260 uint32_t index = GetIndexOfChildWithName(type, name, omit_empty_base_classes);
1261 if (index == UINT_MAX)
1263 child_indexes.push_back(index);
1267 // Converts "s" to a floating point value and place resulting floating
1268 // point bytes in the "dst" buffer.
1270 GoASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type, const char *s, uint8_t *dst, size_t dst_size)
1275 //----------------------------------------------------------------------
1277 //----------------------------------------------------------------------
1278 #define DEPTH_INCREMENT 2
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)
1286 if (IsTypedefType(type))
1287 type = GetTypedefedType(type).GetOpaqueQualType();
1290 GoType *t = static_cast<GoType *>(type);
1292 if (GoStruct *st = t->GetStruct())
1294 if (GetCompleteType(type))
1296 uint32_t field_idx = 0;
1297 for (auto* field = st->GetField(field_idx); field != nullptr; field_idx++)
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.
1308 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
1310 // Print the member type if requested
1313 ConstString field_type_name = field->m_type.GetTypeName();
1314 s->Printf("(%s) ", field_type_name.AsCString());
1316 // Print the member name and equal sign
1317 s->Printf("%s = ", field->m_name.AsCString());
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
1336 // Indent the trailing squiggly bracket
1338 s->Printf("\n%*s}", depth, "");
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());
1348 uint64_t element_idx;
1349 for (element_idx = 0; element_idx < a->GetLength(); ++element_idx)
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)
1359 // Indent and print the index
1360 s->Printf("\n%*s[%" PRIu64 "] ", depth + DEPTH_INCREMENT, "", element_idx);
1362 // Figure out the field offset within the current struct/union/class type
1363 uint64_t element_offset = element_idx * element_byte_size;
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
1380 // Indent the trailing squiggly bracket
1381 if (element_idx > 0)
1382 s->Printf("\n%*s}", depth, "");
1386 DumpSummary (type, exe_ctx, s, data, data_byte_offset, data_byte_size);
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)
1396 if (IsAggregateType(type))
1402 GoType *t = static_cast<GoType *>(type);
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);
1410 return typedef_compiler_type.DumpTypeValue(
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
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...
1427 case eFormatBoolean:
1429 case eFormatComplex:
1430 case eFormatCString: // NULL terminated C strings
1431 case eFormatDecimal:
1434 case eFormatHexUppercase:
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:
1455 case eFormatCharPrintable:
1456 case eFormatCharArray:
1458 case eFormatBytesWithASCII:
1459 item_count = byte_size;
1463 case eFormatUnicode16:
1464 item_count = byte_size / 2;
1468 case eFormatUnicode32:
1469 item_count = byte_size / 4;
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);
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)
1483 if (type && GoType::KIND_STRING == static_cast<GoType *>(type)->GetGoKind())
1485 // TODO(ribrdb): read length and data
1490 GoASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type)
1493 StreamFile s (stdout, false);
1494 DumpTypeDescription (type, &s);
1498 GoASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type, Stream *s)
1502 ConstString name = GetTypeName(type);
1503 GoType *t = static_cast<GoType *>(type);
1505 if (GoStruct *st = t->GetStruct())
1507 if (GetCompleteType(type))
1509 if (NULL == strchr(name.AsCString(), '{'))
1510 s->Printf("type %s ", name.AsCString());
1511 s->PutCString("struct {");
1512 if (st->GetNumFields() == 0) {
1517 uint32_t field_idx = 0;
1518 for (auto* field = st->GetField(field_idx); field != nullptr; field_idx++)
1522 s->Printf("%s %s", field->m_name.AsCString(), field->m_type.GetTypeName().AsCString());
1531 s->PutCString(name.AsCString());
1535 GoASTContext::CreateArrayType(const ConstString &name, const CompilerType &element_type, uint64_t length)
1537 GoType *type = new GoArray(name, length, element_type);
1538 (*m_types)[name].reset(type);
1539 return CompilerType(this, type);
1543 GoASTContext::CreateBaseType(int go_kind, const lldb_private::ConstString &name, uint64_t byte_size)
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);
1553 GoASTContext::CreateTypedefType(int kind, const ConstString &name, CompilerType impl)
1555 GoType *type = new GoElem(kind, name, impl);
1556 (*m_types)[name].reset(type);
1557 return CompilerType(this, type);
1561 GoASTContext::CreateVoidType(const lldb_private::ConstString &name)
1563 GoType *type = new GoType(GoType::KIND_LLDB_VOID, name);
1564 (*m_types)[name].reset(type);
1565 return CompilerType(this, type);
1569 GoASTContext::CreateStructType(int kind, const lldb_private::ConstString &name, uint32_t byte_size)
1571 GoType *type = new GoStruct(kind, name, byte_size);
1572 (*m_types)[name].reset(type);
1573 return CompilerType(this, type);
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)
1582 GoASTContext *ast = llvm::dyn_cast_or_null<GoASTContext>(struct_type.GetTypeSystem());
1585 GoType *type = static_cast<GoType *>(struct_type.GetOpaqueQualType());
1586 if (GoStruct *s = type->GetStruct())
1587 s->AddField(name, field_type, byte_offset);
1591 GoASTContext::CompleteStructType(const lldb_private::CompilerType &struct_type)
1595 GoASTContext *ast = llvm::dyn_cast_or_null<GoASTContext>(struct_type.GetTypeSystem());
1598 GoType *type = static_cast<GoType *>(struct_type.GetOpaqueQualType());
1599 if (GoStruct *s = type->GetStruct())
1604 GoASTContext::CreateFunctionType(const lldb_private::ConstString &name, CompilerType *params, size_t params_count,
1607 GoType *type = new GoFunction(name, is_variadic);
1608 (*m_types)[name].reset(type);
1609 return CompilerType(this, type);
1613 GoASTContext::IsGoString(const lldb_private::CompilerType &type)
1615 if (!type.IsValid() || !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1617 return GoType::KIND_STRING == static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1621 GoASTContext::IsGoSlice(const lldb_private::CompilerType &type)
1623 if (!type.IsValid() || !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1625 return GoType::KIND_SLICE == static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1629 GoASTContext::IsGoInterface(const lldb_private::CompilerType &type)
1631 if (!type.IsValid() || !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1633 return GoType::KIND_INTERFACE == static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1637 GoASTContext::IsPointerKind(uint8_t kind)
1639 return (kind & GoType::KIND_MASK) == GoType::KIND_PTR;
1643 GoASTContext::IsDirectIface(uint8_t kind)
1645 return (kind & GoType::KIND_DIRECT_IFACE) == GoType::KIND_DIRECT_IFACE;
1649 GoASTContext::GetDWARFParser()
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();
1657 GoASTContextForExpr::GetUserExpression(const char *expr, const char *expr_prefix, lldb::LanguageType language,
1658 Expression::ResultType desired_type, const EvaluateExpressionOptions &options)
1660 TargetSP target = m_target_wp.lock();
1662 return new GoUserExpression(*target, expr, expr_prefix, language, desired_type, options);