Repository navigation
Share the standard declarations scope across type checks #1512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
35be08c
74bae63
566dbe8
7f74c60
9ace93d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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 { | ||
|
|
@@ -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; | ||
| } | ||
|
|
@@ -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()); | ||
|
|
@@ -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(); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| 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; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 = | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would do the setup in reverse -- Make |
||
| 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; | ||
| } | ||
|
|
||
|
|
@@ -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() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) -> { | ||
|
|
@@ -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; | ||
|
|
||
| /** | ||
|
|
@@ -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); | ||
| } | ||
|
|
||
|
|
@@ -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; | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
buildOrThrownow that this is an immutable map (our linter will flag this otherwise)