Skip to content

Commit b870465

Browse files
l46kokcopybara-github
authored andcommitted
Add aliasing support to type-checker
PiperOrigin-RevId: 786794897
1 parent e450fd0 commit b870465

23 files changed

Lines changed: 521 additions & 171 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ java_library(
2828
"//common:cel_ast",
2929
"//common:cel_source",
3030
"//common:compiler_common",
31+
"//common:container",
3132
"//common:options",
3233
"//common/internal:env_visitor",
3334
"//common/internal:file_descriptor_converter",

bundle/src/main/java/dev/cel/bundle/CelBuilder.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.protobuf.Message;
2525
import dev.cel.checker.ProtoTypeMask;
2626
import dev.cel.checker.TypeProvider;
27+
import dev.cel.common.CelContainer;
2728
import dev.cel.common.CelFunctionDecl;
2829
import dev.cel.common.CelOptions;
2930
import dev.cel.common.CelVarDecl;
@@ -81,12 +82,19 @@ public interface CelBuilder {
8182
CelBuilder addMacros(Iterable<CelMacro> macros);
8283

8384
/**
84-
* Set the {@code container} name to use as the namespace for resolving CEL expression variables
85-
* and functions.
85+
* @deprecated Use {@link #setContainer(CelContainer)} instead.
8686
*/
8787
@CanIgnoreReturnValue
88+
@Deprecated
8889
CelBuilder setContainer(String container);
8990

91+
/**
92+
* Set the {@link CelContainer} to use as the namespace for resolving CEL expression variables and
93+
* functions.
94+
*/
95+
@CanIgnoreReturnValue
96+
CelBuilder setContainer(CelContainer container);
97+
9098
/** Add a variable declaration with a given {@code name} and {@link Type}. */
9199
@CanIgnoreReturnValue
92100
CelBuilder addVar(String name, Type type);

bundle/src/main/java/dev/cel/bundle/CelImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import dev.cel.checker.ProtoTypeMask;
3232
import dev.cel.checker.TypeProvider;
3333
import dev.cel.common.CelAbstractSyntaxTree;
34+
import dev.cel.common.CelContainer;
3435
import dev.cel.common.CelFunctionDecl;
3536
import dev.cel.common.CelOptions;
3637
import dev.cel.common.CelSource;
@@ -194,6 +195,12 @@ public CelBuilder setContainer(String container) {
194195
return this;
195196
}
196197

198+
@Override
199+
public CelBuilder setContainer(CelContainer container) {
200+
compilerBuilder.setContainer(container);
201+
return this;
202+
}
203+
197204
@Override
198205
public CelBuilder addVar(String name, Type type) {
199206
compilerBuilder.addVar(name, type);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ java_library(
7676
"//common:cel_descriptors",
7777
"//common:cel_source",
7878
"//common:compiler_common",
79+
"//common:container",
7980
"//common:options",
8081
"//common:source_location",
8182
"//common/annotations",
@@ -104,6 +105,7 @@ java_library(
104105
":standard_decl",
105106
"//common:cel_ast",
106107
"//common:compiler_common",
108+
"//common:container",
107109
"//common:options",
108110
"//common/types:type_providers",
109111
"@cel_spec//proto/cel/expr:checked_java_proto",
@@ -175,6 +177,7 @@ java_library(
175177
"//:auto_value",
176178
"//common:cel_ast",
177179
"//common:compiler_common",
180+
"//common:container",
178181
"//common:options",
179182
"//common:proto_ast",
180183
"//common/annotations",

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.protobuf.DescriptorProtos.FileDescriptorSet;
2222
import com.google.protobuf.Descriptors.Descriptor;
2323
import com.google.protobuf.Descriptors.FileDescriptor;
24+
import dev.cel.common.CelContainer;
2425
import dev.cel.common.CelFunctionDecl;
2526
import dev.cel.common.CelOptions;
2627
import dev.cel.common.CelVarDecl;
@@ -35,12 +36,19 @@ public interface CelCheckerBuilder {
3536
CelCheckerBuilder setOptions(CelOptions options);
3637

3738
/**
38-
* Set the {@code container} name to use as the namespace for resolving CEL expression variables
39-
* and functions.
39+
* @deprecated Use {@link #setContainer(CelContainer)} instead.
4040
*/
4141
@CanIgnoreReturnValue
42+
@Deprecated
4243
CelCheckerBuilder setContainer(String container);
4344

45+
/**
46+
* Set the {@link CelContainer} to use as the namespace for resolving CEL expression variables and
47+
* functions.
48+
*/
49+
@CanIgnoreReturnValue
50+
CelCheckerBuilder setContainer(CelContainer container);
51+
4452
/** Add variable and function {@code declarations} to the CEL environment. */
4553
@CanIgnoreReturnValue
4654
CelCheckerBuilder addDeclarations(Decl... declarations);

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

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.google.protobuf.Descriptors.Descriptor;
3232
import com.google.protobuf.Descriptors.FileDescriptor;
3333
import dev.cel.common.CelAbstractSyntaxTree;
34+
import dev.cel.common.CelContainer;
3435
import dev.cel.common.CelDescriptorUtil;
3536
import dev.cel.common.CelFunctionDecl;
3637
import dev.cel.common.CelIssue;
@@ -67,7 +68,7 @@
6768
public final class CelCheckerLegacyImpl implements CelChecker, EnvVisitable {
6869

6970
private final CelOptions celOptions;
70-
private final String container;
71+
private final CelContainer container;
7172
private final ImmutableSet<CelIdentDecl> identDeclarations;
7273
private final ImmutableSet<CelFunctionDecl> functionDeclarations;
7374
private final Optional<CelType> expectedResultType;
@@ -80,9 +81,12 @@ public final class CelCheckerLegacyImpl implements CelChecker, EnvVisitable {
8081

8182
private final CelStandardDeclarations overriddenStandardDeclarations;
8283

83-
// Builder is mutable by design. APIs must make defensive copies in and out of this class.
84+
// This does not affect the type-checking behavior in any manner.
8485
@SuppressWarnings("Immutable")
85-
private final Builder checkerBuilder;
86+
private final ImmutableSet<CelCheckerLibrary> checkerLibraries;
87+
88+
private final ImmutableSet<FileDescriptor> fileDescriptors;
89+
private final ImmutableSet<ProtoTypeMask> protoTypeMasks;
8690

8791
@Override
8892
public CelValidationResult check(CelAbstractSyntaxTree ast) {
@@ -108,7 +112,23 @@ public CelTypeProvider getTypeProvider() {
108112

109113
@Override
110114
public CelCheckerBuilder toCheckerBuilder() {
111-
return new Builder(checkerBuilder);
115+
CelCheckerBuilder builder =
116+
new Builder()
117+
.addIdentDeclarations(identDeclarations)
118+
.setOptions(celOptions)
119+
.setTypeProvider(celTypeProvider)
120+
.setContainer(container)
121+
.setStandardEnvironmentEnabled(standardEnvironmentEnabled)
122+
.addFunctionDeclarations(functionDeclarations)
123+
.addLibraries(checkerLibraries)
124+
.addFileTypes(fileDescriptors)
125+
.addProtoTypeMasks(protoTypeMasks);
126+
127+
if (expectedResultType.isPresent()) {
128+
builder.setResultType(expectedResultType.get());
129+
}
130+
131+
return builder;
112132
}
113133

114134
@Override
@@ -163,8 +183,8 @@ public static final class Builder implements CelCheckerBuilder {
163183
private final ImmutableSet.Builder<Descriptor> messageTypes;
164184
private final ImmutableSet.Builder<FileDescriptor> fileTypes;
165185
private final ImmutableSet.Builder<CelCheckerLibrary> celCheckerLibraries;
186+
private CelContainer container;
166187
private CelOptions celOptions;
167-
private String container;
168188
private CelType expectedResultType;
169189
private TypeProvider customTypeProvider;
170190
private CelTypeProvider celTypeProvider;
@@ -179,6 +199,12 @@ public CelCheckerBuilder setOptions(CelOptions celOptions) {
179199

180200
@Override
181201
public CelCheckerBuilder setContainer(String container) {
202+
checkNotNull(container);
203+
return setContainer(CelContainer.ofName(container));
204+
}
205+
206+
@Override
207+
public CelCheckerBuilder setContainer(CelContainer container) {
182208
checkNotNull(container);
183209
this.container = container;
184210
return this;
@@ -348,6 +374,12 @@ public CelCheckerBuilder addLibraries(Iterable<? extends CelCheckerLibrary> libr
348374
return this;
349375
}
350376

377+
@CanIgnoreReturnValue
378+
Builder addIdentDeclarations(ImmutableSet<CelIdentDecl> identDeclarations) {
379+
this.identDeclarations.addAll(identDeclarations);
380+
return this;
381+
}
382+
351383
// The following getters exist for asserting immutability for collections held by this builder,
352384
// and shouldn't be exposed to the public.
353385
@VisibleForTesting
@@ -388,8 +420,10 @@ public CelCheckerLegacyImpl build() {
388420
"setStandardEnvironmentEnabled must be set to false to override standard"
389421
+ " declarations.");
390422
}
423+
391424
// Add libraries, such as extensions
392-
celCheckerLibraries.build().forEach(celLibrary -> celLibrary.setCheckerOptions(this));
425+
ImmutableSet<CelCheckerLibrary> checkerLibraries = celCheckerLibraries.build();
426+
checkerLibraries.forEach(celLibrary -> celLibrary.setCheckerOptions(this));
393427

394428
// Configure the type provider.
395429
ImmutableSet<FileDescriptor> fileTypeSet = fileTypes.build();
@@ -447,7 +481,9 @@ public CelCheckerLegacyImpl build() {
447481
messageTypeProvider,
448482
standardEnvironmentEnabled,
449483
standardDeclarations,
450-
this);
484+
checkerLibraries,
485+
fileTypeSet,
486+
protoTypeMaskSet);
451487
}
452488

453489
private Builder() {
@@ -458,45 +494,23 @@ private Builder() {
458494
this.messageTypes = ImmutableSet.builder();
459495
this.protoTypeMasks = ImmutableSet.builder();
460496
this.celCheckerLibraries = ImmutableSet.builder();
461-
this.container = "";
462-
}
463-
464-
private Builder(Builder builder) {
465-
// The following properties are either immutable or simple primitives, thus can be assigned
466-
// directly.
467-
this.celOptions = builder.celOptions;
468-
this.celTypeProvider = builder.celTypeProvider;
469-
this.container = builder.container;
470-
this.customTypeProvider = builder.customTypeProvider;
471-
this.expectedResultType = builder.expectedResultType;
472-
this.standardEnvironmentEnabled = builder.standardEnvironmentEnabled;
473-
// The following needs to be deep copied as they are collection builders
474-
this.functionDeclarations = deepCopy(builder.functionDeclarations);
475-
this.identDeclarations = deepCopy(builder.identDeclarations);
476-
this.fileTypes = deepCopy(builder.fileTypes);
477-
this.messageTypes = deepCopy(builder.messageTypes);
478-
this.protoTypeMasks = deepCopy(builder.protoTypeMasks);
479-
this.celCheckerLibraries = deepCopy(builder.celCheckerLibraries);
480-
}
481-
482-
private static <T> ImmutableSet.Builder<T> deepCopy(ImmutableSet.Builder<T> builderToCopy) {
483-
ImmutableSet.Builder<T> newBuilder = ImmutableSet.builder();
484-
newBuilder.addAll(builderToCopy.build());
485-
return newBuilder;
497+
this.container = CelContainer.ofName("");
486498
}
487499
}
488500

489501
private CelCheckerLegacyImpl(
490502
CelOptions celOptions,
491-
String container,
503+
CelContainer container,
492504
ImmutableSet<CelIdentDecl> identDeclarations,
493505
ImmutableSet<CelFunctionDecl> functionDeclarations,
494506
Optional<CelType> expectedResultType,
495507
TypeProvider typeProvider,
496508
CelTypeProvider celTypeProvider,
497509
boolean standardEnvironmentEnabled,
498510
CelStandardDeclarations overriddenStandardDeclarations,
499-
Builder checkerBuilder) {
511+
ImmutableSet<CelCheckerLibrary> checkerLibraries,
512+
ImmutableSet<FileDescriptor> fileDescriptors,
513+
ImmutableSet<ProtoTypeMask> protoTypeMasks) {
500514
this.celOptions = celOptions;
501515
this.container = container;
502516
this.identDeclarations = identDeclarations;
@@ -506,7 +520,9 @@ private CelCheckerLegacyImpl(
506520
this.celTypeProvider = celTypeProvider;
507521
this.standardEnvironmentEnabled = standardEnvironmentEnabled;
508522
this.overriddenStandardDeclarations = overriddenStandardDeclarations;
509-
this.checkerBuilder = new Builder(checkerBuilder);
523+
this.checkerLibraries = checkerLibraries;
524+
this.fileDescriptors = fileDescriptors;
525+
this.protoTypeMasks = protoTypeMasks;
510526
}
511527

512528
private static ImmutableList<CelIssue> errorsToIssues(Errors errors) {

0 commit comments

Comments
 (0)