]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/API/SBTypeCategory.cpp
MFV r304060:
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / API / SBTypeCategory.cpp
1 //===-- SBTypeCategory.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/API/SBTypeCategory.h"
11
12 #include "lldb/API/SBTypeFilter.h"
13 #include "lldb/API/SBTypeFormat.h"
14 #include "lldb/API/SBTypeSummary.h"
15 #include "lldb/API/SBTypeSynthetic.h"
16 #include "lldb/API/SBTypeNameSpecifier.h"
17 #include "lldb/API/SBStream.h"
18
19 #include "lldb/Core/Debugger.h"
20 #include "lldb/DataFormatters/DataVisualization.h"
21 #include "lldb/Interpreter/CommandInterpreter.h"
22 #include "lldb/Interpreter/ScriptInterpreter.h"
23
24 using namespace lldb;
25 using namespace lldb_private;
26
27 typedef std::pair<lldb::TypeCategoryImplSP,user_id_t> ImplType;
28
29 SBTypeCategory::SBTypeCategory() :
30 m_opaque_sp()
31 {
32 }
33
34 SBTypeCategory::SBTypeCategory (const char* name) :
35 m_opaque_sp()
36 {
37     DataVisualization::Categories::GetCategory(ConstString(name), m_opaque_sp);
38 }
39
40 SBTypeCategory::SBTypeCategory (const lldb::SBTypeCategory &rhs) :
41 m_opaque_sp(rhs.m_opaque_sp)
42 {
43 }
44
45 SBTypeCategory::~SBTypeCategory ()
46 {
47 }
48
49 bool
50 SBTypeCategory::IsValid() const
51 {
52     return (m_opaque_sp.get() != NULL);
53 }
54
55 bool
56 SBTypeCategory::GetEnabled ()
57 {
58     if (!IsValid())
59         return false;
60     return m_opaque_sp->IsEnabled();
61 }
62
63 void
64 SBTypeCategory::SetEnabled (bool enabled)
65 {
66     if (!IsValid())
67         return;
68     if (enabled)
69         DataVisualization::Categories::Enable(m_opaque_sp);
70     else
71         DataVisualization::Categories::Disable(m_opaque_sp);
72 }
73
74 const char*
75 SBTypeCategory::GetName()
76 {
77     if (!IsValid())
78         return NULL;
79     return m_opaque_sp->GetName();
80 }
81
82 lldb::LanguageType
83 SBTypeCategory::GetLanguageAtIndex (uint32_t idx)
84 {
85     if (IsValid())
86         return m_opaque_sp->GetLanguageAtIndex(idx);
87     return lldb::eLanguageTypeUnknown;
88 }
89
90 uint32_t
91 SBTypeCategory::GetNumLanguages ()
92 {
93     if (IsValid())
94         return m_opaque_sp->GetNumLanguages();
95     return 0;
96 }
97
98 void
99 SBTypeCategory::AddLanguage (lldb::LanguageType language)
100 {
101     if (IsValid())
102         m_opaque_sp->AddLanguage(language);
103 }
104
105 uint32_t
106 SBTypeCategory::GetNumFormats ()
107 {
108     if (!IsValid())
109         return 0;
110     
111     return m_opaque_sp->GetTypeFormatsContainer()->GetCount() + m_opaque_sp->GetRegexTypeFormatsContainer()->GetCount();
112 }
113
114 uint32_t
115 SBTypeCategory::GetNumSummaries ()
116 {
117     if (!IsValid())
118         return 0;
119     return m_opaque_sp->GetTypeSummariesContainer()->GetCount() + m_opaque_sp->GetRegexTypeSummariesContainer()->GetCount();
120 }
121
122 uint32_t
123 SBTypeCategory::GetNumFilters ()
124 {
125     if (!IsValid())
126         return 0;
127     return m_opaque_sp->GetTypeFiltersContainer()->GetCount() + m_opaque_sp->GetRegexTypeFiltersContainer()->GetCount();
128 }
129
130 #ifndef LLDB_DISABLE_PYTHON
131 uint32_t
132 SBTypeCategory::GetNumSynthetics ()
133 {
134     if (!IsValid())
135         return 0;
136     return m_opaque_sp->GetTypeSyntheticsContainer()->GetCount() + m_opaque_sp->GetRegexTypeSyntheticsContainer()->GetCount();
137 }
138 #endif
139
140 lldb::SBTypeNameSpecifier
141 SBTypeCategory::GetTypeNameSpecifierForFilterAtIndex (uint32_t index)
142 {
143     if (!IsValid())
144         return SBTypeNameSpecifier();
145     return SBTypeNameSpecifier(m_opaque_sp->GetTypeNameSpecifierForFilterAtIndex(index));
146 }
147
148 lldb::SBTypeNameSpecifier
149 SBTypeCategory::GetTypeNameSpecifierForFormatAtIndex (uint32_t index)
150 {
151     if (!IsValid())
152         return SBTypeNameSpecifier();
153     return SBTypeNameSpecifier(m_opaque_sp->GetTypeNameSpecifierForFormatAtIndex(index));
154 }
155
156 lldb::SBTypeNameSpecifier
157 SBTypeCategory::GetTypeNameSpecifierForSummaryAtIndex (uint32_t index)
158 {
159     if (!IsValid())
160         return SBTypeNameSpecifier();
161     return SBTypeNameSpecifier(m_opaque_sp->GetTypeNameSpecifierForSummaryAtIndex(index));
162 }
163
164 #ifndef LLDB_DISABLE_PYTHON
165 lldb::SBTypeNameSpecifier
166 SBTypeCategory::GetTypeNameSpecifierForSyntheticAtIndex (uint32_t index)
167 {
168     if (!IsValid())
169         return SBTypeNameSpecifier();
170     return SBTypeNameSpecifier(m_opaque_sp->GetTypeNameSpecifierForSyntheticAtIndex(index));
171 }
172 #endif
173
174 SBTypeFilter
175 SBTypeCategory::GetFilterForType (SBTypeNameSpecifier spec)
176 {
177     if (!IsValid())
178         return SBTypeFilter();
179     
180     if (!spec.IsValid())
181         return SBTypeFilter();
182     
183     lldb::TypeFilterImplSP children_sp;
184     
185     if (spec.IsRegex())
186         m_opaque_sp->GetRegexTypeFiltersContainer()->GetExact(ConstString(spec.GetName()), children_sp);
187     else
188         m_opaque_sp->GetTypeFiltersContainer()->GetExact(ConstString(spec.GetName()), children_sp);
189         
190     if (!children_sp)
191         return lldb::SBTypeFilter();
192     
193     TypeFilterImplSP filter_sp = std::static_pointer_cast<TypeFilterImpl>(children_sp);
194     
195     return lldb::SBTypeFilter(filter_sp);
196
197 }
198 SBTypeFormat
199 SBTypeCategory::GetFormatForType (SBTypeNameSpecifier spec)
200 {
201     if (!IsValid())
202         return SBTypeFormat();
203         
204     if (!spec.IsValid())
205         return SBTypeFormat();
206     
207     lldb::TypeFormatImplSP format_sp;
208     
209     if (spec.IsRegex())
210         m_opaque_sp->GetRegexTypeFormatsContainer()->GetExact(ConstString(spec.GetName()), format_sp);
211     else
212         m_opaque_sp->GetTypeFormatsContainer()->GetExact(ConstString(spec.GetName()), format_sp);
213     
214     if (!format_sp)
215         return lldb::SBTypeFormat();
216     
217     return lldb::SBTypeFormat(format_sp);
218 }
219
220 #ifndef LLDB_DISABLE_PYTHON
221 SBTypeSummary
222 SBTypeCategory::GetSummaryForType (SBTypeNameSpecifier spec)
223 {
224     if (!IsValid())
225         return SBTypeSummary();
226     
227     if (!spec.IsValid())
228         return SBTypeSummary();
229     
230     lldb::TypeSummaryImplSP summary_sp;
231     
232     if (spec.IsRegex())
233         m_opaque_sp->GetRegexTypeSummariesContainer()->GetExact(ConstString(spec.GetName()), summary_sp);
234     else
235         m_opaque_sp->GetTypeSummariesContainer()->GetExact(ConstString(spec.GetName()), summary_sp);
236     
237     if (!summary_sp)
238         return lldb::SBTypeSummary();
239     
240     return lldb::SBTypeSummary(summary_sp);
241 }
242 #endif // LLDB_DISABLE_PYTHON
243
244 #ifndef LLDB_DISABLE_PYTHON
245 SBTypeSynthetic
246 SBTypeCategory::GetSyntheticForType (SBTypeNameSpecifier spec)
247 {
248     if (!IsValid())
249         return SBTypeSynthetic();
250     
251     if (!spec.IsValid())
252         return SBTypeSynthetic();
253     
254     lldb::SyntheticChildrenSP children_sp;
255     
256     if (spec.IsRegex())
257         m_opaque_sp->GetRegexTypeSyntheticsContainer()->GetExact(ConstString(spec.GetName()), children_sp);
258     else
259         m_opaque_sp->GetTypeSyntheticsContainer()->GetExact(ConstString(spec.GetName()), children_sp);
260     
261     if (!children_sp)
262         return lldb::SBTypeSynthetic();
263     
264     ScriptedSyntheticChildrenSP synth_sp = std::static_pointer_cast<ScriptedSyntheticChildren>(children_sp);
265     
266     return lldb::SBTypeSynthetic(synth_sp);
267 }
268 #endif
269
270 #ifndef LLDB_DISABLE_PYTHON
271 SBTypeFilter
272 SBTypeCategory::GetFilterAtIndex (uint32_t index)
273 {
274     if (!IsValid())
275         return SBTypeFilter();
276     lldb::SyntheticChildrenSP children_sp = m_opaque_sp->GetSyntheticAtIndex((index));
277     
278     if (!children_sp.get())
279         return lldb::SBTypeFilter();
280     
281     TypeFilterImplSP filter_sp = std::static_pointer_cast<TypeFilterImpl>(children_sp);
282     
283     return lldb::SBTypeFilter(filter_sp);
284 }
285 #endif
286
287 SBTypeFormat
288 SBTypeCategory::GetFormatAtIndex (uint32_t index)
289 {
290     if (!IsValid())
291         return SBTypeFormat();
292     return SBTypeFormat(m_opaque_sp->GetFormatAtIndex((index)));
293 }
294
295 #ifndef LLDB_DISABLE_PYTHON
296 SBTypeSummary
297 SBTypeCategory::GetSummaryAtIndex (uint32_t index)
298 {
299     if (!IsValid())
300         return SBTypeSummary();
301     return SBTypeSummary(m_opaque_sp->GetSummaryAtIndex((index)));
302 }
303 #endif
304
305 #ifndef LLDB_DISABLE_PYTHON
306 SBTypeSynthetic
307 SBTypeCategory::GetSyntheticAtIndex (uint32_t index)
308 {
309     if (!IsValid())
310         return SBTypeSynthetic();
311     lldb::SyntheticChildrenSP children_sp = m_opaque_sp->GetSyntheticAtIndex((index));
312     
313     if (!children_sp.get())
314         return lldb::SBTypeSynthetic();
315     
316     ScriptedSyntheticChildrenSP synth_sp = std::static_pointer_cast<ScriptedSyntheticChildren>(children_sp);
317     
318     return lldb::SBTypeSynthetic(synth_sp);
319 }
320 #endif
321
322 bool
323 SBTypeCategory::AddTypeFormat (SBTypeNameSpecifier type_name,
324                                SBTypeFormat format)
325 {
326     if (!IsValid())
327         return false;
328     
329     if (!type_name.IsValid())
330         return false;
331     
332     if (!format.IsValid())
333         return false;
334     
335     if (type_name.IsRegex())
336         m_opaque_sp->GetRegexTypeFormatsContainer()->Add(lldb::RegularExpressionSP(new RegularExpression(type_name.GetName())), format.GetSP());
337     else
338         m_opaque_sp->GetTypeFormatsContainer()->Add(ConstString(type_name.GetName()), format.GetSP());
339     
340     return true;
341 }
342
343 bool
344 SBTypeCategory::DeleteTypeFormat (SBTypeNameSpecifier type_name)
345 {
346     if (!IsValid())
347         return false;
348     
349     if (!type_name.IsValid())
350         return false;
351     
352     if (type_name.IsRegex())
353         return m_opaque_sp->GetRegexTypeFormatsContainer()->Delete(ConstString(type_name.GetName()));
354     else
355         return m_opaque_sp->GetTypeFormatsContainer()->Delete(ConstString(type_name.GetName()));
356 }
357
358 #ifndef LLDB_DISABLE_PYTHON
359 bool
360 SBTypeCategory::AddTypeSummary (SBTypeNameSpecifier type_name,
361                                 SBTypeSummary summary)
362 {
363     if (!IsValid())
364         return false;
365     
366     if (!type_name.IsValid())
367         return false;
368     
369     if (!summary.IsValid())
370         return false;
371     
372     // FIXME: we need to iterate over all the Debugger objects and have each of them contain a copy of the function
373     // since we currently have formatters live in a global space, while Python code lives in a specific Debugger-related environment
374     // this should eventually be fixed by deciding a final location in the LLDB object space for formatters
375     if (summary.IsFunctionCode())
376     {
377         const void *name_token = (const void*)ConstString(type_name.GetName()).GetCString();
378         const char* script = summary.GetData();
379         StringList input; input.SplitIntoLines(script, strlen(script));
380         uint32_t num_debuggers = lldb_private::Debugger::GetNumDebuggers();
381         bool need_set = true;
382         for (uint32_t j = 0;
383              j < num_debuggers;
384              j++)
385         {
386             DebuggerSP debugger_sp = lldb_private::Debugger::GetDebuggerAtIndex(j);
387             if (debugger_sp)
388             {
389                 ScriptInterpreter* interpreter_ptr = debugger_sp->GetCommandInterpreter().GetScriptInterpreter();
390                 if (interpreter_ptr)
391                 {
392                     std::string output;
393                     if (interpreter_ptr->GenerateTypeScriptFunction(input, output, name_token) && !output.empty())
394                     {
395                         if (need_set)
396                         {
397                             need_set = false;
398                             summary.SetFunctionName(output.c_str());
399                         }
400                     }
401                 }
402             }
403         }
404     }
405     
406     if (type_name.IsRegex())
407         m_opaque_sp->GetRegexTypeSummariesContainer()->Add(lldb::RegularExpressionSP(new RegularExpression(type_name.GetName())), summary.GetSP());
408     else
409         m_opaque_sp->GetTypeSummariesContainer()->Add(ConstString(type_name.GetName()), summary.GetSP());
410     
411     return true;
412 }
413 #endif
414
415 bool
416 SBTypeCategory::DeleteTypeSummary (SBTypeNameSpecifier type_name)
417 {
418     if (!IsValid())
419         return false;
420     
421     if (!type_name.IsValid())
422         return false;
423     
424     if (type_name.IsRegex())
425         return m_opaque_sp->GetRegexTypeSummariesContainer()->Delete(ConstString(type_name.GetName()));
426     else
427         return m_opaque_sp->GetTypeSummariesContainer()->Delete(ConstString(type_name.GetName()));
428 }
429
430 bool
431 SBTypeCategory::AddTypeFilter (SBTypeNameSpecifier type_name,
432                                SBTypeFilter filter)
433 {
434     if (!IsValid())
435         return false;
436     
437     if (!type_name.IsValid())
438         return false;
439     
440     if (!filter.IsValid())
441         return false;
442     
443     if (type_name.IsRegex())
444         m_opaque_sp->GetRegexTypeFiltersContainer()->Add(lldb::RegularExpressionSP(new RegularExpression(type_name.GetName())), filter.GetSP());
445     else
446         m_opaque_sp->GetTypeFiltersContainer()->Add(ConstString(type_name.GetName()), filter.GetSP());
447     
448     return true;
449 }
450
451 bool
452 SBTypeCategory::DeleteTypeFilter (SBTypeNameSpecifier type_name)
453 {
454     if (!IsValid())
455         return false;
456     
457     if (!type_name.IsValid())
458         return false;
459     
460     if (type_name.IsRegex())
461         return m_opaque_sp->GetRegexTypeFiltersContainer()->Delete(ConstString(type_name.GetName()));
462     else
463         return m_opaque_sp->GetTypeFiltersContainer()->Delete(ConstString(type_name.GetName()));
464 }
465
466 #ifndef LLDB_DISABLE_PYTHON
467 bool
468 SBTypeCategory::AddTypeSynthetic (SBTypeNameSpecifier type_name,
469                                   SBTypeSynthetic synth)
470 {
471     if (!IsValid())
472         return false;
473     
474     if (!type_name.IsValid())
475         return false;
476     
477     if (!synth.IsValid())
478         return false;
479
480     // FIXME: we need to iterate over all the Debugger objects and have each of them contain a copy of the function
481     // since we currently have formatters live in a global space, while Python code lives in a specific Debugger-related environment
482     // this should eventually be fixed by deciding a final location in the LLDB object space for formatters
483     if (synth.IsClassCode())
484     {
485         const void *name_token = (const void*)ConstString(type_name.GetName()).GetCString();
486         const char* script = synth.GetData();
487         StringList input; input.SplitIntoLines(script, strlen(script));
488         uint32_t num_debuggers = lldb_private::Debugger::GetNumDebuggers();
489         bool need_set = true;
490         for (uint32_t j = 0;
491              j < num_debuggers;
492              j++)
493         {
494             DebuggerSP debugger_sp = lldb_private::Debugger::GetDebuggerAtIndex(j);
495             if (debugger_sp)
496             {
497                 ScriptInterpreter* interpreter_ptr = debugger_sp->GetCommandInterpreter().GetScriptInterpreter();
498                 if (interpreter_ptr)
499                 {
500                     std::string output;
501                     if (interpreter_ptr->GenerateTypeSynthClass(input, output, name_token) && !output.empty())
502                     {
503                         if (need_set)
504                         {
505                             need_set = false;
506                             synth.SetClassName(output.c_str());
507                         }
508                     }
509                 }
510             }
511         }
512     }
513     
514     if (type_name.IsRegex())
515         m_opaque_sp->GetRegexTypeSyntheticsContainer()->Add(lldb::RegularExpressionSP(new RegularExpression(type_name.GetName())), synth.GetSP());
516     else
517         m_opaque_sp->GetTypeSyntheticsContainer()->Add(ConstString(type_name.GetName()), synth.GetSP());
518     
519     return true;
520 }
521
522 bool
523 SBTypeCategory::DeleteTypeSynthetic (SBTypeNameSpecifier type_name)
524 {
525     if (!IsValid())
526         return false;
527     
528     if (!type_name.IsValid())
529         return false;
530     
531     if (type_name.IsRegex())
532         return m_opaque_sp->GetRegexTypeSyntheticsContainer()->Delete(ConstString(type_name.GetName()));
533     else
534         return m_opaque_sp->GetTypeSyntheticsContainer()->Delete(ConstString(type_name.GetName()));
535 }
536 #endif // LLDB_DISABLE_PYTHON
537
538 bool
539 SBTypeCategory::GetDescription (lldb::SBStream &description, 
540                 lldb::DescriptionLevel description_level)
541 {
542     if (!IsValid())
543         return false;
544     description.Printf("Category name: %s\n",GetName());
545     return true;
546 }
547
548 lldb::SBTypeCategory &
549 SBTypeCategory::operator = (const lldb::SBTypeCategory &rhs)
550 {
551     if (this != &rhs)
552     {
553         m_opaque_sp = rhs.m_opaque_sp;
554     }
555     return *this;
556 }
557
558 bool
559 SBTypeCategory::operator == (lldb::SBTypeCategory &rhs)
560 {
561     if (IsValid() == false)
562         return !rhs.IsValid();
563     
564     return m_opaque_sp.get() == rhs.m_opaque_sp.get();
565     
566 }
567
568 bool
569 SBTypeCategory::operator != (lldb::SBTypeCategory &rhs)
570 {
571     if (IsValid() == false)
572         return rhs.IsValid();
573     
574     return m_opaque_sp.get() != rhs.m_opaque_sp.get();
575 }
576
577 lldb::TypeCategoryImplSP
578 SBTypeCategory::GetSP ()
579 {
580     if (!IsValid())
581         return lldb::TypeCategoryImplSP();
582     return m_opaque_sp;
583 }
584
585 void
586 SBTypeCategory::SetSP (const lldb::TypeCategoryImplSP &typecategory_impl_sp)
587 {
588     m_opaque_sp = typecategory_impl_sp;
589 }
590
591 SBTypeCategory::SBTypeCategory (const lldb::TypeCategoryImplSP &typecategory_impl_sp) :
592 m_opaque_sp(typecategory_impl_sp)
593 {
594 }
595
596 bool
597 SBTypeCategory::IsDefaultCategory()
598 {
599     if (!IsValid())
600         return false;
601     
602     return (strcmp(m_opaque_sp->GetName(),"default") == 0);
603 }
604