Skip to content

Commit d9f1ea9

Browse files
l46kokcopybara-github
authored andcommitted
Add CelStandardDeclaration to allow environment subsetting for type-checker
PiperOrigin-RevId: 672004289
1 parent 10bb524 commit d9f1ea9

File tree

15 files changed

+2173
-853
lines changed

15 files changed

+2173
-853
lines changed

checker/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,8 @@ java_library(
5151
name = "proto_expr_visitor",
5252
exports = ["//checker/src/main/java/dev/cel/checker:proto_expr_visitor"],
5353
)
54+
55+
java_library(
56+
name = "standard_decl",
57+
exports = ["//checker/src/main/java/dev/cel/checker:standard_decl"],
58+
)

checker/src/main/java/dev/cel/checker/BUILD.bazel

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ CHECKER_LEGACY_ENV_SOURCES = [
2828
"ExprChecker.java",
2929
"ExprVisitor.java",
3030
"InferenceContext.java",
31-
"Standard.java",
3231
"TypeFormatter.java",
3332
"TypeProvider.java",
3433
"Types.java",
@@ -68,6 +67,7 @@ java_library(
6867
":checker_builder",
6968
":checker_legacy_environment",
7069
":proto_type_mask",
70+
":standard_decl",
7171
":type_provider_legacy_impl",
7272
"//:auto_value",
7373
"//common",
@@ -97,6 +97,7 @@ java_library(
9797
deps = [
9898
":checker_legacy_environment",
9999
":proto_type_mask",
100+
":standard_decl",
100101
"//common",
101102
"//common:compiler_common",
102103
"//common:options",
@@ -166,6 +167,7 @@ java_library(
166167
],
167168
deps = [
168169
":cel_ident_decl",
170+
":standard_decl",
169171
"//:auto_value",
170172
"//common",
171173
"//common:compiler_common",
@@ -217,3 +219,22 @@ java_library(
217219
"//common:proto_ast",
218220
],
219221
)
222+
223+
java_library(
224+
name = "standard_decl",
225+
srcs = [
226+
"CelStandardDeclaration.java",
227+
],
228+
tags = [
229+
],
230+
deps = [
231+
":cel_ident_decl",
232+
"//common:compiler_common",
233+
"//common/types",
234+
"//common/types:cel_types",
235+
"//common/types:type_providers",
236+
"//parser:operator",
237+
"@maven//:com_google_errorprone_error_prone_annotations",
238+
"@maven//:com_google_guava_guava",
239+
],
240+
)

checker/src/main/java/dev/cel/checker/CelCheckerBuilder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ public interface CelCheckerBuilder {
152152
@CanIgnoreReturnValue
153153
CelCheckerBuilder setStandardEnvironmentEnabled(boolean value);
154154

155+
/**
156+
* Override the standard declarations for the type-checker. This can be used to subset the
157+
* standard environment to only expose the desired declarations to the type-checker. {@link
158+
* #setStandardEnvironmentEnabled(boolean)} must be set to false for this to take effect.
159+
*/
160+
@CanIgnoreReturnValue
161+
CelCheckerBuilder setStandardDeclarations(CelStandardDeclaration standardDeclarations);
162+
155163
/** Adds one or more libraries for parsing and type-checking. */
156164
@CanIgnoreReturnValue
157165
CelCheckerBuilder addLibraries(CelCheckerLibrary... libraries);

checker/src/main/java/dev/cel/checker/CelCheckerLegacyImpl.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public final class CelCheckerLegacyImpl implements CelChecker, EnvVisitable {
7878
private final CelTypeProvider celTypeProvider;
7979
private final boolean standardEnvironmentEnabled;
8080

81+
private final CelStandardDeclaration overriddenStandardDeclarations;
82+
8183
// Builder is mutable by design. APIs must make defensive copies in and out of this class.
8284
@SuppressWarnings("Immutable")
8385
private final Builder checkerBuilder;
@@ -137,6 +139,8 @@ private Env getEnv(Errors errors) {
137139
Env env;
138140
if (standardEnvironmentEnabled) {
139141
env = Env.standard(errors, typeProvider, celOptions);
142+
} else if (overriddenStandardDeclarations != null) {
143+
env = Env.standard(overriddenStandardDeclarations, errors, typeProvider, celOptions);
140144
} else {
141145
env = Env.unconfigured(errors, typeProvider, celOptions);
142146
}
@@ -165,6 +169,7 @@ public static final class Builder implements CelCheckerBuilder {
165169
private TypeProvider customTypeProvider;
166170
private CelTypeProvider celTypeProvider;
167171
private boolean standardEnvironmentEnabled;
172+
private CelStandardDeclaration standardDeclarations;
168173

169174
@Override
170175
public CelCheckerBuilder setOptions(CelOptions celOptions) {
@@ -320,7 +325,13 @@ public CelCheckerBuilder addFileTypes(FileDescriptorSet fileDescriptorSet) {
320325

321326
@Override
322327
public CelCheckerBuilder setStandardEnvironmentEnabled(boolean value) {
323-
standardEnvironmentEnabled = value;
328+
this.standardEnvironmentEnabled = value;
329+
return this;
330+
}
331+
332+
@Override
333+
public CelCheckerBuilder setStandardDeclarations(CelStandardDeclaration standardDeclarations) {
334+
this.standardDeclarations = checkNotNull(standardDeclarations);
324335
return this;
325336
}
326337

@@ -372,6 +383,11 @@ ImmutableSet.Builder<CelCheckerLibrary> getCheckerLibraries() {
372383
@Override
373384
@CheckReturnValue
374385
public CelCheckerLegacyImpl build() {
386+
if (standardEnvironmentEnabled && standardDeclarations != null) {
387+
throw new IllegalArgumentException(
388+
"setStandardEnvironmentEnabled must be set to false to override standard"
389+
+ " declarations.");
390+
}
375391
// Add libraries, such as extensions
376392
celCheckerLibraries.build().forEach(celLibrary -> celLibrary.setCheckerOptions(this));
377393

@@ -430,6 +446,7 @@ public CelCheckerLegacyImpl build() {
430446
legacyProvider,
431447
messageTypeProvider,
432448
standardEnvironmentEnabled,
449+
standardDeclarations,
433450
this);
434451
}
435452

@@ -478,6 +495,7 @@ private CelCheckerLegacyImpl(
478495
TypeProvider typeProvider,
479496
CelTypeProvider celTypeProvider,
480497
boolean standardEnvironmentEnabled,
498+
CelStandardDeclaration overriddenStandardDeclarations,
481499
Builder checkerBuilder) {
482500
this.celOptions = celOptions;
483501
this.container = container;
@@ -487,6 +505,7 @@ private CelCheckerLegacyImpl(
487505
this.typeProvider = typeProvider;
488506
this.celTypeProvider = celTypeProvider;
489507
this.standardEnvironmentEnabled = standardEnvironmentEnabled;
508+
this.overriddenStandardDeclarations = overriddenStandardDeclarations;
490509
this.checkerBuilder = new Builder(checkerBuilder);
491510
}
492511

0 commit comments

Comments
 (0)