summaryrefslogtreecommitdiffstats
path: root/tools/slang-cpp-extractor/node.h
blob: c741024e4f784578c2253883a5174b92ee69368d (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
#ifndef CPP_EXTRACT_NODE_H
#define CPP_EXTRACT_NODE_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 Type : uint8_t
    {
        Invalid,

        StructType,
        ClassType,

        Enum,               
        EnumClass,

        Namespace,
        AnonymousNamespace,

        Field,
        EnumCase,

        TypeDef,

        CountOf,
    };

    enum class TypeRange
    {
        ScopeStart = int(Type::StructType),
        ScopeEnd = int(Type::AnonymousNamespace),

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

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

    static bool isScopeType(Type type) { return int(type) >= int(TypeRange::ScopeStart) && int(type) <= int(TypeRange::ScopeEnd); }
    static bool isClassLikeType(Type type) { return int(type) >= int(TypeRange::ClassLikeStart) &&  int(type) <= int(TypeRange::ClassLikeEnd); }
    static bool isEnumLikeType(Type type) { return int(type) >= int(TypeRange::EnumStart) &&  int(type) <= int(TypeRange::EnumEnd); }
    static bool canAcceptTypes(Type type)
    {
        switch (type)
        {
            case Type::StructType:
            case Type::ClassType:
            case Type::Namespace:
            case Type::AnonymousNamespace:
            {
                return true;
            }
            default: break;
        }
        return false;
    }

    static bool isType(Type type) { return true; }

    bool isClassLike() const { return isClassLikeType(m_type); }

    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; }

    ScopeNode* getRootScope();

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

    static bool isClassLikeAndReflected(Node* node) { return node->isClassLike() && node->isReflected(); }
    static bool isClassLike(Node* node) { return isClassLikeType(node->m_type); }

    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);

    Node(Type type) :
        m_type(type),
        m_parentScope(nullptr),
        m_reflectionType(ReflectionType::NotReflected)
    {
    }

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

    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 isType(Type type) { return isScopeType(type); }

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

        /// True if can accept fields (class like types can)
    bool acceptsFields() const { return isClassLike(); }
        /// True if the scope can accept types
    bool acceptsTypes() const { return canAcceptTypes(m_type); }

        /// 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);

        /// 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(Type type) :
        Super(type),
        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 isType(Type type) { return type == Type::Field; }

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

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

    UnownedStringSlice m_fieldType;

    // We may want to add initializer tokens
};

struct ClassLikeNode : public ScopeNode
{
    typedef ScopeNode Super;

    static bool isType(Type type) { return isClassLikeType(type); }

        /// 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(Type type) :
        Super(type),
        m_origin(nullptr),
        m_typeSet(nullptr),
        m_superNode(nullptr)
    {
        SLANG_ASSERT(type == Type::ClassType || type == Type::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 EnumCaseNode : public Node
{
    typedef Node Super;

    static bool isType(Type type) { return type == Type::EnumCase; }

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

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

    Token m_value;              ///< If not defined will be invalid
};

struct EnumNode : public ScopeNode
{
    typedef ScopeNode Super;
    static bool isType(Type type) { return isEnumLikeType(type); }

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

    EnumNode(Type type):
        Super(type)
    {
        SLANG_ASSERT(isEnumLikeType(type));
    }

    Token m_backingToken;
};

struct TypeDefNode : public Node
{
    typedef Node Super;
    static bool isType(Type type) { return type == Type::TypeDef; }

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

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

    List<Token> m_targetTypeTokens;
};

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

} // CppExtract

#endif