Update type checker decls for extension packages. by copybara-service[bot] · Pull Request #1540 · cel-expr/cel-cpp · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions extensions/BUILD
11 changes: 9 additions & 2 deletions extensions/math_ext_decls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,32 @@ absl::Status AddMinMaxDecls(TypeCheckerBuilder& builder) {
max_decl.set_name("math.@max");

for (const Type& type : kNumerics) {
// Unary overloads
CEL_RETURN_IF_ERROR(min_decl.AddOverload(MakeOverloadDecl(
absl::StrCat(kMinOverloadPrefix, OverloadTypeName(type)), type, type)));

CEL_RETURN_IF_ERROR(max_decl.AddOverload(MakeOverloadDecl(
absl::StrCat(kMaxOverloadPrefix, OverloadTypeName(type)), type, type)));

// Pairwise overloads
for (const Type& other_type : kNumerics) {
Type out_type = DynType();
if (type.kind() == other_type.kind()) {
out_type = type;
}
CEL_RETURN_IF_ERROR(min_decl.AddOverload(MakeOverloadDecl(
absl::StrCat(kMinOverloadPrefix, OverloadTypeName(type), "_",
OverloadTypeName(other_type)),
DynType(), type, other_type)));
out_type, type, other_type)));

CEL_RETURN_IF_ERROR(max_decl.AddOverload(MakeOverloadDecl(
absl::StrCat(kMaxOverloadPrefix, OverloadTypeName(type), "_",
OverloadTypeName(other_type)),
DynType(), type, other_type)));
out_type, type, other_type)));
}
}

// List overloads
for (const Type& type : kListNumerics) {
CEL_RETURN_IF_ERROR(min_decl.AddOverload(MakeOverloadDecl(
absl::StrCat(kMinOverloadPrefix, OverloadTypeName(type)),
Expand Down
17 changes: 16 additions & 1 deletion extensions/proto_ext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
#include <vector>

#include "absl/functional/overload.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/types/optional.h"
#include "absl/types/span.h"
#include "absl/types/variant.h"
#include "common/ast.h"
#include "common/expr.h"
#include "compiler/compiler.h"
#include "internal/status_macros.h"
#include "parser/macro.h"
#include "parser/macro_expr_factory.h"
#include "parser/parser_interface.h"

namespace cel::extensions {

Expand Down Expand Up @@ -76,6 +80,13 @@ bool IsExtensionCall(const Expr& target) {
return false;
}

absl::Status ConfigureParser(ParserBuilder& builder) {
for (const auto& macro : proto_macros()) {
CEL_RETURN_IF_ERROR(builder.AddMacro(macro));
}
return absl::OkStatus();
}

} // namespace

std::vector<Macro> proto_macros() {
Expand Down Expand Up @@ -110,4 +121,8 @@ std::vector<Macro> proto_macros() {
return {*hasExt, *getExt};
}

CompilerLibrary ProtoExtCompilerLibrary() {
return CompilerLibrary("cel.lib.ext.proto", ConfigureParser);
}

} // namespace cel::extensions
4 changes: 4 additions & 0 deletions extensions/proto_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <vector>

#include "absl/status/status.h"
#include "compiler/compiler.h"
#include "parser/macro.h"
#include "parser/macro_registry.h"
#include "parser/options.h"
Expand All @@ -28,6 +29,9 @@ namespace cel::extensions {
// objects in CEL. Specifically, the proto.getExt() and proto.hasExt() macros.
std::vector<Macro> proto_macros();

// Library for the proto extensions.
CompilerLibrary ProtoExtCompilerLibrary();

inline absl::Status RegisterProtoMacros(MacroRegistry& registry,
const ParserOptions&) {
return registry.RegisterMacros(proto_macros());
Expand Down
29 changes: 29 additions & 0 deletions extensions/sets_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "base/function_adapter.h"
#include "checker/type_checker_builder.h"
#include "common/decl.h"
#include "common/type.h"
#include "common/value.h"
#include "internal/status_macros.h"
#include "runtime/function_registry.h"
Expand Down Expand Up @@ -118,8 +121,34 @@ absl::Status RegisterSetsEquivalentFunction(FunctionRegistry& registry) {
const ListValue&>::WrapFunction(SetsEquivalent));
}

absl::Status RegisterSetsDecls(TypeCheckerBuilder& b) {
ListType list_t(b.arena(), TypeParamType("T"));
CEL_ASSIGN_OR_RETURN(
auto decl,
MakeFunctionDecl("sets.contains",
MakeOverloadDecl("list_sets_contains_list", BoolType(),
list_t, list_t)));
CEL_RETURN_IF_ERROR(b.AddFunction(decl));

CEL_ASSIGN_OR_RETURN(
decl, MakeFunctionDecl("sets.equivalent",
MakeOverloadDecl("list_sets_equivalent_list",
BoolType(), list_t, list_t)));
CEL_RETURN_IF_ERROR(b.AddFunction(decl));

CEL_ASSIGN_OR_RETURN(
decl, MakeFunctionDecl("sets.intersects",
MakeOverloadDecl("list_sets_intersects_list",
BoolType(), list_t, list_t)));
return b.AddFunction(decl);
}

} // namespace

CheckerLibrary SetsCheckerLibrary() {
return {.id = "cel.lib.ext.sets", .configure = RegisterSetsDecls};
}

absl::Status RegisterSetsFunctions(FunctionRegistry& registry,
const RuntimeOptions& options) {
CEL_RETURN_IF_ERROR(RegisterSetsContainsFunction(registry));
Expand Down
9 changes: 9 additions & 0 deletions extensions/sets_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@
#define THIRD_PARTY_CEL_CPP_EXTENSIONS_SETS_FUNCTIONS_H_

#include "absl/status/status.h"
#include "checker/type_checker_builder.h"
#include "compiler/compiler.h"
#include "runtime/function_registry.h"
#include "runtime/runtime_options.h"

namespace cel::extensions {

// Declarations for the sets functions.
CheckerLibrary SetsCheckerLibrary();

inline CompilerLibrary SetsCompilerLibrary() {
return CompilerLibrary::FromCheckerLibrary(SetsCheckerLibrary());
}

// Register set functions.
absl::Status RegisterSetsFunctions(FunctionRegistry& registry,
const RuntimeOptions& options);
Expand Down
35 changes: 21 additions & 14 deletions extensions/sets_functions_test.cc