mkql_node_visitor.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #pragma once
  2. #include "defs.h"
  3. #include "mkql_node.h"
  4. #include <util/generic/deque.h>
  5. #include <util/generic/hash.h>
  6. #include <util/generic/hash_set.h>
  7. #include <util/generic/vector.h>
  8. #include <library/cpp/containers/stack_vector/stack_vec.h>
  9. #include <functional>
  10. namespace NKikimr {
  11. namespace NMiniKQL {
  12. class INodeVisitor {
  13. public:
  14. virtual ~INodeVisitor() {}
  15. virtual void Visit(TTypeType& node) = 0;
  16. virtual void Visit(TVoidType& node) = 0;
  17. virtual void Visit(TNullType& node) = 0;
  18. virtual void Visit(TEmptyListType& node) = 0;
  19. virtual void Visit(TEmptyDictType& node) = 0;
  20. virtual void Visit(TDataType& node) = 0;
  21. virtual void Visit(TPgType& node) = 0;
  22. virtual void Visit(TStructType& node) = 0;
  23. virtual void Visit(TListType& node) = 0;
  24. virtual void Visit(TOptionalType& node) = 0;
  25. virtual void Visit(TDictType& node) = 0;
  26. virtual void Visit(TCallableType& node) = 0;
  27. virtual void Visit(TAnyType& node) = 0;
  28. virtual void Visit(TTupleType& node) = 0;
  29. virtual void Visit(TResourceType& node) = 0;
  30. virtual void Visit(TVariantType& node) = 0;
  31. virtual void Visit(TVoid& node) = 0;
  32. virtual void Visit(TNull& node) = 0;
  33. virtual void Visit(TEmptyList& node) = 0;
  34. virtual void Visit(TEmptyDict& node) = 0;
  35. virtual void Visit(TDataLiteral& node) = 0;
  36. virtual void Visit(TStructLiteral& node) = 0;
  37. virtual void Visit(TListLiteral& node) = 0;
  38. virtual void Visit(TOptionalLiteral& node) = 0;
  39. virtual void Visit(TDictLiteral& node) = 0;
  40. virtual void Visit(TCallable& node) = 0;
  41. virtual void Visit(TAny& node) = 0;
  42. virtual void Visit(TTupleLiteral& node) = 0;
  43. virtual void Visit(TVariantLiteral& node) = 0;
  44. virtual void Visit(TStreamType& node) = 0;
  45. virtual void Visit(TFlowType& node) = 0;
  46. virtual void Visit(TTaggedType& node) = 0;
  47. virtual void Visit(TBlockType& node) = 0;
  48. virtual void Visit(TMultiType& node) = 0;
  49. };
  50. class TThrowingNodeVisitor : public INodeVisitor {
  51. public:
  52. void Visit(TTypeType& node) override;
  53. void Visit(TVoidType& node) override;
  54. void Visit(TNullType& node) override;
  55. void Visit(TEmptyListType& node) override;
  56. void Visit(TEmptyDictType& node) override;
  57. void Visit(TDataType& node) override;
  58. void Visit(TPgType& node) override;
  59. void Visit(TStructType& node) override;
  60. void Visit(TListType& node) override;
  61. void Visit(TOptionalType& node) override;
  62. void Visit(TDictType& node) override;
  63. void Visit(TCallableType& node) override;
  64. void Visit(TAnyType& node) override;
  65. void Visit(TTupleType& node) override;
  66. void Visit(TResourceType& node) override;
  67. void Visit(TVariantType& node) override;
  68. void Visit(TVoid& node) override;
  69. void Visit(TNull& node) override;
  70. void Visit(TEmptyList& node) override;
  71. void Visit(TEmptyDict& node) override;
  72. void Visit(TDataLiteral& node) override;
  73. void Visit(TStructLiteral& node) override;
  74. void Visit(TListLiteral& node) override;
  75. void Visit(TOptionalLiteral& node) override;
  76. void Visit(TDictLiteral& node) override;
  77. void Visit(TCallable& node) override;
  78. void Visit(TAny& node) override;
  79. void Visit(TTupleLiteral& node) override;
  80. void Visit(TVariantLiteral& node) override;
  81. void Visit(TStreamType& node) override;
  82. void Visit(TFlowType& node) override;
  83. void Visit(TTaggedType& node) override;
  84. void Visit(TBlockType& node) override;
  85. void Visit(TMultiType& node) override;
  86. protected:
  87. static void ThrowUnexpectedNodeType();
  88. };
  89. class TEmptyNodeVisitor: public INodeVisitor {
  90. public:
  91. void Visit(TTypeType& node) override;
  92. void Visit(TVoidType& node) override;
  93. void Visit(TNullType& node) override;
  94. void Visit(TEmptyListType& node) override;
  95. void Visit(TEmptyDictType& node) override;
  96. void Visit(TDataType& node) override;
  97. void Visit(TPgType& node) override;
  98. void Visit(TStructType& node) override;
  99. void Visit(TListType& node) override;
  100. void Visit(TOptionalType& node) override;
  101. void Visit(TDictType& node) override;
  102. void Visit(TCallableType& node) override;
  103. void Visit(TAnyType& node) override;
  104. void Visit(TTupleType& node) override;
  105. void Visit(TResourceType& node) override;
  106. void Visit(TVariantType& node) override;
  107. void Visit(TVoid& node) override;
  108. void Visit(TNull& node) override;
  109. void Visit(TEmptyList& node) override;
  110. void Visit(TEmptyDict& node) override;
  111. void Visit(TDataLiteral& node) override;
  112. void Visit(TStructLiteral& node) override;
  113. void Visit(TListLiteral& node) override;
  114. void Visit(TOptionalLiteral& node) override;
  115. void Visit(TDictLiteral& node) override;
  116. void Visit(TCallable& node) override;
  117. void Visit(TAny& node) override;
  118. void Visit(TTupleLiteral& node) override;
  119. void Visit(TVariantLiteral& node) override;
  120. void Visit(TStreamType& node) override;
  121. void Visit(TFlowType& node) override;
  122. void Visit(TTaggedType& node) override;
  123. void Visit(TBlockType& node) override;
  124. void Visit(TMultiType& node) override;
  125. };
  126. class TExploringNodeVisitor : public INodeVisitor {
  127. public:
  128. using TNodesVec = TStackVec<TNode*, 2>;
  129. public:
  130. void Visit(TTypeType& node) override;
  131. void Visit(TVoidType& node) override;
  132. void Visit(TNullType& node) override;
  133. void Visit(TEmptyListType& node) override;
  134. void Visit(TEmptyDictType& node) override;
  135. void Visit(TDataType& node) override;
  136. void Visit(TPgType& node) override;
  137. void Visit(TStructType& node) override;
  138. void Visit(TListType& node) override;
  139. void Visit(TOptionalType& node) override;
  140. void Visit(TDictType& node) override;
  141. void Visit(TCallableType& node) override;
  142. void Visit(TAnyType& node) override;
  143. void Visit(TTupleType& node) override;
  144. void Visit(TResourceType& node) override;
  145. void Visit(TVariantType& node) override;
  146. void Visit(TVoid& node) override;
  147. void Visit(TNull& node) override;
  148. void Visit(TEmptyList& node) override;
  149. void Visit(TEmptyDict& node) override;
  150. void Visit(TDataLiteral& node) override;
  151. void Visit(TStructLiteral& node) override;
  152. void Visit(TListLiteral& node) override;
  153. void Visit(TOptionalLiteral& node) override;
  154. void Visit(TDictLiteral& node) override;
  155. void Visit(TCallable& node) override;
  156. void Visit(TAny& node) override;
  157. void Visit(TTupleLiteral& node) override;
  158. void Visit(TVariantLiteral& node) override;
  159. void Visit(TStreamType& node) override;
  160. void Visit(TFlowType& node) override;
  161. void Visit(TTaggedType& node) override;
  162. void Visit(TBlockType& node) override;
  163. void Visit(TMultiType& node) override;
  164. void Walk(TNode* root, const TTypeEnvironment& env, const std::vector<TNode*>& terminalNodes = std::vector<TNode*>(),
  165. bool buildConsumersMap = false, size_t nodesCountHint = 0);
  166. const std::vector<TNode*>& GetNodes();
  167. const TNodesVec& GetConsumerNodes(TNode& node);
  168. void Clear();
  169. private:
  170. void AddChildNode(TNode* parent, TNode& child);
  171. private:
  172. std::vector<TNode*> NodeList;
  173. std::vector<TNode*>* Stack = nullptr;
  174. bool BuildConsumersMap = false;
  175. std::unordered_map<TNode*, TNodesVec> ConsumersMap;
  176. };
  177. class TTypeEnvironment;
  178. typedef std::function<TRuntimeNode (TCallable& callable, const TTypeEnvironment& env)> TCallableVisitFunc;
  179. typedef std::function<TCallableVisitFunc (const TInternName& name)> TCallableVisitFuncProvider;
  180. TRuntimeNode SinglePassVisitCallables(TRuntimeNode root, TExploringNodeVisitor& explorer,
  181. const TCallableVisitFuncProvider& funcProvider, const TTypeEnvironment& env, bool inPlace, bool& wereChanges);
  182. }
  183. }