aboutsummaryrefslogtreecommitdiff
path: root/source/API/SBTypeCategory.cpp
blob: e3978693c81ca32d16ecfbf7959cffa2adcbd076 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//===-- SBTypeCategory.cpp ----------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/lldb-python.h"

#include "lldb/API/SBTypeCategory.h"

#include "lldb/API/SBTypeFilter.h"
#include "lldb/API/SBTypeFormat.h"
#include "lldb/API/SBTypeSummary.h"
#include "lldb/API/SBTypeSynthetic.h"
#include "lldb/API/SBTypeNameSpecifier.h"
#include "lldb/API/SBStream.h"

#include "lldb/Core/Debugger.h"
#include "lldb/DataFormatters/DataVisualization.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/ScriptInterpreter.h"

using namespace lldb;
using namespace lldb_private;

typedef std::pair<lldb::TypeCategoryImplSP,user_id_t> ImplType;

SBTypeCategory::SBTypeCategory() :
m_opaque_sp()
{
}

SBTypeCategory::SBTypeCategory (const char* name) :
m_opaque_sp()
{
    DataVisualization::Categories::GetCategory(ConstString(name), m_opaque_sp);
}

SBTypeCategory::SBTypeCategory (const lldb::SBTypeCategory &rhs) :
m_opaque_sp(rhs.m_opaque_sp)
{
}

SBTypeCategory::~SBTypeCategory ()
{
}

bool
SBTypeCategory::IsValid() const
{
    return (m_opaque_sp.get() != NULL);
}

bool
SBTypeCategory::GetEnabled ()
{
    if (!IsValid())
        return false;
    return m_opaque_sp->IsEnabled();
}

void
SBTypeCategory::SetEnabled (bool enabled)
{
    if (!IsValid())
        return;
    if (enabled)
        DataVisualization::Categories::Enable(m_opaque_sp);
    else
        DataVisualization::Categories::Disable(m_opaque_sp);
}

const char*
SBTypeCategory::GetName()
{
    if (!IsValid())
        return NULL;
    return m_opaque_sp->GetName();
}

uint32_t
SBTypeCategory::GetNumFormats ()
{
    if (!IsDefaultCategory())
        return 0;
    
    return DataVisualization::ValueFormats::GetCount();
}

uint32_t
SBTypeCategory::GetNumSummaries ()
{
    if (!IsValid())
        return 0;
    return m_opaque_sp->GetSummaryNavigator()->GetCount() + m_opaque_sp->GetRegexSummaryNavigator()->GetCount();
}

uint32_t
SBTypeCategory::GetNumFilters ()
{
    if (!IsValid())
        return 0;
    return m_opaque_sp->GetFilterNavigator()->GetCount() + m_opaque_sp->GetRegexFilterNavigator()->GetCount();
}

#ifndef LLDB_DISABLE_PYTHON
uint32_t
SBTypeCategory::GetNumSynthetics ()
{
    if (!IsValid())
        return 0;
    return m_opaque_sp->GetSyntheticNavigator()->GetCount() + m_opaque_sp->GetRegexSyntheticNavigator()->GetCount();
}
#endif

lldb::SBTypeNameSpecifier
SBTypeCategory::GetTypeNameSpecifierForFilterAtIndex (uint32_t index)
{
    if (!IsValid())
        return SBTypeNameSpecifier();
    return SBTypeNameSpecifier(m_opaque_sp->GetTypeNameSpecifierForFilterAtIndex(index));
}

lldb::SBTypeNameSpecifier
SBTypeCategory::GetTypeNameSpecifierForFormatAtIndex (uint32_t index)
{
    if (!IsDefaultCategory())
        return SBTypeNameSpecifier();
    return SBTypeNameSpecifier(DataVisualization::ValueFormats::GetTypeNameSpecifierForFormatAtIndex(index));
}

lldb::SBTypeNameSpecifier
SBTypeCategory::GetTypeNameSpecifierForSummaryAtIndex (uint32_t index)
{
    if (!IsValid())
        return SBTypeNameSpecifier();
    return SBTypeNameSpecifier(m_opaque_sp->GetTypeNameSpecifierForSummaryAtIndex(index));
}

#ifndef LLDB_DISABLE_PYTHON
lldb::SBTypeNameSpecifier
SBTypeCategory::GetTypeNameSpecifierForSyntheticAtIndex (uint32_t index)
{
    if (!IsValid())
        return SBTypeNameSpecifier();
    return SBTypeNameSpecifier(m_opaque_sp->GetTypeNameSpecifierForSyntheticAtIndex(index));
}
#endif

SBTypeFilter
SBTypeCategory::GetFilterForType (SBTypeNameSpecifier spec)
{
    if (!IsValid())
        return SBTypeFilter();
    
    if (!spec.IsValid())
        return SBTypeFilter();
    
    lldb::SyntheticChildrenSP children_sp;
    
    if (spec.IsRegex())
        m_opaque_sp->GetRegexFilterNavigator()->GetExact(ConstString(spec.GetName()), children_sp);
    else
        m_opaque_sp->GetFilterNavigator()->GetExact(ConstString(spec.GetName()), children_sp);
        
    if (!children_sp)
        return lldb::SBTypeFilter();
    
    TypeFilterImplSP filter_sp = std::static_pointer_cast<TypeFilterImpl>(children_sp);
    
    return lldb::SBTypeFilter(filter_sp);

}
SBTypeFormat
SBTypeCategory::GetFormatForType (SBTypeNameSpecifier spec)
{
    if (!IsDefaultCategory())
        return SBTypeFormat();
        
    if (!spec.IsValid())
        return SBTypeFormat();
    
    if (spec.IsRegex())
        return SBTypeFormat();
    
    return SBTypeFormat(DataVisualization::ValueFormats::GetFormat(ConstString(spec.GetName())));
}

#ifndef LLDB_DISABLE_PYTHON
SBTypeSummary
SBTypeCategory::GetSummaryForType (SBTypeNameSpecifier spec)
{
    if (!IsValid())
        return SBTypeSummary();
    
    if (!spec.IsValid())
        return SBTypeSummary();
    
    lldb::TypeSummaryImplSP summary_sp;
    
    if (spec.IsRegex())
        m_opaque_sp->GetRegexSummaryNavigator()->GetExact(ConstString(spec.GetName()), summary_sp);
    else
        m_opaque_sp->GetSummaryNavigator()->GetExact(ConstString(spec.GetName()), summary_sp);
    
    if (!summary_sp)
        return lldb::SBTypeSummary();
    
    return lldb::SBTypeSummary(summary_sp);
}
#endif // LLDB_DISABLE_PYTHON

#ifndef LLDB_DISABLE_PYTHON
SBTypeSynthetic
SBTypeCategory::GetSyntheticForType (SBTypeNameSpecifier spec)
{
    if (!IsValid())
        return SBTypeSynthetic();
    
    if (!spec.IsValid())
        return SBTypeSynthetic();
    
    lldb::SyntheticChildrenSP children_sp;
    
    if (spec.IsRegex())
        m_opaque_sp->GetRegexSyntheticNavigator()->GetExact(ConstString(spec.GetName()), children_sp);
    else
        m_opaque_sp->GetSyntheticNavigator()->GetExact(ConstString(spec.GetName()), children_sp);
    
    if (!children_sp)
        return lldb::SBTypeSynthetic();
    
    ScriptedSyntheticChildrenSP synth_sp = std::static_pointer_cast<ScriptedSyntheticChildren>(children_sp);
    
    return lldb::SBTypeSynthetic(synth_sp);
}
#endif

#ifndef LLDB_DISABLE_PYTHON
SBTypeFilter
SBTypeCategory::GetFilterAtIndex (uint32_t index)
{
    if (!IsValid())
        return SBTypeFilter();
    lldb::SyntheticChildrenSP children_sp = m_opaque_sp->GetSyntheticAtIndex((index));
    
    if (!children_sp.get())
        return lldb::SBTypeFilter();
    
    TypeFilterImplSP filter_sp = std::static_pointer_cast<TypeFilterImpl>(children_sp);
    
    return lldb::SBTypeFilter(filter_sp);
}
#endif

SBTypeFormat
SBTypeCategory::GetFormatAtIndex (uint32_t index)
{
    if (!IsDefaultCategory())
        return SBTypeFormat();
    return SBTypeFormat(DataVisualization::ValueFormats::GetFormatAtIndex((index)));
}

#ifndef LLDB_DISABLE_PYTHON
SBTypeSummary
SBTypeCategory::GetSummaryAtIndex (uint32_t index)
{
    if (!IsValid())
        return SBTypeSummary();
    return SBTypeSummary(m_opaque_sp->GetSummaryAtIndex((index)));
}
#endif

#ifndef LLDB_DISABLE_PYTHON
SBTypeSynthetic
SBTypeCategory::GetSyntheticAtIndex (uint32_t index)
{
    if (!IsValid())
        return SBTypeSynthetic();
    lldb::SyntheticChildrenSP children_sp = m_opaque_sp->GetSyntheticAtIndex((index));
    
    if (!children_sp.get())
        return lldb::SBTypeSynthetic();
    
    ScriptedSyntheticChildrenSP synth_sp = std::static_pointer_cast<ScriptedSyntheticChildren>(children_sp);
    
    return lldb::SBTypeSynthetic(synth_sp);
}
#endif

bool
SBTypeCategory::AddTypeFormat (SBTypeNameSpecifier type_name,
                               SBTypeFormat format)
{
    if (!IsDefaultCategory())
        return false;
    
    if (!type_name.IsValid())
        return false;
    
    if (!format.IsValid())
        return false;
    
    if (type_name.IsRegex())
        return false;
    
    DataVisualization::ValueFormats::Add(ConstString(type_name.GetName()), format.GetSP());
    
    return true;
}

bool
SBTypeCategory::DeleteTypeFormat (SBTypeNameSpecifier type_name)
{
    if (!IsDefaultCategory())
        return false;
    
    if (!type_name.IsValid())
        return false;
    
    if (type_name.IsRegex())
        return false;

    return DataVisualization::ValueFormats::Delete(ConstString(type_name.GetName()));
}

#ifndef LLDB_DISABLE_PYTHON
bool
SBTypeCategory::AddTypeSummary (SBTypeNameSpecifier type_name,
                                SBTypeSummary summary)
{
    if (!IsValid())
        return false;
    
    if (!type_name.IsValid())
        return false;
    
    if (!summary.IsValid())
        return false;
    
    // FIXME: we need to iterate over all the Debugger objects and have each of them contain a copy of the function
    // since we currently have formatters live in a global space, while Python code lives in a specific Debugger-related environment
    // this should eventually be fixed by deciding a final location in the LLDB object space for formatters
    if (summary.IsFunctionCode())
    {
        void *name_token = (void*)ConstString(type_name.GetName()).GetCString();
        const char* script = summary.GetData();
        StringList input; input.SplitIntoLines(script, strlen(script));
        uint32_t num_debuggers = lldb_private::Debugger::GetNumDebuggers();
        bool need_set = true;
        for (uint32_t j = 0;
             j < num_debuggers;
             j++)
        {
            DebuggerSP debugger_sp = lldb_private::Debugger::GetDebuggerAtIndex(j);
            if (debugger_sp)
            {
                ScriptInterpreter* interpreter_ptr = debugger_sp->GetCommandInterpreter().GetScriptInterpreter();
                if (interpreter_ptr)
                {
                    std::string output;
                    if (interpreter_ptr->GenerateTypeScriptFunction(input, output, name_token) && !output.empty())
                    {
                        if (need_set)
                        {
                            need_set = false;
                            summary.SetFunctionName(output.c_str());
                        }
                    }
                }
            }
        }
    }
    
    if (type_name.IsRegex())
        m_opaque_sp->GetRegexSummaryNavigator()->Add(lldb::RegularExpressionSP(new RegularExpression(type_name.GetName())), summary.GetSP());
    else
        m_opaque_sp->GetSummaryNavigator()->Add(ConstString(type_name.GetName()), summary.GetSP());
    
    return true;
}
#endif

bool
SBTypeCategory::DeleteTypeSummary (SBTypeNameSpecifier type_name)
{
    if (!IsValid())
        return false;
    
    if (!type_name.IsValid())
        return false;
    
    if (type_name.IsRegex())
        return m_opaque_sp->GetRegexSummaryNavigator()->Delete(ConstString(type_name.GetName()));
    else
        return m_opaque_sp->GetSummaryNavigator()->Delete(ConstString(type_name.GetName()));
}

bool
SBTypeCategory::AddTypeFilter (SBTypeNameSpecifier type_name,
                               SBTypeFilter filter)
{
    if (!IsValid())
        return false;
    
    if (!type_name.IsValid())
        return false;
    
    if (!filter.IsValid())
        return false;
    
    if (type_name.IsRegex())
        m_opaque_sp->GetRegexFilterNavigator()->Add(lldb::RegularExpressionSP(new RegularExpression(type_name.GetName())), filter.GetSP());
    else
        m_opaque_sp->GetFilterNavigator()->Add(ConstString(type_name.GetName()), filter.GetSP());
    
    return true;
}

bool
SBTypeCategory::DeleteTypeFilter (SBTypeNameSpecifier type_name)
{
    if (!IsValid())
        return false;
    
    if (!type_name.IsValid())
        return false;
    
    if (type_name.IsRegex())
        return m_opaque_sp->GetRegexFilterNavigator()->Delete(ConstString(type_name.GetName()));
    else
        return m_opaque_sp->GetFilterNavigator()->Delete(ConstString(type_name.GetName()));
}

#ifndef LLDB_DISABLE_PYTHON
bool
SBTypeCategory::AddTypeSynthetic (SBTypeNameSpecifier type_name,
                                  SBTypeSynthetic synth)
{
    if (!IsValid())
        return false;
    
    if (!type_name.IsValid())
        return false;
    
    if (!synth.IsValid())
        return false;

    // FIXME: we need to iterate over all the Debugger objects and have each of them contain a copy of the function
    // since we currently have formatters live in a global space, while Python code lives in a specific Debugger-related environment
    // this should eventually be fixed by deciding a final location in the LLDB object space for formatters
    if (synth.IsClassCode())
    {
        void *name_token = (void*)ConstString(type_name.GetName()).GetCString();
        const char* script = synth.GetData();
        StringList input; input.SplitIntoLines(script, strlen(script));
        uint32_t num_debuggers = lldb_private::Debugger::GetNumDebuggers();
        bool need_set = true;
        for (uint32_t j = 0;
             j < num_debuggers;
             j++)
        {
            DebuggerSP debugger_sp = lldb_private::Debugger::GetDebuggerAtIndex(j);
            if (debugger_sp)
            {
                ScriptInterpreter* interpreter_ptr = debugger_sp->GetCommandInterpreter().GetScriptInterpreter();
                if (interpreter_ptr)
                {
                    std::string output;
                    if (interpreter_ptr->GenerateTypeSynthClass(input, output, name_token) && !output.empty())
                    {
                        if (need_set)
                        {
                            need_set = false;
                            synth.SetClassName(output.c_str());
                        }
                    }
                }
            }
        }
    }
    
    if (type_name.IsRegex())
        m_opaque_sp->GetRegexSyntheticNavigator()->Add(lldb::RegularExpressionSP(new RegularExpression(type_name.GetName())), synth.GetSP());
    else
        m_opaque_sp->GetSyntheticNavigator()->Add(ConstString(type_name.GetName()), synth.GetSP());
    
    return true;
}

bool
SBTypeCategory::DeleteTypeSynthetic (SBTypeNameSpecifier type_name)
{
    if (!IsValid())
        return false;
    
    if (!type_name.IsValid())
        return false;
    
    if (type_name.IsRegex())
        return m_opaque_sp->GetRegexSyntheticNavigator()->Delete(ConstString(type_name.GetName()));
    else
        return m_opaque_sp->GetSyntheticNavigator()->Delete(ConstString(type_name.GetName()));
}
#endif // LLDB_DISABLE_PYTHON

bool
SBTypeCategory::GetDescription (lldb::SBStream &description, 
                lldb::DescriptionLevel description_level)
{
    if (!IsValid())
        return false;
    description.Printf("Category name: %s\n",GetName());
    return true;
}

lldb::SBTypeCategory &
SBTypeCategory::operator = (const lldb::SBTypeCategory &rhs)
{
    if (this != &rhs)
    {
        m_opaque_sp = rhs.m_opaque_sp;
    }
    return *this;
}

bool
SBTypeCategory::operator == (lldb::SBTypeCategory &rhs)
{
    if (IsValid() == false)
        return !rhs.IsValid();
    
    return m_opaque_sp.get() == rhs.m_opaque_sp.get();
    
}

bool
SBTypeCategory::operator != (lldb::SBTypeCategory &rhs)
{
    if (IsValid() == false)
        return rhs.IsValid();
    
    return m_opaque_sp.get() != rhs.m_opaque_sp.get();
}

lldb::TypeCategoryImplSP
SBTypeCategory::GetSP ()
{
    if (!IsValid())
        return lldb::TypeCategoryImplSP();
    return m_opaque_sp;
}

void
SBTypeCategory::SetSP (const lldb::TypeCategoryImplSP &typecategory_impl_sp)
{
    m_opaque_sp = typecategory_impl_sp;
}

SBTypeCategory::SBTypeCategory (const lldb::TypeCategoryImplSP &typecategory_impl_sp) :
m_opaque_sp(typecategory_impl_sp)
{
}

bool
SBTypeCategory::IsDefaultCategory()
{
    if (!IsValid())
        return false;
    
    return (strcmp(m_opaque_sp->GetName(),"default") == 0);
}