aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/mkql_node_cast.cpp
blob: 25d7aa6e0bda6e010cec7d0ea91ae498bfaa470b (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
#include "mkql_node_cast.h"

#define MKQL_AS_VALUE(name, suffix)                                      \
    template <>                                                          \
    T##name##suffix*                                                     \
    AsValue(TRuntimeNode node, const TSourceLocation& location) {        \
        MKQL_ENSURE_WITH_LOC(                                            \
                location,                                                \
                node.HasValue() && node.GetStaticType()->Is##name(),     \
                "Expected value of T" #name #suffix <<                   \
                " but got " << node.GetStaticType()->GetKindAsStr());    \
        return static_cast<T##name##suffix*>(node.GetValue());           \
    }

#define MKQL_AS_TYPE(name)                                               \
    template <>                                                          \
    T##name##Type*                                                       \
    AsType(TType* type, const TSourceLocation& location) {               \
        MKQL_ENSURE_WITH_LOC(                                            \
                location,                                                \
                type->Is##name(),                                        \
                   "Expected type of T" #name "Type"                     \
                   " but got " << type->GetKindAsStr());                 \
        return static_cast<T##name##Type*>(type);                        \
    }                                                                    \
    template <>                                                          \
    const T##name##Type*                                                 \
    AsType(const TType* type, const TSourceLocation& location) {         \
        MKQL_ENSURE_WITH_LOC(                                            \
                location,                                                \
                type->Is##name(),                                        \
                   "Expected type of T" #name "Type"                     \
                   " but got " << type->GetKindAsStr());                 \
        return static_cast<const T##name##Type*>(type);                  \
    }

namespace NKikimr {
namespace NMiniKQL {

MKQL_AS_TYPE(Any)
MKQL_AS_TYPE(Callable)
MKQL_AS_TYPE(Data)
MKQL_AS_TYPE(Dict)
MKQL_AS_TYPE(List)
MKQL_AS_TYPE(Optional)
MKQL_AS_TYPE(Struct)
MKQL_AS_TYPE(Tuple)
MKQL_AS_TYPE(Type)
MKQL_AS_TYPE(Void)
MKQL_AS_TYPE(Resource)
MKQL_AS_TYPE(Variant)
MKQL_AS_TYPE(Stream)
MKQL_AS_TYPE(Flow)
MKQL_AS_TYPE(Tagged)
MKQL_AS_TYPE(Block)
MKQL_AS_TYPE(Pg)
MKQL_AS_TYPE(Multi)

MKQL_AS_VALUE(Any, Type)
MKQL_AS_VALUE(Callable, Type)
MKQL_AS_VALUE(Data, Type)
MKQL_AS_VALUE(Dict, Type)
MKQL_AS_VALUE(List, Type)
MKQL_AS_VALUE(Optional, Type)
MKQL_AS_VALUE(Struct, Type)
MKQL_AS_VALUE(Tuple, Type)
MKQL_AS_VALUE(Type, Type)
MKQL_AS_VALUE(Void, Type)
MKQL_AS_VALUE(Variant, Type)
MKQL_AS_VALUE(Stream, Type)
MKQL_AS_VALUE(Flow, Type)

MKQL_AS_VALUE(Data, Literal)
MKQL_AS_VALUE(Dict, Literal)
MKQL_AS_VALUE(List, Literal)
MKQL_AS_VALUE(Optional, Literal)
MKQL_AS_VALUE(Struct, Literal)
MKQL_AS_VALUE(Tuple, Literal)
MKQL_AS_VALUE(Variant, Literal)

TCallable* AsCallable(
        const TStringBuf& name,
        TRuntimeNode node,
        const TSourceLocation& location)
{
    MKQL_ENSURE_WITH_LOC(location,
            !node.IsImmediate() && node.GetNode()->GetType()->IsCallable(),
            "Expected callable " << name);

    auto callable = static_cast<TCallable*>(node.GetNode());
    MKQL_ENSURE_WITH_LOC(location,
            callable->GetType()->GetName() == name,
            "Expected callable " << name);

    return callable;
}

} // namespace NMiniKQL
} // namespace NKikimr