summaryrefslogtreecommitdiffstats
path: root/tools/slang-cpp-extractor/node.h
blob: 2c15e460d7059777895883b10a3ac72c386d0fbb (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
#ifndef CPP_EXTRACT_NODE_H
#define CPP_EXTRACT_NODE_H

#include "../../source/compiler-core/slang-doc-extractor.h"
#include "diagnostics.h"

namespace CppExtract
{
using namespace Slang;

enum class ReflectionType : uint8_t
{
    NotReflected,
    Reflected,
};

// Pre-declare
class TypeSet;
class SourceOrigin;

struct ScopeNode;

class Node : public RefObject
{
public:
    enum class Kind : uint8_t
    {
        Invalid,

        StructType,
        ClassType,

        Enum,
        EnumClass,

        Namespace,
        AnonymousNamespace,

        Field,
        EnumCase,

        TypeDef,

        Callable, ///< Functions/methods

        Other,   ///< Used 'other' parsing like for TYPE
        Unknown, ///< Used for marking tokens consumed but usage is not known

        CountOf,
    };

    enum class KindRange
    {
        ScopeStart = int(Kind::StructType),
        ScopeEnd = int(Kind::AnonymousNamespace),

        ClassLikeStart = int(Kind::StructType),
        ClassLikeEnd = int(Kind::ClassType),

        ScopeTypeStart = int(Kind::StructType),
        ScopeTypeEnd = int(Kind::EnumClass),

        OtherTypeStart = int(Kind::TypeDef),
        OtherTypeEnd = int(Kind::TypeDef),

        EnumStart = int(Kind::Enum),
        EnumEnd = int(Kind::EnumClass),
    };

    /// Returns true if kind can cast to this type
    /// Used for implementing as<T> casting
    static bool isOfKind(Kind kind)
    {
        SLANG_UNUSED(kind);
        return true;
    }

    static bool isKindScope(Kind kind)
    {
        return int(kind) >= int(KindRange::ScopeStart) && int(kind) <= int(KindRange::ScopeEnd);
    }
    static bool isKindClassLike(Kind kind)
    {
        return int(kind) >= int(KindRange::ClassLikeStart) &&
               int(kind) <= int(KindRange::ClassLikeEnd);
    }
    static bool isKindEnumLike(Kind kind)
    {
        return int(kind) >= int(KindRange::EnumStart) && int(kind) <= int(KindRange::EnumEnd);
    }

    /// It a type, but doesn't have a scope
    static bool isKindOtherType(Kind kind)
    {
        return int(kind) >= int(KindRange::OtherTypeStart) &&
               int(kind) <= int(KindRange::OtherTypeEnd);
    }
    /// Is a type and has a scope
    static bool isKindScopeType(Kind kind)
    {
        return int(kind) >= int(KindRange::ScopeTypeStart) &&
               int(kind) <= int(KindRange::ScopeTypeEnd);
    }

    /// True if the kind is any type
    static bool isKindType(Kind kind) { return isKindOtherType(kind) || isKindScopeType(kind); }

    /// True if the kind can accept contained types
    static bool canKindContainTypes(Kind type)
    {
        switch (type)
        {
        case Kind::StructType:
        case Kind::ClassType:
        case Kind::Namespace:
        case Kind::AnonymousNamespace:
            {
                return true;
            }
        default:
            break;
        }
        return false;
    }

    bool isNamespace() const { return m_kind == Kind::Namespace; }
    bool isClassLike() const { return isKindClassLike(m_kind); }
    bool isScope() const { return isKindScope(m_kind); }
    bool isEnumLike() const { return isKindEnumLike(m_kind); }

    /// These are useful for the filter
    static bool isClassLikeAndReflected(Node* node)
    {
        return node->isClassLike() && node->isReflected();
    }
    static bool isClassLike(Node* node) { return isKindClassLike(node->m_kind); }

    virtual void dump(int indent, StringBuilder& out) = 0;

    /// Do depth first traversal of nodes in scopes
    virtual void calcScopeDepthFirst(List<Node*>& outNodes);

    /// Calculate the absolute name for this namespace/type
    void calcAbsoluteName(StringBuilder& outName) const;

    /// Get the absolute name
    String getAbsoluteName() const
    {
        StringBuilder buf;
        calcAbsoluteName(buf);
        return buf.produceString();
    }

    /// Calculate the scope path to this node, from the root
    void calcScopePath(List<Node*>& outPath) { calcScopePath(this, outPath); }

    /// True if reflected
    bool isReflected() const { return m_reflectionType == ReflectionType::Reflected; }

    SourceLoc getSourceLoc() const { return m_name.getLoc(); }

    ScopeNode* getRootScope();

    typedef bool (*Filter)(Node* node);

    template<typename T>
    static void filter(Filter filter, List<T*>& io)
    {
        const Node* _isNodeDerived = (T*)nullptr;
        SLANG_UNUSED(_isNodeDerived);
        filterImpl(filter, reinterpret_cast<List<Node*>&>(io));
    }

    static void filterImpl(Filter filter, List<Node*>& io);

    static void calcScopePath(Node* node, List<Node*>& outPath);

    /// Lookup a name in just the specified scope
    /// Handles anonymous namespaces, or name lookups that are in the parents space
    static Node* lookupNameInScope(ScopeNode* scope, const UnownedStringSlice& name);

    /// Lookup from a path
    static Node* lookupFromScope(ScopeNode* scope, const UnownedStringSlice* path, Index pathCount);
    /// Looks up *just* from the specified scope.
    static Node* lookupFromScope(ScopeNode* scope, const UnownedStringSlice& slice);

    /// Look up name (which can contain ::)
    static Node* lookup(ScopeNode* scope, const UnownedStringSlice& name);

    static void splitPath(const UnownedStringSlice& slice, List<UnownedStringSlice>& outSplitPath);

    /// If markup is specified dump it
    void dumpMarkup(int indent, StringBuilder& out);

    Node(Kind type)
        : m_kind(type), m_parentScope(nullptr), m_reflectionType(ReflectionType::NotReflected)
    {
    }

    Kind m_kind;                     ///< The kind of node this is
    ReflectionType m_reflectionType; ///< Classes can be traversed, but not reflected. To be
                                     ///< reflected they have to contain the marker

    MarkupVisibility m_markupVisibility =
        MarkupVisibility::Public; ///< The visibility of the markup
    String m_markup;              ///< Documentation associated with this node

    Token m_name; ///< The name of this scope/type

    ScopeNode* m_parentScope; ///< The scope this type/scope is defined in
};

struct ScopeNode : public Node
{
    typedef Node Super;

    static bool isOfKind(Kind kind) { return isKindScope(kind); }

    virtual void dump(int indent, StringBuilder& out) SLANG_OVERRIDE;
    virtual void calcScopeDepthFirst(List<Node*>& outNodes) SLANG_OVERRIDE;

    /// True if can contain callable entries
    bool canContainCallable() const { return isClassLike() || isNamespace(); }

    /// True if can accept fields (class like types can)
    bool canContainFields() const { return isClassLike(); }

    /// True if the scope can accept types
    bool canContainTypes() const { return canKindContainTypes(m_kind); }

    /// Gets the reflection for any contained types
    ReflectionType getContainedReflectionType() const
    {
        return m_reflectionType == ReflectionType::NotReflected ? ReflectionType::NotReflected
                                                                : m_reflectionOverride;
    }

    /// Add a child node to this nodes scope
    void addChild(Node* child);
    /// Adds the child but does not add the name to the map
    void addChildIgnoringName(Node* child);

    /// Find a child node in this scope with the specified name. Return nullptr if not found
    Node* findChild(const UnownedStringSlice& name) const;

    /// Gets the anonymous namespace associated with this scope
    ScopeNode* getAnonymousNamespace();

    ScopeNode(Kind kind)
        : Super(kind)
        , m_reflectionOverride(ReflectionType::Reflected)
        , m_anonymousNamespace(nullptr)
    {
    }

    /// For child types, fields, how reflection is handled. If this type is not reflected
    ReflectionType m_reflectionOverride;

    /// All of the types and namespaces in this *scope*
    List<RefPtr<Node>> m_children;

    /// Map from a name (in this scope) to the Node
    Dictionary<UnownedStringSlice, Node*> m_childMap;

    /// There can only be one anonymousNamespace for a scope. If there is one it's held here
    ScopeNode* m_anonymousNamespace;
};

struct FieldNode : public Node
{
    typedef Node Super;

    static bool isOfKind(Kind kind) { return kind == Kind::Field; }

    virtual void dump(int indent, StringBuilder& out) SLANG_OVERRIDE;

    FieldNode()
        : Super(Kind::Field)
    {
    }

    UnownedStringSlice m_fieldType;

    bool m_isStatic = false;

    /// TODO(JS): We may want to add initializer tokens
};

struct ClassLikeNode : public ScopeNode
{
    typedef ScopeNode Super;

    static bool isOfKind(Kind kind) { return isKindClassLike(kind); }

    /// Add a node that is derived from this
    void addDerived(ClassLikeNode* derived);

    /// Dump all of the derived types
    void dumpDerived(int indentCount, StringBuilder& out);

    /// Calculates the derived depth
    Index calcDerivedDepth() const;

    /// Find the last (reflected) derived type
    ClassLikeNode* findLastDerived();

    /// Traverse the hierarchy of derived nodes, in depth first order
    void calcDerivedDepthFirst(List<ClassLikeNode*>& outNodes);

    /// True if has a derived type that is reflected
    bool hasReflectedDerivedType() const;

    /// Stores in out any reflected derived types
    void getReflectedDerivedTypes(List<ClassLikeNode*>& out) const;

    // Node Impl
    virtual void dump(int indent, StringBuilder& out) SLANG_OVERRIDE;

    ClassLikeNode(Kind kind)
        : Super(kind), m_origin(nullptr), m_typeSet(nullptr), m_superNode(nullptr)
    {
        SLANG_ASSERT(kind == Kind::ClassType || kind == Kind::StructType);
    }

    SourceOrigin* m_origin; ///< Defines where this was uniquely defined.

    Token m_marker; ///< The marker associated with this scope (typically the marker is SLANG_CLASS
                    ///< etc, that is used to identify reflectedType)

    List<RefPtr<ClassLikeNode>> m_derivedTypes; ///< All of the types derived from this type

    TypeSet* m_typeSet; ///< The typeset this type belongs to.

    Token m_super;              ///< Super class name
    ClassLikeNode* m_superNode; ///< If this is a class/struct, the type it is derived from (or
                                ///< nullptr if base)
};

struct CallableNode : public Node
{
    typedef Node Super;

    static bool isOfKind(Kind kind) { return kind == Kind::Callable; }

    virtual void dump(int indent, StringBuilder& out) SLANG_OVERRIDE;

    CallableNode()
        : Super(Kind::Callable)
    {
    }

    struct Param
    {
        UnownedStringSlice m_type;
        Token m_name;
    };

    UnownedStringSlice m_returnType;

    CallableNode* m_nextOverload = nullptr;

    List<Param> m_params;

    bool m_isStatic = false;
    bool m_isVirtual = false;
    bool m_isPure = false;
};

struct EnumCaseNode : public Node
{
    typedef Node Super;

    static bool isOfKind(Kind kind) { return kind == Kind::EnumCase; }

    virtual void dump(int indent, StringBuilder& out) SLANG_OVERRIDE;

    EnumCaseNode()
        : Super(Kind::EnumCase)
    {
    }

    // Tokens that make up the value. If not defined will be empty
    List<Token> m_valueTokens;
};

struct EnumNode : public ScopeNode
{
    typedef ScopeNode Super;
    static bool isOfKind(Kind kind) { return isKindEnumLike(kind); }

    virtual void dump(int indent, StringBuilder& out) SLANG_OVERRIDE;

    EnumNode(Kind kind)
        : Super(kind)
    {
        SLANG_ASSERT(isKindEnumLike(kind));
    }

    List<Token> m_backingTokens;
};

struct TypeDefNode : public Node
{
    typedef Node Super;
    static bool isOfKind(Kind kind) { return kind == Kind::TypeDef; }

    virtual void dump(int indent, StringBuilder& out) SLANG_OVERRIDE;

    TypeDefNode()
        : Super(Kind::TypeDef)
    {
    }

    List<Token> m_targetTypeTokens;
};

template<typename T>
T* as(Node* node)
{
    return (node && T::isOfKind(node->m_kind)) ? static_cast<T*>(node) : nullptr;
}

} // namespace CppExtract

#endif