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
52 changes: 42 additions & 10 deletions checker/src/main/java/dev/cel/checker/CelCheckerLegacyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.CheckReturnValue;
Expand Down Expand Up @@ -88,6 +89,15 @@ public final class CelCheckerLegacyImpl implements CelChecker, EnvVisitable {
private final ImmutableSet<FileDescriptor> fileDescriptors;
private final ImmutableSet<ProtoTypeMask> protoTypeMasks;

// Standard declarations in effect, null when the standard environment is disabled.
private final @Nullable CelStandardDeclarations standardDeclarations;

// Identifier and function declarations added on top of the standard declarations, as found in
// the top-most scope of the environment. Null when adding them reported errors, in which case
// they are added on every check so that the errors are reported.
private final @Nullable ImmutableMap<String, CelVarDecl> identDeclarationsByName;
private final @Nullable ImmutableMap<String, CelFunctionDecl> functionDeclarationsByName;

@Override
public CelValidationResult check(CelAbstractSyntaxTree ast) {
CelSource source = ast.getSource();
Expand Down Expand Up @@ -164,16 +174,25 @@ public void accept(EnvVisitor envVisitor) {
}

private Env getEnv(Errors errors) {
Env env;
if (overriddenStandardDeclarations != null) {
env =
Env.standard(
overriddenStandardDeclarations, errors, celTypeProvider, typeProvider, celOptions);
} else if (standardEnvironmentEnabled) {
env = Env.standard(errors, celTypeProvider, typeProvider, celOptions);
} else {
env = Env.unconfigured(errors, celTypeProvider, typeProvider, celOptions);
}
if (identDeclarationsByName != null && functionDeclarationsByName != null) {
return Env.withDeclarations(
standardDeclarations,
identDeclarationsByName,
functionDeclarationsByName,
errors,
celTypeProvider,
typeProvider,
celOptions);
}
return newEnv(errors);
}

/** Creates an {@code Env} adding the identifier and function declarations to it. */
private Env newEnv(Errors errors) {
Env env =
standardDeclarations != null
? Env.standard(standardDeclarations, errors, celTypeProvider, typeProvider, celOptions)
: Env.unconfigured(errors, celTypeProvider, typeProvider, celOptions);
identDeclarations.forEach(env::add);
functionDeclarations.forEach(env::add);
return env;
Expand Down Expand Up @@ -522,6 +541,19 @@ private CelCheckerLegacyImpl(
this.checkerLibraries = checkerLibraries;
this.fileDescriptors = fileDescriptors;
this.protoTypeMasks = protoTypeMasks;
if (overriddenStandardDeclarations != null) {
this.standardDeclarations = overriddenStandardDeclarations;
} else if (standardEnvironmentEnabled) {
this.standardDeclarations = Env.newStandardDeclarations(celOptions);
} else {
this.standardDeclarations = null;
}

Errors errors = new Errors("", "");
Env.DeclGroup declGroup = newEnv(errors).getDeclGroup();
boolean valid = errors.getErrorCount() == 0;
this.identDeclarationsByName = valid ? declGroup.getIdents() : null;
this.functionDeclarationsByName = valid ? declGroup.getFunctions() : null;
}

private static ImmutableList<CelIssue> errorsToIssues(Errors errors) {
Expand Down
61 changes: 45 additions & 16 deletions checker/src/main/java/dev/cel/checker/CelStandardDeclarations.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.util.Arrays.stream;

import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Immutable;
Expand Down Expand Up @@ -51,10 +53,12 @@ public final class CelStandardDeclarations {

/** An empty instance of {@link CelStandardDeclarations} with no functions or identifiers. */
public static final CelStandardDeclarations EMPTY =
new CelStandardDeclarations(ImmutableSet.of(), ImmutableSet.of());
new CelStandardDeclarations(ImmutableMap.of(), ImmutableMap.of());

private final ImmutableSet<CelFunctionDecl> celFunctionDecls;
private final ImmutableSet<CelVarDecl> celIdentDecls;
// Declarations keyed by name, as added to the type-checker environment. Declarations are built
// from the standard functions and identifiers, so their names are unique.
private final ImmutableMap<String, CelFunctionDecl> celFunctionDecls;
private final ImmutableMap<String, CelVarDecl> celIdentDecls;

/** Enumeration of Standard Functions. */
public enum StandardFunction implements CelFunctionDecl.Declarer {
Expand Down Expand Up @@ -1686,17 +1690,19 @@ public CelStandardDeclarations build() {
"You may only populate one of the following builder methods: includeIdentifiers,"
+ " excludeIdentifiers or filterIdentifiers");

ImmutableSet.Builder<CelFunctionDecl> functionDeclBuilder = ImmutableSet.builder();
ImmutableMap.Builder<String, CelFunctionDecl> functionDeclBuilder = ImmutableMap.builder();
for (StandardFunction standardFunction : StandardFunction.values()) {
if (hasIncludeFunctions) {
if (this.includeFunctions.contains(standardFunction)) {
functionDeclBuilder.add(standardFunction.celFunctionDecl);
functionDeclBuilder.put(
standardFunction.functionName, standardFunction.celFunctionDecl);
}
continue;
}
if (hasExcludeFunctions) {
if (!this.excludeFunctions.contains(standardFunction)) {
functionDeclBuilder.add(standardFunction.celFunctionDecl);
functionDeclBuilder.put(
standardFunction.functionName, standardFunction.celFunctionDecl);
}
continue;
}
Expand All @@ -1711,39 +1717,40 @@ public CelStandardDeclarations build() {

ImmutableSet<StandardOverload> overloads = overloadBuilder.build();
if (!overloads.isEmpty()) {
functionDeclBuilder.add(standardFunction.withOverloads(overloadBuilder.build()));
functionDeclBuilder.put(
standardFunction.functionName, standardFunction.withOverloads(overloads));
}
continue;
}

functionDeclBuilder.add(standardFunction.celFunctionDecl);
functionDeclBuilder.put(standardFunction.functionName, standardFunction.celFunctionDecl);
}

ImmutableSet.Builder<CelVarDecl> identBuilder = ImmutableSet.builder();
ImmutableMap.Builder<String, CelVarDecl> identBuilder = ImmutableMap.builder();
for (StandardIdentifier standardIdentifier : StandardIdentifier.values()) {
if (hasIncludeIdentifiers) {
if (this.includeIdentifiers.contains(standardIdentifier)) {
identBuilder.add(standardIdentifier.identDecl);
identBuilder.put(standardIdentifier.identDecl.name(), standardIdentifier.identDecl);
}
continue;
}

if (hasExcludeIdentifiers) {
if (!this.excludeIdentifiers.contains(standardIdentifier)) {
identBuilder.add(standardIdentifier.identDecl);
identBuilder.put(standardIdentifier.identDecl.name(), standardIdentifier.identDecl);
}
continue;
}

if (hasIdentifierFilter) {
boolean includeIdent = identifierFilter.include(standardIdentifier);
if (includeIdent) {
identBuilder.add(standardIdentifier.identDecl);
identBuilder.put(standardIdentifier.identDecl.name(), standardIdentifier.identDecl);
}
continue;
}

identBuilder.add(standardIdentifier.identDecl);
identBuilder.put(standardIdentifier.identDecl.name(), standardIdentifier.identDecl);
}

return new CelStandardDeclarations(functionDeclBuilder.build(), identBuilder.build());
Expand Down Expand Up @@ -1779,16 +1786,38 @@ public static Builder newBuilder() {
return new Builder();
}

ImmutableSet<CelFunctionDecl> functionDecls() {
/** Returns the declarations of all the standard functions and identifiers. */
static CelStandardDeclarations all() {
return AllHolder.ALL;
}

/**
* Holds {@link #all()}, built on first use rather than when this class is initialized, since the
* standard functions and identifiers it is built from depend on this class being initialized.
*/
private static final class AllHolder {
private static final CelStandardDeclarations ALL = newBuilder().build();
}

ImmutableCollection<CelFunctionDecl> functionDecls() {
return celFunctionDecls.values();
}

ImmutableCollection<CelVarDecl> identifierDecls() {
return celIdentDecls.values();
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we're primarily working with Map decls, let's remove the extraneous ImmutableSets altogether. We can build the ImmutableMap directly in the Builder.build() (so just make celFunctionDecls and celIdentDecls immutable maps)


ImmutableMap<String, CelFunctionDecl> functionDeclsByName() {
return celFunctionDecls;
}

ImmutableSet<CelVarDecl> identifierDecls() {
ImmutableMap<String, CelVarDecl> identifierDeclsByName() {
return celIdentDecls;
}

private CelStandardDeclarations(
ImmutableSet<CelFunctionDecl> celFunctionDecls, ImmutableSet<CelVarDecl> celIdentDecls) {
ImmutableMap<String, CelFunctionDecl> celFunctionDecls,
ImmutableMap<String, CelVarDecl> celIdentDecls) {
this.celFunctionDecls = celFunctionDecls;
this.celIdentDecls = celIdentDecls;
}
Expand Down
71 changes: 61 additions & 10 deletions checker/src/main/java/dev/cel/checker/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,45 @@ static Env standard(
CelTypeProvider celTypeProvider,
@Nullable TypeProvider legacyTypeProvider,
CelOptions celOptions) {
Env env = Env.unconfigured(errors, celTypeProvider, legacyTypeProvider, celOptions);
// Isolate the standard declarations into their own scope for forward compatibility.
celStandardDeclaration.functionDecls().forEach(env::add);
celStandardDeclaration.identifierDecls().forEach(env::add);
return withDeclarations(
celStandardDeclaration,
ImmutableMap.of(),
ImmutableMap.of(),
errors,
celTypeProvider,
legacyTypeProvider,
celOptions);
}

env.enterScope();
/**
* Creates an {@code Env} value with the standard declarations, when provided, and the given
* declarations, as previously added to an {@code Env} created the same way and read back from its
* top-most scope.
*/
static Env withDeclarations(
@Nullable CelStandardDeclarations celStandardDeclarations,
ImmutableMap<String, CelVarDecl> idents,
ImmutableMap<String, CelFunctionDecl> functions,
Errors errors,
CelTypeProvider celTypeProvider,
@Nullable TypeProvider legacyTypeProvider,
CelOptions celOptions) {
DeclGroup declGroup = new DeclGroup(idents, functions, /* isComprehensionScope= */ false);
if (celStandardDeclarations == null) {
return new Env(errors, celTypeProvider, legacyTypeProvider, declGroup, celOptions);
}
Env env =

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do the setup in reverse -- Make withDeclarations responsible for standing up an environment (so move what's present in standard method) and then proxy the withDeclarations call inside it.

new Env(
errors,
celTypeProvider,
legacyTypeProvider,
new DeclGroup(
celStandardDeclarations.identifierDeclsByName(),
celStandardDeclarations.functionDeclsByName(),
/* isComprehensionScope= */ false),
celOptions);
// Isolate the standard declarations into their own scope for forward compatibility.
env.decls.add(declGroup);
return env;
}

Expand All @@ -222,7 +255,11 @@ public static Env standard(
return standard(celStandardDeclaration, errors, EMPTY_TYPE_PROVIDER, typeProvider, celOptions);
}

private static CelStandardDeclarations newStandardDeclarations(CelOptions celOptions) {
static CelStandardDeclarations newStandardDeclarations(CelOptions celOptions) {
// Nothing is filtered out with these options.
if (celOptions.enableUnsignedLongs() && celOptions.enableTimestampEpoch()) {
return CelStandardDeclarations.all();
}
return CelStandardDeclarations.newBuilder()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a short-circuit here to avoid filtering through all declarations (Skips enumerating 100+ idents/functions):

if (celOptions.enableUnsignedLongs() && celOptions.enableTimestampEpoch()) {
    return CelStandardDeclarations.ALL; // Prebuild this as a constant in advance
}

.filterFunctions(
(function, overload) -> {
Expand Down Expand Up @@ -968,8 +1005,9 @@ public Decl build() {
*/
public static class DeclGroup {

private final Map<String, CelVarDecl> idents;
private final Map<String, CelFunctionDecl> functions;
// Copied into mutable maps on first write, so immutable maps can be shared between groups.
private Map<String, CelVarDecl> idents;
private Map<String, CelFunctionDecl> functions;
private final boolean isComprehensionScope;

/**
Expand All @@ -991,6 +1029,9 @@ public ImmutableMap<String, CelFunctionDecl> getFunctions() {

/** Put an identifier declaration into the {@code DeclGroup}. */
public void putIdent(CelVarDecl ident) {
if (idents instanceof ImmutableMap) {
idents = new HashMap<>(idents);
}
idents.put(ident.name(), ident);
}

Expand All @@ -1001,12 +1042,22 @@ public void putIdent(CelVarDecl ident) {

/** Put a function declaration into the {@code DeclGroup}. */
public void putFunction(CelFunctionDecl function) {
if (functions instanceof ImmutableMap) {
functions = new HashMap<>(functions);
}
functions.put(function.name(), function);
}

private DeclGroup(boolean isComprehensionScope) {
this.idents = new HashMap<>();
this.functions = new HashMap<>();
this(ImmutableMap.of(), ImmutableMap.of(), isComprehensionScope);
}

private DeclGroup(
Map<String, CelVarDecl> idents,
Map<String, CelFunctionDecl> functions,
boolean isComprehensionScope) {
this.idents = idents;
this.functions = functions;
this.isComprehensionScope = isComprehensionScope;
}
}
Expand Down
Loading
Loading