Enrico Granata | e85fe3a | 2014-10-22 20:34:38 +0000 | [diff] [blame] | 1 | //===-- LibCxxInitializerList.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 "lldb/lldb-python.h" |
| 11 | |
| 12 | #include "lldb/DataFormatters/CXXFormatterFunctions.h" |
| 13 | |
| 14 | #include "lldb/Core/ConstString.h" |
| 15 | #include "lldb/Core/ValueObject.h" |
| 16 | |
| 17 | using namespace lldb; |
| 18 | using namespace lldb_private; |
| 19 | using namespace lldb_private::formatters; |
| 20 | |
| 21 | namespace lldb_private { |
| 22 | namespace formatters { |
| 23 | class LibcxxInitializerListSyntheticFrontEnd : public SyntheticChildrenFrontEnd |
| 24 | { |
| 25 | public: |
| 26 | LibcxxInitializerListSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp); |
| 27 | |
| 28 | virtual size_t |
| 29 | CalculateNumChildren (); |
| 30 | |
| 31 | virtual lldb::ValueObjectSP |
| 32 | GetChildAtIndex (size_t idx); |
| 33 | |
| 34 | virtual bool |
| 35 | Update(); |
| 36 | |
| 37 | virtual bool |
| 38 | MightHaveChildren (); |
| 39 | |
| 40 | virtual size_t |
| 41 | GetIndexOfChildWithName (const ConstString &name); |
| 42 | |
| 43 | virtual |
| 44 | ~LibcxxInitializerListSyntheticFrontEnd (); |
| 45 | private: |
| 46 | ValueObject* m_start; |
| 47 | ClangASTType m_element_type; |
| 48 | uint32_t m_element_size; |
| 49 | size_t m_num_elements; |
| 50 | std::map<size_t,lldb::ValueObjectSP> m_children; |
| 51 | }; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::LibcxxInitializerListSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) : |
| 56 | SyntheticChildrenFrontEnd(*valobj_sp.get()), |
| 57 | m_start(NULL), |
| 58 | m_element_type(), |
| 59 | m_element_size(0), |
| 60 | m_num_elements(0), |
| 61 | m_children() |
| 62 | { |
| 63 | if (valobj_sp) |
| 64 | Update(); |
| 65 | } |
| 66 | |
| 67 | size_t |
| 68 | lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::CalculateNumChildren () |
| 69 | { |
| 70 | static ConstString g___size_("__size_"); |
| 71 | m_num_elements = 0; |
| 72 | ValueObjectSP size_sp(m_backend.GetChildMemberWithName(g___size_, true)); |
| 73 | if (size_sp) |
| 74 | m_num_elements = size_sp->GetValueAsUnsigned(0); |
| 75 | return m_num_elements; |
| 76 | } |
| 77 | |
| 78 | lldb::ValueObjectSP |
| 79 | lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::GetChildAtIndex (size_t idx) |
| 80 | { |
| 81 | if (!m_start) |
| 82 | return lldb::ValueObjectSP(); |
| 83 | |
| 84 | auto cached = m_children.find(idx); |
| 85 | if (cached != m_children.end()) |
| 86 | return cached->second; |
| 87 | |
| 88 | uint64_t offset = idx * m_element_size; |
| 89 | offset = offset + m_start->GetValueAsUnsigned(0); |
| 90 | StreamString name; |
| 91 | name.Printf("[%" PRIu64 "]", (uint64_t)idx); |
Enrico Granata | e29df23 | 2014-12-09 19:51:20 +0000 | [diff] [blame] | 92 | ValueObjectSP child_sp = CreateValueObjectFromAddress(name.GetData(), offset, m_backend.GetExecutionContextRef(), m_element_type); |
Enrico Granata | e85fe3a | 2014-10-22 20:34:38 +0000 | [diff] [blame] | 93 | m_children[idx] = child_sp; |
| 94 | return child_sp; |
| 95 | } |
| 96 | |
| 97 | bool |
| 98 | lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::Update() |
| 99 | { |
| 100 | static ConstString g___begin_("__begin_"); |
| 101 | |
| 102 | m_start = nullptr; |
| 103 | m_num_elements = 0; |
| 104 | m_children.clear(); |
| 105 | lldb::TemplateArgumentKind kind; |
| 106 | m_element_type = m_backend.GetClangType().GetTemplateArgument(0, kind); |
| 107 | if (kind != lldb::eTemplateArgumentKindType || false == m_element_type.IsValid()) |
| 108 | return false; |
| 109 | |
Enrico Granata | 1cd5e92 | 2015-01-28 00:07:51 +0000 | [diff] [blame] | 110 | m_element_size = m_element_type.GetByteSize(nullptr); |
Enrico Granata | e85fe3a | 2014-10-22 20:34:38 +0000 | [diff] [blame] | 111 | |
| 112 | if (m_element_size > 0) |
| 113 | m_start = m_backend.GetChildMemberWithName(g___begin_,true).get(); // store raw pointers or end up with a circular dependency |
| 114 | |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | bool |
| 119 | lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::MightHaveChildren () |
| 120 | { |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | size_t |
| 125 | lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name) |
| 126 | { |
| 127 | if (!m_start) |
| 128 | return UINT32_MAX; |
| 129 | return ExtractIndexFromString(name.GetCString()); |
| 130 | } |
| 131 | |
| 132 | lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::~LibcxxInitializerListSyntheticFrontEnd () |
| 133 | { |
| 134 | // this needs to stay around because it's a child object who will follow its parent's life cycle |
| 135 | // delete m_start; |
| 136 | } |
| 137 | |
| 138 | lldb_private::SyntheticChildrenFrontEnd* |
| 139 | lldb_private::formatters::LibcxxInitializerListSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp) |
| 140 | { |
| 141 | if (!valobj_sp) |
| 142 | return NULL; |
| 143 | return (new LibcxxInitializerListSyntheticFrontEnd(valobj_sp)); |
| 144 | } |
| 145 | |