Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1 | //===-- 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 Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Module.h" |
| 15 | #include "lldb/Core/PluginManager.h" |
Ryan Brown | 47d072ca | 2016-01-15 19:35:48 +0000 | [diff] [blame^] | 16 | #include "lldb/Core/StreamFile.h" |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 17 | #include "lldb/Core/UniqueCStringMap.h" |
| 18 | #include "lldb/Core/ValueObject.h" |
| 19 | #include "lldb/DataFormatters/StringPrinter.h" |
| 20 | #include "lldb/Symbol/CompilerType.h" |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 21 | #include "lldb/Symbol/ObjectFile.h" |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 22 | #include "lldb/Symbol/SymbolFile.h" |
| 23 | #include "lldb/Symbol/GoASTContext.h" |
| 24 | #include "lldb/Symbol/Type.h" |
| 25 | #include "lldb/Target/ExecutionContext.h" |
Greg Clayton | 5beec21 | 2015-10-08 21:04:34 +0000 | [diff] [blame] | 26 | #include "lldb/Target/Target.h" |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 27 | |
Ryan Brown | 998c8a1c1 | 2015-11-02 19:30:40 +0000 | [diff] [blame] | 28 | #include "Plugins/ExpressionParser/Go/GoUserExpression.h" |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 29 | #include "Plugins/SymbolFile/DWARF/DWARFASTParserGo.h" |
| 30 | |
| 31 | using namespace lldb; |
| 32 | |
| 33 | namespace lldb_private |
| 34 | { |
| 35 | class GoArray; |
| 36 | class GoFunction; |
| 37 | class GoStruct; |
| 38 | |
| 39 | class GoType |
| 40 | { |
| 41 | public: |
| 42 | enum |
| 43 | { |
| 44 | KIND_BOOL = 1, |
| 45 | KIND_INT = 2, |
| 46 | KIND_INT8 = 3, |
| 47 | KIND_INT16 = 4, |
| 48 | KIND_INT32 = 5, |
| 49 | KIND_INT64 = 6, |
| 50 | KIND_UINT = 7, |
| 51 | KIND_UINT8 = 8, |
| 52 | KIND_UINT16 = 9, |
| 53 | KIND_UINT32 = 10, |
| 54 | KIND_UINT64 = 11, |
| 55 | KIND_UINTPTR = 12, |
| 56 | KIND_FLOAT32 = 13, |
| 57 | KIND_FLOAT64 = 14, |
| 58 | KIND_COMPLEX64 = 15, |
| 59 | KIND_COMPLEX128 = 16, |
| 60 | KIND_ARRAY = 17, |
| 61 | KIND_CHAN = 18, |
| 62 | KIND_FUNC = 19, |
| 63 | KIND_INTERFACE = 20, |
| 64 | KIND_MAP = 21, |
| 65 | KIND_PTR = 22, |
| 66 | KIND_SLICE = 23, |
| 67 | KIND_STRING = 24, |
| 68 | KIND_STRUCT = 25, |
| 69 | KIND_UNSAFEPOINTER = 26, |
| 70 | KIND_LLDB_VOID, // Extension for LLDB, not used by go runtime. |
| 71 | KIND_MASK = (1 << 5) - 1, |
| 72 | KIND_DIRECT_IFACE = 1 << 5 |
| 73 | }; |
| 74 | GoType(int kind, const ConstString &name) |
| 75 | : m_kind(kind & KIND_MASK) |
| 76 | , m_name(name) |
| 77 | { |
| 78 | if (m_kind == KIND_FUNC) |
| 79 | m_kind = KIND_FUNC; |
| 80 | } |
| 81 | virtual ~GoType() {} |
| 82 | |
| 83 | int |
| 84 | GetGoKind() const |
| 85 | { |
| 86 | return m_kind; |
| 87 | } |
| 88 | const ConstString & |
| 89 | GetName() const |
| 90 | { |
| 91 | return m_name; |
| 92 | } |
| 93 | virtual CompilerType |
| 94 | GetElementType() const |
| 95 | { |
| 96 | return CompilerType(); |
| 97 | } |
| 98 | |
| 99 | bool |
| 100 | IsTypedef() const |
| 101 | { |
| 102 | switch (m_kind) |
| 103 | { |
| 104 | case KIND_CHAN: |
| 105 | case KIND_MAP: |
| 106 | case KIND_INTERFACE: |
| 107 | return true; |
| 108 | default: |
| 109 | return false; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | GoArray *GetArray(); |
| 114 | GoFunction *GetFunction(); |
| 115 | GoStruct *GetStruct(); |
| 116 | |
| 117 | private: |
| 118 | int m_kind; |
| 119 | ConstString m_name; |
| 120 | GoType(const GoType &) = delete; |
| 121 | const GoType &operator=(const GoType &) = delete; |
| 122 | }; |
| 123 | |
| 124 | class GoElem : public GoType |
| 125 | { |
| 126 | public: |
| 127 | GoElem(int kind, const ConstString &name, const CompilerType &elem) |
| 128 | : GoType(kind, name) |
| 129 | , m_elem(elem) |
| 130 | { |
| 131 | } |
| 132 | virtual CompilerType |
| 133 | GetElementType() const |
| 134 | { |
| 135 | return m_elem; |
| 136 | } |
| 137 | |
| 138 | private: |
| 139 | // TODO: should we store this differently? |
| 140 | CompilerType m_elem; |
| 141 | |
| 142 | GoElem(const GoElem &) = delete; |
| 143 | const GoElem &operator=(const GoElem &) = delete; |
| 144 | }; |
| 145 | |
| 146 | class GoArray : public GoElem |
| 147 | { |
| 148 | public: |
Bruce Mitchener | 500737e | 2015-09-15 04:33:48 +0000 | [diff] [blame] | 149 | GoArray(const ConstString &name, uint64_t length, const CompilerType &elem) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 150 | : GoElem(KIND_ARRAY, name, elem) |
| 151 | , m_length(length) |
| 152 | { |
| 153 | } |
| 154 | |
Bruce Mitchener | 500737e | 2015-09-15 04:33:48 +0000 | [diff] [blame] | 155 | uint64_t |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 156 | GetLength() const |
| 157 | { |
| 158 | return m_length; |
| 159 | } |
| 160 | |
| 161 | private: |
Bruce Mitchener | 500737e | 2015-09-15 04:33:48 +0000 | [diff] [blame] | 162 | uint64_t m_length; |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 163 | GoArray(const GoArray &) = delete; |
| 164 | const GoArray &operator=(const GoArray &) = delete; |
| 165 | }; |
| 166 | |
| 167 | class GoFunction : public GoType |
| 168 | { |
| 169 | public: |
| 170 | GoFunction(const ConstString &name, bool is_variadic) |
| 171 | : GoType(KIND_FUNC, name) |
| 172 | , m_is_variadic(is_variadic) |
| 173 | { |
| 174 | } |
| 175 | |
| 176 | bool |
| 177 | IsVariadic() const |
| 178 | { |
| 179 | return m_is_variadic; |
| 180 | } |
| 181 | |
| 182 | private: |
| 183 | bool m_is_variadic; |
| 184 | GoFunction(const GoFunction &) = delete; |
| 185 | const GoFunction &operator=(const GoFunction &) = delete; |
| 186 | }; |
| 187 | |
| 188 | class GoStruct : public GoType |
| 189 | { |
| 190 | public: |
| 191 | struct Field |
| 192 | { |
| 193 | Field(const ConstString &name, const CompilerType &type, uint64_t offset) |
| 194 | : m_name(name) |
| 195 | , m_type(type) |
| 196 | , m_byte_offset(offset) |
| 197 | { |
| 198 | } |
| 199 | ConstString m_name; |
| 200 | CompilerType m_type; |
| 201 | uint64_t m_byte_offset; |
| 202 | }; |
| 203 | |
| 204 | GoStruct(int kind, const ConstString &name, int64_t byte_size) |
Ryan Brown | 998c8a1c1 | 2015-11-02 19:30:40 +0000 | [diff] [blame] | 205 | : GoType(kind == 0 ? KIND_STRUCT : kind, name), m_is_complete(false), m_byte_size(byte_size) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 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 | |
| 256 | GoArray * |
| 257 | GoType::GetArray() |
| 258 | { |
| 259 | if (m_kind == KIND_ARRAY) |
| 260 | { |
| 261 | return static_cast<GoArray *>(this); |
| 262 | } |
| 263 | return nullptr; |
| 264 | } |
| 265 | |
| 266 | GoFunction * |
| 267 | GoType::GetFunction() |
| 268 | { |
| 269 | if (m_kind == KIND_FUNC) |
| 270 | { |
| 271 | return static_cast<GoFunction *>(this); |
| 272 | } |
| 273 | return nullptr; |
| 274 | } |
| 275 | |
| 276 | GoStruct * |
| 277 | GoType::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 |
| 289 | using namespace lldb_private; |
| 290 | |
| 291 | GoASTContext::GoASTContext() |
| 292 | : TypeSystem(eKindGo) |
| 293 | , m_pointer_byte_size(0) |
| 294 | , m_int_byte_size(0) |
| 295 | , m_types(new TypeMap) |
| 296 | { |
| 297 | } |
| 298 | GoASTContext::~GoASTContext() |
| 299 | { |
| 300 | } |
| 301 | |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 302 | //------------------------------------------------------------------ |
| 303 | // PluginInterface functions |
| 304 | //------------------------------------------------------------------ |
| 305 | |
| 306 | ConstString |
| 307 | GoASTContext::GetPluginNameStatic() |
| 308 | { |
| 309 | return ConstString("go"); |
| 310 | } |
| 311 | |
| 312 | ConstString |
| 313 | GoASTContext::GetPluginName() |
| 314 | { |
| 315 | return GoASTContext::GetPluginNameStatic(); |
| 316 | } |
| 317 | |
| 318 | uint32_t |
| 319 | GoASTContext::GetPluginVersion() |
| 320 | { |
| 321 | return 1; |
| 322 | } |
| 323 | |
| 324 | lldb::TypeSystemSP |
Greg Clayton | 5beec21 | 2015-10-08 21:04:34 +0000 | [diff] [blame] | 325 | GoASTContext::CreateInstance (lldb::LanguageType language, Module *module, Target *target) |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 326 | { |
| 327 | if (language == eLanguageTypeGo) |
| 328 | { |
Greg Clayton | 5beec21 | 2015-10-08 21:04:34 +0000 | [diff] [blame] | 329 | ArchSpec arch; |
Ryan Brown | 998c8a1c1 | 2015-11-02 19:30:40 +0000 | [diff] [blame] | 330 | std::shared_ptr<GoASTContext> go_ast_sp; |
Greg Clayton | 5beec21 | 2015-10-08 21:04:34 +0000 | [diff] [blame] | 331 | if (module) |
Ryan Brown | 998c8a1c1 | 2015-11-02 19:30:40 +0000 | [diff] [blame] | 332 | { |
Greg Clayton | 5beec21 | 2015-10-08 21:04:34 +0000 | [diff] [blame] | 333 | arch = module->GetArchitecture(); |
Ryan Brown | 998c8a1c1 | 2015-11-02 19:30:40 +0000 | [diff] [blame] | 334 | go_ast_sp = std::shared_ptr<GoASTContext>(new GoASTContext); |
| 335 | } |
Greg Clayton | 5beec21 | 2015-10-08 21:04:34 +0000 | [diff] [blame] | 336 | else if (target) |
Ryan Brown | 998c8a1c1 | 2015-11-02 19:30:40 +0000 | [diff] [blame] | 337 | { |
Greg Clayton | 5beec21 | 2015-10-08 21:04:34 +0000 | [diff] [blame] | 338 | arch = target->GetArchitecture(); |
Ryan Brown | 998c8a1c1 | 2015-11-02 19:30:40 +0000 | [diff] [blame] | 339 | go_ast_sp = std::shared_ptr<GoASTContextForExpr>(new GoASTContextForExpr(target->shared_from_this())); |
| 340 | } |
Greg Clayton | 5beec21 | 2015-10-08 21:04:34 +0000 | [diff] [blame] | 341 | |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 342 | if (arch.IsValid()) |
| 343 | { |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 344 | go_ast_sp->SetAddressByteSize(arch.GetAddressByteSize()); |
| 345 | return go_ast_sp; |
| 346 | } |
| 347 | } |
| 348 | return lldb::TypeSystemSP(); |
| 349 | } |
| 350 | |
Sean Callanan | fe38c85 | 2015-10-08 23:07:53 +0000 | [diff] [blame] | 351 | void |
| 352 | GoASTContext::EnumerateSupportedLanguages(std::set<lldb::LanguageType> &languages_for_types, std::set<lldb::LanguageType> &languages_for_expressions) |
| 353 | { |
| 354 | static std::vector<lldb::LanguageType> s_supported_languages_for_types({ |
| 355 | lldb::eLanguageTypeGo}); |
| 356 | |
| 357 | static std::vector<lldb::LanguageType> s_supported_languages_for_expressions({}); |
| 358 | |
| 359 | languages_for_types.insert(s_supported_languages_for_types.begin(), s_supported_languages_for_types.end()); |
| 360 | languages_for_expressions.insert(s_supported_languages_for_expressions.begin(), s_supported_languages_for_expressions.end()); |
| 361 | } |
| 362 | |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 363 | |
| 364 | void |
| 365 | GoASTContext::Initialize() |
| 366 | { |
| 367 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 368 | "AST context plug-in", |
Sean Callanan | fe38c85 | 2015-10-08 23:07:53 +0000 | [diff] [blame] | 369 | CreateInstance, |
| 370 | EnumerateSupportedLanguages); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | void |
| 374 | GoASTContext::Terminate() |
| 375 | { |
| 376 | PluginManager::UnregisterPlugin (CreateInstance); |
| 377 | } |
| 378 | |
| 379 | |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 380 | //---------------------------------------------------------------------- |
| 381 | // Tests |
| 382 | //---------------------------------------------------------------------- |
| 383 | |
| 384 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 385 | GoASTContext::IsArrayType(lldb::opaque_compiler_type_t type, CompilerType *element_type, uint64_t *size, bool *is_incomplete) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 386 | { |
| 387 | if (element_type) |
| 388 | element_type->Clear(); |
| 389 | if (size) |
| 390 | *size = 0; |
| 391 | if (is_incomplete) |
| 392 | *is_incomplete = false; |
| 393 | GoArray *array = static_cast<GoType *>(type)->GetArray(); |
| 394 | if (array) |
| 395 | { |
Bruce Mitchener | 500737e | 2015-09-15 04:33:48 +0000 | [diff] [blame] | 396 | if (size) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 397 | *size = array->GetLength(); |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 398 | if (element_type) |
| 399 | *element_type = array->GetElementType(); |
| 400 | return true; |
| 401 | } |
| 402 | return false; |
| 403 | } |
| 404 | |
| 405 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 406 | GoASTContext::IsVectorType(lldb::opaque_compiler_type_t type, CompilerType *element_type, uint64_t *size) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 407 | { |
| 408 | if (element_type) |
| 409 | element_type->Clear(); |
| 410 | if (size) |
| 411 | *size = 0; |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 416 | GoASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 417 | { |
| 418 | int kind = static_cast<GoType *>(type)->GetGoKind(); |
| 419 | if (kind < GoType::KIND_ARRAY) |
| 420 | return false; |
| 421 | if (kind == GoType::KIND_PTR) |
| 422 | return false; |
Ryan Brown | 998c8a1c1 | 2015-11-02 19:30:40 +0000 | [diff] [blame] | 423 | if (kind == GoType::KIND_CHAN) |
| 424 | return false; |
| 425 | if (kind == GoType::KIND_MAP) |
| 426 | return false; |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 427 | if (kind == GoType::KIND_STRING) |
| 428 | return false; |
| 429 | if (kind == GoType::KIND_UNSAFEPOINTER) |
| 430 | return false; |
| 431 | return true; |
| 432 | } |
| 433 | |
| 434 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 435 | GoASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 436 | { |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 441 | GoASTContext::IsCharType(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 442 | { |
| 443 | // Go's DWARF doesn't distinguish between rune and int32. |
| 444 | return false; |
| 445 | } |
| 446 | |
| 447 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 448 | GoASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 449 | { |
| 450 | if (!type) |
| 451 | return false; |
| 452 | GoType *t = static_cast<GoType *>(type); |
| 453 | if (GoStruct *s = t->GetStruct()) |
| 454 | return s->IsComplete(); |
| 455 | if (t->IsTypedef() || t->GetGoKind() == GoType::KIND_PTR) |
| 456 | return t->GetElementType().IsCompleteType(); |
| 457 | return true; |
| 458 | } |
| 459 | |
| 460 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 461 | GoASTContext::IsConst(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 462 | { |
| 463 | return false; |
| 464 | } |
| 465 | |
| 466 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 467 | GoASTContext::IsCStringType(lldb::opaque_compiler_type_t type, uint32_t &length) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 468 | { |
| 469 | return false; |
| 470 | } |
| 471 | |
| 472 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 473 | GoASTContext::IsDefined(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 474 | { |
| 475 | return type != nullptr; |
| 476 | } |
| 477 | |
| 478 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 479 | GoASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type, uint32_t &count, bool &is_complex) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 480 | { |
| 481 | int kind = static_cast<GoType *>(type)->GetGoKind(); |
| 482 | if (kind >= GoType::KIND_FLOAT32 && kind <= GoType::KIND_COMPLEX128) |
| 483 | { |
| 484 | if (kind >= GoType::KIND_COMPLEX64) |
| 485 | { |
| 486 | is_complex = true; |
| 487 | count = 2; |
| 488 | } |
| 489 | else |
| 490 | { |
| 491 | is_complex = false; |
| 492 | count = 1; |
| 493 | } |
| 494 | return true; |
| 495 | } |
| 496 | count = 0; |
| 497 | is_complex = false; |
| 498 | return false; |
| 499 | } |
| 500 | |
| 501 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 502 | GoASTContext::IsFunctionType(lldb::opaque_compiler_type_t type, bool *is_variadic_ptr) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 503 | { |
| 504 | GoFunction *func = static_cast<GoType *>(type)->GetFunction(); |
| 505 | if (func) |
| 506 | { |
| 507 | if (is_variadic_ptr) |
| 508 | *is_variadic_ptr = func->IsVariadic(); |
| 509 | return true; |
| 510 | } |
| 511 | if (is_variadic_ptr) |
| 512 | *is_variadic_ptr = false; |
| 513 | return false; |
| 514 | } |
| 515 | |
| 516 | uint32_t |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 517 | GoASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type, CompilerType *base_type_ptr) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 518 | { |
| 519 | return false; |
| 520 | } |
| 521 | |
| 522 | size_t |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 523 | GoASTContext::GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 524 | { |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | CompilerType |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 529 | GoASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type, const size_t index) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 530 | { |
| 531 | return CompilerType(); |
| 532 | } |
| 533 | |
| 534 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 535 | GoASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 536 | { |
| 537 | return IsFunctionType(type); |
| 538 | } |
| 539 | |
| 540 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 541 | GoASTContext::IsIntegerType(lldb::opaque_compiler_type_t type, bool &is_signed) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 542 | { |
| 543 | is_signed = false; |
| 544 | // TODO: Is bool an integer? |
| 545 | if (type) |
| 546 | { |
| 547 | int kind = static_cast<GoType *>(type)->GetGoKind(); |
| 548 | if (kind <= GoType::KIND_UINTPTR) |
| 549 | { |
| 550 | is_signed = (kind != GoType::KIND_BOOL) & (kind <= GoType::KIND_INT64); |
| 551 | return true; |
| 552 | } |
| 553 | } |
| 554 | return false; |
| 555 | } |
| 556 | |
| 557 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 558 | GoASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 559 | { |
| 560 | return false; |
| 561 | } |
| 562 | |
| 563 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 564 | GoASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type, |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 565 | CompilerType *target_type, // Can pass NULL |
| 566 | bool check_cplusplus, bool check_objc) |
| 567 | { |
| 568 | if (target_type) |
| 569 | target_type->Clear(); |
| 570 | if (type) |
| 571 | return static_cast<GoType *>(type)->GetGoKind() == GoType::KIND_INTERFACE; |
| 572 | return false; |
| 573 | } |
| 574 | |
| 575 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 576 | GoASTContext::IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 577 | { |
| 578 | return false; |
| 579 | } |
| 580 | |
| 581 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 582 | GoASTContext::IsPointerType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 583 | { |
| 584 | if (!type) |
| 585 | return false; |
| 586 | GoType *t = static_cast<GoType *>(type); |
| 587 | if (pointee_type) |
| 588 | { |
| 589 | *pointee_type = t->GetElementType(); |
| 590 | } |
| 591 | switch (t->GetGoKind()) |
| 592 | { |
| 593 | case GoType::KIND_PTR: |
| 594 | case GoType::KIND_UNSAFEPOINTER: |
| 595 | case GoType::KIND_CHAN: |
Ryan Brown | 998c8a1c1 | 2015-11-02 19:30:40 +0000 | [diff] [blame] | 596 | case GoType::KIND_MAP: |
| 597 | // TODO: is function a pointer? |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 598 | return true; |
| 599 | default: |
| 600 | return false; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 605 | GoASTContext::IsPointerOrReferenceType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 606 | { |
| 607 | return IsPointerType(type, pointee_type); |
| 608 | } |
| 609 | |
| 610 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 611 | GoASTContext::IsReferenceType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type, bool *is_rvalue) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 612 | { |
| 613 | return false; |
| 614 | } |
| 615 | |
| 616 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 617 | GoASTContext::IsScalarType(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 618 | { |
| 619 | return !IsAggregateType(type); |
| 620 | } |
| 621 | |
| 622 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 623 | GoASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 624 | { |
| 625 | if (type) |
| 626 | return static_cast<GoType *>(type)->IsTypedef(); |
| 627 | return false; |
| 628 | } |
| 629 | |
| 630 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 631 | GoASTContext::IsVoidType(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 632 | { |
| 633 | if (!type) |
| 634 | return false; |
| 635 | return static_cast<GoType *>(type)->GetGoKind() == GoType::KIND_LLDB_VOID; |
| 636 | } |
| 637 | |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 638 | bool |
| 639 | GoASTContext::SupportsLanguage (lldb::LanguageType language) |
| 640 | { |
| 641 | return language == eLanguageTypeGo; |
| 642 | } |
| 643 | |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 644 | //---------------------------------------------------------------------- |
| 645 | // Type Completion |
| 646 | //---------------------------------------------------------------------- |
| 647 | |
| 648 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 649 | GoASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 650 | { |
| 651 | if (!type) |
| 652 | return false; |
| 653 | GoType *t = static_cast<GoType *>(type); |
| 654 | if (t->IsTypedef() || t->GetGoKind() == GoType::KIND_PTR || t->GetArray()) |
| 655 | return t->GetElementType().GetCompleteType(); |
| 656 | if (GoStruct *s = t->GetStruct()) |
| 657 | { |
| 658 | if (s->IsComplete()) |
| 659 | return true; |
| 660 | CompilerType compiler_type(this, s); |
| 661 | SymbolFile *symbols = GetSymbolFile(); |
| 662 | return symbols && symbols->CompleteType(compiler_type); |
| 663 | } |
| 664 | return true; |
| 665 | } |
| 666 | |
| 667 | //---------------------------------------------------------------------- |
| 668 | // AST related queries |
| 669 | //---------------------------------------------------------------------- |
| 670 | |
| 671 | uint32_t |
| 672 | GoASTContext::GetPointerByteSize() |
| 673 | { |
| 674 | return m_pointer_byte_size; |
| 675 | } |
| 676 | |
| 677 | //---------------------------------------------------------------------- |
| 678 | // Accessors |
| 679 | //---------------------------------------------------------------------- |
| 680 | |
| 681 | ConstString |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 682 | GoASTContext::GetTypeName(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 683 | { |
| 684 | if (type) |
| 685 | return static_cast<GoType *>(type)->GetName(); |
| 686 | return ConstString(); |
| 687 | } |
| 688 | |
| 689 | uint32_t |
Bruce Mitchener | 3ad353f | 2015-09-24 03:54:50 +0000 | [diff] [blame] | 690 | GoASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type, CompilerType *pointee_or_element_compiler_type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 691 | { |
Bruce Mitchener | 3ad353f | 2015-09-24 03:54:50 +0000 | [diff] [blame] | 692 | if (pointee_or_element_compiler_type) |
| 693 | pointee_or_element_compiler_type->Clear(); |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 694 | if (!type) |
| 695 | return 0; |
| 696 | GoType *t = static_cast<GoType *>(type); |
Bruce Mitchener | 3ad353f | 2015-09-24 03:54:50 +0000 | [diff] [blame] | 697 | if (pointee_or_element_compiler_type) |
| 698 | *pointee_or_element_compiler_type = t->GetElementType(); |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 699 | int kind = t->GetGoKind(); |
| 700 | if (kind == GoType::KIND_ARRAY) |
| 701 | return eTypeHasChildren | eTypeIsArray; |
| 702 | if (kind < GoType::KIND_ARRAY) |
| 703 | { |
| 704 | uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue; |
| 705 | if (kind < GoType::KIND_FLOAT32) |
| 706 | { |
| 707 | builtin_type_flags |= eTypeIsInteger | eTypeIsScalar; |
| 708 | if (kind >= GoType::KIND_INT && kind <= GoType::KIND_INT64) |
| 709 | builtin_type_flags |= eTypeIsSigned; |
| 710 | } |
| 711 | else |
| 712 | { |
| 713 | builtin_type_flags |= eTypeIsFloat; |
| 714 | if (kind < GoType::KIND_COMPLEX64) |
| 715 | builtin_type_flags |= eTypeIsComplex; |
| 716 | else |
| 717 | builtin_type_flags |= eTypeIsScalar; |
| 718 | } |
| 719 | return builtin_type_flags; |
| 720 | } |
| 721 | if (kind == GoType::KIND_STRING) |
| 722 | return eTypeHasValue | eTypeIsBuiltIn; |
| 723 | if (kind == GoType::KIND_FUNC) |
| 724 | return eTypeIsFuncPrototype | eTypeHasValue; |
| 725 | if (IsPointerType(type)) |
| 726 | return eTypeIsPointer | eTypeHasValue | eTypeHasChildren; |
| 727 | if (kind == GoType::KIND_LLDB_VOID) |
| 728 | return 0; |
| 729 | return eTypeHasChildren | eTypeIsStructUnion; |
| 730 | } |
| 731 | |
| 732 | lldb::TypeClass |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 733 | GoASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 734 | { |
| 735 | if (!type) |
| 736 | return eTypeClassInvalid; |
| 737 | int kind = static_cast<GoType *>(type)->GetGoKind(); |
| 738 | if (kind == GoType::KIND_FUNC) |
| 739 | return eTypeClassFunction; |
| 740 | if (IsPointerType(type)) |
| 741 | return eTypeClassPointer; |
| 742 | if (kind < GoType::KIND_COMPLEX64) |
| 743 | return eTypeClassBuiltin; |
| 744 | if (kind <= GoType::KIND_COMPLEX128) |
| 745 | return eTypeClassComplexFloat; |
| 746 | if (kind == GoType::KIND_LLDB_VOID) |
| 747 | return eTypeClassInvalid; |
| 748 | return eTypeClassStruct; |
| 749 | } |
| 750 | |
| 751 | lldb::BasicType |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 752 | GoASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 753 | { |
| 754 | ConstString name = GetTypeName(type); |
| 755 | if (name) |
| 756 | { |
| 757 | typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap; |
| 758 | static TypeNameToBasicTypeMap g_type_map; |
| 759 | static std::once_flag g_once_flag; |
| 760 | std::call_once(g_once_flag, [](){ |
| 761 | // "void" |
| 762 | g_type_map.Append(ConstString("void").GetCString(), eBasicTypeVoid); |
| 763 | // "int" |
| 764 | g_type_map.Append(ConstString("int").GetCString(), eBasicTypeInt); |
| 765 | g_type_map.Append(ConstString("uint").GetCString(), eBasicTypeUnsignedInt); |
| 766 | |
| 767 | // Miscellaneous |
| 768 | g_type_map.Append(ConstString("bool").GetCString(), eBasicTypeBool); |
| 769 | |
| 770 | // Others. Should these map to C types? |
| 771 | g_type_map.Append(ConstString("byte").GetCString(), eBasicTypeOther); |
| 772 | g_type_map.Append(ConstString("uint8").GetCString(), eBasicTypeOther); |
| 773 | g_type_map.Append(ConstString("uint16").GetCString(), eBasicTypeOther); |
| 774 | g_type_map.Append(ConstString("uint32").GetCString(), eBasicTypeOther); |
| 775 | g_type_map.Append(ConstString("uint64").GetCString(), eBasicTypeOther); |
| 776 | g_type_map.Append(ConstString("int8").GetCString(), eBasicTypeOther); |
| 777 | g_type_map.Append(ConstString("int16").GetCString(), eBasicTypeOther); |
| 778 | g_type_map.Append(ConstString("int32").GetCString(), eBasicTypeOther); |
| 779 | g_type_map.Append(ConstString("int64").GetCString(), eBasicTypeOther); |
| 780 | g_type_map.Append(ConstString("float32").GetCString(), eBasicTypeOther); |
| 781 | g_type_map.Append(ConstString("float64").GetCString(), eBasicTypeOther); |
| 782 | g_type_map.Append(ConstString("uintptr").GetCString(), eBasicTypeOther); |
| 783 | |
| 784 | g_type_map.Sort(); |
| 785 | }); |
| 786 | |
| 787 | return g_type_map.Find(name.GetCString(), eBasicTypeInvalid); |
| 788 | } |
| 789 | return eBasicTypeInvalid; |
| 790 | } |
| 791 | |
| 792 | lldb::LanguageType |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 793 | GoASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 794 | { |
| 795 | return lldb::eLanguageTypeGo; |
| 796 | } |
| 797 | |
| 798 | unsigned |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 799 | GoASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 800 | { |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | //---------------------------------------------------------------------- |
| 805 | // Creating related types |
| 806 | //---------------------------------------------------------------------- |
| 807 | |
| 808 | CompilerType |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 809 | GoASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type, uint64_t *stride) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 810 | { |
| 811 | GoArray *array = static_cast<GoType *>(type)->GetArray(); |
| 812 | if (array) |
| 813 | { |
| 814 | if (stride) |
| 815 | { |
| 816 | *stride = array->GetElementType().GetByteSize(nullptr); |
| 817 | } |
| 818 | return array->GetElementType(); |
| 819 | } |
| 820 | return CompilerType(); |
| 821 | } |
| 822 | |
| 823 | CompilerType |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 824 | GoASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 825 | { |
| 826 | GoType *t = static_cast<GoType *>(type); |
| 827 | if (t->IsTypedef()) |
| 828 | return t->GetElementType(); |
| 829 | return CompilerType(this, type); |
| 830 | } |
| 831 | |
| 832 | CompilerType |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 833 | GoASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 834 | { |
| 835 | return CompilerType(this, type); |
| 836 | } |
| 837 | |
| 838 | // Returns -1 if this isn't a function of if the function doesn't have a prototype |
| 839 | // Returns a value >= 0 if there is a prototype. |
| 840 | int |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 841 | GoASTContext::GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 842 | { |
| 843 | return GetNumberOfFunctionArguments(type); |
| 844 | } |
| 845 | |
| 846 | CompilerType |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 847 | GoASTContext::GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type, size_t idx) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 848 | { |
| 849 | return GetFunctionArgumentAtIndex(type, idx); |
| 850 | } |
| 851 | |
| 852 | CompilerType |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 853 | GoASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 854 | { |
| 855 | CompilerType result; |
| 856 | if (type) |
| 857 | { |
| 858 | GoType *t = static_cast<GoType *>(type); |
| 859 | if (t->GetGoKind() == GoType::KIND_FUNC) |
| 860 | result = t->GetElementType(); |
| 861 | } |
| 862 | return result; |
| 863 | } |
| 864 | |
| 865 | size_t |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 866 | GoASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 867 | { |
| 868 | return 0; |
| 869 | } |
| 870 | |
| 871 | TypeMemberFunctionImpl |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 872 | GoASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, size_t idx) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 873 | { |
| 874 | return TypeMemberFunctionImpl(); |
| 875 | } |
| 876 | |
| 877 | CompilerType |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 878 | GoASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 879 | { |
| 880 | return CompilerType(this, type); |
| 881 | } |
| 882 | |
| 883 | CompilerType |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 884 | GoASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 885 | { |
| 886 | if (!type) |
| 887 | return CompilerType(); |
| 888 | return static_cast<GoType *>(type)->GetElementType(); |
| 889 | } |
| 890 | |
| 891 | CompilerType |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 892 | GoASTContext::GetPointerType(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 893 | { |
| 894 | if (!type) |
| 895 | return CompilerType(); |
| 896 | ConstString type_name = GetTypeName(type); |
| 897 | ConstString pointer_name(std::string("*") + type_name.GetCString()); |
| 898 | GoType *pointer = (*m_types)[pointer_name].get(); |
| 899 | if (pointer == nullptr) |
| 900 | { |
| 901 | pointer = new GoElem(GoType::KIND_PTR, pointer_name, CompilerType(this, type)); |
| 902 | (*m_types)[pointer_name].reset(pointer); |
| 903 | } |
| 904 | return CompilerType(this, pointer); |
| 905 | } |
| 906 | |
| 907 | // If the current object represents a typedef type, get the underlying type |
| 908 | CompilerType |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 909 | GoASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 910 | { |
| 911 | if (IsTypedefType(type)) |
| 912 | return static_cast<GoType *>(type)->GetElementType(); |
| 913 | return CompilerType(); |
| 914 | } |
| 915 | |
| 916 | //---------------------------------------------------------------------- |
| 917 | // Create related types using the current type's AST |
| 918 | //---------------------------------------------------------------------- |
| 919 | CompilerType |
| 920 | GoASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) |
| 921 | { |
| 922 | return CompilerType(); |
| 923 | } |
| 924 | |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 925 | CompilerType |
| 926 | GoASTContext::GetBuiltinTypeForEncodingAndBitSize (lldb::Encoding encoding, |
| 927 | size_t bit_size) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 928 | { |
| 929 | return CompilerType(); |
| 930 | } |
| 931 | |
| 932 | |
| 933 | //---------------------------------------------------------------------- |
| 934 | // Exploring the type |
| 935 | //---------------------------------------------------------------------- |
| 936 | |
| 937 | uint64_t |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 938 | GoASTContext::GetBitSize(lldb::opaque_compiler_type_t type, ExecutionContextScope *exe_scope) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 939 | { |
| 940 | if (!type) |
| 941 | return 0; |
| 942 | if (!GetCompleteType(type)) |
| 943 | return 0; |
| 944 | GoType *t = static_cast<GoType *>(type); |
| 945 | GoArray *array = nullptr; |
| 946 | switch (t->GetGoKind()) |
| 947 | { |
| 948 | case GoType::KIND_BOOL: |
| 949 | case GoType::KIND_INT8: |
| 950 | case GoType::KIND_UINT8: |
| 951 | return 8; |
| 952 | case GoType::KIND_INT16: |
| 953 | case GoType::KIND_UINT16: |
| 954 | return 16; |
| 955 | case GoType::KIND_INT32: |
| 956 | case GoType::KIND_UINT32: |
| 957 | case GoType::KIND_FLOAT32: |
| 958 | return 32; |
| 959 | case GoType::KIND_INT64: |
| 960 | case GoType::KIND_UINT64: |
| 961 | case GoType::KIND_FLOAT64: |
| 962 | case GoType::KIND_COMPLEX64: |
| 963 | return 64; |
| 964 | case GoType::KIND_COMPLEX128: |
| 965 | return 128; |
| 966 | case GoType::KIND_INT: |
| 967 | case GoType::KIND_UINT: |
| 968 | return m_int_byte_size * 8; |
| 969 | case GoType::KIND_UINTPTR: |
| 970 | case GoType::KIND_FUNC: // I assume this is a pointer? |
| 971 | case GoType::KIND_CHAN: |
| 972 | case GoType::KIND_PTR: |
| 973 | case GoType::KIND_UNSAFEPOINTER: |
| 974 | case GoType::KIND_MAP: |
| 975 | return m_pointer_byte_size * 8; |
| 976 | case GoType::KIND_ARRAY: |
| 977 | array = t->GetArray(); |
| 978 | return array->GetLength() * array->GetElementType().GetBitSize(exe_scope); |
| 979 | case GoType::KIND_INTERFACE: |
| 980 | return t->GetElementType().GetBitSize(exe_scope); |
| 981 | case GoType::KIND_SLICE: |
| 982 | case GoType::KIND_STRING: |
| 983 | case GoType::KIND_STRUCT: |
| 984 | return t->GetStruct()->GetByteSize() * 8; |
| 985 | default: |
| 986 | assert(false); |
| 987 | } |
Ryan Brown | d03c2e0 | 2015-09-15 00:50:43 +0000 | [diff] [blame] | 988 | return 0; |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | lldb::Encoding |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 992 | GoASTContext::GetEncoding(lldb::opaque_compiler_type_t type, uint64_t &count) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 993 | { |
| 994 | count = 1; |
| 995 | bool is_signed; |
| 996 | if (IsIntegerType(type, is_signed)) |
| 997 | return is_signed ? lldb::eEncodingSint : eEncodingUint; |
| 998 | bool is_complex; |
| 999 | uint32_t complex_count; |
| 1000 | if (IsFloatingPointType(type, complex_count, is_complex)) |
| 1001 | { |
| 1002 | count = complex_count; |
| 1003 | return eEncodingIEEE754; |
| 1004 | } |
| 1005 | if (IsPointerType(type)) |
| 1006 | return eEncodingUint; |
| 1007 | return eEncodingInvalid; |
| 1008 | } |
| 1009 | |
| 1010 | lldb::Format |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 1011 | GoASTContext::GetFormat(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1012 | { |
| 1013 | if (!type) |
| 1014 | return eFormatDefault; |
| 1015 | switch (static_cast<GoType *>(type)->GetGoKind()) |
| 1016 | { |
| 1017 | case GoType::KIND_BOOL: |
| 1018 | return eFormatBoolean; |
| 1019 | case GoType::KIND_INT: |
| 1020 | case GoType::KIND_INT8: |
| 1021 | case GoType::KIND_INT16: |
| 1022 | case GoType::KIND_INT32: |
| 1023 | case GoType::KIND_INT64: |
| 1024 | return eFormatDecimal; |
| 1025 | case GoType::KIND_UINT: |
| 1026 | case GoType::KIND_UINT8: |
| 1027 | case GoType::KIND_UINT16: |
| 1028 | case GoType::KIND_UINT32: |
| 1029 | case GoType::KIND_UINT64: |
| 1030 | return eFormatUnsigned; |
| 1031 | case GoType::KIND_FLOAT32: |
| 1032 | case GoType::KIND_FLOAT64: |
| 1033 | return eFormatFloat; |
| 1034 | case GoType::KIND_COMPLEX64: |
| 1035 | case GoType::KIND_COMPLEX128: |
| 1036 | return eFormatComplexFloat; |
| 1037 | case GoType::KIND_UINTPTR: |
| 1038 | case GoType::KIND_CHAN: |
| 1039 | case GoType::KIND_PTR: |
| 1040 | case GoType::KIND_MAP: |
| 1041 | case GoType::KIND_UNSAFEPOINTER: |
| 1042 | return eFormatHex; |
| 1043 | case GoType::KIND_STRING: |
| 1044 | return eFormatCString; |
| 1045 | case GoType::KIND_ARRAY: |
| 1046 | case GoType::KIND_INTERFACE: |
| 1047 | case GoType::KIND_SLICE: |
| 1048 | case GoType::KIND_STRUCT: |
| 1049 | default: |
| 1050 | // Don't know how to display this. |
| 1051 | return eFormatBytes; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | size_t |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 1056 | GoASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1057 | { |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
| 1061 | uint32_t |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 1062 | GoASTContext::GetNumChildren(lldb::opaque_compiler_type_t type, bool omit_empty_base_classes) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1063 | { |
| 1064 | if (!type || !GetCompleteType(type)) |
| 1065 | return 0; |
| 1066 | GoType *t = static_cast<GoType *>(type); |
| 1067 | if (t->GetGoKind() == GoType::KIND_PTR) |
| 1068 | { |
| 1069 | CompilerType elem = t->GetElementType(); |
| 1070 | if (elem.IsAggregateType()) |
| 1071 | return elem.GetNumChildren(omit_empty_base_classes); |
| 1072 | return 1; |
| 1073 | } |
| 1074 | else if (GoArray *array = t->GetArray()) |
| 1075 | { |
| 1076 | return array->GetLength(); |
| 1077 | } |
Ryan Brown | 998c8a1c1 | 2015-11-02 19:30:40 +0000 | [diff] [blame] | 1078 | else if (t->IsTypedef()) |
| 1079 | { |
| 1080 | return t->GetElementType().GetNumChildren(omit_empty_base_classes); |
| 1081 | } |
| 1082 | |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1083 | return GetNumFields(type); |
| 1084 | } |
| 1085 | |
| 1086 | uint32_t |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 1087 | GoASTContext::GetNumFields(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1088 | { |
| 1089 | if (!type || !GetCompleteType(type)) |
| 1090 | return 0; |
| 1091 | GoType *t = static_cast<GoType *>(type); |
| 1092 | if (t->IsTypedef()) |
| 1093 | return t->GetElementType().GetNumFields(); |
| 1094 | GoStruct *s = t->GetStruct(); |
| 1095 | if (s) |
| 1096 | return s->GetNumFields(); |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | CompilerType |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 1101 | GoASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type, size_t idx, std::string &name, uint64_t *bit_offset_ptr, |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1102 | uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) |
| 1103 | { |
| 1104 | if (bit_offset_ptr) |
| 1105 | *bit_offset_ptr = 0; |
| 1106 | if (bitfield_bit_size_ptr) |
| 1107 | *bitfield_bit_size_ptr = 0; |
| 1108 | if (is_bitfield_ptr) |
| 1109 | *is_bitfield_ptr = false; |
| 1110 | |
| 1111 | if (!type || !GetCompleteType(type)) |
| 1112 | return CompilerType(); |
| 1113 | |
| 1114 | GoType *t = static_cast<GoType *>(type); |
| 1115 | if (t->IsTypedef()) |
| 1116 | return t->GetElementType().GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, is_bitfield_ptr); |
| 1117 | |
| 1118 | GoStruct *s = t->GetStruct(); |
| 1119 | if (s) |
| 1120 | { |
| 1121 | const auto *field = s->GetField(idx); |
| 1122 | if (field) |
| 1123 | { |
| 1124 | name = field->m_name.GetStringRef(); |
| 1125 | if (bit_offset_ptr) |
| 1126 | *bit_offset_ptr = field->m_byte_offset * 8; |
| 1127 | return field->m_type; |
| 1128 | } |
| 1129 | } |
| 1130 | return CompilerType(); |
| 1131 | } |
| 1132 | |
| 1133 | CompilerType |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 1134 | GoASTContext::GetChildCompilerTypeAtIndex(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers, |
Bruce Mitchener | 4ad8334 | 2015-09-21 16:48:48 +0000 | [diff] [blame] | 1135 | bool omit_empty_base_classes, bool ignore_array_bounds, std::string &child_name, |
| 1136 | uint32_t &child_byte_size, int32_t &child_byte_offset, |
| 1137 | uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset, |
Enrico Granata | dc62ffd | 2015-11-09 19:27:34 +0000 | [diff] [blame] | 1138 | bool &child_is_base_class, bool &child_is_deref_of_parent, ValueObject *valobj, uint64_t &language_flags) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1139 | { |
| 1140 | child_name.clear(); |
| 1141 | child_byte_size = 0; |
| 1142 | child_byte_offset = 0; |
| 1143 | child_bitfield_bit_size = 0; |
| 1144 | child_bitfield_bit_offset = 0; |
| 1145 | child_is_base_class = false; |
| 1146 | child_is_deref_of_parent = false; |
Enrico Granata | dc62ffd | 2015-11-09 19:27:34 +0000 | [diff] [blame] | 1147 | language_flags = 0; |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1148 | |
| 1149 | if (!type || !GetCompleteType(type)) |
| 1150 | return CompilerType(); |
| 1151 | |
| 1152 | GoType *t = static_cast<GoType *>(type); |
| 1153 | if (t->GetStruct()) |
| 1154 | { |
| 1155 | uint64_t bit_offset; |
| 1156 | CompilerType ret = GetFieldAtIndex(type, idx, child_name, &bit_offset, nullptr, nullptr); |
| 1157 | child_byte_size = ret.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr); |
| 1158 | child_byte_offset = bit_offset / 8; |
| 1159 | return ret; |
| 1160 | } |
| 1161 | else if (t->GetGoKind() == GoType::KIND_PTR) |
| 1162 | { |
| 1163 | CompilerType pointee = t->GetElementType(); |
| 1164 | if (!pointee.IsValid() || pointee.IsVoidType()) |
| 1165 | return CompilerType(); |
| 1166 | if (transparent_pointers && pointee.IsAggregateType()) |
| 1167 | { |
| 1168 | bool tmp_child_is_deref_of_parent = false; |
Bruce Mitchener | 4ad8334 | 2015-09-21 16:48:48 +0000 | [diff] [blame] | 1169 | return pointee.GetChildCompilerTypeAtIndex(exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1170 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 1171 | child_bitfield_bit_size, child_bitfield_bit_offset, |
Enrico Granata | dc62ffd | 2015-11-09 19:27:34 +0000 | [diff] [blame] | 1172 | child_is_base_class, tmp_child_is_deref_of_parent, valobj, language_flags); |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1173 | } |
| 1174 | else |
| 1175 | { |
| 1176 | child_is_deref_of_parent = true; |
| 1177 | const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL; |
| 1178 | if (parent_name) |
| 1179 | { |
| 1180 | child_name.assign(1, '*'); |
| 1181 | child_name += parent_name; |
| 1182 | } |
| 1183 | |
| 1184 | // We have a pointer to an simple type |
| 1185 | if (idx == 0 && pointee.GetCompleteType()) |
| 1186 | { |
| 1187 | child_byte_size = pointee.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL); |
| 1188 | child_byte_offset = 0; |
| 1189 | return pointee; |
| 1190 | } |
| 1191 | } |
| 1192 | } |
| 1193 | else if (GoArray *a = t->GetArray()) |
| 1194 | { |
| 1195 | if (ignore_array_bounds || idx < a->GetLength()) |
| 1196 | { |
| 1197 | CompilerType element_type = a->GetElementType(); |
| 1198 | if (element_type.GetCompleteType()) |
| 1199 | { |
| 1200 | char element_name[64]; |
| 1201 | ::snprintf(element_name, sizeof(element_name), "[%zu]", idx); |
| 1202 | child_name.assign(element_name); |
| 1203 | child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL); |
| 1204 | child_byte_offset = (int32_t)idx * (int32_t)child_byte_size; |
| 1205 | return element_type; |
| 1206 | } |
| 1207 | } |
| 1208 | } |
| 1209 | else if (t->IsTypedef()) |
| 1210 | { |
Bruce Mitchener | 4ad8334 | 2015-09-21 16:48:48 +0000 | [diff] [blame] | 1211 | return t->GetElementType().GetChildCompilerTypeAtIndex( |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1212 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, ignore_array_bounds, child_name, |
| 1213 | child_byte_size, child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class, |
Enrico Granata | dc62ffd | 2015-11-09 19:27:34 +0000 | [diff] [blame] | 1214 | child_is_deref_of_parent, valobj, language_flags); |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1215 | } |
| 1216 | return CompilerType(); |
| 1217 | } |
| 1218 | |
| 1219 | // Lookup a child given a name. This function will match base class names |
| 1220 | // and member member names in "clang_type" only, not descendants. |
| 1221 | uint32_t |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 1222 | GoASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type, const char *name, bool omit_empty_base_classes) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1223 | { |
Ryan Brown | 07a1c45 | 2015-10-06 20:29:31 +0000 | [diff] [blame] | 1224 | if (!type || !GetCompleteType(type)) |
| 1225 | return UINT_MAX; |
| 1226 | |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1227 | GoType *t = static_cast<GoType *>(type); |
| 1228 | GoStruct *s = t->GetStruct(); |
| 1229 | if (s) |
| 1230 | { |
| 1231 | for (uint32_t i = 0; i < s->GetNumFields(); ++i) |
| 1232 | { |
| 1233 | const GoStruct::Field *f = s->GetField(i); |
| 1234 | if (f->m_name.GetStringRef() == name) |
| 1235 | return i; |
| 1236 | } |
| 1237 | } |
| 1238 | else if (t->GetGoKind() == GoType::KIND_PTR || t->IsTypedef()) |
| 1239 | { |
| 1240 | return t->GetElementType().GetIndexOfChildWithName(name, omit_empty_base_classes); |
| 1241 | } |
| 1242 | return UINT_MAX; |
| 1243 | } |
| 1244 | |
| 1245 | // Lookup a child member given a name. This function will match member names |
| 1246 | // only and will descend into "clang_type" children in search for the first |
| 1247 | // member in this class, or any base class that matches "name". |
| 1248 | // TODO: Return all matches for a given name by returning a vector<vector<uint32_t>> |
| 1249 | // so we catch all names that match a given child name, not just the first. |
| 1250 | size_t |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 1251 | GoASTContext::GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type, const char *name, bool omit_empty_base_classes, |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1252 | std::vector<uint32_t> &child_indexes) |
| 1253 | { |
| 1254 | uint32_t index = GetIndexOfChildWithName(type, name, omit_empty_base_classes); |
| 1255 | if (index == UINT_MAX) |
| 1256 | return 0; |
| 1257 | child_indexes.push_back(index); |
| 1258 | return 1; |
| 1259 | } |
| 1260 | |
| 1261 | // Converts "s" to a floating point value and place resulting floating |
| 1262 | // point bytes in the "dst" buffer. |
| 1263 | size_t |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 1264 | GoASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type, const char *s, uint8_t *dst, size_t dst_size) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1265 | { |
| 1266 | assert(false); |
Ryan Brown | d03c2e0 | 2015-09-15 00:50:43 +0000 | [diff] [blame] | 1267 | return 0; |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1268 | } |
| 1269 | //---------------------------------------------------------------------- |
| 1270 | // Dumping types |
| 1271 | //---------------------------------------------------------------------- |
Ryan Brown | 47d072ca | 2016-01-15 19:35:48 +0000 | [diff] [blame^] | 1272 | #define DEPTH_INCREMENT 2 |
| 1273 | |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1274 | void |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 1275 | GoASTContext::DumpValue(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s, lldb::Format format, |
Ryan Brown | 47d072ca | 2016-01-15 19:35:48 +0000 | [diff] [blame^] | 1276 | const DataExtractor &data, lldb::offset_t data_byte_offset, size_t data_byte_size, |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1277 | uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types, bool show_summary, |
| 1278 | bool verbose, uint32_t depth) |
| 1279 | { |
Ryan Brown | 47d072ca | 2016-01-15 19:35:48 +0000 | [diff] [blame^] | 1280 | if (IsTypedefType(type)) |
| 1281 | type = GetTypedefedType(type).GetOpaqueQualType(); |
| 1282 | if (!type) |
| 1283 | return; |
| 1284 | GoType *t = static_cast<GoType *>(type); |
| 1285 | |
| 1286 | if (GoStruct *st = t->GetStruct()) |
| 1287 | { |
| 1288 | if (GetCompleteType(type)) |
| 1289 | { |
| 1290 | uint32_t field_idx = 0; |
| 1291 | for (auto* field = st->GetField(field_idx); field != nullptr; field_idx++) |
| 1292 | { |
| 1293 | // Print the starting squiggly bracket (if this is the |
| 1294 | // first member) or comma (for member 2 and beyond) for |
| 1295 | // the struct/union/class member. |
| 1296 | if (field_idx == 0) |
| 1297 | s->PutChar('{'); |
| 1298 | else |
| 1299 | s->PutChar(','); |
| 1300 | |
| 1301 | // Indent |
| 1302 | s->Printf("\n%*s", depth + DEPTH_INCREMENT, ""); |
| 1303 | |
| 1304 | // Print the member type if requested |
| 1305 | if (show_types) |
| 1306 | { |
| 1307 | ConstString field_type_name = field->m_type.GetTypeName(); |
| 1308 | s->Printf("(%s) ", field_type_name.AsCString()); |
| 1309 | } |
| 1310 | // Print the member name and equal sign |
| 1311 | s->Printf("%s = ", field->m_name.AsCString()); |
| 1312 | |
| 1313 | |
| 1314 | // Dump the value of the member |
| 1315 | CompilerType field_type = field->m_type; |
| 1316 | field_type.DumpValue (exe_ctx, |
| 1317 | s, // Stream to dump to |
| 1318 | field_type.GetFormat(), // The format with which to display the member |
| 1319 | data, // Data buffer containing all bytes for this type |
| 1320 | data_byte_offset + field->m_byte_offset,// Offset into "data" where to grab value from |
| 1321 | field->m_type.GetByteSize(exe_ctx->GetBestExecutionContextScope()), // Size of this type in bytes |
| 1322 | 0, // Bitfield bit size |
| 1323 | 0, // Bitfield bit offset |
| 1324 | show_types, // Boolean indicating if we should show the variable types |
| 1325 | show_summary, // Boolean indicating if we should show a summary for the current type |
| 1326 | verbose, // Verbose output? |
| 1327 | depth + DEPTH_INCREMENT); // Scope depth for any types that have children |
| 1328 | } |
| 1329 | |
| 1330 | // Indent the trailing squiggly bracket |
| 1331 | if (field_idx > 0) |
| 1332 | s->Printf("\n%*s}", depth, ""); |
| 1333 | |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | if (GoArray *a = t->GetArray()) { |
| 1338 | CompilerType element_clang_type = a->GetElementType(); |
| 1339 | lldb::Format element_format = element_clang_type.GetFormat(); |
| 1340 | uint32_t element_byte_size = element_clang_type.GetByteSize(exe_ctx->GetBestExecutionContextScope()); |
| 1341 | |
| 1342 | uint64_t element_idx; |
| 1343 | for (element_idx = 0; element_idx < a->GetLength(); ++element_idx) |
| 1344 | { |
| 1345 | // Print the starting squiggly bracket (if this is the |
| 1346 | // first member) or comman (for member 2 and beyong) for |
| 1347 | // the struct/union/class member. |
| 1348 | if (element_idx == 0) |
| 1349 | s->PutChar('{'); |
| 1350 | else |
| 1351 | s->PutChar(','); |
| 1352 | |
| 1353 | // Indent and print the index |
| 1354 | s->Printf("\n%*s[%" PRIu64 "] ", depth + DEPTH_INCREMENT, "", element_idx); |
| 1355 | |
| 1356 | // Figure out the field offset within the current struct/union/class type |
| 1357 | uint64_t element_offset = element_idx * element_byte_size; |
| 1358 | |
| 1359 | // Dump the value of the member |
| 1360 | element_clang_type.DumpValue (exe_ctx, |
| 1361 | s, // Stream to dump to |
| 1362 | element_format, // The format with which to display the element |
| 1363 | data, // Data buffer containing all bytes for this type |
| 1364 | data_byte_offset + element_offset,// Offset into "data" where to grab value from |
| 1365 | element_byte_size, // Size of this type in bytes |
| 1366 | 0, // Bitfield bit size |
| 1367 | 0, // Bitfield bit offset |
| 1368 | show_types, // Boolean indicating if we should show the variable types |
| 1369 | show_summary, // Boolean indicating if we should show a summary for the current type |
| 1370 | verbose, // Verbose output? |
| 1371 | depth + DEPTH_INCREMENT); // Scope depth for any types that have children |
| 1372 | } |
| 1373 | |
| 1374 | // Indent the trailing squiggly bracket |
| 1375 | if (element_idx > 0) |
| 1376 | s->Printf("\n%*s}", depth, ""); |
| 1377 | } |
| 1378 | |
| 1379 | if (show_summary) |
| 1380 | DumpSummary (type, exe_ctx, s, data, data_byte_offset, data_byte_size); |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | bool |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 1384 | GoASTContext::DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format, const DataExtractor &data, |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1385 | lldb::offset_t byte_offset, size_t byte_size, uint32_t bitfield_bit_size, |
| 1386 | uint32_t bitfield_bit_offset, ExecutionContextScope *exe_scope) |
| 1387 | { |
| 1388 | if (!type) |
| 1389 | return false; |
| 1390 | if (IsAggregateType(type)) |
| 1391 | { |
| 1392 | return false; |
| 1393 | } |
| 1394 | else |
| 1395 | { |
| 1396 | GoType *t = static_cast<GoType *>(type); |
| 1397 | if (t->IsTypedef()) |
| 1398 | { |
Bruce Mitchener | 3ad353f | 2015-09-24 03:54:50 +0000 | [diff] [blame] | 1399 | CompilerType typedef_compiler_type = t->GetElementType(); |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1400 | if (format == eFormatDefault) |
Bruce Mitchener | 3ad353f | 2015-09-24 03:54:50 +0000 | [diff] [blame] | 1401 | format = typedef_compiler_type.GetFormat(); |
| 1402 | uint64_t typedef_byte_size = typedef_compiler_type.GetByteSize(exe_scope); |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1403 | |
Bruce Mitchener | 3ad353f | 2015-09-24 03:54:50 +0000 | [diff] [blame] | 1404 | return typedef_compiler_type.DumpTypeValue( |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1405 | s, |
| 1406 | format, // The format with which to display the element |
| 1407 | data, // Data buffer containing all bytes for this type |
| 1408 | byte_offset, // Offset into "data" where to grab value from |
| 1409 | typedef_byte_size, // Size of this type in bytes |
| 1410 | bitfield_bit_size, // Size in bits of a bitfield value, if zero don't treat as a bitfield |
| 1411 | bitfield_bit_offset, // Offset in bits of a bitfield value if bitfield_bit_size != 0 |
| 1412 | exe_scope); |
| 1413 | } |
| 1414 | |
| 1415 | uint32_t item_count = 1; |
| 1416 | // A few formats, we might need to modify our size and count for depending |
| 1417 | // on how we are trying to display the value... |
| 1418 | switch (format) |
| 1419 | { |
| 1420 | default: |
| 1421 | case eFormatBoolean: |
| 1422 | case eFormatBinary: |
| 1423 | case eFormatComplex: |
| 1424 | case eFormatCString: // NULL terminated C strings |
| 1425 | case eFormatDecimal: |
| 1426 | case eFormatEnum: |
| 1427 | case eFormatHex: |
| 1428 | case eFormatHexUppercase: |
| 1429 | case eFormatFloat: |
| 1430 | case eFormatOctal: |
| 1431 | case eFormatOSType: |
| 1432 | case eFormatUnsigned: |
| 1433 | case eFormatPointer: |
| 1434 | case eFormatVectorOfChar: |
| 1435 | case eFormatVectorOfSInt8: |
| 1436 | case eFormatVectorOfUInt8: |
| 1437 | case eFormatVectorOfSInt16: |
| 1438 | case eFormatVectorOfUInt16: |
| 1439 | case eFormatVectorOfSInt32: |
| 1440 | case eFormatVectorOfUInt32: |
| 1441 | case eFormatVectorOfSInt64: |
| 1442 | case eFormatVectorOfUInt64: |
| 1443 | case eFormatVectorOfFloat32: |
| 1444 | case eFormatVectorOfFloat64: |
| 1445 | case eFormatVectorOfUInt128: |
| 1446 | break; |
| 1447 | |
| 1448 | case eFormatChar: |
| 1449 | case eFormatCharPrintable: |
| 1450 | case eFormatCharArray: |
| 1451 | case eFormatBytes: |
| 1452 | case eFormatBytesWithASCII: |
| 1453 | item_count = byte_size; |
| 1454 | byte_size = 1; |
| 1455 | break; |
| 1456 | |
| 1457 | case eFormatUnicode16: |
| 1458 | item_count = byte_size / 2; |
| 1459 | byte_size = 2; |
| 1460 | break; |
| 1461 | |
| 1462 | case eFormatUnicode32: |
| 1463 | item_count = byte_size / 4; |
| 1464 | byte_size = 4; |
| 1465 | break; |
| 1466 | } |
| 1467 | return data.Dump(s, byte_offset, format, byte_size, item_count, UINT32_MAX, LLDB_INVALID_ADDRESS, |
| 1468 | bitfield_bit_size, bitfield_bit_offset, exe_scope); |
| 1469 | } |
| 1470 | return 0; |
| 1471 | } |
| 1472 | |
| 1473 | void |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 1474 | GoASTContext::DumpSummary(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s, const DataExtractor &data, |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1475 | lldb::offset_t data_offset, size_t data_byte_size) |
| 1476 | { |
Ryan Brown | 47d072ca | 2016-01-15 19:35:48 +0000 | [diff] [blame^] | 1477 | if (type && GoType::KIND_STRING == static_cast<GoType *>(type)->GetGoKind()) |
| 1478 | { |
| 1479 | // TODO(ribrdb): read length and data |
| 1480 | } |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | void |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 1484 | GoASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1485 | { |
Ryan Brown | 47d072ca | 2016-01-15 19:35:48 +0000 | [diff] [blame^] | 1486 | // Dump to stdout |
| 1487 | StreamFile s (stdout, false); |
| 1488 | DumpTypeDescription (type, &s); |
| 1489 | } |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1490 | |
| 1491 | void |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 1492 | GoASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type, Stream *s) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1493 | { |
Ryan Brown | 47d072ca | 2016-01-15 19:35:48 +0000 | [diff] [blame^] | 1494 | if (!type) |
| 1495 | return; |
| 1496 | ConstString name = GetTypeName(type); |
| 1497 | GoType *t = static_cast<GoType *>(type); |
| 1498 | |
| 1499 | if (GoStruct *st = t->GetStruct()) |
| 1500 | { |
| 1501 | if (GetCompleteType(type)) |
| 1502 | { |
| 1503 | if (NULL == strchr(name.AsCString(), '{')) |
| 1504 | s->Printf("type %s ", name.AsCString()); |
| 1505 | s->PutCString("struct {"); |
| 1506 | if (st->GetNumFields() == 0) { |
| 1507 | s->PutChar('}'); |
| 1508 | return; |
| 1509 | } |
| 1510 | s->IndentMore(); |
| 1511 | uint32_t field_idx = 0; |
| 1512 | for (auto* field = st->GetField(field_idx); field != nullptr; field_idx++) |
| 1513 | { |
| 1514 | s->PutChar('\n'); |
| 1515 | s->Indent(); |
| 1516 | s->Printf("%s %s", field->m_name.AsCString(), field->m_type.GetTypeName().AsCString()); |
| 1517 | } |
| 1518 | s->IndentLess(); |
| 1519 | s->PutChar('\n'); |
| 1520 | s->Indent("}"); |
| 1521 | return; |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | s->PutCString(name.AsCString()); |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | CompilerType |
Bruce Mitchener | 500737e | 2015-09-15 04:33:48 +0000 | [diff] [blame] | 1529 | GoASTContext::CreateArrayType(const ConstString &name, const CompilerType &element_type, uint64_t length) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1530 | { |
| 1531 | GoType *type = new GoArray(name, length, element_type); |
| 1532 | (*m_types)[name].reset(type); |
| 1533 | return CompilerType(this, type); |
| 1534 | } |
| 1535 | |
| 1536 | CompilerType |
| 1537 | GoASTContext::CreateBaseType(int go_kind, const lldb_private::ConstString &name, uint64_t byte_size) |
| 1538 | { |
| 1539 | if (go_kind == GoType::KIND_UINT || go_kind == GoType::KIND_INT) |
| 1540 | m_int_byte_size = byte_size; |
| 1541 | GoType *type = new GoType(go_kind, name); |
| 1542 | (*m_types)[name].reset(type); |
| 1543 | return CompilerType(this, type); |
| 1544 | } |
| 1545 | |
| 1546 | CompilerType |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 1547 | GoASTContext::CreateTypedefType(int kind, const ConstString &name, CompilerType impl) |
Ryan Brown | 57bee1e | 2015-09-14 22:45:11 +0000 | [diff] [blame] | 1548 | { |
| 1549 | GoType *type = new GoElem(kind, name, impl); |
| 1550 | (*m_types)[name].reset(type); |
| 1551 | return CompilerType(this, type); |
| 1552 | } |
| 1553 | |
| 1554 | CompilerType |
| 1555 | GoASTContext::CreateVoidType(const lldb_private::ConstString &name) |
| 1556 | { |
| 1557 | GoType *type = new GoType(GoType::KIND_LLDB_VOID, name); |
| 1558 | (*m_types)[name].reset(type); |
| 1559 | return CompilerType(this, type); |
| 1560 | } |
| 1561 | |
| 1562 | CompilerType |
| 1563 | GoASTContext::CreateStructType(int kind, const lldb_private::ConstString &name, uint32_t byte_size) |
| 1564 | { |
| 1565 | GoType *type = new GoStruct(kind, name, byte_size); |
| 1566 | (*m_types)[name].reset(type); |
| 1567 | return CompilerType(this, type); |
| 1568 | } |
| 1569 | |
| 1570 | void |
| 1571 | GoASTContext::AddFieldToStruct(const lldb_private::CompilerType &struct_type, const lldb_private::ConstString &name, |
| 1572 | const lldb_private::CompilerType &field_type, uint32_t byte_offset) |
| 1573 | { |
| 1574 | if (!struct_type) |
| 1575 | return; |
| 1576 | GoASTContext *ast = llvm::dyn_cast_or_null<GoASTContext>(struct_type.GetTypeSystem()); |
| 1577 | if (!ast) |
| 1578 | return; |
| 1579 | GoType *type = static_cast<GoType *>(struct_type.GetOpaqueQualType()); |
| 1580 | if (GoStruct *s = type->GetStruct()) |
| 1581 | s->AddField(name, field_type, byte_offset); |
| 1582 | } |
| 1583 | |
| 1584 | void |
| 1585 | GoASTContext::CompleteStructType(const lldb_private::CompilerType &struct_type) |
| 1586 | { |
| 1587 | if (!struct_type) |
| 1588 | return; |
| 1589 | GoASTContext *ast = llvm::dyn_cast_or_null<GoASTContext>(struct_type.GetTypeSystem()); |
| 1590 | if (!ast) |
| 1591 | return; |
| 1592 | GoType *type = static_cast<GoType *>(struct_type.GetOpaqueQualType()); |
| 1593 | if (GoStruct *s = type->GetStruct()) |
| 1594 | s->SetComplete(); |
| 1595 | } |
| 1596 | |
| 1597 | CompilerType |
| 1598 | GoASTContext::CreateFunctionType(const lldb_private::ConstString &name, CompilerType *params, size_t params_count, |
| 1599 | bool is_variadic) |
| 1600 | { |
| 1601 | GoType *type = new GoFunction(name, is_variadic); |
| 1602 | (*m_types)[name].reset(type); |
| 1603 | return CompilerType(this, type); |
| 1604 | } |
| 1605 | |
| 1606 | bool |
| 1607 | GoASTContext::IsGoString(const lldb_private::CompilerType &type) |
| 1608 | { |
| 1609 | if (!type.IsValid() || !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem())) |
| 1610 | return false; |
| 1611 | return GoType::KIND_STRING == static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind(); |
| 1612 | } |
| 1613 | |
| 1614 | bool |
| 1615 | GoASTContext::IsGoSlice(const lldb_private::CompilerType &type) |
| 1616 | { |
| 1617 | if (!type.IsValid() || !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem())) |
| 1618 | return false; |
| 1619 | return GoType::KIND_SLICE == static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind(); |
| 1620 | } |
| 1621 | |
| 1622 | bool |
| 1623 | GoASTContext::IsGoInterface(const lldb_private::CompilerType &type) |
| 1624 | { |
| 1625 | if (!type.IsValid() || !llvm::dyn_cast_or_null<GoASTContext>(type.GetTypeSystem())) |
| 1626 | return false; |
| 1627 | return GoType::KIND_INTERFACE == static_cast<GoType *>(type.GetOpaqueQualType())->GetGoKind(); |
| 1628 | } |
| 1629 | |
| 1630 | bool |
| 1631 | GoASTContext::IsPointerKind(uint8_t kind) |
| 1632 | { |
| 1633 | return (kind & GoType::KIND_MASK) == GoType::KIND_PTR; |
| 1634 | } |
| 1635 | |
| 1636 | bool |
| 1637 | GoASTContext::IsDirectIface(uint8_t kind) |
| 1638 | { |
| 1639 | return (kind & GoType::KIND_DIRECT_IFACE) == GoType::KIND_DIRECT_IFACE; |
| 1640 | } |
| 1641 | |
| 1642 | DWARFASTParser * |
| 1643 | GoASTContext::GetDWARFParser() |
| 1644 | { |
| 1645 | if (!m_dwarf_ast_parser_ap) |
| 1646 | m_dwarf_ast_parser_ap.reset(new DWARFASTParserGo(*this)); |
| 1647 | return m_dwarf_ast_parser_ap.get(); |
| 1648 | } |
Ryan Brown | 998c8a1c1 | 2015-11-02 19:30:40 +0000 | [diff] [blame] | 1649 | |
| 1650 | UserExpression * |
| 1651 | GoASTContextForExpr::GetUserExpression(const char *expr, const char *expr_prefix, lldb::LanguageType language, |
Jim Ingham | 19a63fc | 2015-11-03 02:11:24 +0000 | [diff] [blame] | 1652 | Expression::ResultType desired_type, const EvaluateExpressionOptions &options) |
Ryan Brown | 998c8a1c1 | 2015-11-02 19:30:40 +0000 | [diff] [blame] | 1653 | { |
| 1654 | TargetSP target = m_target_wp.lock(); |
| 1655 | if (target) |
Jim Ingham | 19a63fc | 2015-11-03 02:11:24 +0000 | [diff] [blame] | 1656 | return new GoUserExpression(*target, expr, expr_prefix, language, desired_type, options); |
Ryan Brown | 998c8a1c1 | 2015-11-02 19:30:40 +0000 | [diff] [blame] | 1657 | return nullptr; |
| 1658 | } |