Skip to content

Commit 9779448

Browse files
jnthntatumcopybara-github
authored andcommitted
File moves:
- /base/function.h -> /runtime/function.h - /base/function_descriptor.h -> /common/function_descriptor.h PiperOrigin-RevId: 720265078
1 parent 5f95693 commit 9779448

8 files changed

Lines changed: 188 additions & 128 deletions

File tree

base/BUILD

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,17 @@ cc_library(
9696
"function.h",
9797
],
9898
deps = [
99-
"//common:value",
100-
"@com_google_absl//absl/status:statusor",
101-
"@com_google_absl//absl/types:span",
99+
"//runtime:function",
102100
],
103101
)
104102

105103
cc_library(
106104
name = "function_descriptor",
107-
srcs = [
108-
"function_descriptor.cc",
109-
],
110105
hdrs = [
111106
"function_descriptor.h",
112107
],
113108
deps = [
114-
":kind",
115-
"@com_google_absl//absl/base:core_headers",
116-
"@com_google_absl//absl/strings",
117-
"@com_google_absl//absl/types:span",
109+
"//common:function_descriptor",
118110
],
119111
)
120112

base/function.h

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,54 +15,6 @@
1515
#ifndef THIRD_PARTY_CEL_CPP_BASE_FUNCTION_H_
1616
#define THIRD_PARTY_CEL_CPP_BASE_FUNCTION_H_
1717

18-
#include "absl/status/statusor.h"
19-
#include "absl/types/span.h"
20-
#include "common/value.h"
21-
#include "common/value_manager.h"
22-
23-
namespace cel {
24-
25-
// Interface for extension functions.
26-
//
27-
// The host for the CEL environment may provide implementations to define custom
28-
// extensions functions.
29-
//
30-
// The interpreter expects functions to be deterministic and side-effect free.
31-
class Function {
32-
public:
33-
virtual ~Function() = default;
34-
35-
// InvokeContext provides access to current evaluator state.
36-
class InvokeContext final {
37-
public:
38-
explicit InvokeContext(cel::ValueManager& value_manager)
39-
: value_manager_(value_manager) {}
40-
41-
// Return the value_factory defined for the evaluation invoking the
42-
// extension function.
43-
cel::ValueManager& value_factory() const { return value_manager_; }
44-
45-
// TODO: Add accessors for getting attribute stack and mutable
46-
// value stack.
47-
private:
48-
cel::ValueManager& value_manager_;
49-
};
50-
51-
// Attempt to evaluate an extension function based on the runtime arguments
52-
// during the evaluation of a CEL expression.
53-
//
54-
// A non-ok status is interpreted as an unrecoverable error in evaluation (
55-
// e.g. data corruption). This stops evaluation and is propagated immediately.
56-
//
57-
// A cel::ErrorValue typed result is considered a recoverable error and
58-
// follows CEL's logical short-circuiting behavior.
59-
virtual absl::StatusOr<Value> Invoke(const InvokeContext& context,
60-
absl::Span<const Value> args) const = 0;
61-
};
62-
63-
// Legacy type, aliased to the actual type.
64-
using FunctionEvaluationContext = Function::InvokeContext;
65-
66-
} // namespace cel
18+
#include "runtime/function.h" // IWYU pragma: export
6719

6820
#endif // THIRD_PARTY_CEL_CPP_BASE_FUNCTION_H_

base/function_descriptor.h

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -15,71 +15,6 @@
1515
#ifndef THIRD_PARTY_CEL_CPP_BASE_FUNCTION_DESCRIPTOR_H_
1616
#define THIRD_PARTY_CEL_CPP_BASE_FUNCTION_DESCRIPTOR_H_
1717

18-
#include <memory>
19-
#include <string>
20-
#include <utility>
21-
#include <vector>
22-
23-
#include "absl/strings/string_view.h"
24-
#include "absl/types/span.h"
25-
#include "base/kind.h"
26-
27-
namespace cel {
28-
29-
// Describes a function.
30-
class FunctionDescriptor final {
31-
public:
32-
FunctionDescriptor(absl::string_view name, bool receiver_style,
33-
std::vector<Kind> types, bool is_strict = true)
34-
: impl_(std::make_shared<Impl>(name, receiver_style, std::move(types),
35-
is_strict)) {}
36-
37-
// Function name.
38-
const std::string& name() const { return impl_->name; }
39-
40-
// Whether function is receiver style i.e. true means arg0.name(args[1:]...).
41-
bool receiver_style() const { return impl_->receiver_style; }
42-
43-
// The argmument types the function accepts.
44-
//
45-
// TODO: make this kinds
46-
const std::vector<Kind>& types() const { return impl_->types; }
47-
48-
// if true (strict, default), error or unknown arguments are propagated
49-
// instead of calling the function. if false (non-strict), the function may
50-
// receive error or unknown values as arguments.
51-
bool is_strict() const { return impl_->is_strict; }
52-
53-
// Helper for matching a descriptor. This tests that the shape is the same --
54-
// |other| accepts the same number and types of arguments and is the same call
55-
// style).
56-
bool ShapeMatches(const FunctionDescriptor& other) const {
57-
return ShapeMatches(other.receiver_style(), other.types());
58-
}
59-
bool ShapeMatches(bool receiver_style, absl::Span<const Kind> types) const;
60-
61-
bool operator==(const FunctionDescriptor& other) const;
62-
63-
bool operator<(const FunctionDescriptor& other) const;
64-
65-
private:
66-
struct Impl final {
67-
Impl(absl::string_view name, bool receiver_style, std::vector<Kind> types,
68-
bool is_strict)
69-
: name(name),
70-
types(std::move(types)),
71-
receiver_style(receiver_style),
72-
is_strict(is_strict) {}
73-
74-
std::string name;
75-
std::vector<Kind> types;
76-
bool receiver_style;
77-
bool is_strict;
78-
};
79-
80-
std::shared_ptr<const Impl> impl_;
81-
};
82-
83-
} // namespace cel
18+
#include "common/function_descriptor.h" // IWYU pragma: export
8419

8520
#endif // THIRD_PARTY_CEL_CPP_BASE_FUNCTION_DESCRIPTOR_H_

common/BUILD

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,3 +866,19 @@ cc_test(
866866
"@com_google_protobuf//:protobuf",
867867
],
868868
)
869+
870+
cc_library(
871+
name = "function_descriptor",
872+
srcs = [
873+
"function_descriptor.cc",
874+
],
875+
hdrs = [
876+
"function_descriptor.h",
877+
],
878+
deps = [
879+
":kind",
880+
"@com_google_absl//absl/base:core_headers",
881+
"@com_google_absl//absl/strings",
882+
"@com_google_absl//absl/types:span",
883+
],
884+
)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "base/function_descriptor.h"
15+
#include "common/function_descriptor.h"
1616

1717
#include <algorithm>
1818
#include <cstddef>
1919

2020
#include "absl/base/macros.h"
2121
#include "absl/types/span.h"
22-
#include "base/kind.h"
22+
#include "common/kind.h"
2323

2424
namespace cel {
2525

common/function_descriptor.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef THIRD_PARTY_CEL_CPP_COMMON_FUNCTION_DESCRIPTOR_H_
16+
#define THIRD_PARTY_CEL_CPP_COMMON_FUNCTION_DESCRIPTOR_H_
17+
18+
#include <memory>
19+
#include <string>
20+
#include <utility>
21+
#include <vector>
22+
23+
#include "absl/strings/string_view.h"
24+
#include "absl/types/span.h"
25+
#include "common/kind.h"
26+
27+
namespace cel {
28+
29+
// Coarsely describes a function for the purpose of runtime resolution of
30+
// overloads.
31+
class FunctionDescriptor final {
32+
public:
33+
FunctionDescriptor(absl::string_view name, bool receiver_style,
34+
std::vector<Kind> types, bool is_strict = true)
35+
: impl_(std::make_shared<Impl>(name, receiver_style, std::move(types),
36+
is_strict)) {}
37+
38+
// Function name.
39+
const std::string& name() const { return impl_->name; }
40+
41+
// Whether function is receiver style i.e. true means arg0.name(args[1:]...).
42+
bool receiver_style() const { return impl_->receiver_style; }
43+
44+
// The argmument types the function accepts.
45+
//
46+
// TODO: make this kinds
47+
const std::vector<Kind>& types() const { return impl_->types; }
48+
49+
// if true (strict, default), error or unknown arguments are propagated
50+
// instead of calling the function. if false (non-strict), the function may
51+
// receive error or unknown values as arguments.
52+
bool is_strict() const { return impl_->is_strict; }
53+
54+
// Helper for matching a descriptor. This tests that the shape is the same --
55+
// |other| accepts the same number and types of arguments and is the same call
56+
// style).
57+
bool ShapeMatches(const FunctionDescriptor& other) const {
58+
return ShapeMatches(other.receiver_style(), other.types());
59+
}
60+
bool ShapeMatches(bool receiver_style, absl::Span<const Kind> types) const;
61+
62+
bool operator==(const FunctionDescriptor& other) const;
63+
64+
bool operator<(const FunctionDescriptor& other) const;
65+
66+
private:
67+
struct Impl final {
68+
Impl(absl::string_view name, bool receiver_style, std::vector<Kind> types,
69+
bool is_strict)
70+
: name(name),
71+
types(std::move(types)),
72+
receiver_style(receiver_style),
73+
is_strict(is_strict) {}
74+
75+
std::string name;
76+
std::vector<Kind> types;
77+
bool receiver_style;
78+
bool is_strict;
79+
};
80+
81+
std::shared_ptr<const Impl> impl_;
82+
};
83+
84+
} // namespace cel
85+
86+
#endif // THIRD_PARTY_CEL_CPP_COMMON_FUNCTION_DESCRIPTOR_H_

runtime/BUILD

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ cc_library(
220220
deps = [
221221
":runtime_builder",
222222
":runtime_options",
223-
":type_registry",
224223
"//internal:noop_delete",
225224
"//internal:status_macros",
226225
"//runtime/internal:runtime_env",
@@ -582,3 +581,15 @@ cc_test(
582581
"@com_google_protobuf//:protobuf",
583582
],
584583
)
584+
585+
cc_library(
586+
name = "function",
587+
hdrs = [
588+
"function.h",
589+
],
590+
deps = [
591+
"//common:value",
592+
"@com_google_absl//absl/status:statusor",
593+
"@com_google_absl//absl/types:span",
594+
],
595+
)

runtime/function.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef THIRD_PARTY_CEL_CPP_COMMON_FUNCTION_H_
16+
#define THIRD_PARTY_CEL_CPP_COMMON_FUNCTION_H_
17+
18+
#include "absl/status/statusor.h"
19+
#include "absl/types/span.h"
20+
#include "common/value.h"
21+
#include "common/value_manager.h"
22+
23+
namespace cel {
24+
25+
// Interface for extension functions.
26+
//
27+
// The host for the CEL environment may provide implementations to define custom
28+
// extension functions.
29+
//
30+
// The runtime expects functions to be deterministic and side-effect free.
31+
class Function {
32+
public:
33+
virtual ~Function() = default;
34+
35+
// InvokeContext provides access to current evaluator state.
36+
class InvokeContext final {
37+
public:
38+
explicit InvokeContext(cel::ValueManager& value_manager)
39+
: value_manager_(value_manager) {}
40+
41+
// Return the value_factory defined for the evaluation invoking the
42+
// extension function.
43+
cel::ValueManager& value_factory() const { return value_manager_; }
44+
45+
// TODO: Add accessors for getting attribute stack and mutable
46+
// value stack.
47+
private:
48+
cel::ValueManager& value_manager_;
49+
};
50+
51+
// Attempt to evaluate an extension function based on the runtime arguments
52+
// during the evaluation of a CEL expression.
53+
//
54+
// A non-ok status is interpreted as an unrecoverable error in evaluation (
55+
// e.g. data corruption). This stops evaluation and is propagated immediately.
56+
//
57+
// A cel::ErrorValue typed result is considered a recoverable error and
58+
// follows CEL's logical short-circuiting behavior.
59+
virtual absl::StatusOr<Value> Invoke(const InvokeContext& context,
60+
absl::Span<const Value> args) const = 0;
61+
};
62+
63+
// Legacy type, aliased to the actual type.
64+
using FunctionEvaluationContext = Function::InvokeContext;
65+
66+
} // namespace cel
67+
68+
#endif // THIRD_PARTY_CEL_CPP_COMMON_FUNCTION_H_

0 commit comments

Comments
 (0)