aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/mkql_node_visitor.h
blob: dfd76942da5084f099cc3a99d4a580532f537268 (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
#pragma once
#include "defs.h"
#include "mkql_node.h"
#include <util/generic/deque.h>
#include <util/generic/hash.h>
#include <util/generic/hash_set.h>
#include <util/generic/vector.h>
#include <library/cpp/containers/stack_vector/stack_vec.h>
#include <functional>

namespace NKikimr {
namespace NMiniKQL {

class INodeVisitor {
public:
    virtual ~INodeVisitor() {}
    virtual void Visit(TTypeType& node) = 0;
    virtual void Visit(TVoidType& node) = 0;
    virtual void Visit(TNullType& node) = 0;
    virtual void Visit(TEmptyListType& node) = 0;
    virtual void Visit(TEmptyDictType& node) = 0;
    virtual void Visit(TDataType& node) = 0;
    virtual void Visit(TPgType& node) = 0;
    virtual void Visit(TStructType& node) = 0;
    virtual void Visit(TListType& node) = 0;
    virtual void Visit(TOptionalType& node) = 0;
    virtual void Visit(TDictType& node) = 0;
    virtual void Visit(TCallableType& node) = 0;
    virtual void Visit(TAnyType& node) = 0;
    virtual void Visit(TTupleType& node) = 0;
    virtual void Visit(TResourceType& node) = 0;
    virtual void Visit(TVariantType& node) = 0;
    virtual void Visit(TVoid& node) = 0;
    virtual void Visit(TNull& node) = 0;
    virtual void Visit(TEmptyList& node) = 0;
    virtual void Visit(TEmptyDict& node) = 0;
    virtual void Visit(TDataLiteral& node) = 0;
    virtual void Visit(TStructLiteral& node) = 0;
    virtual void Visit(TListLiteral& node) = 0;
    virtual void Visit(TOptionalLiteral& node) = 0;
    virtual void Visit(TDictLiteral& node) = 0;
    virtual void Visit(TCallable& node) = 0;
    virtual void Visit(TAny& node) = 0;
    virtual void Visit(TTupleLiteral& node) = 0;
    virtual void Visit(TVariantLiteral& node) = 0;
    virtual void Visit(TStreamType& node) = 0;
    virtual void Visit(TFlowType& node) = 0;
    virtual void Visit(TTaggedType& node) = 0;
    virtual void Visit(TBlockType& node) = 0;
    virtual void Visit(TMultiType& node) = 0;
};

class TThrowingNodeVisitor : public INodeVisitor {
public:
    void Visit(TTypeType& node) override;
    void Visit(TVoidType& node) override;
    void Visit(TNullType& node) override;
    void Visit(TEmptyListType& node) override;
    void Visit(TEmptyDictType& node) override;
    void Visit(TDataType& node) override;
    void Visit(TPgType& node) override;
    void Visit(TStructType& node) override;
    void Visit(TListType& node) override;
    void Visit(TOptionalType& node) override;
    void Visit(TDictType& node) override;
    void Visit(TCallableType& node) override;
    void Visit(TAnyType& node) override;
    void Visit(TTupleType& node) override;
    void Visit(TResourceType& node) override;
    void Visit(TVariantType& node) override;
    void Visit(TVoid& node) override;
    void Visit(TNull& node) override;
    void Visit(TEmptyList& node) override;
    void Visit(TEmptyDict& node) override;
    void Visit(TDataLiteral& node) override;
    void Visit(TStructLiteral& node) override;
    void Visit(TListLiteral& node) override;
    void Visit(TOptionalLiteral& node) override;
    void Visit(TDictLiteral& node) override;
    void Visit(TCallable& node) override;
    void Visit(TAny& node) override;
    void Visit(TTupleLiteral& node) override;
    void Visit(TVariantLiteral& node) override;
    void Visit(TStreamType& node) override;
    void Visit(TFlowType& node) override;
    void Visit(TTaggedType& node) override;
    void Visit(TBlockType& node) override;
    void Visit(TMultiType& node) override;

protected:
    static void ThrowUnexpectedNodeType();
};

class TEmptyNodeVisitor: public INodeVisitor {
public:
    void Visit(TTypeType& node) override;
    void Visit(TVoidType& node) override;
    void Visit(TNullType& node) override;
    void Visit(TEmptyListType& node) override;
    void Visit(TEmptyDictType& node) override;
    void Visit(TDataType& node) override;
    void Visit(TPgType& node) override;
    void Visit(TStructType& node) override;
    void Visit(TListType& node) override;
    void Visit(TOptionalType& node) override;
    void Visit(TDictType& node) override;
    void Visit(TCallableType& node) override;
    void Visit(TAnyType& node) override;
    void Visit(TTupleType& node) override;
    void Visit(TResourceType& node) override;
    void Visit(TVariantType& node) override;
    void Visit(TVoid& node) override;
    void Visit(TNull& node) override;
    void Visit(TEmptyList& node) override;
    void Visit(TEmptyDict& node) override;
    void Visit(TDataLiteral& node) override;
    void Visit(TStructLiteral& node) override;
    void Visit(TListLiteral& node) override;
    void Visit(TOptionalLiteral& node) override;
    void Visit(TDictLiteral& node) override;
    void Visit(TCallable& node) override;
    void Visit(TAny& node) override;
    void Visit(TTupleLiteral& node) override;
    void Visit(TVariantLiteral& node) override;
    void Visit(TStreamType& node) override;
    void Visit(TFlowType& node) override;
    void Visit(TTaggedType& node) override;
    void Visit(TBlockType& node) override;
    void Visit(TMultiType& node) override;
};

class TExploringNodeVisitor : public INodeVisitor {
public:
    using TNodesVec = TStackVec<TNode*, 2>;
public:
    void Visit(TTypeType& node) override;
    void Visit(TVoidType& node) override;
    void Visit(TNullType& node) override;
    void Visit(TEmptyListType& node) override;
    void Visit(TEmptyDictType& node) override;
    void Visit(TDataType& node) override;
    void Visit(TPgType& node) override;
    void Visit(TStructType& node) override;
    void Visit(TListType& node) override;
    void Visit(TOptionalType& node) override;
    void Visit(TDictType& node) override;
    void Visit(TCallableType& node) override;
    void Visit(TAnyType& node) override;
    void Visit(TTupleType& node) override;
    void Visit(TResourceType& node) override;
    void Visit(TVariantType& node) override;
    void Visit(TVoid& node) override;
    void Visit(TNull& node) override;
    void Visit(TEmptyList& node) override;
    void Visit(TEmptyDict& node) override;
    void Visit(TDataLiteral& node) override;
    void Visit(TStructLiteral& node) override;
    void Visit(TListLiteral& node) override;
    void Visit(TOptionalLiteral& node) override;
    void Visit(TDictLiteral& node) override;
    void Visit(TCallable& node) override;
    void Visit(TAny& node) override;
    void Visit(TTupleLiteral& node) override;
    void Visit(TVariantLiteral& node) override;
    void Visit(TStreamType& node) override;
    void Visit(TFlowType& node) override;
    void Visit(TTaggedType& node) override;
    void Visit(TBlockType& node) override;
    void Visit(TMultiType& node) override;

    void Walk(TNode* root, const TTypeEnvironment& env, const std::vector<TNode*>& terminalNodes = std::vector<TNode*>(),
        bool buildConsumersMap = false, size_t nodesCountHint = 0);
    const std::vector<TNode*>& GetNodes();
    const TNodesVec& GetConsumerNodes(TNode& node);
    void Clear();

private:
    void AddChildNode(TNode* parent, TNode& child);

private:
    std::vector<TNode*> NodeList;
    std::vector<TNode*>* Stack = nullptr;
    bool BuildConsumersMap = false;
    std::unordered_map<TNode*, TNodesVec> ConsumersMap;
};

class TTypeEnvironment;
typedef std::function<TRuntimeNode (TCallable& callable, const TTypeEnvironment& env)> TCallableVisitFunc;
typedef std::function<TCallableVisitFunc (const TInternName& name)> TCallableVisitFuncProvider;

TRuntimeNode SinglePassVisitCallables(TRuntimeNode root, TExploringNodeVisitor& explorer,
    const TCallableVisitFuncProvider& funcProvider, const TTypeEnvironment& env, bool inPlace, bool& wereChanges);

}
}