]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/DataFormatters/TypeSynthetic.cpp
MFV: r344395
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / DataFormatters / TypeSynthetic.cpp
1 //===-- TypeSynthetic.cpp ----------------------------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 // C Includes
12
13 // C++ Includes
14
15 // Other libraries and framework includes
16
17 // Project includes
18 #include "lldb/lldb-enumerations.h"
19 #include "lldb/lldb-public.h"
20
21 #include "lldb/Core/Debugger.h"
22 #include "lldb/DataFormatters/TypeSynthetic.h"
23 #include "lldb/Interpreter/CommandInterpreter.h"
24 #include "lldb/Interpreter/ScriptInterpreter.h"
25 #include "lldb/Symbol/CompilerType.h"
26 #include "lldb/Target/Target.h"
27 #include "lldb/Utility/StreamString.h"
28
29 using namespace lldb;
30 using namespace lldb_private;
31
32 void TypeFilterImpl::AddExpressionPath(const std::string &path) {
33   bool need_add_dot = true;
34   if (path[0] == '.' || (path[0] == '-' && path[1] == '>') || path[0] == '[')
35     need_add_dot = false;
36   // add a '.' symbol to help forgetful users
37   if (!need_add_dot)
38     m_expression_paths.push_back(path);
39   else
40     m_expression_paths.push_back(std::string(".") + path);
41 }
42
43 bool TypeFilterImpl::SetExpressionPathAtIndex(size_t i,
44                                               const std::string &path) {
45   if (i >= GetCount())
46     return false;
47   bool need_add_dot = true;
48   if (path[0] == '.' || (path[0] == '-' && path[1] == '>') || path[0] == '[')
49     need_add_dot = false;
50   // add a '.' symbol to help forgetful users
51   if (!need_add_dot)
52     m_expression_paths[i] = path;
53   else
54     m_expression_paths[i] = std::string(".") + path;
55   return true;
56 }
57
58 size_t
59 TypeFilterImpl::FrontEnd::GetIndexOfChildWithName(const ConstString &name) {
60   const char *name_cstr = name.GetCString();
61   if (name_cstr) {
62     for (size_t i = 0; i < filter->GetCount(); i++) {
63       const char *expr_cstr = filter->GetExpressionPathAtIndex(i);
64       if (expr_cstr) {
65         if (*expr_cstr == '.')
66           expr_cstr++;
67         else if (*expr_cstr == '-' && *(expr_cstr + 1) == '>')
68           expr_cstr += 2;
69       }
70       if (expr_cstr) {
71         if (!::strcmp(name_cstr, expr_cstr))
72           return i;
73       }
74     }
75   }
76   return UINT32_MAX;
77 }
78
79 std::string TypeFilterImpl::GetDescription() {
80   StreamString sstr;
81   sstr.Printf("%s%s%s {\n", Cascades() ? "" : " (not cascading)",
82               SkipsPointers() ? " (skip pointers)" : "",
83               SkipsReferences() ? " (skip references)" : "");
84
85   for (size_t i = 0; i < GetCount(); i++) {
86     sstr.Printf("    %s\n", GetExpressionPathAtIndex(i));
87   }
88
89   sstr.Printf("}");
90   return sstr.GetString();
91 }
92
93 std::string CXXSyntheticChildren::GetDescription() {
94   StreamString sstr;
95   sstr.Printf("%s%s%s %s", Cascades() ? "" : " (not cascading)",
96               SkipsPointers() ? " (skip pointers)" : "",
97               SkipsReferences() ? " (skip references)" : "",
98               m_description.c_str());
99
100   return sstr.GetString();
101 }
102
103 lldb::ValueObjectSP SyntheticChildrenFrontEnd::CreateValueObjectFromExpression(
104     llvm::StringRef name, llvm::StringRef expression,
105     const ExecutionContext &exe_ctx) {
106   ValueObjectSP valobj_sp(
107       ValueObject::CreateValueObjectFromExpression(name, expression, exe_ctx));
108   if (valobj_sp)
109     valobj_sp->SetSyntheticChildrenGenerated(true);
110   return valobj_sp;
111 }
112
113 lldb::ValueObjectSP SyntheticChildrenFrontEnd::CreateValueObjectFromAddress(
114     llvm::StringRef name, uint64_t address, const ExecutionContext &exe_ctx,
115     CompilerType type) {
116   ValueObjectSP valobj_sp(
117       ValueObject::CreateValueObjectFromAddress(name, address, exe_ctx, type));
118   if (valobj_sp)
119     valobj_sp->SetSyntheticChildrenGenerated(true);
120   return valobj_sp;
121 }
122
123 lldb::ValueObjectSP SyntheticChildrenFrontEnd::CreateValueObjectFromData(
124     llvm::StringRef name, const DataExtractor &data,
125     const ExecutionContext &exe_ctx, CompilerType type) {
126   ValueObjectSP valobj_sp(
127       ValueObject::CreateValueObjectFromData(name, data, exe_ctx, type));
128   if (valobj_sp)
129     valobj_sp->SetSyntheticChildrenGenerated(true);
130   return valobj_sp;
131 }
132
133 #ifndef LLDB_DISABLE_PYTHON
134
135 ScriptedSyntheticChildren::FrontEnd::FrontEnd(std::string pclass,
136                                               ValueObject &backend)
137     : SyntheticChildrenFrontEnd(backend), m_python_class(pclass),
138       m_wrapper_sp(), m_interpreter(NULL) {
139   if (backend == LLDB_INVALID_UID)
140     return;
141
142   TargetSP target_sp = backend.GetTargetSP();
143
144   if (!target_sp)
145     return;
146
147   m_interpreter =
148       target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
149
150   if (m_interpreter != NULL)
151     m_wrapper_sp = m_interpreter->CreateSyntheticScriptedProvider(
152         m_python_class.c_str(), backend.GetSP());
153 }
154
155 ScriptedSyntheticChildren::FrontEnd::~FrontEnd() {}
156
157 lldb::ValueObjectSP
158 ScriptedSyntheticChildren::FrontEnd::GetChildAtIndex(size_t idx) {
159   if (!m_wrapper_sp || !m_interpreter)
160     return lldb::ValueObjectSP();
161
162   return m_interpreter->GetChildAtIndex(m_wrapper_sp, idx);
163 }
164
165 bool ScriptedSyntheticChildren::FrontEnd::IsValid() {
166   return (m_wrapper_sp && m_wrapper_sp->IsValid() && m_interpreter);
167 }
168
169 size_t ScriptedSyntheticChildren::FrontEnd::CalculateNumChildren() {
170   if (!m_wrapper_sp || m_interpreter == NULL)
171     return 0;
172   return m_interpreter->CalculateNumChildren(m_wrapper_sp, UINT32_MAX);
173 }
174
175 size_t ScriptedSyntheticChildren::FrontEnd::CalculateNumChildren(uint32_t max) {
176   if (!m_wrapper_sp || m_interpreter == NULL)
177     return 0;
178   return m_interpreter->CalculateNumChildren(m_wrapper_sp, max);
179 }
180
181 bool ScriptedSyntheticChildren::FrontEnd::Update() {
182   if (!m_wrapper_sp || m_interpreter == NULL)
183     return false;
184
185   return m_interpreter->UpdateSynthProviderInstance(m_wrapper_sp);
186 }
187
188 bool ScriptedSyntheticChildren::FrontEnd::MightHaveChildren() {
189   if (!m_wrapper_sp || m_interpreter == NULL)
190     return false;
191
192   return m_interpreter->MightHaveChildrenSynthProviderInstance(m_wrapper_sp);
193 }
194
195 size_t ScriptedSyntheticChildren::FrontEnd::GetIndexOfChildWithName(
196     const ConstString &name) {
197   if (!m_wrapper_sp || m_interpreter == NULL)
198     return UINT32_MAX;
199   return m_interpreter->GetIndexOfChildWithName(m_wrapper_sp,
200                                                 name.GetCString());
201 }
202
203 lldb::ValueObjectSP ScriptedSyntheticChildren::FrontEnd::GetSyntheticValue() {
204   if (!m_wrapper_sp || m_interpreter == NULL)
205     return nullptr;
206
207   return m_interpreter->GetSyntheticValue(m_wrapper_sp);
208 }
209
210 ConstString ScriptedSyntheticChildren::FrontEnd::GetSyntheticTypeName() {
211   if (!m_wrapper_sp || m_interpreter == NULL)
212     return ConstString();
213
214   return m_interpreter->GetSyntheticTypeName(m_wrapper_sp);
215 }
216
217 std::string ScriptedSyntheticChildren::GetDescription() {
218   StreamString sstr;
219   sstr.Printf("%s%s%s Python class %s", Cascades() ? "" : " (not cascading)",
220               SkipsPointers() ? " (skip pointers)" : "",
221               SkipsReferences() ? " (skip references)" : "",
222               m_python_class.c_str());
223
224   return sstr.GetString();
225 }
226
227 #endif // #ifndef LLDB_DISABLE_PYTHON