blob: e025f21a649b263e613587257aed7d28f9ce93b2 [file] [log] [blame]
Ryan Brown57bee1e2015-09-14 22:45:11 +00001//===-- GoASTContext.cpp ----------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include <mutex>
11#include <utility>
12#include <vector>
13
Greg Clayton56939cb2015-09-17 22:23:34 +000014#include "lldb/Core/Module.h"
15#include "lldb/Core/PluginManager.h"
Ryan Brown57bee1e2015-09-14 22:45:11 +000016#include "lldb/Core/UniqueCStringMap.h"
17#include "lldb/Core/ValueObject.h"
18#include "lldb/DataFormatters/StringPrinter.h"
19#include "lldb/Symbol/CompilerType.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000020#include "lldb/Symbol/ObjectFile.h"
Ryan Brown57bee1e2015-09-14 22:45:11 +000021#include "lldb/Symbol/SymbolFile.h"
22#include "lldb/Symbol/GoASTContext.h"
23#include "lldb/Symbol/Type.h"
24#include "lldb/Target/ExecutionContext.h"
Greg Clayton5beec212015-10-08 21:04:34 +000025#include "lldb/Target/Target.h"
Ryan Brown57bee1e2015-09-14 22:45:11 +000026
Ryan Brown998c8a1c12015-11-02 19:30:40 +000027#include "Plugins/ExpressionParser/Go/GoUserExpression.h"
Ryan Brown57bee1e2015-09-14 22:45:11 +000028#include "Plugins/SymbolFile/DWARF/DWARFASTParserGo.h"
29
30using namespace lldb;
31
32namespace lldb_private
33{
34class GoArray;
35class GoFunction;
36class GoStruct;
37
38class GoType
39{
40 public:
41 enum
42 {
43 KIND_BOOL = 1,
44 KIND_INT = 2,
45 KIND_INT8 = 3,
46 KIND_INT16 = 4,
47 KIND_INT32 = 5,
48 KIND_INT64 = 6,
49 KIND_UINT = 7,
50 KIND_UINT8 = 8,
51 KIND_UINT16 = 9,
52 KIND_UINT32 = 10,
53 KIND_UINT64 = 11,
54 KIND_UINTPTR = 12,
55 KIND_FLOAT32 = 13,
56 KIND_FLOAT64 = 14,
57 KIND_COMPLEX64 = 15,
58 KIND_COMPLEX128 = 16,
59 KIND_ARRAY = 17,
60 KIND_CHAN = 18,
61 KIND_FUNC = 19,
62 KIND_INTERFACE = 20,
63 KIND_MAP = 21,
64 KIND_PTR = 22,
65 KIND_SLICE = 23,
66 KIND_STRING = 24,
67 KIND_STRUCT = 25,
68 KIND_UNSAFEPOINTER = 26,
69 KIND_LLDB_VOID, // Extension for LLDB, not used by go runtime.
70 KIND_MASK = (1 << 5) - 1,
71 KIND_DIRECT_IFACE = 1 << 5
72 };
73 GoType(int kind, const ConstString &name)
74 : m_kind(kind & KIND_MASK)
75 , m_name(name)
76 {
77 if (m_kind == KIND_FUNC)
78 m_kind = KIND_FUNC;
79 }
80 virtual ~GoType() {}
81
82 int
83 GetGoKind() const
84 {
85 return m_kind;
86 }
87 const ConstString &
88 GetName() const
89 {
90 return m_name;
91 }
92 virtual CompilerType
93 GetElementType() const
94 {
95 return CompilerType();
96 }
97
98 bool
99 IsTypedef() const
100 {
101 switch (m_kind)
102 {
103 case KIND_CHAN:
104 case KIND_MAP:
105 case KIND_INTERFACE:
106 return true;
107 default:
108 return false;
109 }
110 }
111
112 GoArray *GetArray();
113 GoFunction *GetFunction();
114 GoStruct *GetStruct();
115
116 private:
117 int m_kind;
118 ConstString m_name;
119 GoType(const GoType &) = delete;
120 const GoType &operator=(const GoType &) = delete;
121};
122
123class GoElem : public GoType
124{
125 public:
126 GoElem(int kind, const ConstString &name, const CompilerType &elem)
127 : GoType(kind, name)
128 , m_elem(elem)
129 {
130 }
131 virtual CompilerType
132 GetElementType() const
133 {
134 return m_elem;
135 }
136
137 private:
138 // TODO: should we store this differently?
139 CompilerType m_elem;
140
141 GoElem(const GoElem &) = delete;
142 const GoElem &operator=(const GoElem &) = delete;
143};
144
145class GoArray : public GoElem
146{
147 public:
Bruce Mitchener500737e2015-09-15 04:33:48 +0000148 GoArray(const ConstString &name, uint64_t length, const CompilerType &elem)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000149 : GoElem(KIND_ARRAY, name, elem)
150 , m_length(length)
151 {
152 }
153
Bruce Mitchener500737e2015-09-15 04:33:48 +0000154 uint64_t
Ryan Brown57bee1e2015-09-14 22:45:11 +0000155 GetLength() const
156 {
157 return m_length;
158 }
159
160 private:
Bruce Mitchener500737e2015-09-15 04:33:48 +0000161 uint64_t m_length;
Ryan Brown57bee1e2015-09-14 22:45:11 +0000162 GoArray(const GoArray &) = delete;
163 const GoArray &operator=(const GoArray &) = delete;
164};
165
166class GoFunction : public GoType
167{
168 public:
169 GoFunction(const ConstString &name, bool is_variadic)
170 : GoType(KIND_FUNC, name)
171 , m_is_variadic(is_variadic)
172 {
173 }
174
175 bool
176 IsVariadic() const
177 {
178 return m_is_variadic;
179 }
180
181 private:
182 bool m_is_variadic;
183 GoFunction(const GoFunction &) = delete;
184 const GoFunction &operator=(const GoFunction &) = delete;
185};
186
187class GoStruct : public GoType
188{
189 public:
190 struct Field
191 {
192 Field(const ConstString &name, const CompilerType &type, uint64_t offset)
193 : m_name(name)
194 , m_type(type)
195 , m_byte_offset(offset)
196 {
197 }
198 ConstString m_name;
199 CompilerType m_type;
200 uint64_t m_byte_offset;
201 };
202
203 GoStruct(int kind, const ConstString &name, int64_t byte_size)
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000204 : GoType(kind == 0 ? KIND_STRUCT : kind, name), m_is_complete(false), m_byte_size(byte_size)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000205 {
206 }
207
208 uint32_t
209 GetNumFields() const
210 {
211 return m_fields.size();
212 }
213
214 const Field *
215 GetField(uint32_t i) const
216 {
217 if (i < m_fields.size())
218 return &m_fields[i];
219 return nullptr;
220 }
221
222 void
223 AddField(const ConstString &name, const CompilerType &type, uint64_t offset)
224 {
225 m_fields.push_back(Field(name, type, offset));
226 }
227
228 bool
229 IsComplete() const
230 {
231 return m_is_complete;
232 }
233
234 void
235 SetComplete()
236 {
237 m_is_complete = true;
238 }
239
240 int64_t
241 GetByteSize() const
242 {
243 return m_byte_size;
244 }
245
246 private:
247 bool m_is_complete;
248 int64_t m_byte_size;
249 std::vector<Field> m_fields;
250
251 GoStruct(const GoStruct &) = delete;
252 const GoStruct &operator=(const GoStruct &) = delete;
253};
254
255GoArray *
256GoType::GetArray()
257{
258 if (m_kind == KIND_ARRAY)
259 {
260 return static_cast<GoArray *>(this);
261 }
262 return nullptr;
263}
264
265GoFunction *
266GoType::GetFunction()
267{
268 if (m_kind == KIND_FUNC)
269 {
270 return static_cast<GoFunction *>(this);
271 }
272 return nullptr;
273}
274
275GoStruct *
276GoType::GetStruct()
277{
278 switch (m_kind)
279 {
280 case KIND_STRING:
281 case KIND_STRUCT:
282 case KIND_SLICE:
283 return static_cast<GoStruct *>(this);
284 }
285 return nullptr;
286}
287} // namespace lldb_private
288using namespace lldb_private;
289
290GoASTContext::GoASTContext()
291 : TypeSystem(eKindGo)
292 , m_pointer_byte_size(0)
293 , m_int_byte_size(0)
294 , m_types(new TypeMap)
295{
296}
297GoASTContext::~GoASTContext()
298{
299}
300
Greg Clayton56939cb2015-09-17 22:23:34 +0000301//------------------------------------------------------------------
302// PluginInterface functions
303//------------------------------------------------------------------
304
305ConstString
306GoASTContext::GetPluginNameStatic()
307{
308 return ConstString("go");
309}
310
311ConstString
312GoASTContext::GetPluginName()
313{
314 return GoASTContext::GetPluginNameStatic();
315}
316
317uint32_t
318GoASTContext::GetPluginVersion()
319{
320 return 1;
321}
322
323lldb::TypeSystemSP
Greg Clayton5beec212015-10-08 21:04:34 +0000324GoASTContext::CreateInstance (lldb::LanguageType language, Module *module, Target *target)
Greg Clayton56939cb2015-09-17 22:23:34 +0000325{
326 if (language == eLanguageTypeGo)
327 {
Greg Clayton5beec212015-10-08 21:04:34 +0000328 ArchSpec arch;
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000329 std::shared_ptr<GoASTContext> go_ast_sp;
Greg Clayton5beec212015-10-08 21:04:34 +0000330 if (module)
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000331 {
Greg Clayton5beec212015-10-08 21:04:34 +0000332 arch = module->GetArchitecture();
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000333 go_ast_sp = std::shared_ptr<GoASTContext>(new GoASTContext);
334 }
Greg Clayton5beec212015-10-08 21:04:34 +0000335 else if (target)
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000336 {
Greg Clayton5beec212015-10-08 21:04:34 +0000337 arch = target->GetArchitecture();
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000338 go_ast_sp = std::shared_ptr<GoASTContextForExpr>(new GoASTContextForExpr(target->shared_from_this()));
339 }
Greg Clayton5beec212015-10-08 21:04:34 +0000340
Greg Clayton56939cb2015-09-17 22:23:34 +0000341 if (arch.IsValid())
342 {
Greg Clayton56939cb2015-09-17 22:23:34 +0000343 go_ast_sp->SetAddressByteSize(arch.GetAddressByteSize());
344 return go_ast_sp;
345 }
346 }
347 return lldb::TypeSystemSP();
348}
349
Sean Callananfe38c852015-10-08 23:07:53 +0000350void
351GoASTContext::EnumerateSupportedLanguages(std::set<lldb::LanguageType> &languages_for_types, std::set<lldb::LanguageType> &languages_for_expressions)
352{
353 static std::vector<lldb::LanguageType> s_supported_languages_for_types({
354 lldb::eLanguageTypeGo});
355
356 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions({});
357
358 languages_for_types.insert(s_supported_languages_for_types.begin(), s_supported_languages_for_types.end());
359 languages_for_expressions.insert(s_supported_languages_for_expressions.begin(), s_supported_languages_for_expressions.end());
360}
361
Greg Clayton56939cb2015-09-17 22:23:34 +0000362
363void
364GoASTContext::Initialize()
365{
366 PluginManager::RegisterPlugin (GetPluginNameStatic(),
367 "AST context plug-in",
Sean Callananfe38c852015-10-08 23:07:53 +0000368 CreateInstance,
369 EnumerateSupportedLanguages);
Greg Clayton56939cb2015-09-17 22:23:34 +0000370}
371
372void
373GoASTContext::Terminate()
374{
375 PluginManager::UnregisterPlugin (CreateInstance);
376}
377
378
Ryan Brown57bee1e2015-09-14 22:45:11 +0000379//----------------------------------------------------------------------
380// Tests
381//----------------------------------------------------------------------
382
383bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000384GoASTContext::IsArrayType(lldb::opaque_compiler_type_t type, CompilerType *element_type, uint64_t *size, bool *is_incomplete)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000385{
386 if (element_type)
387 element_type->Clear();
388 if (size)
389 *size = 0;
390 if (is_incomplete)
391 *is_incomplete = false;
392 GoArray *array = static_cast<GoType *>(type)->GetArray();
393 if (array)
394 {
Bruce Mitchener500737e2015-09-15 04:33:48 +0000395 if (size)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000396 *size = array->GetLength();
Ryan Brown57bee1e2015-09-14 22:45:11 +0000397 if (element_type)
398 *element_type = array->GetElementType();
399 return true;
400 }
401 return false;
402}
403
404bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000405GoASTContext::IsVectorType(lldb::opaque_compiler_type_t type, CompilerType *element_type, uint64_t *size)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000406{
407 if (element_type)
408 element_type->Clear();
409 if (size)
410 *size = 0;
411 return false;
412}
413
414bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000415GoASTContext::IsAggregateType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000416{
417 int kind = static_cast<GoType *>(type)->GetGoKind();
418 if (kind < GoType::KIND_ARRAY)
419 return false;
420 if (kind == GoType::KIND_PTR)
421 return false;
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000422 if (kind == GoType::KIND_CHAN)
423 return false;
424 if (kind == GoType::KIND_MAP)
425 return false;
Ryan Brown57bee1e2015-09-14 22:45:11 +0000426 if (kind == GoType::KIND_STRING)
427 return false;
428 if (kind == GoType::KIND_UNSAFEPOINTER)
429 return false;
430 return true;
431}
432
433bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000434GoASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000435{
436 return false;
437}
438
439bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000440GoASTContext::IsCharType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000441{
442 // Go's DWARF doesn't distinguish between rune and int32.
443 return false;
444}
445
446bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000447GoASTContext::IsCompleteType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000448{
449 if (!type)
450 return false;
451 GoType *t = static_cast<GoType *>(type);
452 if (GoStruct *s = t->GetStruct())
453 return s->IsComplete();
454 if (t->IsTypedef() || t->GetGoKind() == GoType::KIND_PTR)
455 return t->GetElementType().IsCompleteType();
456 return true;
457}
458
459bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000460GoASTContext::IsConst(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000461{
462 return false;
463}
464
465bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000466GoASTContext::IsCStringType(lldb::opaque_compiler_type_t type, uint32_t &length)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000467{
468 return false;
469}
470
471bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000472GoASTContext::IsDefined(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000473{
474 return type != nullptr;
475}
476
477bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000478GoASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type, uint32_t &count, bool &is_complex)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000479{
480 int kind = static_cast<GoType *>(type)->GetGoKind();
481 if (kind >= GoType::KIND_FLOAT32 && kind <= GoType::KIND_COMPLEX128)
482 {
483 if (kind >= GoType::KIND_COMPLEX64)
484 {
485 is_complex = true;
486 count = 2;
487 }
488 else
489 {
490 is_complex = false;
491 count = 1;
492 }
493 return true;
494 }
495 count = 0;
496 is_complex = false;
497 return false;
498}
499
500bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000501GoASTContext::IsFunctionType(lldb::opaque_compiler_type_t type, bool *is_variadic_ptr)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000502{
503 GoFunction *func = static_cast<GoType *>(type)->GetFunction();
504 if (func)
505 {
506 if (is_variadic_ptr)
507 *is_variadic_ptr = func->IsVariadic();
508 return true;
509 }
510 if (is_variadic_ptr)
511 *is_variadic_ptr = false;
512 return false;
513}
514
515uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000516GoASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type, CompilerType *base_type_ptr)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000517{
518 return false;
519}
520
521size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000522GoASTContext::GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000523{
524 return 0;
525}
526
527CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000528GoASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type, const size_t index)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000529{
530 return CompilerType();
531}
532
533bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000534GoASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000535{
536 return IsFunctionType(type);
537}
538
539bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000540GoASTContext::IsIntegerType(lldb::opaque_compiler_type_t type, bool &is_signed)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000541{
542 is_signed = false;
543 // TODO: Is bool an integer?
544 if (type)
545 {
546 int kind = static_cast<GoType *>(type)->GetGoKind();
547 if (kind <= GoType::KIND_UINTPTR)
548 {
549 is_signed = (kind != GoType::KIND_BOOL) & (kind <= GoType::KIND_INT64);
550 return true;
551 }
552 }
553 return false;
554}
555
556bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000557GoASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000558{
559 return false;
560}
561
562bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000563GoASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
Ryan Brown57bee1e2015-09-14 22:45:11 +0000564 CompilerType *target_type, // Can pass NULL
565 bool check_cplusplus, bool check_objc)
566{
567 if (target_type)
568 target_type->Clear();
569 if (type)
570 return static_cast<GoType *>(type)->GetGoKind() == GoType::KIND_INTERFACE;
571 return false;
572}
573
574bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000575GoASTContext::IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000576{
577 return false;
578}
579
580bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000581GoASTContext::IsPointerType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000582{
583 if (!type)
584 return false;
585 GoType *t = static_cast<GoType *>(type);
586 if (pointee_type)
587 {
588 *pointee_type = t->GetElementType();
589 }
590 switch (t->GetGoKind())
591 {
592 case GoType::KIND_PTR:
593 case GoType::KIND_UNSAFEPOINTER:
594 case GoType::KIND_CHAN:
Ryan Brown998c8a1c12015-11-02 19:30:40 +0000595 case GoType::KIND_MAP:
596 // TODO: is function a pointer?
Ryan Brown57bee1e2015-09-14 22:45:11 +0000597 return true;
598 default:
599 return false;
600 }
601}
602
603bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000604GoASTContext::IsPointerOrReferenceType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000605{
606 return IsPointerType(type, pointee_type);
607}
608
609bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000610GoASTContext::IsReferenceType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type, bool *is_rvalue)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000611{
612 return false;
613}
614
615bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000616GoASTContext::IsScalarType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000617{
618 return !IsAggregateType(type);
619}
620
621bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000622GoASTContext::IsTypedefType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000623{
624 if (type)
625 return static_cast<GoType *>(type)->IsTypedef();
626 return false;
627}
628
629bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000630GoASTContext::IsVoidType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000631{
632 if (!type)
633 return false;
634 return static_cast<GoType *>(type)->GetGoKind() == GoType::KIND_LLDB_VOID;
635}
636
Greg Clayton56939cb2015-09-17 22:23:34 +0000637bool
638GoASTContext::SupportsLanguage (lldb::LanguageType language)
639{
640 return language == eLanguageTypeGo;
641}
642
Ryan Brown57bee1e2015-09-14 22:45:11 +0000643//----------------------------------------------------------------------
644// Type Completion
645//----------------------------------------------------------------------
646
647bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000648GoASTContext::GetCompleteType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000649{
650 if (!type)
651 return false;
652 GoType *t = static_cast<GoType *>(type);
653 if (t->IsTypedef() || t->GetGoKind() == GoType::KIND_PTR || t->GetArray())
654 return t->GetElementType().GetCompleteType();
655 if (GoStruct *s = t->GetStruct())
656 {
657 if (s->IsComplete())
658 return true;
659 CompilerType compiler_type(this, s);
660 SymbolFile *symbols = GetSymbolFile();
661 return symbols && symbols->CompleteType(compiler_type);
662 }
663 return true;
664}
665
666//----------------------------------------------------------------------
667// AST related queries
668//----------------------------------------------------------------------
669
670uint32_t
671GoASTContext::GetPointerByteSize()
672{
673 return m_pointer_byte_size;
674}
675
676//----------------------------------------------------------------------
677// Accessors
678//----------------------------------------------------------------------
679
680ConstString
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000681GoASTContext::GetTypeName(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000682{
683 if (type)
684 return static_cast<GoType *>(type)->GetName();
685 return ConstString();
686}
687
688uint32_t
Bruce Mitchener3ad353f2015-09-24 03:54:50 +0000689GoASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type, CompilerType *pointee_or_element_compiler_type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000690{
Bruce Mitchener3ad353f2015-09-24 03:54:50 +0000691 if (pointee_or_element_compiler_type)
692 pointee_or_element_compiler_type->Clear();
Ryan Brown57bee1e2015-09-14 22:45:11 +0000693 if (!type)
694 return 0;
695 GoType *t = static_cast<GoType *>(type);
Bruce Mitchener3ad353f2015-09-24 03:54:50 +0000696 if (pointee_or_element_compiler_type)
697 *pointee_or_element_compiler_type = t->GetElementType();
Ryan Brown57bee1e2015-09-14 22:45:11 +0000698 int kind = t->GetGoKind();
699 if (kind == GoType::KIND_ARRAY)
700 return eTypeHasChildren | eTypeIsArray;
701 if (kind < GoType::KIND_ARRAY)
702 {
703 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
704 if (kind < GoType::KIND_FLOAT32)
705 {
706 builtin_type_flags |= eTypeIsInteger | eTypeIsScalar;
707 if (kind >= GoType::KIND_INT && kind <= GoType::KIND_INT64)
708 builtin_type_flags |= eTypeIsSigned;
709 }
710 else
711 {
712 builtin_type_flags |= eTypeIsFloat;
713 if (kind < GoType::KIND_COMPLEX64)
714 builtin_type_flags |= eTypeIsComplex;
715 else
716 builtin_type_flags |= eTypeIsScalar;
717 }
718 return builtin_type_flags;
719 }
720 if (kind == GoType::KIND_STRING)
721 return eTypeHasValue | eTypeIsBuiltIn;
722 if (kind == GoType::KIND_FUNC)
723 return eTypeIsFuncPrototype | eTypeHasValue;
724 if (IsPointerType(type))
725 return eTypeIsPointer | eTypeHasValue | eTypeHasChildren;
726 if (kind == GoType::KIND_LLDB_VOID)
727 return 0;
728 return eTypeHasChildren | eTypeIsStructUnion;
729}
730
731lldb::TypeClass
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000732GoASTContext::GetTypeClass(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000733{
734 if (!type)
735 return eTypeClassInvalid;
736 int kind = static_cast<GoType *>(type)->GetGoKind();
737 if (kind == GoType::KIND_FUNC)
738 return eTypeClassFunction;
739 if (IsPointerType(type))
740 return eTypeClassPointer;
741 if (kind < GoType::KIND_COMPLEX64)
742 return eTypeClassBuiltin;
743 if (kind <= GoType::KIND_COMPLEX128)
744 return eTypeClassComplexFloat;
745 if (kind == GoType::KIND_LLDB_VOID)
746 return eTypeClassInvalid;
747 return eTypeClassStruct;
748}
749
750lldb::BasicType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000751GoASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000752{
753 ConstString name = GetTypeName(type);
754 if (name)
755 {
756 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
757 static TypeNameToBasicTypeMap g_type_map;
758 static std::once_flag g_once_flag;
759 std::call_once(g_once_flag, [](){
760 // "void"
761 g_type_map.Append(ConstString("void").GetCString(), eBasicTypeVoid);
762 // "int"
763 g_type_map.Append(ConstString("int").GetCString(), eBasicTypeInt);
764 g_type_map.Append(ConstString("uint").GetCString(), eBasicTypeUnsignedInt);
765
766 // Miscellaneous
767 g_type_map.Append(ConstString("bool").GetCString(), eBasicTypeBool);
768
769 // Others. Should these map to C types?
770 g_type_map.Append(ConstString("byte").GetCString(), eBasicTypeOther);
771 g_type_map.Append(ConstString("uint8").GetCString(), eBasicTypeOther);
772 g_type_map.Append(ConstString("uint16").GetCString(), eBasicTypeOther);
773 g_type_map.Append(ConstString("uint32").GetCString(), eBasicTypeOther);
774 g_type_map.Append(ConstString("uint64").GetCString(), eBasicTypeOther);
775 g_type_map.Append(ConstString("int8").GetCString(), eBasicTypeOther);
776 g_type_map.Append(ConstString("int16").GetCString(), eBasicTypeOther);
777 g_type_map.Append(ConstString("int32").GetCString(), eBasicTypeOther);
778 g_type_map.Append(ConstString("int64").GetCString(), eBasicTypeOther);
779 g_type_map.Append(ConstString("float32").GetCString(), eBasicTypeOther);
780 g_type_map.Append(ConstString("float64").GetCString(), eBasicTypeOther);
781 g_type_map.Append(ConstString("uintptr").GetCString(), eBasicTypeOther);
782
783 g_type_map.Sort();
784 });
785
786 return g_type_map.Find(name.GetCString(), eBasicTypeInvalid);
787 }
788 return eBasicTypeInvalid;
789}
790
791lldb::LanguageType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000792GoASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000793{
794 return lldb::eLanguageTypeGo;
795}
796
797unsigned
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000798GoASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000799{
800 return 0;
801}
802
803//----------------------------------------------------------------------
804// Creating related types
805//----------------------------------------------------------------------
806
807CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000808GoASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type, uint64_t *stride)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000809{
810 GoArray *array = static_cast<GoType *>(type)->GetArray();
811 if (array)
812 {
813 if (stride)
814 {
815 *stride = array->GetElementType().GetByteSize(nullptr);
816 }
817 return array->GetElementType();
818 }
819 return CompilerType();
820}
821
822CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000823GoASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000824{
825 GoType *t = static_cast<GoType *>(type);
826 if (t->IsTypedef())
827 return t->GetElementType();
828 return CompilerType(this, type);
829}
830
831CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000832GoASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000833{
834 return CompilerType(this, type);
835}
836
837// Returns -1 if this isn't a function of if the function doesn't have a prototype
838// Returns a value >= 0 if there is a prototype.
839int
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000840GoASTContext::GetFunctionArgumentCount(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000841{
842 return GetNumberOfFunctionArguments(type);
843}
844
845CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000846GoASTContext::GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type, size_t idx)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000847{
848 return GetFunctionArgumentAtIndex(type, idx);
849}
850
851CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000852GoASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000853{
854 CompilerType result;
855 if (type)
856 {
857 GoType *t = static_cast<GoType *>(type);
858 if (t->GetGoKind() == GoType::KIND_FUNC)
859 result = t->GetElementType();
860 }
861 return result;
862}
863
864size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000865GoASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000866{
867 return 0;
868}
869
870TypeMemberFunctionImpl
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000871GoASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, size_t idx)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000872{
873 return TypeMemberFunctionImpl();
874}
875
876CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000877GoASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000878{
879 return CompilerType(this, type);
880}
881
882CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000883GoASTContext::GetPointeeType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000884{
885 if (!type)
886 return CompilerType();
887 return static_cast<GoType *>(type)->GetElementType();
888}
889
890CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000891GoASTContext::GetPointerType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000892{
893 if (!type)
894 return CompilerType();
895 ConstString type_name = GetTypeName(type);
896 ConstString pointer_name(std::string("*") + type_name.GetCString());
897 GoType *pointer = (*m_types)[pointer_name].get();
898 if (pointer == nullptr)
899 {
900 pointer = new GoElem(GoType::KIND_PTR, pointer_name, CompilerType(this, type));
901 (*m_types)[pointer_name].reset(pointer);
902 }
903 return CompilerType(this, pointer);
904}
905
906// If the current object represents a typedef type, get the underlying type
907CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000908GoASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000909{
910 if (IsTypedefType(type))
911 return static_cast<GoType *>(type)->GetElementType();
912 return CompilerType();
913}
914
915//----------------------------------------------------------------------
916// Create related types using the current type's AST
917//----------------------------------------------------------------------
918CompilerType
919GoASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type)
920{
921 return CompilerType();
922}
923
Greg Clayton56939cb2015-09-17 22:23:34 +0000924CompilerType
925GoASTContext::GetBuiltinTypeForEncodingAndBitSize (lldb::Encoding encoding,
926 size_t bit_size)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000927{
928 return CompilerType();
929}
930
931
932//----------------------------------------------------------------------
933// Exploring the type
934//----------------------------------------------------------------------
935
936uint64_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000937GoASTContext::GetBitSize(lldb::opaque_compiler_type_t type, ExecutionContextScope *exe_scope)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000938{
939 if (!type)
940 return 0;
941 if (!GetCompleteType(type))
942 return 0;
943 GoType *t = static_cast<GoType *>(type);
944 GoArray *array = nullptr;
945 switch (t->GetGoKind())
946 {
947 case GoType::KIND_BOOL:
948 case GoType::KIND_INT8:
949 case GoType::KIND_UINT8:
950 return 8;
951 case GoType::KIND_INT16:
952 case GoType::KIND_UINT16:
953 return 16;
954 case GoType::KIND_INT32:
955 case GoType::KIND_UINT32:
956 case GoType::KIND_FLOAT32:
957 return 32;
958 case GoType::KIND_INT64:
959 case GoType::KIND_UINT64:
960 case GoType::KIND_FLOAT64:
961 case GoType::KIND_COMPLEX64:
962 return 64;
963 case GoType::KIND_COMPLEX128:
964 return 128;
965 case GoType::KIND_INT:
966 case GoType::KIND_UINT:
967 return m_int_byte_size * 8;
968 case GoType::KIND_UINTPTR:
969 case GoType::KIND_FUNC: // I assume this is a pointer?
970 case GoType::KIND_CHAN:
971 case GoType::KIND_PTR:
972 case GoType::KIND_UNSAFEPOINTER:
973 case GoType::KIND_MAP:
974 return m_pointer_byte_size * 8;
975 case GoType::KIND_ARRAY:
976 array = t->GetArray();
977 return array->GetLength() * array->GetElementType().GetBitSize(exe_scope);
978 case GoType::KIND_INTERFACE:
979 return t->GetElementType().GetBitSize(exe_scope);
980 case GoType::KIND_SLICE:
981 case GoType::KIND_STRING:
982 case GoType::KIND_STRUCT:
983 return t->GetStruct()->GetByteSize() * 8;
984 default:
985 assert(false);
986 }
Ryan Brownd03c2e02015-09-15 00:50:43 +0000987 return 0;
Ryan Brown57bee1e2015-09-14 22:45:11 +0000988}
989
990lldb::Encoding
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000991GoASTContext::GetEncoding(lldb::opaque_compiler_type_t type, uint64_t &count)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000992{
993 count = 1;
994 bool is_signed;
995 if (IsIntegerType(type, is_signed))
996 return is_signed ? lldb::eEncodingSint : eEncodingUint;
997 bool is_complex;
998 uint32_t complex_count;
999 if (IsFloatingPointType(type, complex_count, is_complex))
1000 {
1001 count = complex_count;
1002 return eEncodingIEEE754;
1003 }
1004 if (IsPointerType(type))
1005 return eEncodingUint;
1006 return eEncodingInvalid;
1007}
1008
1009lldb::Format
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001010GoASTContext::GetFormat(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001011{
1012 if (!type)
1013 return eFormatDefault;
1014 switch (static_cast<GoType *>(type)->GetGoKind())
1015 {
1016 case GoType::KIND_BOOL:
1017 return eFormatBoolean;
1018 case GoType::KIND_INT:
1019 case GoType::KIND_INT8:
1020 case GoType::KIND_INT16:
1021 case GoType::KIND_INT32:
1022 case GoType::KIND_INT64:
1023 return eFormatDecimal;
1024 case GoType::KIND_UINT:
1025 case GoType::KIND_UINT8:
1026 case GoType::KIND_UINT16:
1027 case GoType::KIND_UINT32:
1028 case GoType::KIND_UINT64:
1029 return eFormatUnsigned;
1030 case GoType::KIND_FLOAT32:
1031 case GoType::KIND_FLOAT64:
1032 return eFormatFloat;
1033 case GoType::KIND_COMPLEX64:
1034 case GoType::KIND_COMPLEX128:
1035 return eFormatComplexFloat;
1036 case GoType::KIND_UINTPTR:
1037 case GoType::KIND_CHAN:
1038 case GoType::KIND_PTR:
1039 case GoType::KIND_MAP:
1040 case GoType::KIND_UNSAFEPOINTER:
1041 return eFormatHex;
1042 case GoType::KIND_STRING:
1043 return eFormatCString;
1044 case GoType::KIND_ARRAY:
1045 case GoType::KIND_INTERFACE:
1046 case GoType::KIND_SLICE:
1047 case GoType::KIND_STRUCT:
1048 default:
1049 // Don't know how to display this.
1050 return eFormatBytes;
1051 }
1052}
1053
1054size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001055GoASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001056{
1057 return 0;
1058}
1059
1060uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001061GoASTContext::GetNumChildren(lldb::opaque_compiler_type_t type, bool omit_empty_base_classes)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001062{
1063 if (!type || !GetCompleteType(type))
1064 return 0;
1065 GoType *t = static_cast<GoType *>(type);
1066 if (t->GetGoKind() == GoType::KIND_PTR)
1067 {
1068 CompilerType elem = t->GetElementType();
1069 if (elem.IsAggregateType())
1070 return elem.GetNumChildren(omit_empty_base_classes);
1071 return 1;
1072 }
1073 else if (GoArray *array = t->GetArray())
1074 {
1075 return array->GetLength();
1076 }
Ryan Brown998c8a1c12015-11-02 19:30:40 +00001077 else if (t->IsTypedef())
1078 {
1079 return t->GetElementType().GetNumChildren(omit_empty_base_classes);
1080 }
1081
Ryan Brown57bee1e2015-09-14 22:45:11 +00001082 return GetNumFields(type);
1083}
1084
1085uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001086GoASTContext::GetNumFields(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001087{
1088 if (!type || !GetCompleteType(type))
1089 return 0;
1090 GoType *t = static_cast<GoType *>(type);
1091 if (t->IsTypedef())
1092 return t->GetElementType().GetNumFields();
1093 GoStruct *s = t->GetStruct();
1094 if (s)
1095 return s->GetNumFields();
1096 return 0;
1097}
1098
1099CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001100GoASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type, size_t idx, std::string &name, uint64_t *bit_offset_ptr,
Ryan Brown57bee1e2015-09-14 22:45:11 +00001101 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr)
1102{
1103 if (bit_offset_ptr)
1104 *bit_offset_ptr = 0;
1105 if (bitfield_bit_size_ptr)
1106 *bitfield_bit_size_ptr = 0;
1107 if (is_bitfield_ptr)
1108 *is_bitfield_ptr = false;
1109
1110 if (!type || !GetCompleteType(type))
1111 return CompilerType();
1112
1113 GoType *t = static_cast<GoType *>(type);
1114 if (t->IsTypedef())
1115 return t->GetElementType().GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, is_bitfield_ptr);
1116
1117 GoStruct *s = t->GetStruct();
1118 if (s)
1119 {
1120 const auto *field = s->GetField(idx);
1121 if (field)
1122 {
1123 name = field->m_name.GetStringRef();
1124 if (bit_offset_ptr)
1125 *bit_offset_ptr = field->m_byte_offset * 8;
1126 return field->m_type;
1127 }
1128 }
1129 return CompilerType();
1130}
1131
1132CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001133GoASTContext::GetChildCompilerTypeAtIndex(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
Bruce Mitchener4ad83342015-09-21 16:48:48 +00001134 bool omit_empty_base_classes, bool ignore_array_bounds, std::string &child_name,
1135 uint32_t &child_byte_size, int32_t &child_byte_offset,
1136 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
1137 bool &child_is_base_class, bool &child_is_deref_of_parent, ValueObject *valobj)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001138{
1139 child_name.clear();
1140 child_byte_size = 0;
1141 child_byte_offset = 0;
1142 child_bitfield_bit_size = 0;
1143 child_bitfield_bit_offset = 0;
1144 child_is_base_class = false;
1145 child_is_deref_of_parent = false;
1146
1147 if (!type || !GetCompleteType(type))
1148 return CompilerType();
1149
1150 GoType *t = static_cast<GoType *>(type);
1151 if (t->GetStruct())
1152 {
1153 uint64_t bit_offset;
1154 CompilerType ret = GetFieldAtIndex(type, idx, child_name, &bit_offset, nullptr, nullptr);
1155 child_byte_size = ret.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr);
1156 child_byte_offset = bit_offset / 8;
1157 return ret;
1158 }
1159 else if (t->GetGoKind() == GoType::KIND_PTR)
1160 {
1161 CompilerType pointee = t->GetElementType();
1162 if (!pointee.IsValid() || pointee.IsVoidType())
1163 return CompilerType();
1164 if (transparent_pointers && pointee.IsAggregateType())
1165 {
1166 bool tmp_child_is_deref_of_parent = false;
Bruce Mitchener4ad83342015-09-21 16:48:48 +00001167 return pointee.GetChildCompilerTypeAtIndex(exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
Ryan Brown57bee1e2015-09-14 22:45:11 +00001168 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
1169 child_bitfield_bit_size, child_bitfield_bit_offset,
1170 child_is_base_class, tmp_child_is_deref_of_parent, valobj);
1171 }
1172 else
1173 {
1174 child_is_deref_of_parent = true;
1175 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
1176 if (parent_name)
1177 {
1178 child_name.assign(1, '*');
1179 child_name += parent_name;
1180 }
1181
1182 // We have a pointer to an simple type
1183 if (idx == 0 && pointee.GetCompleteType())
1184 {
1185 child_byte_size = pointee.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
1186 child_byte_offset = 0;
1187 return pointee;
1188 }
1189 }
1190 }
1191 else if (GoArray *a = t->GetArray())
1192 {
1193 if (ignore_array_bounds || idx < a->GetLength())
1194 {
1195 CompilerType element_type = a->GetElementType();
1196 if (element_type.GetCompleteType())
1197 {
1198 char element_name[64];
1199 ::snprintf(element_name, sizeof(element_name), "[%zu]", idx);
1200 child_name.assign(element_name);
1201 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
1202 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
1203 return element_type;
1204 }
1205 }
1206 }
1207 else if (t->IsTypedef())
1208 {
Bruce Mitchener4ad83342015-09-21 16:48:48 +00001209 return t->GetElementType().GetChildCompilerTypeAtIndex(
Ryan Brown57bee1e2015-09-14 22:45:11 +00001210 exe_ctx, idx, transparent_pointers, omit_empty_base_classes, ignore_array_bounds, child_name,
1211 child_byte_size, child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
1212 child_is_deref_of_parent, valobj);
1213 }
1214 return CompilerType();
1215}
1216
1217// Lookup a child given a name. This function will match base class names
1218// and member member names in "clang_type" only, not descendants.
1219uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001220GoASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type, const char *name, bool omit_empty_base_classes)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001221{
Ryan Brown07a1c452015-10-06 20:29:31 +00001222 if (!type || !GetCompleteType(type))
1223 return UINT_MAX;
1224
Ryan Brown57bee1e2015-09-14 22:45:11 +00001225 GoType *t = static_cast<GoType *>(type);
1226 GoStruct *s = t->GetStruct();
1227 if (s)
1228 {
1229 for (uint32_t i = 0; i < s->GetNumFields(); ++i)
1230 {
1231 const GoStruct::Field *f = s->GetField(i);
1232 if (f->m_name.GetStringRef() == name)
1233 return i;
1234 }
1235 }
1236 else if (t->GetGoKind() == GoType::KIND_PTR || t->IsTypedef())
1237 {
1238 return t->GetElementType().GetIndexOfChildWithName(name, omit_empty_base_classes);
1239 }
1240 return UINT_MAX;
1241}
1242
1243// Lookup a child member given a name. This function will match member names
1244// only and will descend into "clang_type" children in search for the first
1245// member in this class, or any base class that matches "name".
1246// TODO: Return all matches for a given name by returning a vector<vector<uint32_t>>
1247// so we catch all names that match a given child name, not just the first.
1248size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001249GoASTContext::GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type, const char *name, bool omit_empty_base_classes,
Ryan Brown57bee1e2015-09-14 22:45:11 +00001250 std::vector<uint32_t> &child_indexes)
1251{
1252 uint32_t index = GetIndexOfChildWithName(type, name, omit_empty_base_classes);
1253 if (index == UINT_MAX)
1254 return 0;
1255 child_indexes.push_back(index);
1256 return 1;
1257}
1258
1259// Converts "s" to a floating point value and place resulting floating
1260// point bytes in the "dst" buffer.
1261size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001262GoASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type, const char *s, uint8_t *dst, size_t dst_size)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001263{
1264 assert(false);
Ryan Brownd03c2e02015-09-15 00:50:43 +00001265 return 0;
Ryan Brown57bee1e2015-09-14 22:45:11 +00001266}
1267//----------------------------------------------------------------------
1268// Dumping types
1269//----------------------------------------------------------------------
1270void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001271GoASTContext::DumpValue(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s, lldb::Format format,
Ryan Brown57bee1e2015-09-14 22:45:11 +00001272 const DataExtractor &data, lldb::offset_t data_offset, size_t data_byte_size,
1273 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types, bool show_summary,
1274 bool verbose, uint32_t depth)
1275{
1276 assert(false);
1277}
1278
1279bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001280GoASTContext::DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format, const DataExtractor &data,
Ryan Brown57bee1e2015-09-14 22:45:11 +00001281 lldb::offset_t byte_offset, size_t byte_size, uint32_t bitfield_bit_size,
1282 uint32_t bitfield_bit_offset, ExecutionContextScope *exe_scope)
1283{
1284 if (!type)
1285 return false;
1286 if (IsAggregateType(type))
1287 {
1288 return false;
1289 }
1290 else
1291 {
1292 GoType *t = static_cast<GoType *>(type);
1293 if (t->IsTypedef())
1294 {
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001295 CompilerType typedef_compiler_type = t->GetElementType();
Ryan Brown57bee1e2015-09-14 22:45:11 +00001296 if (format == eFormatDefault)
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001297 format = typedef_compiler_type.GetFormat();
1298 uint64_t typedef_byte_size = typedef_compiler_type.GetByteSize(exe_scope);
Ryan Brown57bee1e2015-09-14 22:45:11 +00001299
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001300 return typedef_compiler_type.DumpTypeValue(
Ryan Brown57bee1e2015-09-14 22:45:11 +00001301 s,
1302 format, // The format with which to display the element
1303 data, // Data buffer containing all bytes for this type
1304 byte_offset, // Offset into "data" where to grab value from
1305 typedef_byte_size, // Size of this type in bytes
1306 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't treat as a bitfield
1307 bitfield_bit_offset, // Offset in bits of a bitfield value if bitfield_bit_size != 0
1308 exe_scope);
1309 }
1310
1311 uint32_t item_count = 1;
1312 // A few formats, we might need to modify our size and count for depending
1313 // on how we are trying to display the value...
1314 switch (format)
1315 {
1316 default:
1317 case eFormatBoolean:
1318 case eFormatBinary:
1319 case eFormatComplex:
1320 case eFormatCString: // NULL terminated C strings
1321 case eFormatDecimal:
1322 case eFormatEnum:
1323 case eFormatHex:
1324 case eFormatHexUppercase:
1325 case eFormatFloat:
1326 case eFormatOctal:
1327 case eFormatOSType:
1328 case eFormatUnsigned:
1329 case eFormatPointer:
1330 case eFormatVectorOfChar:
1331 case eFormatVectorOfSInt8:
1332 case eFormatVectorOfUInt8:
1333 case eFormatVectorOfSInt16:
1334 case eFormatVectorOfUInt16:
1335 case eFormatVectorOfSInt32:
1336 case eFormatVectorOfUInt32:
1337 case eFormatVectorOfSInt64:
1338 case eFormatVectorOfUInt64:
1339 case eFormatVectorOfFloat32:
1340 case eFormatVectorOfFloat64:
1341 case eFormatVectorOfUInt128:
1342 break;
1343
1344 case eFormatChar:
1345 case eFormatCharPrintable:
1346 case eFormatCharArray:
1347 case eFormatBytes:
1348 case eFormatBytesWithASCII:
1349 item_count = byte_size;
1350 byte_size = 1;
1351 break;
1352
1353 case eFormatUnicode16:
1354 item_count = byte_size / 2;
1355 byte_size = 2;
1356 break;
1357
1358 case eFormatUnicode32:
1359 item_count = byte_size / 4;
1360 byte_size = 4;
1361 break;
1362 }
1363 return data.Dump(s, byte_offset, format, byte_size, item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
1364 bitfield_bit_size, bitfield_bit_offset, exe_scope);
1365 }
1366 return 0;
1367}
1368
1369void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001370GoASTContext::DumpSummary(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s, const DataExtractor &data,
Ryan Brown57bee1e2015-09-14 22:45:11 +00001371 lldb::offset_t data_offset, size_t data_byte_size)
1372{
1373 assert(false);
1374}
1375
1376void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001377GoASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001378{
1379 assert(false);
1380} // Dump to stdout
1381
1382void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001383GoASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type, Stream *s)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001384{
1385 assert(false);
1386}
1387
1388CompilerType
Bruce Mitchener500737e2015-09-15 04:33:48 +00001389GoASTContext::CreateArrayType(const ConstString &name, const CompilerType &element_type, uint64_t length)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001390{
1391 GoType *type = new GoArray(name, length, element_type);
1392 (*m_types)[name].reset(type);
1393 return CompilerType(this, type);
1394}
1395
1396CompilerType
1397GoASTContext::CreateBaseType(int go_kind, const lldb_private::ConstString &name, uint64_t byte_size)
1398{
1399 if (go_kind == GoType::KIND_UINT || go_kind == GoType::KIND_INT)
1400 m_int_byte_size = byte_size;
1401 GoType *type = new GoType(go_kind, name);
1402 (*m_types)[name].reset(type);
1403 return CompilerType(this, type);
1404}
1405
1406CompilerType
Greg Clayton56939cb2015-09-17 22:23:34 +00001407GoASTContext::CreateTypedefType(int kind, const ConstString &name, CompilerType impl)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001408{
1409 GoType *type = new GoElem(kind, name, impl);
1410 (*m_types)[name].reset(type);
1411 return CompilerType(this, type);
1412}
1413
1414CompilerType
1415GoASTContext::CreateVoidType(const lldb_private::ConstString &name)
1416{
1417 GoType *type = new GoType(GoType::KIND_LLDB_VOID, name);
1418 (*m_types)[name].reset(type);
1419 return CompilerType(this, type);
1420}
1421
1422CompilerType
1423GoASTContext::CreateStructType(int kind, const lldb_private::ConstString &name, uint32_t byte_size)
1424{
1425 GoType *type = new GoStruct(kind, name, byte_size);
1426 (*m_types)[name].reset(type);
1427 return CompilerType(this, type);
1428}
1429
1430void
1431GoASTContext::AddFieldToStruct(const lldb_private::CompilerType &struct_type, const lldb_private::ConstString &name,
1432 const lldb_private::CompilerType &field_type, uint32_t byte_offset)
1433{
1434 if (!struct_type)
1435 return;
1436 GoASTContext *ast = llvm::dyn_cast_or_null<GoASTContext>(struct_type.GetTypeSystem());
1437 if (!ast)
1438 return;
1439 GoType *type = static_cast<GoType *>(struct_type.GetOpaqueQualType());
1440 if (GoStruct *s = type->GetStruct())
1441 s->AddField(name, field_type, byte_offset);
1442}
1443
1444void
1445GoASTContext::CompleteStructType(const lldb_private::CompilerType &struct_type)
1446{
1447 if (!struct_type)
1448 return;
1449 GoASTContext *ast = llvm::dyn_cast_or_null<GoASTContext>(struct_type.GetTypeSystem());
1450 if (!ast)
1451 return;
1452 GoType *type = static_cast<GoType *>(struct_type.GetOpaqueQualType());
1453 if (GoStruct *s = type->GetStruct())
1454 s->SetComplete();
1455}
1456
1457CompilerType
1458GoASTContext::CreateFunctionType(const lldb_private::ConstString &name, CompilerType *params, size_t params_count,
1459 bool is_variadic)
1460{
1461 GoType *type = new GoFunction(name, is_variadic);
1462 (*m_types)[name].reset(type);
1463 return CompilerType(this, type);
1464}
1465
1466bool
1467GoASTContext::IsGoString(const lldb_private::CompilerType &type)
1468{
1469 if (!type.IsValid() || !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1470 return false;
1471 return GoType::KIND_STRING == static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1472}
1473
1474bool
1475GoASTContext::IsGoSlice(const lldb_private::CompilerType &type)
1476{
1477 if (!type.IsValid() || !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1478 return false;
1479 return GoType::KIND_SLICE == static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1480}
1481
1482bool
1483GoASTContext::IsGoInterface(const lldb_private::CompilerType &type)
1484{
1485 if (!type.IsValid() || !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1486 return false;
1487 return GoType::KIND_INTERFACE == static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1488}
1489
1490bool
1491GoASTContext::IsPointerKind(uint8_t kind)
1492{
1493 return (kind & GoType::KIND_MASK) == GoType::KIND_PTR;
1494}
1495
1496bool
1497GoASTContext::IsDirectIface(uint8_t kind)
1498{
1499 return (kind & GoType::KIND_DIRECT_IFACE) == GoType::KIND_DIRECT_IFACE;
1500}
1501
1502DWARFASTParser *
1503GoASTContext::GetDWARFParser()
1504{
1505 if (!m_dwarf_ast_parser_ap)
1506 m_dwarf_ast_parser_ap.reset(new DWARFASTParserGo(*this));
1507 return m_dwarf_ast_parser_ap.get();
1508}
Ryan Brown998c8a1c12015-11-02 19:30:40 +00001509
1510UserExpression *
1511GoASTContextForExpr::GetUserExpression(const char *expr, const char *expr_prefix, lldb::LanguageType language,
1512 Expression::ResultType desired_type)
1513{
1514 TargetSP target = m_target_wp.lock();
1515 if (target)
1516 return new GoUserExpression(*target, expr, expr_prefix, language, desired_type);
1517 return nullptr;
1518}