]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/DataFormatters/TypeCategory.cpp
Merge from head
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / DataFormatters / TypeCategory.cpp
1 //===-- TypeCategory.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/DataFormatters/TypeCategory.h"
11
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16
17 using namespace lldb;
18 using namespace lldb_private;
19
20 TypeCategoryImpl::TypeCategoryImpl(IFormatChangeListener* clist,
21                                    ConstString name) :
22 m_format_cont("format","regex-format",clist),
23 m_summary_cont("summary","regex-summary",clist),
24 m_filter_cont("filter","regex-filter",clist),
25 #ifndef LLDB_DISABLE_PYTHON
26 m_synth_cont("synth","regex-synth",clist),
27 #endif
28 m_validator_cont("validator","regex-validator",clist),
29 m_enabled(false),
30 m_change_listener(clist),
31 m_mutex(Mutex::eMutexTypeRecursive),
32 m_name(name)
33 {}
34
35 bool
36 TypeCategoryImpl::Get (ValueObject& valobj,
37                        const FormattersMatchVector& candidates,
38                        lldb::TypeFormatImplSP& entry,
39                        uint32_t* reason)
40 {
41     if (!IsEnabled())
42         return false;
43     if (GetTypeFormatsContainer()->Get(candidates, entry, reason))
44         return true;
45     bool regex = GetRegexTypeFormatsContainer()->Get(candidates, entry, reason);
46     if (regex && reason)
47         *reason |= lldb_private::eFormatterChoiceCriterionRegularExpressionSummary;
48     return regex;
49 }
50
51 bool
52 TypeCategoryImpl::Get (ValueObject& valobj,
53                        const FormattersMatchVector& candidates,
54                        lldb::TypeSummaryImplSP& entry,
55                        uint32_t* reason)
56 {
57     if (!IsEnabled())
58         return false;
59     if (GetTypeSummariesContainer()->Get(candidates, entry, reason))
60         return true;
61     bool regex = GetRegexTypeSummariesContainer()->Get(candidates, entry, reason);
62     if (regex && reason)
63         *reason |= lldb_private::eFormatterChoiceCriterionRegularExpressionSummary;
64     return regex;
65 }
66
67 bool
68 TypeCategoryImpl::Get (ValueObject& valobj,
69                        const FormattersMatchVector& candidates,
70                        lldb::SyntheticChildrenSP& entry,
71                        uint32_t* reason)
72 {
73     if (!IsEnabled())
74         return false;
75     TypeFilterImpl::SharedPointer filter_sp;
76     uint32_t reason_filter = 0;
77     bool regex_filter = false;
78     // first find both Filter and Synth, and then check which is most recent
79     
80     if (!GetTypeFiltersContainer()->Get(candidates, filter_sp, &reason_filter))
81         regex_filter = GetRegexTypeFiltersContainer()->Get (candidates, filter_sp, &reason_filter);
82     
83 #ifndef LLDB_DISABLE_PYTHON
84     bool regex_synth = false;
85     uint32_t reason_synth = 0;
86     bool pick_synth = false;
87     ScriptedSyntheticChildren::SharedPointer synth;
88     if (!GetTypeSyntheticsContainer()->Get(candidates, synth, &reason_synth))
89         regex_synth = GetRegexTypeSyntheticsContainer()->Get (candidates, synth, &reason_synth);
90     if (!filter_sp.get() && !synth.get())
91         return false;
92     else if (!filter_sp.get() && synth.get())
93         pick_synth = true;
94     
95     else if (filter_sp.get() && !synth.get())
96         pick_synth = false;
97     
98     else /*if (filter_sp.get() && synth.get())*/
99     {
100         if (filter_sp->GetRevision() > synth->GetRevision())
101             pick_synth = false;
102         else
103             pick_synth = true;
104     }
105     if (pick_synth)
106     {
107         if (regex_synth && reason)
108             *reason |= lldb_private::eFormatterChoiceCriterionRegularExpressionFilter;
109         entry = synth;
110         return true;
111     }
112     else
113     {
114         if (regex_filter && reason)
115             *reason |= lldb_private::eFormatterChoiceCriterionRegularExpressionFilter;
116         entry = filter_sp;
117         return true;
118     }
119     
120 #else
121     if (filter_sp)
122     {
123         entry = filter_sp;
124         return true;
125     }
126 #endif
127     
128     return false;
129 }
130
131 bool
132 TypeCategoryImpl::Get (ValueObject& valobj,
133                        const FormattersMatchVector& candidates,
134                        lldb::TypeValidatorImplSP& entry,
135                        uint32_t* reason)
136 {
137     if (!IsEnabled())
138         return false;
139     if (GetTypeValidatorsContainer()->Get(candidates, entry, reason))
140         return true;
141     bool regex = GetRegexTypeValidatorsContainer()->Get(candidates, entry, reason);
142     if (regex && reason)
143         *reason |= lldb_private::eFormatterChoiceCriterionRegularExpressionSummary;
144     return regex;
145 }
146
147 void
148 TypeCategoryImpl::Clear (FormatCategoryItems items)
149 {
150     if ( (items & eFormatCategoryItemValue)  == eFormatCategoryItemValue )
151         GetTypeFormatsContainer()->Clear();
152     if ( (items & eFormatCategoryItemRegexValue) == eFormatCategoryItemRegexValue )
153         GetRegexTypeFormatsContainer()->Clear();
154
155     if ( (items & eFormatCategoryItemSummary) == eFormatCategoryItemSummary )
156         GetTypeSummariesContainer()->Clear();
157     if ( (items & eFormatCategoryItemRegexSummary) == eFormatCategoryItemRegexSummary )
158         GetRegexTypeSummariesContainer()->Clear();
159
160     if ( (items & eFormatCategoryItemFilter)  == eFormatCategoryItemFilter )
161         GetTypeFiltersContainer()->Clear();
162     if ( (items & eFormatCategoryItemRegexFilter) == eFormatCategoryItemRegexFilter )
163         GetRegexTypeFiltersContainer()->Clear();
164
165 #ifndef LLDB_DISABLE_PYTHON
166     if ( (items & eFormatCategoryItemSynth)  == eFormatCategoryItemSynth )
167         GetTypeSyntheticsContainer()->Clear();
168     if ( (items & eFormatCategoryItemRegexSynth) == eFormatCategoryItemRegexSynth )
169         GetRegexTypeSyntheticsContainer()->Clear();
170 #endif
171     
172     if ( (items & eFormatCategoryItemValidator)  == eFormatCategoryItemValidator )
173         GetTypeValidatorsContainer()->Clear();
174     if ( (items & eFormatCategoryItemRegexValidator) == eFormatCategoryItemRegexValidator )
175         GetRegexTypeValidatorsContainer()->Clear();
176 }
177
178 bool
179 TypeCategoryImpl::Delete (ConstString name,
180                           FormatCategoryItems items)
181 {
182     bool success = false;
183     
184     if ( (items & eFormatCategoryItemValue)  == eFormatCategoryItemValue )
185         success = GetTypeFormatsContainer()->Delete(name) || success;
186     if ( (items & eFormatCategoryItemRegexValue) == eFormatCategoryItemRegexValue )
187         success = GetRegexTypeFormatsContainer()->Delete(name) || success;
188
189     if ( (items & eFormatCategoryItemSummary) == eFormatCategoryItemSummary )
190         success = GetTypeSummariesContainer()->Delete(name) || success;
191     if ( (items & eFormatCategoryItemRegexSummary) == eFormatCategoryItemRegexSummary )
192         success = GetRegexTypeSummariesContainer()->Delete(name) || success;
193
194     if ( (items & eFormatCategoryItemFilter)  == eFormatCategoryItemFilter )
195         success = GetTypeFiltersContainer()->Delete(name) || success;
196     if ( (items & eFormatCategoryItemRegexFilter) == eFormatCategoryItemRegexFilter )
197         success = GetRegexTypeFiltersContainer()->Delete(name) || success;
198
199 #ifndef LLDB_DISABLE_PYTHON
200     if ( (items & eFormatCategoryItemSynth)  == eFormatCategoryItemSynth )
201         success = GetTypeSyntheticsContainer()->Delete(name) || success;
202     if ( (items & eFormatCategoryItemRegexSynth) == eFormatCategoryItemRegexSynth )
203         success = GetRegexTypeSyntheticsContainer()->Delete(name) || success;
204 #endif
205     
206     if ( (items & eFormatCategoryItemValidator)  == eFormatCategoryItemValidator )
207         success = GetTypeValidatorsContainer()->Delete(name) || success;
208     if ( (items & eFormatCategoryItemRegexValidator) == eFormatCategoryItemRegexValidator )
209         success = GetRegexTypeValidatorsContainer()->Delete(name) || success;
210     
211     return success;
212 }
213
214 uint32_t
215 TypeCategoryImpl::GetCount (FormatCategoryItems items)
216 {
217     uint32_t count = 0;
218
219     if ( (items & eFormatCategoryItemValue) == eFormatCategoryItemValue )
220         count += GetTypeFormatsContainer()->GetCount();
221     if ( (items & eFormatCategoryItemRegexValue) == eFormatCategoryItemRegexValue )
222         count += GetRegexTypeFormatsContainer()->GetCount();
223     
224     if ( (items & eFormatCategoryItemSummary) == eFormatCategoryItemSummary )
225         count += GetTypeSummariesContainer()->GetCount();
226     if ( (items & eFormatCategoryItemRegexSummary) == eFormatCategoryItemRegexSummary )
227         count += GetRegexTypeSummariesContainer()->GetCount();
228
229     if ( (items & eFormatCategoryItemFilter)  == eFormatCategoryItemFilter )
230         count += GetTypeFiltersContainer()->GetCount();
231     if ( (items & eFormatCategoryItemRegexFilter) == eFormatCategoryItemRegexFilter )
232         count += GetRegexTypeFiltersContainer()->GetCount();
233
234 #ifndef LLDB_DISABLE_PYTHON
235     if ( (items & eFormatCategoryItemSynth)  == eFormatCategoryItemSynth )
236         count += GetTypeSyntheticsContainer()->GetCount();
237     if ( (items & eFormatCategoryItemRegexSynth) == eFormatCategoryItemRegexSynth )
238         count += GetRegexTypeSyntheticsContainer()->GetCount();
239 #endif
240     
241     if ( (items & eFormatCategoryItemValidator)  == eFormatCategoryItemValidator )
242         count += GetTypeValidatorsContainer()->GetCount();
243     if ( (items & eFormatCategoryItemRegexValidator) == eFormatCategoryItemRegexValidator )
244         count += GetRegexTypeValidatorsContainer()->GetCount();
245     
246     return count;
247 }
248
249 bool
250 TypeCategoryImpl::AnyMatches(ConstString type_name,
251                              FormatCategoryItems items,
252                              bool only_enabled,
253                              const char** matching_category,
254                              FormatCategoryItems* matching_type)
255 {
256     if (!IsEnabled() && only_enabled)
257         return false;
258     
259     lldb::TypeFormatImplSP format_sp;
260     lldb::TypeSummaryImplSP summary_sp;
261     TypeFilterImpl::SharedPointer filter_sp;
262 #ifndef LLDB_DISABLE_PYTHON
263     ScriptedSyntheticChildren::SharedPointer synth_sp;
264 #endif
265     TypeValidatorImpl::SharedPointer validator_sp;
266     
267     if ( (items & eFormatCategoryItemValue) == eFormatCategoryItemValue )
268     {
269         if (GetTypeFormatsContainer()->Get(type_name, format_sp))
270         {
271             if (matching_category)
272                 *matching_category = m_name.GetCString();
273             if (matching_type)
274                 *matching_type = eFormatCategoryItemValue;
275             return true;
276         }
277     }
278     if ( (items & eFormatCategoryItemRegexValue) == eFormatCategoryItemRegexValue )
279     {
280         if (GetRegexTypeFormatsContainer()->Get(type_name, format_sp))
281         {
282             if (matching_category)
283                 *matching_category = m_name.GetCString();
284             if (matching_type)
285                 *matching_type = eFormatCategoryItemRegexValue;
286             return true;
287         }
288     }
289     
290     if ( (items & eFormatCategoryItemSummary) == eFormatCategoryItemSummary )
291     {
292         if (GetTypeSummariesContainer()->Get(type_name, summary_sp))
293         {
294             if (matching_category)
295                 *matching_category = m_name.GetCString();
296             if (matching_type)
297                 *matching_type = eFormatCategoryItemSummary;
298             return true;
299         }
300     }
301     if ( (items & eFormatCategoryItemRegexSummary) == eFormatCategoryItemRegexSummary )
302     {
303         if (GetRegexTypeSummariesContainer()->Get(type_name, summary_sp))
304         {
305             if (matching_category)
306                 *matching_category = m_name.GetCString();
307             if (matching_type)
308                 *matching_type = eFormatCategoryItemRegexSummary;
309             return true;
310         }
311     }
312     
313     if ( (items & eFormatCategoryItemFilter)  == eFormatCategoryItemFilter )
314     {
315         if (GetTypeFiltersContainer()->Get(type_name, filter_sp))
316         {
317             if (matching_category)
318                 *matching_category = m_name.GetCString();
319             if (matching_type)
320                 *matching_type = eFormatCategoryItemFilter;
321             return true;
322         }
323     }
324     if ( (items & eFormatCategoryItemRegexFilter) == eFormatCategoryItemRegexFilter )
325     {
326         if (GetRegexTypeFiltersContainer()->Get(type_name, filter_sp))
327         {
328             if (matching_category)
329                 *matching_category = m_name.GetCString();
330             if (matching_type)
331                 *matching_type = eFormatCategoryItemRegexFilter;
332             return true;
333         }
334     }
335     
336 #ifndef LLDB_DISABLE_PYTHON
337     if ( (items & eFormatCategoryItemSynth)  == eFormatCategoryItemSynth )
338     {
339         if (GetTypeSyntheticsContainer()->Get(type_name, synth_sp))
340         {
341             if (matching_category)
342                 *matching_category = m_name.GetCString();
343             if (matching_type)
344                 *matching_type = eFormatCategoryItemSynth;
345             return true;
346         }
347     }
348     if ( (items & eFormatCategoryItemRegexSynth) == eFormatCategoryItemRegexSynth )
349     {
350         if (GetRegexTypeSyntheticsContainer()->Get(type_name, synth_sp))
351         {
352             if (matching_category)
353                 *matching_category = m_name.GetCString();
354             if (matching_type)
355                 *matching_type = eFormatCategoryItemRegexSynth;
356             return true;
357         }
358     }
359 #endif
360     
361     if ( (items & eFormatCategoryItemValidator)  == eFormatCategoryItemValidator )
362     {
363         if (GetTypeValidatorsContainer()->Get(type_name, validator_sp))
364         {
365             if (matching_category)
366                 *matching_category = m_name.GetCString();
367             if (matching_type)
368                 *matching_type = eFormatCategoryItemValidator;
369             return true;
370         }
371     }
372     if ( (items & eFormatCategoryItemRegexValidator) == eFormatCategoryItemRegexValidator )
373     {
374         if (GetRegexTypeValidatorsContainer()->Get(type_name, validator_sp))
375         {
376             if (matching_category)
377                 *matching_category = m_name.GetCString();
378             if (matching_type)
379                 *matching_type = eFormatCategoryItemRegexValidator;
380             return true;
381         }
382     }
383     
384     return false;
385 }
386
387 TypeCategoryImpl::FormatContainer::MapValueType
388 TypeCategoryImpl::GetFormatForType (lldb::TypeNameSpecifierImplSP type_sp)
389 {
390     FormatContainer::MapValueType retval;
391     
392     if (type_sp)
393     {
394         if (type_sp->IsRegex())
395             GetRegexTypeFormatsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
396         else
397             GetTypeFormatsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
398     }
399     
400     return retval;
401 }
402
403 TypeCategoryImpl::SummaryContainer::MapValueType
404 TypeCategoryImpl::GetSummaryForType (lldb::TypeNameSpecifierImplSP type_sp)
405 {
406     SummaryContainer::MapValueType retval;
407     
408     if (type_sp)
409     {
410         if (type_sp->IsRegex())
411             GetRegexTypeSummariesContainer()->GetExact(ConstString(type_sp->GetName()),retval);
412         else
413             GetTypeSummariesContainer()->GetExact(ConstString(type_sp->GetName()),retval);
414     }
415     
416     return retval;
417 }
418
419 TypeCategoryImpl::FilterContainer::MapValueType
420 TypeCategoryImpl::GetFilterForType (lldb::TypeNameSpecifierImplSP type_sp)
421 {
422     FilterContainer::MapValueType retval;
423     
424     if (type_sp)
425     {
426         if (type_sp->IsRegex())
427             GetRegexTypeFiltersContainer()->GetExact(ConstString(type_sp->GetName()),retval);
428         else
429             GetTypeFiltersContainer()->GetExact(ConstString(type_sp->GetName()),retval);
430     }
431     
432     return retval;
433 }
434
435 #ifndef LLDB_DISABLE_PYTHON
436 TypeCategoryImpl::SynthContainer::MapValueType
437 TypeCategoryImpl::GetSyntheticForType (lldb::TypeNameSpecifierImplSP type_sp)
438 {
439     SynthContainer::MapValueType retval;
440     
441     if (type_sp)
442     {
443         if (type_sp->IsRegex())
444             GetRegexTypeSyntheticsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
445         else
446             GetTypeSyntheticsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
447     }
448     
449     return retval;
450 }
451 #endif
452
453 TypeCategoryImpl::ValidatorContainer::MapValueType
454 TypeCategoryImpl::GetValidatorForType (lldb::TypeNameSpecifierImplSP type_sp)
455 {
456     ValidatorContainer::MapValueType retval;
457     
458     if (type_sp)
459     {
460         if (type_sp->IsRegex())
461             GetRegexTypeValidatorsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
462         else
463             GetTypeValidatorsContainer()->GetExact(ConstString(type_sp->GetName()),retval);
464     }
465     
466     return retval;
467 }
468
469 lldb::TypeNameSpecifierImplSP
470 TypeCategoryImpl::GetTypeNameSpecifierForSummaryAtIndex (size_t index)
471 {
472     if (index < GetTypeSummariesContainer()->GetCount())
473         return GetTypeSummariesContainer()->GetTypeNameSpecifierAtIndex(index);
474     else
475         return GetRegexTypeSummariesContainer()->GetTypeNameSpecifierAtIndex(index-GetTypeSummariesContainer()->GetCount());
476 }
477
478 TypeCategoryImpl::FormatContainer::MapValueType
479 TypeCategoryImpl::GetFormatAtIndex (size_t index)
480 {
481     if (index < GetTypeFormatsContainer()->GetCount())
482         return GetTypeFormatsContainer()->GetAtIndex(index);
483     else
484         return GetRegexTypeFormatsContainer()->GetAtIndex(index-GetTypeFormatsContainer()->GetCount());
485 }
486
487 TypeCategoryImpl::SummaryContainer::MapValueType
488 TypeCategoryImpl::GetSummaryAtIndex (size_t index)
489 {
490     if (index < GetTypeSummariesContainer()->GetCount())
491         return GetTypeSummariesContainer()->GetAtIndex(index);
492     else
493         return GetRegexTypeSummariesContainer()->GetAtIndex(index-GetTypeSummariesContainer()->GetCount());
494 }
495
496 TypeCategoryImpl::FilterContainer::MapValueType
497 TypeCategoryImpl::GetFilterAtIndex (size_t index)
498 {
499     if (index < GetTypeFiltersContainer()->GetCount())
500         return GetTypeFiltersContainer()->GetAtIndex(index);
501     else
502         return GetRegexTypeFiltersContainer()->GetAtIndex(index-GetTypeFiltersContainer()->GetCount());
503 }
504
505 lldb::TypeNameSpecifierImplSP
506 TypeCategoryImpl::GetTypeNameSpecifierForFormatAtIndex (size_t index)
507 {
508     if (index < GetTypeFormatsContainer()->GetCount())
509         return GetTypeFormatsContainer()->GetTypeNameSpecifierAtIndex(index);
510     else
511         return GetRegexTypeFormatsContainer()->GetTypeNameSpecifierAtIndex(index-GetTypeFormatsContainer()->GetCount());
512 }
513
514 lldb::TypeNameSpecifierImplSP
515 TypeCategoryImpl::GetTypeNameSpecifierForFilterAtIndex (size_t index)
516 {
517     if (index < GetTypeFiltersContainer()->GetCount())
518         return GetTypeFiltersContainer()->GetTypeNameSpecifierAtIndex(index);
519     else
520         return GetRegexTypeFiltersContainer()->GetTypeNameSpecifierAtIndex(index-GetTypeFiltersContainer()->GetCount());
521 }
522
523 #ifndef LLDB_DISABLE_PYTHON
524 TypeCategoryImpl::SynthContainer::MapValueType
525 TypeCategoryImpl::GetSyntheticAtIndex (size_t index)
526 {
527     if (index < GetTypeSyntheticsContainer()->GetCount())
528         return GetTypeSyntheticsContainer()->GetAtIndex(index);
529     else
530         return GetRegexTypeSyntheticsContainer()->GetAtIndex(index-GetTypeSyntheticsContainer()->GetCount());
531 }
532
533 lldb::TypeNameSpecifierImplSP
534 TypeCategoryImpl::GetTypeNameSpecifierForSyntheticAtIndex (size_t index)
535 {
536     if (index < GetTypeSyntheticsContainer()->GetCount())
537         return GetTypeSyntheticsContainer()->GetTypeNameSpecifierAtIndex(index);
538     else
539         return GetRegexTypeSyntheticsContainer()->GetTypeNameSpecifierAtIndex(index - GetTypeSyntheticsContainer()->GetCount());
540 }
541 #endif
542
543 TypeCategoryImpl::ValidatorContainer::MapValueType
544 TypeCategoryImpl::GetValidatorAtIndex (size_t index)
545 {
546     if (index < GetTypeValidatorsContainer()->GetCount())
547         return GetTypeValidatorsContainer()->GetAtIndex(index);
548     else
549         return GetRegexTypeValidatorsContainer()->GetAtIndex(index-GetTypeValidatorsContainer()->GetCount());
550 }
551
552 lldb::TypeNameSpecifierImplSP
553 TypeCategoryImpl::GetTypeNameSpecifierForValidatorAtIndex (size_t index)
554 {
555     if (index < GetTypeValidatorsContainer()->GetCount())
556         return GetTypeValidatorsContainer()->GetTypeNameSpecifierAtIndex(index);
557     else
558         return GetRegexTypeValidatorsContainer()->GetTypeNameSpecifierAtIndex(index - GetTypeValidatorsContainer()->GetCount());
559 }
560
561 void
562 TypeCategoryImpl::Enable (bool value, uint32_t position)
563 {
564     Mutex::Locker locker(m_mutex);
565     if ( (m_enabled = value) )
566         m_enabled_position = position;
567     if (m_change_listener)
568         m_change_listener->Changed();
569 }