1 //===-- SBTypeCategory.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "lldb/API/SBTypeCategory.h"
14 #include "lldb/API/SBTypeFilter.h"
15 #include "lldb/API/SBTypeFormat.h"
16 #include "lldb/API/SBTypeSummary.h"
17 #include "lldb/API/SBTypeSynthetic.h"
18 #include "lldb/API/SBTypeNameSpecifier.h"
19 #include "lldb/API/SBStream.h"
21 #include "lldb/Core/Debugger.h"
22 #include "lldb/DataFormatters/DataVisualization.h"
23 #include "lldb/Interpreter/CommandInterpreter.h"
24 #include "lldb/Interpreter/ScriptInterpreter.h"
27 using namespace lldb_private;
29 typedef std::pair<lldb::TypeCategoryImplSP,user_id_t> ImplType;
31 SBTypeCategory::SBTypeCategory() :
36 SBTypeCategory::SBTypeCategory (const char* name) :
39 DataVisualization::Categories::GetCategory(ConstString(name), m_opaque_sp);
42 SBTypeCategory::SBTypeCategory (const lldb::SBTypeCategory &rhs) :
43 m_opaque_sp(rhs.m_opaque_sp)
47 SBTypeCategory::~SBTypeCategory ()
52 SBTypeCategory::IsValid() const
54 return (m_opaque_sp.get() != NULL);
58 SBTypeCategory::GetEnabled ()
62 return m_opaque_sp->IsEnabled();
66 SBTypeCategory::SetEnabled (bool enabled)
71 DataVisualization::Categories::Enable(m_opaque_sp);
73 DataVisualization::Categories::Disable(m_opaque_sp);
77 SBTypeCategory::GetName()
81 return m_opaque_sp->GetName();
85 SBTypeCategory::GetNumFormats ()
87 if (!IsDefaultCategory())
90 return DataVisualization::ValueFormats::GetCount();
94 SBTypeCategory::GetNumSummaries ()
98 return m_opaque_sp->GetSummaryNavigator()->GetCount() + m_opaque_sp->GetRegexSummaryNavigator()->GetCount();
102 SBTypeCategory::GetNumFilters ()
106 return m_opaque_sp->GetFilterNavigator()->GetCount() + m_opaque_sp->GetRegexFilterNavigator()->GetCount();
109 #ifndef LLDB_DISABLE_PYTHON
111 SBTypeCategory::GetNumSynthetics ()
115 return m_opaque_sp->GetSyntheticNavigator()->GetCount() + m_opaque_sp->GetRegexSyntheticNavigator()->GetCount();
119 lldb::SBTypeNameSpecifier
120 SBTypeCategory::GetTypeNameSpecifierForFilterAtIndex (uint32_t index)
123 return SBTypeNameSpecifier();
124 return SBTypeNameSpecifier(m_opaque_sp->GetTypeNameSpecifierForFilterAtIndex(index));
127 lldb::SBTypeNameSpecifier
128 SBTypeCategory::GetTypeNameSpecifierForFormatAtIndex (uint32_t index)
130 if (!IsDefaultCategory())
131 return SBTypeNameSpecifier();
132 return SBTypeNameSpecifier(DataVisualization::ValueFormats::GetTypeNameSpecifierForFormatAtIndex(index));
135 lldb::SBTypeNameSpecifier
136 SBTypeCategory::GetTypeNameSpecifierForSummaryAtIndex (uint32_t index)
139 return SBTypeNameSpecifier();
140 return SBTypeNameSpecifier(m_opaque_sp->GetTypeNameSpecifierForSummaryAtIndex(index));
143 #ifndef LLDB_DISABLE_PYTHON
144 lldb::SBTypeNameSpecifier
145 SBTypeCategory::GetTypeNameSpecifierForSyntheticAtIndex (uint32_t index)
148 return SBTypeNameSpecifier();
149 return SBTypeNameSpecifier(m_opaque_sp->GetTypeNameSpecifierForSyntheticAtIndex(index));
154 SBTypeCategory::GetFilterForType (SBTypeNameSpecifier spec)
157 return SBTypeFilter();
160 return SBTypeFilter();
162 lldb::SyntheticChildrenSP children_sp;
165 m_opaque_sp->GetRegexFilterNavigator()->GetExact(ConstString(spec.GetName()), children_sp);
167 m_opaque_sp->GetFilterNavigator()->GetExact(ConstString(spec.GetName()), children_sp);
170 return lldb::SBTypeFilter();
172 TypeFilterImplSP filter_sp = std::static_pointer_cast<TypeFilterImpl>(children_sp);
174 return lldb::SBTypeFilter(filter_sp);
178 SBTypeCategory::GetFormatForType (SBTypeNameSpecifier spec)
180 if (!IsDefaultCategory())
181 return SBTypeFormat();
184 return SBTypeFormat();
187 return SBTypeFormat();
189 return SBTypeFormat(DataVisualization::ValueFormats::GetFormat(ConstString(spec.GetName())));
192 #ifndef LLDB_DISABLE_PYTHON
194 SBTypeCategory::GetSummaryForType (SBTypeNameSpecifier spec)
197 return SBTypeSummary();
200 return SBTypeSummary();
202 lldb::TypeSummaryImplSP summary_sp;
205 m_opaque_sp->GetRegexSummaryNavigator()->GetExact(ConstString(spec.GetName()), summary_sp);
207 m_opaque_sp->GetSummaryNavigator()->GetExact(ConstString(spec.GetName()), summary_sp);
210 return lldb::SBTypeSummary();
212 return lldb::SBTypeSummary(summary_sp);
214 #endif // LLDB_DISABLE_PYTHON
216 #ifndef LLDB_DISABLE_PYTHON
218 SBTypeCategory::GetSyntheticForType (SBTypeNameSpecifier spec)
221 return SBTypeSynthetic();
224 return SBTypeSynthetic();
226 lldb::SyntheticChildrenSP children_sp;
229 m_opaque_sp->GetRegexSyntheticNavigator()->GetExact(ConstString(spec.GetName()), children_sp);
231 m_opaque_sp->GetSyntheticNavigator()->GetExact(ConstString(spec.GetName()), children_sp);
234 return lldb::SBTypeSynthetic();
236 ScriptedSyntheticChildrenSP synth_sp = std::static_pointer_cast<ScriptedSyntheticChildren>(children_sp);
238 return lldb::SBTypeSynthetic(synth_sp);
242 #ifndef LLDB_DISABLE_PYTHON
244 SBTypeCategory::GetFilterAtIndex (uint32_t index)
247 return SBTypeFilter();
248 lldb::SyntheticChildrenSP children_sp = m_opaque_sp->GetSyntheticAtIndex((index));
250 if (!children_sp.get())
251 return lldb::SBTypeFilter();
253 TypeFilterImplSP filter_sp = std::static_pointer_cast<TypeFilterImpl>(children_sp);
255 return lldb::SBTypeFilter(filter_sp);
260 SBTypeCategory::GetFormatAtIndex (uint32_t index)
262 if (!IsDefaultCategory())
263 return SBTypeFormat();
264 return SBTypeFormat(DataVisualization::ValueFormats::GetFormatAtIndex((index)));
267 #ifndef LLDB_DISABLE_PYTHON
269 SBTypeCategory::GetSummaryAtIndex (uint32_t index)
272 return SBTypeSummary();
273 return SBTypeSummary(m_opaque_sp->GetSummaryAtIndex((index)));
277 #ifndef LLDB_DISABLE_PYTHON
279 SBTypeCategory::GetSyntheticAtIndex (uint32_t index)
282 return SBTypeSynthetic();
283 lldb::SyntheticChildrenSP children_sp = m_opaque_sp->GetSyntheticAtIndex((index));
285 if (!children_sp.get())
286 return lldb::SBTypeSynthetic();
288 ScriptedSyntheticChildrenSP synth_sp = std::static_pointer_cast<ScriptedSyntheticChildren>(children_sp);
290 return lldb::SBTypeSynthetic(synth_sp);
295 SBTypeCategory::AddTypeFormat (SBTypeNameSpecifier type_name,
298 if (!IsDefaultCategory())
301 if (!type_name.IsValid())
304 if (!format.IsValid())
307 if (type_name.IsRegex())
310 DataVisualization::ValueFormats::Add(ConstString(type_name.GetName()), format.GetSP());
316 SBTypeCategory::DeleteTypeFormat (SBTypeNameSpecifier type_name)
318 if (!IsDefaultCategory())
321 if (!type_name.IsValid())
324 if (type_name.IsRegex())
327 return DataVisualization::ValueFormats::Delete(ConstString(type_name.GetName()));
330 #ifndef LLDB_DISABLE_PYTHON
332 SBTypeCategory::AddTypeSummary (SBTypeNameSpecifier type_name,
333 SBTypeSummary summary)
338 if (!type_name.IsValid())
341 if (!summary.IsValid())
344 // FIXME: we need to iterate over all the Debugger objects and have each of them contain a copy of the function
345 // since we currently have formatters live in a global space, while Python code lives in a specific Debugger-related environment
346 // this should eventually be fixed by deciding a final location in the LLDB object space for formatters
347 if (summary.IsFunctionCode())
349 void *name_token = (void*)ConstString(type_name.GetName()).GetCString();
350 const char* script = summary.GetData();
351 StringList input; input.SplitIntoLines(script, strlen(script));
352 uint32_t num_debuggers = lldb_private::Debugger::GetNumDebuggers();
353 bool need_set = true;
358 DebuggerSP debugger_sp = lldb_private::Debugger::GetDebuggerAtIndex(j);
361 ScriptInterpreter* interpreter_ptr = debugger_sp->GetCommandInterpreter().GetScriptInterpreter();
365 if (interpreter_ptr->GenerateTypeScriptFunction(input, output, name_token) && !output.empty())
370 summary.SetFunctionName(output.c_str());
378 if (type_name.IsRegex())
379 m_opaque_sp->GetRegexSummaryNavigator()->Add(lldb::RegularExpressionSP(new RegularExpression(type_name.GetName())), summary.GetSP());
381 m_opaque_sp->GetSummaryNavigator()->Add(ConstString(type_name.GetName()), summary.GetSP());
388 SBTypeCategory::DeleteTypeSummary (SBTypeNameSpecifier type_name)
393 if (!type_name.IsValid())
396 if (type_name.IsRegex())
397 return m_opaque_sp->GetRegexSummaryNavigator()->Delete(ConstString(type_name.GetName()));
399 return m_opaque_sp->GetSummaryNavigator()->Delete(ConstString(type_name.GetName()));
403 SBTypeCategory::AddTypeFilter (SBTypeNameSpecifier type_name,
409 if (!type_name.IsValid())
412 if (!filter.IsValid())
415 if (type_name.IsRegex())
416 m_opaque_sp->GetRegexFilterNavigator()->Add(lldb::RegularExpressionSP(new RegularExpression(type_name.GetName())), filter.GetSP());
418 m_opaque_sp->GetFilterNavigator()->Add(ConstString(type_name.GetName()), filter.GetSP());
424 SBTypeCategory::DeleteTypeFilter (SBTypeNameSpecifier type_name)
429 if (!type_name.IsValid())
432 if (type_name.IsRegex())
433 return m_opaque_sp->GetRegexFilterNavigator()->Delete(ConstString(type_name.GetName()));
435 return m_opaque_sp->GetFilterNavigator()->Delete(ConstString(type_name.GetName()));
438 #ifndef LLDB_DISABLE_PYTHON
440 SBTypeCategory::AddTypeSynthetic (SBTypeNameSpecifier type_name,
441 SBTypeSynthetic synth)
446 if (!type_name.IsValid())
449 if (!synth.IsValid())
452 // FIXME: we need to iterate over all the Debugger objects and have each of them contain a copy of the function
453 // since we currently have formatters live in a global space, while Python code lives in a specific Debugger-related environment
454 // this should eventually be fixed by deciding a final location in the LLDB object space for formatters
455 if (synth.IsClassCode())
457 void *name_token = (void*)ConstString(type_name.GetName()).GetCString();
458 const char* script = synth.GetData();
459 StringList input; input.SplitIntoLines(script, strlen(script));
460 uint32_t num_debuggers = lldb_private::Debugger::GetNumDebuggers();
461 bool need_set = true;
466 DebuggerSP debugger_sp = lldb_private::Debugger::GetDebuggerAtIndex(j);
469 ScriptInterpreter* interpreter_ptr = debugger_sp->GetCommandInterpreter().GetScriptInterpreter();
473 if (interpreter_ptr->GenerateTypeSynthClass(input, output, name_token) && !output.empty())
478 synth.SetClassName(output.c_str());
486 if (type_name.IsRegex())
487 m_opaque_sp->GetRegexSyntheticNavigator()->Add(lldb::RegularExpressionSP(new RegularExpression(type_name.GetName())), synth.GetSP());
489 m_opaque_sp->GetSyntheticNavigator()->Add(ConstString(type_name.GetName()), synth.GetSP());
495 SBTypeCategory::DeleteTypeSynthetic (SBTypeNameSpecifier type_name)
500 if (!type_name.IsValid())
503 if (type_name.IsRegex())
504 return m_opaque_sp->GetRegexSyntheticNavigator()->Delete(ConstString(type_name.GetName()));
506 return m_opaque_sp->GetSyntheticNavigator()->Delete(ConstString(type_name.GetName()));
508 #endif // LLDB_DISABLE_PYTHON
511 SBTypeCategory::GetDescription (lldb::SBStream &description,
512 lldb::DescriptionLevel description_level)
516 description.Printf("Category name: %s\n",GetName());
520 lldb::SBTypeCategory &
521 SBTypeCategory::operator = (const lldb::SBTypeCategory &rhs)
525 m_opaque_sp = rhs.m_opaque_sp;
531 SBTypeCategory::operator == (lldb::SBTypeCategory &rhs)
533 if (IsValid() == false)
534 return !rhs.IsValid();
536 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
541 SBTypeCategory::operator != (lldb::SBTypeCategory &rhs)
543 if (IsValid() == false)
544 return rhs.IsValid();
546 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
549 lldb::TypeCategoryImplSP
550 SBTypeCategory::GetSP ()
553 return lldb::TypeCategoryImplSP();
558 SBTypeCategory::SetSP (const lldb::TypeCategoryImplSP &typecategory_impl_sp)
560 m_opaque_sp = typecategory_impl_sp;
563 SBTypeCategory::SBTypeCategory (const lldb::TypeCategoryImplSP &typecategory_impl_sp) :
564 m_opaque_sp(typecategory_impl_sp)
569 SBTypeCategory::IsDefaultCategory()
574 return (strcmp(m_opaque_sp->GetName(),"default") == 0);