blob: 91f1f90507a759e08798f745026cd82e13aa3a96 [file] [log] [blame]
Enrico Granatae85fe3a2014-10-22 20:34:38 +00001//===-- 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
17using namespace lldb;
18using namespace lldb_private;
19using namespace lldb_private::formatters;
20
21namespace 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
55lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::LibcxxInitializerListSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
56SyntheticChildrenFrontEnd(*valobj_sp.get()),
57m_start(NULL),
58m_element_type(),
59m_element_size(0),
60m_num_elements(0),
61m_children()
62{
63 if (valobj_sp)
64 Update();
65}
66
67size_t
68lldb_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
78lldb::ValueObjectSP
79lldb_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 Granatae29df232014-12-09 19:51:20 +000092 ValueObjectSP child_sp = CreateValueObjectFromAddress(name.GetData(), offset, m_backend.GetExecutionContextRef(), m_element_type);
Enrico Granatae85fe3a2014-10-22 20:34:38 +000093 m_children[idx] = child_sp;
94 return child_sp;
95}
96
97bool
98lldb_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 Granata1cd5e922015-01-28 00:07:51 +0000110 m_element_size = m_element_type.GetByteSize(nullptr);
Enrico Granatae85fe3a2014-10-22 20:34:38 +0000111
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
118bool
119lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::MightHaveChildren ()
120{
121 return true;
122}
123
124size_t
125lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name)
126{
127 if (!m_start)
128 return UINT32_MAX;
129 return ExtractIndexFromString(name.GetCString());
130}
131
132lldb_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
138lldb_private::SyntheticChildrenFrontEnd*
139lldb_private::formatters::LibcxxInitializerListSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
140{
141 if (!valobj_sp)
142 return NULL;
143 return (new LibcxxInitializerListSyntheticFrontEnd(valobj_sp));
144}
145