Skip to content
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

Change AST Mutator to accept mutable ASTs #305

Merged
merged 1 commit into from
Apr 18, 2024
Merged
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
4 changes: 4 additions & 0 deletions common/src/main/java/dev/cel/common/CelSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ public Builder clearMacroCall(long exprId) {
return this;
}

public ImmutableSet<Extension> getExtensions() {
return extensions.build();
}

/**
* Adds one or more {@link Extension}s to the source information. Extensions implement set
* semantics and deduped if same ones are provided.
Expand Down
1 change: 1 addition & 0 deletions common/src/main/java/dev/cel/common/navigation/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ java_library(
":common",
"//:auto_value",
"//common/ast:mutable_ast",
"//common/types:type_providers",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:org_jspecify_jspecify",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package dev.cel.common.navigation;

import dev.cel.common.ast.CelMutableAst;
import dev.cel.common.types.CelType;
import java.util.Optional;

/**
* Decorates a {@link CelMutableAst} with navigational properties. This allows us to visit a node's
Expand Down Expand Up @@ -44,4 +46,15 @@ public CelNavigableMutableExpr getRoot() {
public CelMutableAst getAst() {
return ast;
}

/**
* Returns the type of the expression node for a type-checked AST. This simply proxies down the
* call to {@link CelMutableAst#getType(long)}.
*
* @return Optional of {@link CelType} or {@link Optional#empty} if the type does not exist at the
* ID.
*/
public Optional<CelType> getType(long exprId) {
return ast.getType(exprId);
}
}
843 changes: 476 additions & 367 deletions optimizer/src/main/java/dev/cel/optimizer/AstMutator.java

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions optimizer/src/main/java/dev/cel/optimizer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ java_library(
"//common/ast:mutable_ast",
"//common/navigation",
"//common/navigation:common",
"//common/navigation:mutable_navigation",
"//common/types:type_providers",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,7 @@ private OptimizationResult optimizeUsingCelBlock(CelNavigableAst navigableAst, C
iterVarType ->
newVarDecls.add(
CelVarDecl.newVarDeclaration(name.iterVarName(), iterVarType)));
type.resultType()
.ifPresent(
comprehensionResultType ->
newVarDecls.add(
CelVarDecl.newVarDeclaration(
name.resultName(), comprehensionResultType)));
newVarDecls.add(CelVarDecl.newVarDeclaration(name.resultName(), type.resultType()));
});

// Type-check all sub-expressions then create new block index identifiers.
Expand Down
478 changes: 257 additions & 221 deletions optimizer/src/test/java/dev/cel/optimizer/AstMutatorTest.java

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions optimizer/src/test/java/dev/cel/optimizer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ java_library(
"//common:compiler_common",
"//common:options",
"//common/ast",
"//common/ast:mutable_ast",
"//common/navigation",
"//common/navigation:mutable_navigation",
"//common/resources/testdata/proto3:test_all_types_java_proto",
"//common/types",
"//compiler",
Expand Down
Loading