Skip to content
Open
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
1 change: 1 addition & 0 deletions bundle/src/main/java/dev/cel/bundle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ java_library(
"//common:compiler_common",
"//common:container",
"//common:options",
"//common/internal:cel_descriptor_pools",
"//common/internal:env_visitor",
"//common/internal:file_descriptor_converter",
"//common/types:cel_proto_types",
Expand Down
12 changes: 11 additions & 1 deletion bundle/src/main/java/dev/cel/bundle/CelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import dev.cel.common.CelSource;
import dev.cel.common.CelValidationResult;
import dev.cel.common.CelVarDecl;
import dev.cel.common.internal.CelDescriptorPool;
import dev.cel.common.internal.EnvVisitable;
import dev.cel.common.internal.EnvVisitor;
import dev.cel.common.internal.FileDescriptorSetConverter;
Expand All @@ -58,6 +59,7 @@
import dev.cel.runtime.CelRuntimeLibrary;
import dev.cel.runtime.CelStandardFunctions;
import java.util.Arrays;
import java.util.Optional;
import java.util.function.Function;

/**
Expand All @@ -66,7 +68,7 @@
* <p>Note, the underlying {@link CelCompiler} and {@link CelRuntime} values are constructed lazily.
*/
@Immutable
final class CelImpl implements Cel, EnvVisitable {
final class CelImpl implements Cel, EnvVisitable, CelDescriptorPool.Provider {

// The lazily constructed compiler and runtime values are memoized and guaranteed to be
// constructed only once without side effects, thus making them effectively immutable.
Expand Down Expand Up @@ -109,6 +111,14 @@ public void accept(EnvVisitor envVisitor) {
}
}

@Override
public Optional<CelDescriptorPool> getDescriptorPool() {
CelCompiler celCompiler = compiler.get();
return celCompiler instanceof CelDescriptorPool.Provider
? ((CelDescriptorPool.Provider) celCompiler).getDescriptorPool()
: Optional.empty();
}

@Override
public CelBuilder toCelBuilder() {
return newBuilder(toCompilerBuilder(), toRuntimeBuilder());
Expand Down
1 change: 1 addition & 0 deletions checker/src/main/java/dev/cel/checker/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ java_library(
"//common:options",
"//common:source_location",
"//common/annotations",
"//common/internal:cel_descriptor_pools",
"//common/internal:env_visitor",
"//common/internal:errors",
"//common/types",
Expand Down
32 changes: 30 additions & 2 deletions checker/src/main/java/dev/cel/checker/CelCheckerLegacyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import dev.cel.common.CelValidationResult;
import dev.cel.common.CelVarDecl;
import dev.cel.common.annotations.Internal;
import dev.cel.common.internal.CelDescriptorPool;
import dev.cel.common.internal.CombinedDescriptorPool;
import dev.cel.common.internal.EnvVisitable;
import dev.cel.common.internal.EnvVisitor;
import dev.cel.common.internal.Errors;
Expand All @@ -65,7 +67,8 @@
*/
@Immutable
@Internal
public final class CelCheckerLegacyImpl implements CelChecker, EnvVisitable {
public final class CelCheckerLegacyImpl
implements CelChecker, EnvVisitable, CelDescriptorPool.Provider {

private final CelOptions celOptions;
private final CelContainer container;
Expand All @@ -87,6 +90,7 @@ public final class CelCheckerLegacyImpl implements CelChecker, EnvVisitable {

private final ImmutableSet<FileDescriptor> fileDescriptors;
private final ImmutableSet<ProtoTypeMask> protoTypeMasks;
private final @Nullable CelDescriptorPool descriptorPool;

@Override
public CelValidationResult check(CelAbstractSyntaxTree ast) {
Expand All @@ -110,6 +114,11 @@ public CelTypeProvider getTypeProvider() {
return this.celTypeProvider;
}

@Override
public java.util.Optional<CelDescriptorPool> getDescriptorPool() {
return java.util.Optional.ofNullable(descriptorPool);
}

@Override
public CelCheckerBuilder toCheckerBuilder() {
CelCheckerBuilder builder =
Expand Down Expand Up @@ -179,6 +188,25 @@ private Env getEnv(Errors errors) {
return env;
}

static @Nullable CelDescriptorPool resolveDescriptorPool(CelTypeProvider provider) {
ImmutableList.Builder<CelDescriptorPool> pools = ImmutableList.builder();
collectDescriptorPools(provider, pools);
ImmutableList<CelDescriptorPool> list = pools.build();
return list.isEmpty() ? null : CombinedDescriptorPool.create(list);
}

private static void collectDescriptorPools(
CelTypeProvider provider, ImmutableList.Builder<CelDescriptorPool> pools) {
if (provider instanceof CelDescriptorPool.Provider) {
((CelDescriptorPool.Provider) provider).getDescriptorPool().ifPresent(pools::add);
} else if (provider instanceof CelTypeProvider.CombinedCelTypeProvider) {
for (CelTypeProvider child :
((CelTypeProvider.CombinedCelTypeProvider) provider).typeProviders()) {
collectDescriptorPools(child, pools);
}
}
}

/** Create a new builder to construct a {@code CelChecker} instance. */
public static CelCheckerBuilder newBuilder() {
return new Builder();
Expand Down Expand Up @@ -439,7 +467,6 @@ public CelCheckerLegacyImpl build() {
.setAllowJsonFieldNames(celOptions.enableJsonFieldNames())
.addFileDescriptors(fileTypeSet)
.build();

if (celTypeProvider != null && fileTypeSet.isEmpty()) {
messageTypeProvider = celTypeProvider;
} else if (celTypeProvider != null) {
Expand Down Expand Up @@ -522,6 +549,7 @@ private CelCheckerLegacyImpl(
this.checkerLibraries = checkerLibraries;
this.fileDescriptors = fileDescriptors;
this.protoTypeMasks = protoTypeMasks;
this.descriptorPool = resolveDescriptorPool(celTypeProvider);
}

private static ImmutableList<CelIssue> errorsToIssues(Errors errors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.CelVarDecl;
import dev.cel.common.annotations.Internal;
import dev.cel.common.internal.CelDescriptorPool;
import dev.cel.common.types.CelKind;
import dev.cel.common.types.CelType;
import dev.cel.common.types.CelTypeProvider;
Expand All @@ -32,25 +34,35 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.jspecify.annotations.Nullable;

/**
* The {@code ProtoTypeMaskTypeProvider} binds a set of {@code ProtoTypeMask} instances to type
* definitions and ensures that only fields which have been explicitly listed by the set of {@code
* ProtoTypeMask} values is exposed within the CEL type system.
*/
@Immutable
public final class ProtoTypeMaskTypeProvider implements CelTypeProvider {
public final class ProtoTypeMaskTypeProvider
implements CelTypeProvider, CelDescriptorPool.Provider {

// The set of visible fields by type is not exposed after it is configured.
@SuppressWarnings("Immutable")
private final ImmutableMap<String, CelType> allTypes;

private final ImmutableSet<ProtoTypeMask> protoTypeMasks;
private final @Nullable CelDescriptorPool descriptorPool;

ProtoTypeMaskTypeProvider(
CelTypeProvider delegateProvider, ImmutableSet<ProtoTypeMask> protoTypeMasks) {
this.protoTypeMasks = protoTypeMasks;
this.allTypes = computeVisibleFieldsMap(delegateProvider, protoTypeMasks);
this.descriptorPool = CelCheckerLegacyImpl.resolveDescriptorPool(delegateProvider);
}

@Internal
@Override
public Optional<CelDescriptorPool> getDescriptorPool() {
return Optional.ofNullable(descriptorPool);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ Optional<FieldDescriptor> findExtensionDescriptor(
* Any messages containing Proto2 extension messages.
*/
ExtensionRegistry getExtensionRegistry();

/** Provides a {@link CelDescriptorPool} for internal CEL components. */
interface Provider {
Optional<CelDescriptorPool> getDescriptorPool();
}
}
4 changes: 4 additions & 0 deletions common/src/main/java/dev/cel/common/types/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ java_library(
tags = [
],
deps = [
"//common/annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
Expand All @@ -177,6 +178,8 @@ java_library(
"//:auto_value",
"//common:cel_descriptor_util",
"//common:cel_descriptors",
"//common/annotations",
"//common/internal:cel_descriptor_pools",
"//common/internal:file_descriptor_converter",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
Expand Down Expand Up @@ -233,6 +236,7 @@ cel_android_library(
tags = [
],
deps = [
"//common/annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven_android//:com_google_guava_guava",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.CheckReturnValue;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.annotations.Internal;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -51,6 +52,11 @@ final class CombinedCelTypeProvider implements CelTypeProvider {
private final ImmutableList<CelTypeProvider> typeProviders;
private final ImmutableMap<String, CelType> allTypes;

@Internal
public ImmutableList<CelTypeProvider> typeProviders() {
return typeProviders;
}

@Override
public ImmutableCollection<CelType> types() {
return allTypes.values();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
import com.google.protobuf.Descriptors.FileDescriptor;
import dev.cel.common.CelDescriptorUtil;
import dev.cel.common.CelDescriptors;
import dev.cel.common.annotations.Internal;
import dev.cel.common.internal.CelDescriptorPool;
import dev.cel.common.internal.DefaultDescriptorPool;
import dev.cel.common.internal.FileDescriptorSetConverter;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -46,7 +49,7 @@
*/
@CheckReturnValue
@Immutable
public final class ProtoMessageTypeProvider implements CelTypeProvider {
public final class ProtoMessageTypeProvider implements CelTypeProvider, CelDescriptorPool.Provider {

private static final ImmutableMap<FieldDescriptor.Type, CelType> PROTO_TYPE_TO_CEL_TYPE =
ImmutableMap.<FieldDescriptor.Type, CelType>builder()
Expand All @@ -69,6 +72,7 @@ public final class ProtoMessageTypeProvider implements CelTypeProvider {

private final ImmutableMap<String, CelType> allTypes;
private final boolean allowJsonFieldNames;
private final CelDescriptorPool descriptorPool;

/** Returns a new builder for {@link ProtoMessageTypeProvider}. */
public static Builder newBuilder() {
Expand Down Expand Up @@ -115,6 +119,7 @@ public ProtoMessageTypeProvider(ImmutableSet<FileDescriptor> fileDescriptors) {

private ProtoMessageTypeProvider(CelDescriptors celDescriptors, boolean allowJsonFieldNames) {
this.allowJsonFieldNames = allowJsonFieldNames;
this.descriptorPool = DefaultDescriptorPool.create(celDescriptors);
this.allTypes =
ImmutableMap.<String, CelType>builder()
.putAll(createEnumTypes(celDescriptors.enumDescriptors()))
Expand All @@ -124,6 +129,12 @@ private ProtoMessageTypeProvider(CelDescriptors celDescriptors, boolean allowJso
.buildOrThrow();
}

@Internal
@Override
public Optional<CelDescriptorPool> getDescriptorPool() {
return Optional.of(descriptorPool);
}

@Override
public ImmutableCollection<CelType> types() {
return allTypes.values();
Expand Down
1 change: 1 addition & 0 deletions compiler/src/main/java/dev/cel/compiler/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ java_library(
"//common:container",
"//common:options",
"//common/annotations",
"//common/internal:cel_descriptor_pools",
"//common/internal:env_visitor",
"//common/types:cel_proto_types",
"//common/types:type_providers",
Expand Down
12 changes: 11 additions & 1 deletion compiler/src/main/java/dev/cel/compiler/CelCompilerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import dev.cel.common.CelValidationResult;
import dev.cel.common.CelVarDecl;
import dev.cel.common.annotations.Internal;
import dev.cel.common.internal.CelDescriptorPool;
import dev.cel.common.internal.EnvVisitable;
import dev.cel.common.internal.EnvVisitor;
import dev.cel.common.types.CelProtoTypes;
Expand All @@ -46,6 +47,7 @@
import dev.cel.parser.CelParserBuilder;
import dev.cel.parser.CelStandardMacro;
import java.util.Arrays;
import java.util.Optional;

/**
* CelCompiler implementation which uses either the legacy or modernized CEL-Java stack to offer a
Expand All @@ -56,7 +58,8 @@
*/
@Immutable
@Internal
public final class CelCompilerImpl implements CelCompiler, EnvVisitable {
public final class CelCompilerImpl
implements CelCompiler, EnvVisitable, CelDescriptorPool.Provider {

private final CelParser parser;
private final CelChecker checker;
Expand All @@ -81,6 +84,13 @@ public CelTypeProvider getTypeProvider() {
return checker.getTypeProvider();
}

@Override
public Optional<CelDescriptorPool> getDescriptorPool() {
return checker instanceof CelDescriptorPool.Provider
? ((CelDescriptorPool.Provider) checker).getDescriptorPool()
: Optional.empty();
}

@Override
public void accept(EnvVisitor envVisitor) {
if (checker instanceof EnvVisitable) {
Expand Down
10 changes: 10 additions & 0 deletions verifier/src/main/java/dev/cel/verifier/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,20 @@ java_library(
"CelVerifierZ3Impl.java",
"CelZ3ComprehensionTranslator.java",
"CelZ3CounterexampleGenerator.java",
"CelZ3Equality.java",
"CelZ3ExtensionalityAxioms.java",
"CelZ3FunctionRegistry.java",
"CelZ3OperatorTranslator.java",
"CelZ3StructTranslator.java",
"CelZ3TranslationContext.java",
"CelZ3TypeConstraintGenerator.java",
"ListValueModel.java",
"MapValueModel.java",
"NumericValueModel.java",
"OptionalValueModel.java",
"RuntimeOracle.java",
"TranslatedValue.java",
"ValueModel.java",
],
compatible_with = [],
tags = [
Expand All @@ -189,10 +195,13 @@ java_library(
"//common/ast",
"//common/ast:cel_block",
"//common/exceptions:runtime_exception",
"//common/internal:cel_descriptor_pools",
"//common/internal:map_keys",
"//common/internal:proto_time_utils",
"//common/internal:well_known_proto",
"//common/types",
"//common/types:cel_types",
"//common/types:default_type_provider",
"//common/types:type_providers",
"//common/values",
"//common/values:cel_byte_string",
Expand All @@ -206,6 +215,7 @@ java_library(
"//verifier/axioms",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java",
"@maven//:org_jspecify_jspecify",
"@maven//:tools_aqua_z3_turnkey",
],
Expand Down
Loading
Loading