mkql_node_cast.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "mkql_node_cast.h"
  2. #define MKQL_AS_VALUE(name, suffix) \
  3. template <> \
  4. T##name##suffix* \
  5. AsValue(TRuntimeNode node, const TSourceLocation& location) { \
  6. MKQL_ENSURE_WITH_LOC( \
  7. location, \
  8. node.HasValue() && node.GetStaticType()->Is##name(), \
  9. "Expected value of T" #name #suffix << \
  10. " but got " << node.GetStaticType()->GetKindAsStr()); \
  11. return static_cast<T##name##suffix*>(node.GetValue()); \
  12. }
  13. #define MKQL_AS_TYPE(name) \
  14. template <> \
  15. T##name##Type* \
  16. AsType(TType* type, const TSourceLocation& location) { \
  17. MKQL_ENSURE_WITH_LOC( \
  18. location, \
  19. type->Is##name(), \
  20. "Expected type of T" #name "Type" \
  21. " but got " << type->GetKindAsStr()); \
  22. return static_cast<T##name##Type*>(type); \
  23. } \
  24. template <> \
  25. const T##name##Type* \
  26. AsType(const TType* type, const TSourceLocation& location) { \
  27. MKQL_ENSURE_WITH_LOC( \
  28. location, \
  29. type->Is##name(), \
  30. "Expected type of T" #name "Type" \
  31. " but got " << type->GetKindAsStr()); \
  32. return static_cast<const T##name##Type*>(type); \
  33. }
  34. namespace NKikimr {
  35. namespace NMiniKQL {
  36. MKQL_AS_TYPE(Any)
  37. MKQL_AS_TYPE(Callable)
  38. MKQL_AS_TYPE(Data)
  39. MKQL_AS_TYPE(Dict)
  40. MKQL_AS_TYPE(List)
  41. MKQL_AS_TYPE(Optional)
  42. MKQL_AS_TYPE(Struct)
  43. MKQL_AS_TYPE(Tuple)
  44. MKQL_AS_TYPE(Type)
  45. MKQL_AS_TYPE(Void)
  46. MKQL_AS_TYPE(Resource)
  47. MKQL_AS_TYPE(Variant)
  48. MKQL_AS_TYPE(Stream)
  49. MKQL_AS_TYPE(Flow)
  50. MKQL_AS_TYPE(Tagged)
  51. MKQL_AS_TYPE(Block)
  52. MKQL_AS_TYPE(Pg)
  53. MKQL_AS_TYPE(Multi)
  54. MKQL_AS_VALUE(Any, Type)
  55. MKQL_AS_VALUE(Callable, Type)
  56. MKQL_AS_VALUE(Data, Type)
  57. MKQL_AS_VALUE(Dict, Type)
  58. MKQL_AS_VALUE(List, Type)
  59. MKQL_AS_VALUE(Optional, Type)
  60. MKQL_AS_VALUE(Struct, Type)
  61. MKQL_AS_VALUE(Tuple, Type)
  62. MKQL_AS_VALUE(Type, Type)
  63. MKQL_AS_VALUE(Void, Type)
  64. MKQL_AS_VALUE(Variant, Type)
  65. MKQL_AS_VALUE(Stream, Type)
  66. MKQL_AS_VALUE(Flow, Type)
  67. MKQL_AS_VALUE(Data, Literal)
  68. MKQL_AS_VALUE(Dict, Literal)
  69. MKQL_AS_VALUE(List, Literal)
  70. MKQL_AS_VALUE(Optional, Literal)
  71. MKQL_AS_VALUE(Struct, Literal)
  72. MKQL_AS_VALUE(Tuple, Literal)
  73. MKQL_AS_VALUE(Variant, Literal)
  74. TCallable* AsCallable(
  75. const TStringBuf& name,
  76. TRuntimeNode node,
  77. const TSourceLocation& location)
  78. {
  79. MKQL_ENSURE_WITH_LOC(location,
  80. !node.IsImmediate() && node.GetNode()->GetType()->IsCallable(),
  81. "Expected callable " << name);
  82. auto callable = static_cast<TCallable*>(node.GetNode());
  83. MKQL_ENSURE_WITH_LOC(location,
  84. callable->GetType()->GetName() == name,
  85. "Expected callable " << name);
  86. return callable;
  87. }
  88. } // namespace NMiniKQL
  89. } // namespace NKikimr