summaryrefslogtreecommitdiffstats
path: root/tools/slang-cpp-extractor/node.cpp
blob: b606e4edf3f68d601ddba14e1cd3c5af010b939c (plain)
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
#include "node.h"

#include "file-util.h"

#include "../../source/core/slang-string-util.h"
#include "../../source/core/slang-string-escape-util.h"

namespace CppExtract {

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Node Impl !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

SLANG_FORCE_INLINE static void _indent(Index indentCount, StringBuilder& out) { FileUtil::indent(indentCount, out); }

void Node::dumpMarkup(int indentCount, StringBuilder& out)
{
    if (m_markup.getLength() <= 0)
    {
        return;
    }

    List<UnownedStringSlice> lines;
    StringUtil::calcLines(m_markup.getUnownedSlice(), lines);

    // Remove empty lines from the end
    while (lines.getCount())
    {
        auto lastLine = lines.getLast();
        if (lastLine.trim().getLength() == 0)
        {
            lines.removeLast();
            continue;
        }
        break;
    }

    if (lines.getCount() == 0)
    {
        return;
    }

    for (auto line : lines)
    {
        _indent(indentCount, out);
        out << "// " << line << "\n";
    }
}

ScopeNode* Node::getRootScope()
{
    if (m_parentScope)
    {
        ScopeNode* scope = m_parentScope;
        while (scope->m_parentScope)
        {
            scope = scope->m_parentScope;
        }
        return scope;
    }
    else
    {
        return as<ScopeNode>(this);
    }
}

void Node::calcScopeDepthFirst(List<Node*>& outNodes)
{
    outNodes.add(this);
}

void Node::calcAbsoluteName(StringBuilder& outName) const
{
    List<Node*> path;
    calcScopePath(const_cast<Node*>(this), path);

    // 1 so we skip the global scope
    for (Index i = 1; i < path.getCount(); ++i)
    {
        Node* node = path[i];

        if (i > 1)
        {
            outName << "::";
        }

        if (node->m_kind == Kind::AnonymousNamespace)
        {
            outName << "{Anonymous}";
        }
        else
        {
            outName << node->m_name.getContent();
        }
    }
}

/* static */void Node::calcScopePath(Node* node, List<Node*>& outPath)
{
    outPath.clear();

    while (node)
    {
        outPath.add(node);
        node = node->m_parentScope;
    }

    // reverse the order, so we go from root to the node
    outPath.reverse();
}

/* static */void Node::filterImpl(Filter inFilter, List<Node*>& ioNodes)
{
    // Filter out all the unreflected nodes
    Index count = ioNodes.getCount();
    for (Index j = 0; j < count; )
    {
        Node* node = ioNodes[j];

        if (!inFilter(node))
        {
            ioNodes.removeAt(j);
            count--;
        }
        else
        {
            j++;
        }
    }
}

/* static */Node* Node::lookupNameInScope(ScopeNode* scope, const UnownedStringSlice& name)
{
    // TODO(JS): Doesn't handle 'using namespace'.

    // Must be unqualified name
    SLANG_ASSERT(name.indexOf(UnownedStringSlice::fromLiteral("::")) < 0);

    Node* childNode = scope->findChild(name);
    if (childNode)
    {
        return childNode;
    }

    // If we have an anonymous namespace in this scope, try looking up in there..
    if (scope->m_anonymousNamespace)
    {
        Node* childNode = scope->m_anonymousNamespace->findChild(name);
        if (childNode)
        {
            return childNode;
        }
    }

    // I could have an enum (that's not an enum class)
    for (Node* node : scope->m_children)
    {
        EnumNode* enumNode = as<EnumNode>(node);
        if (enumNode && enumNode->m_kind == Node::Kind::Enum)
        {
            Node** nodePtr = enumNode->m_childMap.tryGetValue(name);
            if (nodePtr)
            {
                return *nodePtr;
            }
        }
    }

    return nullptr;
}

/* static */Node* Node::lookupFromScope(ScopeNode* scope, const UnownedStringSlice* parts, Index partsCount)
{
    SLANG_ASSERT(partsCount > 0);
    if (partsCount == 1)
    {
        return lookupNameInScope(scope, parts[0]);
    }

    for (Index i = 0; i < partsCount; ++i)
    {
        const UnownedStringSlice& part = parts[i];

        Node* node = lookupNameInScope(scope, part);
        if (node == nullptr)
        {
            return node;
        }
        // If at end, then we are done
        if (i == partsCount - 1)
        {
            return node;
        }

        // If there are more elements, then node must be some kind of scope,
        // if we are going to find it
        scope = as<ScopeNode>(node);
        if (scope == nullptr)
        {
            break;
        }
    }

    return nullptr;
}

/* static */void Node::splitPath(const UnownedStringSlice& inPath, List<UnownedStringSlice>& outParts)
{
    if (inPath.indexOf(UnownedStringSlice::fromLiteral("::")) >= 0)
    {
        StringUtil::split(inPath, UnownedStringSlice::fromLiteral("::"), outParts);
        // Remove any whitespace
        for (auto& part : outParts)
        {
            part = part.trim();
        }
    }
    else
    {
        outParts.clear();
        outParts.add(inPath.trim());
    }
}

/* static */Node* Node::lookupFromScope(ScopeNode* scope, const UnownedStringSlice& inPath)
{
    if (inPath.indexOf(UnownedStringSlice::fromLiteral("::")) >= 0)
    {
        List<UnownedStringSlice> parts;
        splitPath(inPath, parts);

        return lookupFromScope(scope, parts.getBuffer(), parts.getCount());
    }
    else
    {
        return lookupNameInScope(scope, inPath);
    }
}

/* static */Node* Node::lookup(ScopeNode* scope, const UnownedStringSlice& inPath)
{
    if (inPath.indexOf(UnownedStringSlice::fromLiteral("::")) >= 0)
    {
        List<UnownedStringSlice> parts;
        splitPath(inPath, parts);

        if (parts[0].getLength() == 0)
        {
            // It's a lookup from global scope
            ScopeNode* rootScope = scope->getRootScope();
            return lookupFromScope(rootScope, parts.getBuffer() + 1, parts.getCount() + 1);
        }

        // Okay lets try a lookup from each scope up to the global scope
        while (scope)
        {
            Node* node = lookupFromScope(scope, parts.getBuffer(), parts.getCount());
            if (node)
            {
                return node;
            }

            scope = scope->m_parentScope;
        }
    }
    else
    {
        while (scope)
        {
            // Lookup in this scope
            Node* node = lookupNameInScope(scope, inPath);
            if (node)
            {
                return node;
            }

            // Try parent scope
            scope = scope->m_parentScope;
        }
    }

    return nullptr;
}

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ScopeNode !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

ScopeNode* ScopeNode::getAnonymousNamespace()
{
    if (!m_anonymousNamespace)
    {
        m_anonymousNamespace = new ScopeNode(Kind::AnonymousNamespace);
        m_anonymousNamespace->m_parentScope = this;
        m_children.add(m_anonymousNamespace);
    }

    return m_anonymousNamespace;
}

void ScopeNode::addChildIgnoringName(Node* child)
{
    SLANG_ASSERT(child->m_parentScope == nullptr);
    // Can't add anonymous namespace this way - should be added via getAnonymousNamespace
    SLANG_ASSERT(child->m_kind != Kind::AnonymousNamespace);

    child->m_parentScope = this;
    m_children.add(child);
}

void ScopeNode::addChild(Node* child)
{
    addChildIgnoringName(child);

    if (child->m_name.hasContent())
    {
        m_childMap.add(child->m_name.getContent(), child);
    }
}

Node* ScopeNode::findChild(const UnownedStringSlice& name) const
{
    Node** nodePtr = m_childMap.tryGetValue(name);
    if (nodePtr)
    {
        return *nodePtr;
    }
    return nullptr;
}

void ScopeNode::calcScopeDepthFirst(List<Node*>& outNodes)
{
    outNodes.add(this);
    for (Node* child : m_children)
    {
        child->calcScopeDepthFirst(outNodes);
    }
}

void ScopeNode::dump(int indentCount, StringBuilder& out)
{
    dumpMarkup(indentCount, out);

    _indent(indentCount, out);

    switch (m_kind)
    {
        case Kind::AnonymousNamespace:
        {
            out << "namespace {\n";
        }
        case Kind::Namespace:
        {
            if (m_name.hasContent())
            {
                out << "namespace " << m_name.getContent() << " {\n";
            }
            else
            {
                out << "{\n";
            }
            break;
        }
    }

    for (Node* child : m_children)
    {
        child->dump(indentCount + 1, out);
    }

    _indent(indentCount, out);
    out << "}\n";
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! EnumCaseNode !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

/* Returns true if needs space between the tokens.
It determines this based on the locs, and if they contain something between them.
*/
static bool _needsSpace(const Token& prevTok, const Token& tok)
{
    auto prevLoc = prevTok.getLoc();
    auto loc = tok.getLoc();

    auto prevContent = prevTok.getContent();
    
    if (prevLoc + prevContent.getLength() == loc)
    {
        return false;
    }

    return true;
}


static void _dumpTokens(const Token* toks, Index count, StringBuilder& out)
{
    if (count > 0)
    {
        out << toks[0].getContent();

        for (Index i = 1; i < count; ++i)
        {
            const auto& prevToken = toks[i - 1];
            const auto& token = toks[i];

            if (_needsSpace(prevToken, token))
            {
                out << " ";
            }

            out << token.getContent();
        }
    }
}

static void _dumpTokens(const List<Token>& toks, StringBuilder& out)
{
    _dumpTokens(toks.getBuffer(), toks.getCount(), out);
}


void EnumCaseNode::dump(int indent, StringBuilder& out)
{
    if (isReflected())
    {
        dumpMarkup(indent, out);

        _indent(indent, out);
        out << m_name.getContent();

        if (m_valueTokens.getCount())
        {
            out << " = ";
            _dumpTokens(m_valueTokens, out);
        }

        out << ",\n";
    }
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! EnumNode !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

void TypeDefNode::dump(int indent, StringBuilder& out)
{
    if (isReflected())
    {
        dumpMarkup(indent, out);

        _indent(indent, out);

        out << "typedef ";
        _dumpTokens(m_targetTypeTokens, out);
        out << " " << m_name.getContent() << ";\n";
    }
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! EnumNode !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

void EnumNode::dump(int indent, StringBuilder& out)
{
    if (!isReflected())
    {
        return;
    }

    dumpMarkup(indent, out);

    _indent(indent, out);

    out << "enum ";

    if (m_kind == Kind::EnumClass)
    {
        out << "class ";
    }

    if (m_name.type != TokenType::Invalid)
    {
        out << m_name.getContent();
    }

    if (m_backingTokens.getCount() > 0)
    { 
        out << " : ";
        _dumpTokens(m_backingTokens, out);
    }

    out << "\n";
    _indent(indent, out);
    out << "{\n";

    for (Node* child : m_children)
    {
        child->dump(indent + 1, out);
    }

    _indent(indent, out);
    out << "}\n";
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!! CallableNode !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

void CallableNode::dump(int indent, StringBuilder& out)
{
    if (!isReflected())
    {
        return;
    }

    dumpMarkup(indent, out);

    _indent(indent, out);

    if (m_isStatic)
    {
        out << "static ";
    }
    if (m_isVirtual)
    {
        out << "virtual ";
    }

    out << m_returnType << " ";
    out << m_name.getContent() << "(";

    const Index count = m_params.getCount();
    for (Index i = 0; i < count; ++i)
    {
        if (i > 0)
        {
            out << ", ";
        }

        const auto& param = m_params[i];
        out << param.m_type;
        if (param.m_name.type == TokenType::Identifier)
        {
            out << " " << param.m_name.getContent();
        }
    }

    out << ")";

    if (m_isPure)
    {
        out << " = 0";
    }

    out << "\n";
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! FieldNode !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

void FieldNode::dump(int indent, StringBuilder& out)
{
    if (!isReflected())
    {
        return;
    }

    dumpMarkup(indent, out);

    _indent(indent, out);

    if (m_isStatic)
    {
        out << "static ";
    }

    out << m_fieldType << " " << m_name.getContent() << "\n";
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ClassLikeNode !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

/// Add a node that is derived from this
void ClassLikeNode::addDerived(ClassLikeNode* derived)
{
    SLANG_ASSERT(derived->m_superNode == nullptr);
    derived->m_superNode = this;
    m_derivedTypes.add(derived);
}

void ClassLikeNode::calcDerivedDepthFirst(List<ClassLikeNode*>& outNodes)
{
    outNodes.add(this);
    for (ClassLikeNode* derivedType : m_derivedTypes)
    {
        derivedType->calcDerivedDepthFirst(outNodes);
    }
}

void ClassLikeNode::dumpDerived(int indentCount, StringBuilder& out)
{
    if (isClassLike() && isReflected() && m_name.hasContent())
    {
        _indent(indentCount, out);
        out << m_name.getContent() << "\n";
    }

    for (ClassLikeNode* derivedType : m_derivedTypes)
    {
        derivedType->dumpDerived(indentCount + 1, out);
    }
}

Index ClassLikeNode::calcDerivedDepth() const
{
    const ClassLikeNode* node = this;
    Index count = 0;

    while (node)
    {
        count++;
        node = node->m_superNode;
    }

    return count;
}

ClassLikeNode* ClassLikeNode::findLastDerived()
{
    for (Index i = m_derivedTypes.getCount() - 1; i >= 0; --i)
    {
        ClassLikeNode* derivedType = m_derivedTypes[i];
        ClassLikeNode* found = derivedType->findLastDerived();
        if (found)
        {
            return found;
        }
    }
    return this;
}

bool ClassLikeNode::hasReflectedDerivedType() const
{
    for (ClassLikeNode* type : m_derivedTypes)
    {
        if (type->isReflected())
        {
            return true;
        }
    }
    return false;
}

void ClassLikeNode::getReflectedDerivedTypes(List<ClassLikeNode*>& out) const
{
    out.clear();
    for (ClassLikeNode* type : m_derivedTypes)
    {
        if (type->isReflected())
        {
            out.add(type);
        }
    }
}

void ClassLikeNode::dump(int indentCount, StringBuilder& out)
{
    dumpMarkup(indentCount, out);

    _indent(indentCount, out);

    const char* typeName = (m_kind == Kind::StructType) ? "struct" : "class";

    out << typeName << " ";

    if (!isReflected())
    {
        out << " (";
    }
    out << m_name.getContent();
    if (!isReflected())
    {
        out << ") ";
    }

    if (m_super.hasContent())
    {
        out << " : " << m_super.getContent();
    }

    out << " {\n";

    for (Node* child : m_children)
    {
        child->dump(indentCount + 1, out);
    }

    _indent(indentCount, out);
    out << "}\n";
}

} // namespace CppExtract