forked from google/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_descriptor.h
More file actions
124 lines (102 loc) · 4.68 KB
/
function_descriptor.h
File metadata and controls
124 lines (102 loc) · 4.68 KB
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
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_CEL_CPP_COMMON_FUNCTION_DESCRIPTOR_H_
#define THIRD_PARTY_CEL_CPP_COMMON_FUNCTION_DESCRIPTOR_H_
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "common/kind.h"
namespace cel {
struct FunctionDescriptorOptions {
// If true (strict, default), error or unknown arguments are propagated
// instead of calling the function. if false (non-strict), the function may
// receive error or unknown values as arguments.
bool is_strict = true;
// Whether the function is impure or context-sensitive.
//
// Impure functions depend on state other than the arguments received during
// the CEL expression evaluation or have visible side effects. This breaks
// some of the assumptions of the CEL evaluation model. This flag is used as a
// hint to the planner that some optimizations are not safe or not effective.
bool is_contextual = false;
};
// Coarsely describes a function for the purpose of runtime resolution of
// overloads.
class FunctionDescriptor final {
public:
FunctionDescriptor(absl::string_view name, bool receiver_style,
std::vector<Kind> types, bool is_strict)
: impl_(std::make_shared<Impl>(
name, std::move(types), receiver_style,
FunctionDescriptorOptions{is_strict,
/*is_contextual=*/false})) {}
FunctionDescriptor(absl::string_view name, bool receiver_style,
std::vector<Kind> types, bool is_strict,
bool is_contextual)
: impl_(std::make_shared<Impl>(
name, std::move(types), receiver_style,
FunctionDescriptorOptions{is_strict, is_contextual})) {}
FunctionDescriptor(absl::string_view name, bool is_receiver_style,
std::vector<Kind> types,
FunctionDescriptorOptions options = {})
: impl_(std::make_shared<Impl>(name, std::move(types), is_receiver_style,
options)) {}
// Function name.
const std::string& name() const { return impl_->name; }
// Whether function is receiver style i.e. true means arg0.name(args[1:]...).
bool receiver_style() const { return impl_->is_receiver_style; }
// The argument types the function accepts.
//
// TODO(uncreated-issue/17): make this kinds
const std::vector<Kind>& types() const { return impl_->types; }
// if true (strict, default), error or unknown arguments are propagated
// instead of calling the function. if false (non-strict), the function may
// receive error or unknown values as arguments.
bool is_strict() const { return impl_->options.is_strict; }
// Whether the function is contextual (impure).
//
// Contextual functions depend on state other than the arguments received in
// the CEL expression evaluation or have visible side effects. This breaks
// some of the assumptions of CEL. This flag is used as a hint to the planner
// that some optimizations are not safe or not effective.
bool is_contextual() const { return impl_->options.is_contextual; }
// Helper for matching a descriptor. This tests that the shape is the same --
// |other| accepts the same number and types of arguments and is the same call
// style).
bool ShapeMatches(const FunctionDescriptor& other) const {
return ShapeMatches(other.receiver_style(), other.types());
}
bool ShapeMatches(bool receiver_style, absl::Span<const Kind> types) const;
bool operator==(const FunctionDescriptor& other) const;
bool operator<(const FunctionDescriptor& other) const;
private:
struct Impl final {
Impl(absl::string_view name, std::vector<Kind> types,
bool is_receiver_style, FunctionDescriptorOptions options)
: name(name),
types(std::move(types)),
is_receiver_style(is_receiver_style),
options(options) {}
std::string name;
std::vector<Kind> types;
bool is_receiver_style;
FunctionDescriptorOptions options;
};
std::shared_ptr<const Impl> impl_;
};
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_COMMON_FUNCTION_DESCRIPTOR_H_