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