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