blob: c46fcb1e7a15fd90b55f3657a1534be022b6ab4f [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
27#include "Plugins/SymbolFile/DWARF/DWARFASTParserGo.h"
28
29using namespace lldb;
30
31namespace lldb_private
32{
33class GoArray;
34class GoFunction;
35class GoStruct;
36
37class GoType
38{
39 public:
40 enum
41 {
42 KIND_BOOL = 1,
43 KIND_INT = 2,
44 KIND_INT8 = 3,
45 KIND_INT16 = 4,
46 KIND_INT32 = 5,
47 KIND_INT64 = 6,
48 KIND_UINT = 7,
49 KIND_UINT8 = 8,
50 KIND_UINT16 = 9,
51 KIND_UINT32 = 10,
52 KIND_UINT64 = 11,
53 KIND_UINTPTR = 12,
54 KIND_FLOAT32 = 13,
55 KIND_FLOAT64 = 14,
56 KIND_COMPLEX64 = 15,
57 KIND_COMPLEX128 = 16,
58 KIND_ARRAY = 17,
59 KIND_CHAN = 18,
60 KIND_FUNC = 19,
61 KIND_INTERFACE = 20,
62 KIND_MAP = 21,
63 KIND_PTR = 22,
64 KIND_SLICE = 23,
65 KIND_STRING = 24,
66 KIND_STRUCT = 25,
67 KIND_UNSAFEPOINTER = 26,
68 KIND_LLDB_VOID, // Extension for LLDB, not used by go runtime.
69 KIND_MASK = (1 << 5) - 1,
70 KIND_DIRECT_IFACE = 1 << 5
71 };
72 GoType(int kind, const ConstString &name)
73 : m_kind(kind & KIND_MASK)
74 , m_name(name)
75 {
76 if (m_kind == KIND_FUNC)
77 m_kind = KIND_FUNC;
78 }
79 virtual ~GoType() {}
80
81 int
82 GetGoKind() const
83 {
84 return m_kind;
85 }
86 const ConstString &
87 GetName() const
88 {
89 return m_name;
90 }
91 virtual CompilerType
92 GetElementType() const
93 {
94 return CompilerType();
95 }
96
97 bool
98 IsTypedef() const
99 {
100 switch (m_kind)
101 {
102 case KIND_CHAN:
103 case KIND_MAP:
104 case KIND_INTERFACE:
105 return true;
106 default:
107 return false;
108 }
109 }
110
111 GoArray *GetArray();
112 GoFunction *GetFunction();
113 GoStruct *GetStruct();
114
115 private:
116 int m_kind;
117 ConstString m_name;
118 GoType(const GoType &) = delete;
119 const GoType &operator=(const GoType &) = delete;
120};
121
122class GoElem : public GoType
123{
124 public:
125 GoElem(int kind, const ConstString &name, const CompilerType &elem)
126 : GoType(kind, name)
127 , m_elem(elem)
128 {
129 }
130 virtual CompilerType
131 GetElementType() const
132 {
133 return m_elem;
134 }
135
136 private:
137 // TODO: should we store this differently?
138 CompilerType m_elem;
139
140 GoElem(const GoElem &) = delete;
141 const GoElem &operator=(const GoElem &) = delete;
142};
143
144class GoArray : public GoElem
145{
146 public:
Bruce Mitchener500737e2015-09-15 04:33:48 +0000147 GoArray(const ConstString &name, uint64_t length, const CompilerType &elem)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000148 : GoElem(KIND_ARRAY, name, elem)
149 , m_length(length)
150 {
151 }
152
Bruce Mitchener500737e2015-09-15 04:33:48 +0000153 uint64_t
Ryan Brown57bee1e2015-09-14 22:45:11 +0000154 GetLength() const
155 {
156 return m_length;
157 }
158
159 private:
Bruce Mitchener500737e2015-09-15 04:33:48 +0000160 uint64_t m_length;
Ryan Brown57bee1e2015-09-14 22:45:11 +0000161 GoArray(const GoArray &) = delete;
162 const GoArray &operator=(const GoArray &) = delete;
163};
164
165class GoFunction : public GoType
166{
167 public:
168 GoFunction(const ConstString &name, bool is_variadic)
169 : GoType(KIND_FUNC, name)
170 , m_is_variadic(is_variadic)
171 {
172 }
173
174 bool
175 IsVariadic() const
176 {
177 return m_is_variadic;
178 }
179
180 private:
181 bool m_is_variadic;
182 GoFunction(const GoFunction &) = delete;
183 const GoFunction &operator=(const GoFunction &) = delete;
184};
185
186class GoStruct : public GoType
187{
188 public:
189 struct Field
190 {
191 Field(const ConstString &name, const CompilerType &type, uint64_t offset)
192 : m_name(name)
193 , m_type(type)
194 , m_byte_offset(offset)
195 {
196 }
197 ConstString m_name;
198 CompilerType m_type;
199 uint64_t m_byte_offset;
200 };
201
202 GoStruct(int kind, const ConstString &name, int64_t byte_size)
203 : GoType(kind, name)
204 , m_is_complete(false)
205 , m_byte_size(byte_size)
206 {
207 }
208
209 uint32_t
210 GetNumFields() const
211 {
212 return m_fields.size();
213 }
214
215 const Field *
216 GetField(uint32_t i) const
217 {
218 if (i < m_fields.size())
219 return &m_fields[i];
220 return nullptr;
221 }
222
223 void
224 AddField(const ConstString &name, const CompilerType &type, uint64_t offset)
225 {
226 m_fields.push_back(Field(name, type, offset));
227 }
228
229 bool
230 IsComplete() const
231 {
232 return m_is_complete;
233 }
234
235 void
236 SetComplete()
237 {
238 m_is_complete = true;
239 }
240
241 int64_t
242 GetByteSize() const
243 {
244 return m_byte_size;
245 }
246
247 private:
248 bool m_is_complete;
249 int64_t m_byte_size;
250 std::vector<Field> m_fields;
251
252 GoStruct(const GoStruct &) = delete;
253 const GoStruct &operator=(const GoStruct &) = delete;
254};
255
256GoArray *
257GoType::GetArray()
258{
259 if (m_kind == KIND_ARRAY)
260 {
261 return static_cast<GoArray *>(this);
262 }
263 return nullptr;
264}
265
266GoFunction *
267GoType::GetFunction()
268{
269 if (m_kind == KIND_FUNC)
270 {
271 return static_cast<GoFunction *>(this);
272 }
273 return nullptr;
274}
275
276GoStruct *
277GoType::GetStruct()
278{
279 switch (m_kind)
280 {
281 case KIND_STRING:
282 case KIND_STRUCT:
283 case KIND_SLICE:
284 return static_cast<GoStruct *>(this);
285 }
286 return nullptr;
287}
288} // namespace lldb_private
289using namespace lldb_private;
290
291GoASTContext::GoASTContext()
292 : TypeSystem(eKindGo)
293 , m_pointer_byte_size(0)
294 , m_int_byte_size(0)
295 , m_types(new TypeMap)
296{
297}
298GoASTContext::~GoASTContext()
299{
300}
301
Greg Clayton56939cb2015-09-17 22:23:34 +0000302//------------------------------------------------------------------
303// PluginInterface functions
304//------------------------------------------------------------------
305
306ConstString
307GoASTContext::GetPluginNameStatic()
308{
309 return ConstString("go");
310}
311
312ConstString
313GoASTContext::GetPluginName()
314{
315 return GoASTContext::GetPluginNameStatic();
316}
317
318uint32_t
319GoASTContext::GetPluginVersion()
320{
321 return 1;
322}
323
324lldb::TypeSystemSP
Greg Clayton5beec212015-10-08 21:04:34 +0000325GoASTContext::CreateInstance (lldb::LanguageType language, Module *module, Target *target)
Greg Clayton56939cb2015-09-17 22:23:34 +0000326{
327 if (language == eLanguageTypeGo)
328 {
Greg Clayton5beec212015-10-08 21:04:34 +0000329 ArchSpec arch;
330 if (module)
331 arch = module->GetArchitecture();
332 else if (target)
333 arch = target->GetArchitecture();
334
Greg Clayton56939cb2015-09-17 22:23:34 +0000335 if (arch.IsValid())
336 {
337 std::shared_ptr<GoASTContext> go_ast_sp(new GoASTContext);
338 go_ast_sp->SetAddressByteSize(arch.GetAddressByteSize());
339 return go_ast_sp;
340 }
341 }
342 return lldb::TypeSystemSP();
343}
344
Sean Callananfe38c852015-10-08 23:07:53 +0000345void
346GoASTContext::EnumerateSupportedLanguages(std::set<lldb::LanguageType> &languages_for_types, std::set<lldb::LanguageType> &languages_for_expressions)
347{
348 static std::vector<lldb::LanguageType> s_supported_languages_for_types({
349 lldb::eLanguageTypeGo});
350
351 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions({});
352
353 languages_for_types.insert(s_supported_languages_for_types.begin(), s_supported_languages_for_types.end());
354 languages_for_expressions.insert(s_supported_languages_for_expressions.begin(), s_supported_languages_for_expressions.end());
355}
356
Greg Clayton56939cb2015-09-17 22:23:34 +0000357
358void
359GoASTContext::Initialize()
360{
361 PluginManager::RegisterPlugin (GetPluginNameStatic(),
362 "AST context plug-in",
Sean Callananfe38c852015-10-08 23:07:53 +0000363 CreateInstance,
364 EnumerateSupportedLanguages);
Greg Clayton56939cb2015-09-17 22:23:34 +0000365}
366
367void
368GoASTContext::Terminate()
369{
370 PluginManager::UnregisterPlugin (CreateInstance);
371}
372
373
Ryan Brown57bee1e2015-09-14 22:45:11 +0000374//----------------------------------------------------------------------
375// Tests
376//----------------------------------------------------------------------
377
378bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000379GoASTContext::IsArrayType(lldb::opaque_compiler_type_t type, CompilerType *element_type, uint64_t *size, bool *is_incomplete)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000380{
381 if (element_type)
382 element_type->Clear();
383 if (size)
384 *size = 0;
385 if (is_incomplete)
386 *is_incomplete = false;
387 GoArray *array = static_cast<GoType *>(type)->GetArray();
388 if (array)
389 {
Bruce Mitchener500737e2015-09-15 04:33:48 +0000390 if (size)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000391 *size = array->GetLength();
Ryan Brown57bee1e2015-09-14 22:45:11 +0000392 if (element_type)
393 *element_type = array->GetElementType();
394 return true;
395 }
396 return false;
397}
398
399bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000400GoASTContext::IsVectorType(lldb::opaque_compiler_type_t type, CompilerType *element_type, uint64_t *size)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000401{
402 if (element_type)
403 element_type->Clear();
404 if (size)
405 *size = 0;
406 return false;
407}
408
409bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000410GoASTContext::IsAggregateType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000411{
412 int kind = static_cast<GoType *>(type)->GetGoKind();
413 if (kind < GoType::KIND_ARRAY)
414 return false;
415 if (kind == GoType::KIND_PTR)
416 return false;
417 if (kind == GoType::KIND_STRING)
418 return false;
419 if (kind == GoType::KIND_UNSAFEPOINTER)
420 return false;
421 return true;
422}
423
424bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000425GoASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000426{
427 return false;
428}
429
430bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000431GoASTContext::IsCharType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000432{
433 // Go's DWARF doesn't distinguish between rune and int32.
434 return false;
435}
436
437bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000438GoASTContext::IsCompleteType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000439{
440 if (!type)
441 return false;
442 GoType *t = static_cast<GoType *>(type);
443 if (GoStruct *s = t->GetStruct())
444 return s->IsComplete();
445 if (t->IsTypedef() || t->GetGoKind() == GoType::KIND_PTR)
446 return t->GetElementType().IsCompleteType();
447 return true;
448}
449
450bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000451GoASTContext::IsConst(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000452{
453 return false;
454}
455
456bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000457GoASTContext::IsCStringType(lldb::opaque_compiler_type_t type, uint32_t &length)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000458{
459 return false;
460}
461
462bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000463GoASTContext::IsDefined(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000464{
465 return type != nullptr;
466}
467
468bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000469GoASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type, uint32_t &count, bool &is_complex)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000470{
471 int kind = static_cast<GoType *>(type)->GetGoKind();
472 if (kind >= GoType::KIND_FLOAT32 && kind <= GoType::KIND_COMPLEX128)
473 {
474 if (kind >= GoType::KIND_COMPLEX64)
475 {
476 is_complex = true;
477 count = 2;
478 }
479 else
480 {
481 is_complex = false;
482 count = 1;
483 }
484 return true;
485 }
486 count = 0;
487 is_complex = false;
488 return false;
489}
490
491bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000492GoASTContext::IsFunctionType(lldb::opaque_compiler_type_t type, bool *is_variadic_ptr)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000493{
494 GoFunction *func = static_cast<GoType *>(type)->GetFunction();
495 if (func)
496 {
497 if (is_variadic_ptr)
498 *is_variadic_ptr = func->IsVariadic();
499 return true;
500 }
501 if (is_variadic_ptr)
502 *is_variadic_ptr = false;
503 return false;
504}
505
506uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000507GoASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type, CompilerType *base_type_ptr)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000508{
509 return false;
510}
511
512size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000513GoASTContext::GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000514{
515 return 0;
516}
517
518CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000519GoASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type, const size_t index)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000520{
521 return CompilerType();
522}
523
524bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000525GoASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000526{
527 return IsFunctionType(type);
528}
529
530bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000531GoASTContext::IsIntegerType(lldb::opaque_compiler_type_t type, bool &is_signed)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000532{
533 is_signed = false;
534 // TODO: Is bool an integer?
535 if (type)
536 {
537 int kind = static_cast<GoType *>(type)->GetGoKind();
538 if (kind <= GoType::KIND_UINTPTR)
539 {
540 is_signed = (kind != GoType::KIND_BOOL) & (kind <= GoType::KIND_INT64);
541 return true;
542 }
543 }
544 return false;
545}
546
547bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000548GoASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000549{
550 return false;
551}
552
553bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000554GoASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
Ryan Brown57bee1e2015-09-14 22:45:11 +0000555 CompilerType *target_type, // Can pass NULL
556 bool check_cplusplus, bool check_objc)
557{
558 if (target_type)
559 target_type->Clear();
560 if (type)
561 return static_cast<GoType *>(type)->GetGoKind() == GoType::KIND_INTERFACE;
562 return false;
563}
564
565bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000566GoASTContext::IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000567{
568 return false;
569}
570
571bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000572GoASTContext::IsPointerType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000573{
574 if (!type)
575 return false;
576 GoType *t = static_cast<GoType *>(type);
577 if (pointee_type)
578 {
579 *pointee_type = t->GetElementType();
580 }
581 switch (t->GetGoKind())
582 {
583 case GoType::KIND_PTR:
584 case GoType::KIND_UNSAFEPOINTER:
585 case GoType::KIND_CHAN:
586 // TODO: is map a pointer? string? function?
587 return true;
588 default:
589 return false;
590 }
591}
592
593bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000594GoASTContext::IsPointerOrReferenceType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000595{
596 return IsPointerType(type, pointee_type);
597}
598
599bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000600GoASTContext::IsReferenceType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type, bool *is_rvalue)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000601{
602 return false;
603}
604
605bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000606GoASTContext::IsScalarType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000607{
608 return !IsAggregateType(type);
609}
610
611bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000612GoASTContext::IsTypedefType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000613{
614 if (type)
615 return static_cast<GoType *>(type)->IsTypedef();
616 return false;
617}
618
619bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000620GoASTContext::IsVoidType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000621{
622 if (!type)
623 return false;
624 return static_cast<GoType *>(type)->GetGoKind() == GoType::KIND_LLDB_VOID;
625}
626
Greg Clayton56939cb2015-09-17 22:23:34 +0000627bool
628GoASTContext::SupportsLanguage (lldb::LanguageType language)
629{
630 return language == eLanguageTypeGo;
631}
632
Ryan Brown57bee1e2015-09-14 22:45:11 +0000633//----------------------------------------------------------------------
634// Type Completion
635//----------------------------------------------------------------------
636
637bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000638GoASTContext::GetCompleteType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000639{
640 if (!type)
641 return false;
642 GoType *t = static_cast<GoType *>(type);
643 if (t->IsTypedef() || t->GetGoKind() == GoType::KIND_PTR || t->GetArray())
644 return t->GetElementType().GetCompleteType();
645 if (GoStruct *s = t->GetStruct())
646 {
647 if (s->IsComplete())
648 return true;
649 CompilerType compiler_type(this, s);
650 SymbolFile *symbols = GetSymbolFile();
651 return symbols && symbols->CompleteType(compiler_type);
652 }
653 return true;
654}
655
656//----------------------------------------------------------------------
657// AST related queries
658//----------------------------------------------------------------------
659
660uint32_t
661GoASTContext::GetPointerByteSize()
662{
663 return m_pointer_byte_size;
664}
665
666//----------------------------------------------------------------------
667// Accessors
668//----------------------------------------------------------------------
669
670ConstString
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000671GoASTContext::GetTypeName(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000672{
673 if (type)
674 return static_cast<GoType *>(type)->GetName();
675 return ConstString();
676}
677
678uint32_t
Bruce Mitchener3ad353f2015-09-24 03:54:50 +0000679GoASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type, CompilerType *pointee_or_element_compiler_type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000680{
Bruce Mitchener3ad353f2015-09-24 03:54:50 +0000681 if (pointee_or_element_compiler_type)
682 pointee_or_element_compiler_type->Clear();
Ryan Brown57bee1e2015-09-14 22:45:11 +0000683 if (!type)
684 return 0;
685 GoType *t = static_cast<GoType *>(type);
Bruce Mitchener3ad353f2015-09-24 03:54:50 +0000686 if (pointee_or_element_compiler_type)
687 *pointee_or_element_compiler_type = t->GetElementType();
Ryan Brown57bee1e2015-09-14 22:45:11 +0000688 int kind = t->GetGoKind();
689 if (kind == GoType::KIND_ARRAY)
690 return eTypeHasChildren | eTypeIsArray;
691 if (kind < GoType::KIND_ARRAY)
692 {
693 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
694 if (kind < GoType::KIND_FLOAT32)
695 {
696 builtin_type_flags |= eTypeIsInteger | eTypeIsScalar;
697 if (kind >= GoType::KIND_INT && kind <= GoType::KIND_INT64)
698 builtin_type_flags |= eTypeIsSigned;
699 }
700 else
701 {
702 builtin_type_flags |= eTypeIsFloat;
703 if (kind < GoType::KIND_COMPLEX64)
704 builtin_type_flags |= eTypeIsComplex;
705 else
706 builtin_type_flags |= eTypeIsScalar;
707 }
708 return builtin_type_flags;
709 }
710 if (kind == GoType::KIND_STRING)
711 return eTypeHasValue | eTypeIsBuiltIn;
712 if (kind == GoType::KIND_FUNC)
713 return eTypeIsFuncPrototype | eTypeHasValue;
714 if (IsPointerType(type))
715 return eTypeIsPointer | eTypeHasValue | eTypeHasChildren;
716 if (kind == GoType::KIND_LLDB_VOID)
717 return 0;
718 return eTypeHasChildren | eTypeIsStructUnion;
719}
720
721lldb::TypeClass
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000722GoASTContext::GetTypeClass(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000723{
724 if (!type)
725 return eTypeClassInvalid;
726 int kind = static_cast<GoType *>(type)->GetGoKind();
727 if (kind == GoType::KIND_FUNC)
728 return eTypeClassFunction;
729 if (IsPointerType(type))
730 return eTypeClassPointer;
731 if (kind < GoType::KIND_COMPLEX64)
732 return eTypeClassBuiltin;
733 if (kind <= GoType::KIND_COMPLEX128)
734 return eTypeClassComplexFloat;
735 if (kind == GoType::KIND_LLDB_VOID)
736 return eTypeClassInvalid;
737 return eTypeClassStruct;
738}
739
740lldb::BasicType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000741GoASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000742{
743 ConstString name = GetTypeName(type);
744 if (name)
745 {
746 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
747 static TypeNameToBasicTypeMap g_type_map;
748 static std::once_flag g_once_flag;
749 std::call_once(g_once_flag, [](){
750 // "void"
751 g_type_map.Append(ConstString("void").GetCString(), eBasicTypeVoid);
752 // "int"
753 g_type_map.Append(ConstString("int").GetCString(), eBasicTypeInt);
754 g_type_map.Append(ConstString("uint").GetCString(), eBasicTypeUnsignedInt);
755
756 // Miscellaneous
757 g_type_map.Append(ConstString("bool").GetCString(), eBasicTypeBool);
758
759 // Others. Should these map to C types?
760 g_type_map.Append(ConstString("byte").GetCString(), eBasicTypeOther);
761 g_type_map.Append(ConstString("uint8").GetCString(), eBasicTypeOther);
762 g_type_map.Append(ConstString("uint16").GetCString(), eBasicTypeOther);
763 g_type_map.Append(ConstString("uint32").GetCString(), eBasicTypeOther);
764 g_type_map.Append(ConstString("uint64").GetCString(), eBasicTypeOther);
765 g_type_map.Append(ConstString("int8").GetCString(), eBasicTypeOther);
766 g_type_map.Append(ConstString("int16").GetCString(), eBasicTypeOther);
767 g_type_map.Append(ConstString("int32").GetCString(), eBasicTypeOther);
768 g_type_map.Append(ConstString("int64").GetCString(), eBasicTypeOther);
769 g_type_map.Append(ConstString("float32").GetCString(), eBasicTypeOther);
770 g_type_map.Append(ConstString("float64").GetCString(), eBasicTypeOther);
771 g_type_map.Append(ConstString("uintptr").GetCString(), eBasicTypeOther);
772
773 g_type_map.Sort();
774 });
775
776 return g_type_map.Find(name.GetCString(), eBasicTypeInvalid);
777 }
778 return eBasicTypeInvalid;
779}
780
781lldb::LanguageType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000782GoASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000783{
784 return lldb::eLanguageTypeGo;
785}
786
787unsigned
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000788GoASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000789{
790 return 0;
791}
792
793//----------------------------------------------------------------------
794// Creating related types
795//----------------------------------------------------------------------
796
797CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000798GoASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type, uint64_t *stride)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000799{
800 GoArray *array = static_cast<GoType *>(type)->GetArray();
801 if (array)
802 {
803 if (stride)
804 {
805 *stride = array->GetElementType().GetByteSize(nullptr);
806 }
807 return array->GetElementType();
808 }
809 return CompilerType();
810}
811
812CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000813GoASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000814{
815 GoType *t = static_cast<GoType *>(type);
816 if (t->IsTypedef())
817 return t->GetElementType();
818 return CompilerType(this, type);
819}
820
821CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000822GoASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000823{
824 return CompilerType(this, type);
825}
826
827// Returns -1 if this isn't a function of if the function doesn't have a prototype
828// Returns a value >= 0 if there is a prototype.
829int
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000830GoASTContext::GetFunctionArgumentCount(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000831{
832 return GetNumberOfFunctionArguments(type);
833}
834
835CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000836GoASTContext::GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type, size_t idx)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000837{
838 return GetFunctionArgumentAtIndex(type, idx);
839}
840
841CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000842GoASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000843{
844 CompilerType result;
845 if (type)
846 {
847 GoType *t = static_cast<GoType *>(type);
848 if (t->GetGoKind() == GoType::KIND_FUNC)
849 result = t->GetElementType();
850 }
851 return result;
852}
853
854size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000855GoASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000856{
857 return 0;
858}
859
860TypeMemberFunctionImpl
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000861GoASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, size_t idx)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000862{
863 return TypeMemberFunctionImpl();
864}
865
866CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000867GoASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000868{
869 return CompilerType(this, type);
870}
871
872CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000873GoASTContext::GetPointeeType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000874{
875 if (!type)
876 return CompilerType();
877 return static_cast<GoType *>(type)->GetElementType();
878}
879
880CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000881GoASTContext::GetPointerType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000882{
883 if (!type)
884 return CompilerType();
885 ConstString type_name = GetTypeName(type);
886 ConstString pointer_name(std::string("*") + type_name.GetCString());
887 GoType *pointer = (*m_types)[pointer_name].get();
888 if (pointer == nullptr)
889 {
890 pointer = new GoElem(GoType::KIND_PTR, pointer_name, CompilerType(this, type));
891 (*m_types)[pointer_name].reset(pointer);
892 }
893 return CompilerType(this, pointer);
894}
895
896// If the current object represents a typedef type, get the underlying type
897CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000898GoASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000899{
900 if (IsTypedefType(type))
901 return static_cast<GoType *>(type)->GetElementType();
902 return CompilerType();
903}
904
905//----------------------------------------------------------------------
906// Create related types using the current type's AST
907//----------------------------------------------------------------------
908CompilerType
909GoASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type)
910{
911 return CompilerType();
912}
913
Greg Clayton56939cb2015-09-17 22:23:34 +0000914CompilerType
915GoASTContext::GetBuiltinTypeForEncodingAndBitSize (lldb::Encoding encoding,
916 size_t bit_size)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000917{
918 return CompilerType();
919}
920
921
922//----------------------------------------------------------------------
923// Exploring the type
924//----------------------------------------------------------------------
925
926uint64_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000927GoASTContext::GetBitSize(lldb::opaque_compiler_type_t type, ExecutionContextScope *exe_scope)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000928{
929 if (!type)
930 return 0;
931 if (!GetCompleteType(type))
932 return 0;
933 GoType *t = static_cast<GoType *>(type);
934 GoArray *array = nullptr;
935 switch (t->GetGoKind())
936 {
937 case GoType::KIND_BOOL:
938 case GoType::KIND_INT8:
939 case GoType::KIND_UINT8:
940 return 8;
941 case GoType::KIND_INT16:
942 case GoType::KIND_UINT16:
943 return 16;
944 case GoType::KIND_INT32:
945 case GoType::KIND_UINT32:
946 case GoType::KIND_FLOAT32:
947 return 32;
948 case GoType::KIND_INT64:
949 case GoType::KIND_UINT64:
950 case GoType::KIND_FLOAT64:
951 case GoType::KIND_COMPLEX64:
952 return 64;
953 case GoType::KIND_COMPLEX128:
954 return 128;
955 case GoType::KIND_INT:
956 case GoType::KIND_UINT:
957 return m_int_byte_size * 8;
958 case GoType::KIND_UINTPTR:
959 case GoType::KIND_FUNC: // I assume this is a pointer?
960 case GoType::KIND_CHAN:
961 case GoType::KIND_PTR:
962 case GoType::KIND_UNSAFEPOINTER:
963 case GoType::KIND_MAP:
964 return m_pointer_byte_size * 8;
965 case GoType::KIND_ARRAY:
966 array = t->GetArray();
967 return array->GetLength() * array->GetElementType().GetBitSize(exe_scope);
968 case GoType::KIND_INTERFACE:
969 return t->GetElementType().GetBitSize(exe_scope);
970 case GoType::KIND_SLICE:
971 case GoType::KIND_STRING:
972 case GoType::KIND_STRUCT:
973 return t->GetStruct()->GetByteSize() * 8;
974 default:
975 assert(false);
976 }
Ryan Brownd03c2e02015-09-15 00:50:43 +0000977 return 0;
Ryan Brown57bee1e2015-09-14 22:45:11 +0000978}
979
980lldb::Encoding
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000981GoASTContext::GetEncoding(lldb::opaque_compiler_type_t type, uint64_t &count)
Ryan Brown57bee1e2015-09-14 22:45:11 +0000982{
983 count = 1;
984 bool is_signed;
985 if (IsIntegerType(type, is_signed))
986 return is_signed ? lldb::eEncodingSint : eEncodingUint;
987 bool is_complex;
988 uint32_t complex_count;
989 if (IsFloatingPointType(type, complex_count, is_complex))
990 {
991 count = complex_count;
992 return eEncodingIEEE754;
993 }
994 if (IsPointerType(type))
995 return eEncodingUint;
996 return eEncodingInvalid;
997}
998
999lldb::Format
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001000GoASTContext::GetFormat(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001001{
1002 if (!type)
1003 return eFormatDefault;
1004 switch (static_cast<GoType *>(type)->GetGoKind())
1005 {
1006 case GoType::KIND_BOOL:
1007 return eFormatBoolean;
1008 case GoType::KIND_INT:
1009 case GoType::KIND_INT8:
1010 case GoType::KIND_INT16:
1011 case GoType::KIND_INT32:
1012 case GoType::KIND_INT64:
1013 return eFormatDecimal;
1014 case GoType::KIND_UINT:
1015 case GoType::KIND_UINT8:
1016 case GoType::KIND_UINT16:
1017 case GoType::KIND_UINT32:
1018 case GoType::KIND_UINT64:
1019 return eFormatUnsigned;
1020 case GoType::KIND_FLOAT32:
1021 case GoType::KIND_FLOAT64:
1022 return eFormatFloat;
1023 case GoType::KIND_COMPLEX64:
1024 case GoType::KIND_COMPLEX128:
1025 return eFormatComplexFloat;
1026 case GoType::KIND_UINTPTR:
1027 case GoType::KIND_CHAN:
1028 case GoType::KIND_PTR:
1029 case GoType::KIND_MAP:
1030 case GoType::KIND_UNSAFEPOINTER:
1031 return eFormatHex;
1032 case GoType::KIND_STRING:
1033 return eFormatCString;
1034 case GoType::KIND_ARRAY:
1035 case GoType::KIND_INTERFACE:
1036 case GoType::KIND_SLICE:
1037 case GoType::KIND_STRUCT:
1038 default:
1039 // Don't know how to display this.
1040 return eFormatBytes;
1041 }
1042}
1043
1044size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001045GoASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001046{
1047 return 0;
1048}
1049
1050uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001051GoASTContext::GetNumChildren(lldb::opaque_compiler_type_t type, bool omit_empty_base_classes)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001052{
1053 if (!type || !GetCompleteType(type))
1054 return 0;
1055 GoType *t = static_cast<GoType *>(type);
1056 if (t->GetGoKind() == GoType::KIND_PTR)
1057 {
1058 CompilerType elem = t->GetElementType();
1059 if (elem.IsAggregateType())
1060 return elem.GetNumChildren(omit_empty_base_classes);
1061 return 1;
1062 }
1063 else if (GoArray *array = t->GetArray())
1064 {
1065 return array->GetLength();
1066 }
1067 return GetNumFields(type);
1068}
1069
1070uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001071GoASTContext::GetNumFields(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001072{
1073 if (!type || !GetCompleteType(type))
1074 return 0;
1075 GoType *t = static_cast<GoType *>(type);
1076 if (t->IsTypedef())
1077 return t->GetElementType().GetNumFields();
1078 GoStruct *s = t->GetStruct();
1079 if (s)
1080 return s->GetNumFields();
1081 return 0;
1082}
1083
1084CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001085GoASTContext::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 +00001086 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr)
1087{
1088 if (bit_offset_ptr)
1089 *bit_offset_ptr = 0;
1090 if (bitfield_bit_size_ptr)
1091 *bitfield_bit_size_ptr = 0;
1092 if (is_bitfield_ptr)
1093 *is_bitfield_ptr = false;
1094
1095 if (!type || !GetCompleteType(type))
1096 return CompilerType();
1097
1098 GoType *t = static_cast<GoType *>(type);
1099 if (t->IsTypedef())
1100 return t->GetElementType().GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, is_bitfield_ptr);
1101
1102 GoStruct *s = t->GetStruct();
1103 if (s)
1104 {
1105 const auto *field = s->GetField(idx);
1106 if (field)
1107 {
1108 name = field->m_name.GetStringRef();
1109 if (bit_offset_ptr)
1110 *bit_offset_ptr = field->m_byte_offset * 8;
1111 return field->m_type;
1112 }
1113 }
1114 return CompilerType();
1115}
1116
1117CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001118GoASTContext::GetChildCompilerTypeAtIndex(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
Bruce Mitchener4ad83342015-09-21 16:48:48 +00001119 bool omit_empty_base_classes, bool ignore_array_bounds, std::string &child_name,
1120 uint32_t &child_byte_size, int32_t &child_byte_offset,
1121 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
1122 bool &child_is_base_class, bool &child_is_deref_of_parent, ValueObject *valobj)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001123{
1124 child_name.clear();
1125 child_byte_size = 0;
1126 child_byte_offset = 0;
1127 child_bitfield_bit_size = 0;
1128 child_bitfield_bit_offset = 0;
1129 child_is_base_class = false;
1130 child_is_deref_of_parent = false;
1131
1132 if (!type || !GetCompleteType(type))
1133 return CompilerType();
1134
1135 GoType *t = static_cast<GoType *>(type);
1136 if (t->GetStruct())
1137 {
1138 uint64_t bit_offset;
1139 CompilerType ret = GetFieldAtIndex(type, idx, child_name, &bit_offset, nullptr, nullptr);
1140 child_byte_size = ret.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr);
1141 child_byte_offset = bit_offset / 8;
1142 return ret;
1143 }
1144 else if (t->GetGoKind() == GoType::KIND_PTR)
1145 {
1146 CompilerType pointee = t->GetElementType();
1147 if (!pointee.IsValid() || pointee.IsVoidType())
1148 return CompilerType();
1149 if (transparent_pointers && pointee.IsAggregateType())
1150 {
1151 bool tmp_child_is_deref_of_parent = false;
Bruce Mitchener4ad83342015-09-21 16:48:48 +00001152 return pointee.GetChildCompilerTypeAtIndex(exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
Ryan Brown57bee1e2015-09-14 22:45:11 +00001153 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
1154 child_bitfield_bit_size, child_bitfield_bit_offset,
1155 child_is_base_class, tmp_child_is_deref_of_parent, valobj);
1156 }
1157 else
1158 {
1159 child_is_deref_of_parent = true;
1160 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
1161 if (parent_name)
1162 {
1163 child_name.assign(1, '*');
1164 child_name += parent_name;
1165 }
1166
1167 // We have a pointer to an simple type
1168 if (idx == 0 && pointee.GetCompleteType())
1169 {
1170 child_byte_size = pointee.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
1171 child_byte_offset = 0;
1172 return pointee;
1173 }
1174 }
1175 }
1176 else if (GoArray *a = t->GetArray())
1177 {
1178 if (ignore_array_bounds || idx < a->GetLength())
1179 {
1180 CompilerType element_type = a->GetElementType();
1181 if (element_type.GetCompleteType())
1182 {
1183 char element_name[64];
1184 ::snprintf(element_name, sizeof(element_name), "[%zu]", idx);
1185 child_name.assign(element_name);
1186 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
1187 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
1188 return element_type;
1189 }
1190 }
1191 }
1192 else if (t->IsTypedef())
1193 {
Bruce Mitchener4ad83342015-09-21 16:48:48 +00001194 return t->GetElementType().GetChildCompilerTypeAtIndex(
Ryan Brown57bee1e2015-09-14 22:45:11 +00001195 exe_ctx, idx, transparent_pointers, omit_empty_base_classes, ignore_array_bounds, child_name,
1196 child_byte_size, child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
1197 child_is_deref_of_parent, valobj);
1198 }
1199 return CompilerType();
1200}
1201
1202// Lookup a child given a name. This function will match base class names
1203// and member member names in "clang_type" only, not descendants.
1204uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001205GoASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type, const char *name, bool omit_empty_base_classes)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001206{
Ryan Brown07a1c452015-10-06 20:29:31 +00001207 if (!type || !GetCompleteType(type))
1208 return UINT_MAX;
1209
Ryan Brown57bee1e2015-09-14 22:45:11 +00001210 GoType *t = static_cast<GoType *>(type);
1211 GoStruct *s = t->GetStruct();
1212 if (s)
1213 {
1214 for (uint32_t i = 0; i < s->GetNumFields(); ++i)
1215 {
1216 const GoStruct::Field *f = s->GetField(i);
1217 if (f->m_name.GetStringRef() == name)
1218 return i;
1219 }
1220 }
1221 else if (t->GetGoKind() == GoType::KIND_PTR || t->IsTypedef())
1222 {
1223 return t->GetElementType().GetIndexOfChildWithName(name, omit_empty_base_classes);
1224 }
1225 return UINT_MAX;
1226}
1227
1228// Lookup a child member given a name. This function will match member names
1229// only and will descend into "clang_type" children in search for the first
1230// member in this class, or any base class that matches "name".
1231// TODO: Return all matches for a given name by returning a vector<vector<uint32_t>>
1232// so we catch all names that match a given child name, not just the first.
1233size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001234GoASTContext::GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type, const char *name, bool omit_empty_base_classes,
Ryan Brown57bee1e2015-09-14 22:45:11 +00001235 std::vector<uint32_t> &child_indexes)
1236{
1237 uint32_t index = GetIndexOfChildWithName(type, name, omit_empty_base_classes);
1238 if (index == UINT_MAX)
1239 return 0;
1240 child_indexes.push_back(index);
1241 return 1;
1242}
1243
1244// Converts "s" to a floating point value and place resulting floating
1245// point bytes in the "dst" buffer.
1246size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001247GoASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type, const char *s, uint8_t *dst, size_t dst_size)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001248{
1249 assert(false);
Ryan Brownd03c2e02015-09-15 00:50:43 +00001250 return 0;
Ryan Brown57bee1e2015-09-14 22:45:11 +00001251}
1252//----------------------------------------------------------------------
1253// Dumping types
1254//----------------------------------------------------------------------
1255void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001256GoASTContext::DumpValue(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s, lldb::Format format,
Ryan Brown57bee1e2015-09-14 22:45:11 +00001257 const DataExtractor &data, lldb::offset_t data_offset, size_t data_byte_size,
1258 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types, bool show_summary,
1259 bool verbose, uint32_t depth)
1260{
1261 assert(false);
1262}
1263
1264bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001265GoASTContext::DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format, const DataExtractor &data,
Ryan Brown57bee1e2015-09-14 22:45:11 +00001266 lldb::offset_t byte_offset, size_t byte_size, uint32_t bitfield_bit_size,
1267 uint32_t bitfield_bit_offset, ExecutionContextScope *exe_scope)
1268{
1269 if (!type)
1270 return false;
1271 if (IsAggregateType(type))
1272 {
1273 return false;
1274 }
1275 else
1276 {
1277 GoType *t = static_cast<GoType *>(type);
1278 if (t->IsTypedef())
1279 {
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001280 CompilerType typedef_compiler_type = t->GetElementType();
Ryan Brown57bee1e2015-09-14 22:45:11 +00001281 if (format == eFormatDefault)
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001282 format = typedef_compiler_type.GetFormat();
1283 uint64_t typedef_byte_size = typedef_compiler_type.GetByteSize(exe_scope);
Ryan Brown57bee1e2015-09-14 22:45:11 +00001284
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001285 return typedef_compiler_type.DumpTypeValue(
Ryan Brown57bee1e2015-09-14 22:45:11 +00001286 s,
1287 format, // The format with which to display the element
1288 data, // Data buffer containing all bytes for this type
1289 byte_offset, // Offset into "data" where to grab value from
1290 typedef_byte_size, // Size of this type in bytes
1291 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't treat as a bitfield
1292 bitfield_bit_offset, // Offset in bits of a bitfield value if bitfield_bit_size != 0
1293 exe_scope);
1294 }
1295
1296 uint32_t item_count = 1;
1297 // A few formats, we might need to modify our size and count for depending
1298 // on how we are trying to display the value...
1299 switch (format)
1300 {
1301 default:
1302 case eFormatBoolean:
1303 case eFormatBinary:
1304 case eFormatComplex:
1305 case eFormatCString: // NULL terminated C strings
1306 case eFormatDecimal:
1307 case eFormatEnum:
1308 case eFormatHex:
1309 case eFormatHexUppercase:
1310 case eFormatFloat:
1311 case eFormatOctal:
1312 case eFormatOSType:
1313 case eFormatUnsigned:
1314 case eFormatPointer:
1315 case eFormatVectorOfChar:
1316 case eFormatVectorOfSInt8:
1317 case eFormatVectorOfUInt8:
1318 case eFormatVectorOfSInt16:
1319 case eFormatVectorOfUInt16:
1320 case eFormatVectorOfSInt32:
1321 case eFormatVectorOfUInt32:
1322 case eFormatVectorOfSInt64:
1323 case eFormatVectorOfUInt64:
1324 case eFormatVectorOfFloat32:
1325 case eFormatVectorOfFloat64:
1326 case eFormatVectorOfUInt128:
1327 break;
1328
1329 case eFormatChar:
1330 case eFormatCharPrintable:
1331 case eFormatCharArray:
1332 case eFormatBytes:
1333 case eFormatBytesWithASCII:
1334 item_count = byte_size;
1335 byte_size = 1;
1336 break;
1337
1338 case eFormatUnicode16:
1339 item_count = byte_size / 2;
1340 byte_size = 2;
1341 break;
1342
1343 case eFormatUnicode32:
1344 item_count = byte_size / 4;
1345 byte_size = 4;
1346 break;
1347 }
1348 return data.Dump(s, byte_offset, format, byte_size, item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
1349 bitfield_bit_size, bitfield_bit_offset, exe_scope);
1350 }
1351 return 0;
1352}
1353
1354void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001355GoASTContext::DumpSummary(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s, const DataExtractor &data,
Ryan Brown57bee1e2015-09-14 22:45:11 +00001356 lldb::offset_t data_offset, size_t data_byte_size)
1357{
1358 assert(false);
1359}
1360
1361void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001362GoASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001363{
1364 assert(false);
1365} // Dump to stdout
1366
1367void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00001368GoASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type, Stream *s)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001369{
1370 assert(false);
1371}
1372
1373CompilerType
Bruce Mitchener500737e2015-09-15 04:33:48 +00001374GoASTContext::CreateArrayType(const ConstString &name, const CompilerType &element_type, uint64_t length)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001375{
1376 GoType *type = new GoArray(name, length, element_type);
1377 (*m_types)[name].reset(type);
1378 return CompilerType(this, type);
1379}
1380
1381CompilerType
1382GoASTContext::CreateBaseType(int go_kind, const lldb_private::ConstString &name, uint64_t byte_size)
1383{
1384 if (go_kind == GoType::KIND_UINT || go_kind == GoType::KIND_INT)
1385 m_int_byte_size = byte_size;
1386 GoType *type = new GoType(go_kind, name);
1387 (*m_types)[name].reset(type);
1388 return CompilerType(this, type);
1389}
1390
1391CompilerType
Greg Clayton56939cb2015-09-17 22:23:34 +00001392GoASTContext::CreateTypedefType(int kind, const ConstString &name, CompilerType impl)
Ryan Brown57bee1e2015-09-14 22:45:11 +00001393{
1394 GoType *type = new GoElem(kind, name, impl);
1395 (*m_types)[name].reset(type);
1396 return CompilerType(this, type);
1397}
1398
1399CompilerType
1400GoASTContext::CreateVoidType(const lldb_private::ConstString &name)
1401{
1402 GoType *type = new GoType(GoType::KIND_LLDB_VOID, name);
1403 (*m_types)[name].reset(type);
1404 return CompilerType(this, type);
1405}
1406
1407CompilerType
1408GoASTContext::CreateStructType(int kind, const lldb_private::ConstString &name, uint32_t byte_size)
1409{
1410 GoType *type = new GoStruct(kind, name, byte_size);
1411 (*m_types)[name].reset(type);
1412 return CompilerType(this, type);
1413}
1414
1415void
1416GoASTContext::AddFieldToStruct(const lldb_private::CompilerType &struct_type, const lldb_private::ConstString &name,
1417 const lldb_private::CompilerType &field_type, uint32_t byte_offset)
1418{
1419 if (!struct_type)
1420 return;
1421 GoASTContext *ast = llvm::dyn_cast_or_null<GoASTContext>(struct_type.GetTypeSystem());
1422 if (!ast)
1423 return;
1424 GoType *type = static_cast<GoType *>(struct_type.GetOpaqueQualType());
1425 if (GoStruct *s = type->GetStruct())
1426 s->AddField(name, field_type, byte_offset);
1427}
1428
1429void
1430GoASTContext::CompleteStructType(const lldb_private::CompilerType &struct_type)
1431{
1432 if (!struct_type)
1433 return;
1434 GoASTContext *ast = llvm::dyn_cast_or_null<GoASTContext>(struct_type.GetTypeSystem());
1435 if (!ast)
1436 return;
1437 GoType *type = static_cast<GoType *>(struct_type.GetOpaqueQualType());
1438 if (GoStruct *s = type->GetStruct())
1439 s->SetComplete();
1440}
1441
1442CompilerType
1443GoASTContext::CreateFunctionType(const lldb_private::ConstString &name, CompilerType *params, size_t params_count,
1444 bool is_variadic)
1445{
1446 GoType *type = new GoFunction(name, is_variadic);
1447 (*m_types)[name].reset(type);
1448 return CompilerType(this, type);
1449}
1450
1451bool
1452GoASTContext::IsGoString(const lldb_private::CompilerType &type)
1453{
1454 if (!type.IsValid() || !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1455 return false;
1456 return GoType::KIND_STRING == static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1457}
1458
1459bool
1460GoASTContext::IsGoSlice(const lldb_private::CompilerType &type)
1461{
1462 if (!type.IsValid() || !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1463 return false;
1464 return GoType::KIND_SLICE == static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1465}
1466
1467bool
1468GoASTContext::IsGoInterface(const lldb_private::CompilerType &type)
1469{
1470 if (!type.IsValid() || !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem()))
1471 return false;
1472 return GoType::KIND_INTERFACE == static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind();
1473}
1474
1475bool
1476GoASTContext::IsPointerKind(uint8_t kind)
1477{
1478 return (kind & GoType::KIND_MASK) == GoType::KIND_PTR;
1479}
1480
1481bool
1482GoASTContext::IsDirectIface(uint8_t kind)
1483{
1484 return (kind & GoType::KIND_DIRECT_IFACE) == GoType::KIND_DIRECT_IFACE;
1485}
1486
1487DWARFASTParser *
1488GoASTContext::GetDWARFParser()
1489{
1490 if (!m_dwarf_ast_parser_ap)
1491 m_dwarf_ast_parser_ap.reset(new DWARFASTParserGo(*this));
1492 return m_dwarf_ast_parser_ap.get();
1493}