Skip to content

Commit 0a289d9

Browse files
l46kokcopybara-github
authored andcommitted
Allow configuration of MutableAst by making it instantiable
PiperOrigin-RevId: 598974666
1 parent cd11baf commit 0a289d9

File tree

11 files changed

+450
-381
lines changed

11 files changed

+450
-381
lines changed

optimizer/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ java_library(
2525

2626
java_library(
2727
name = "mutable_ast",
28-
testonly = 1,
29-
visibility = ["//optimizer/src/test/java/dev/cel/optimizer:__pkg__"],
3028
exports = ["//optimizer/src/main/java/dev/cel/optimizer:mutable_ast"],
3129
)
3230

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,19 @@ java_library(
6666
tags = [
6767
],
6868
deps = [
69-
":mutable_ast",
7069
":optimization_exception",
7170
"//bundle:cel",
7271
"//common",
73-
"//common/ast",
7472
"//common/navigation",
7573
],
7674
)
7775

7876
java_library(
7977
name = "mutable_ast",
80-
srcs = ["MutableAst.java"],
78+
srcs = [
79+
"MutableAst.java",
80+
"MutableExprVisitor.java",
81+
],
8182
tags = [
8283
],
8384
deps = [
@@ -87,6 +88,7 @@ java_library(
8788
"//common/ast",
8889
"//common/ast:expr_factory",
8990
"//common/navigation",
91+
"@maven//:com_google_errorprone_error_prone_annotations",
9092
"@maven//:com_google_guava_guava",
9193
],
9294
)

optimizer/src/main/java/dev/cel/optimizer/CelAstOptimizer.java

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import dev.cel.bundle.Cel;
1818
import dev.cel.common.CelAbstractSyntaxTree;
19-
import dev.cel.common.ast.CelExpr;
2019
import dev.cel.common.navigation.CelNavigableAst;
2120

2221
/** Public interface for performing a single, custom optimization on an AST. */
@@ -25,106 +24,4 @@ public interface CelAstOptimizer {
2524
/** Optimizes a single AST. */
2625
CelAbstractSyntaxTree optimize(CelNavigableAst navigableAst, Cel cel)
2726
throws CelOptimizationException;
28-
29-
/**
30-
* Replaces a subtree in the given expression node. This operation is intended for AST
31-
* optimization purposes.
32-
*
33-
* <p>This is a very dangerous operation. Callers should re-typecheck the mutated AST and
34-
* additionally verify that the resulting AST is semantically valid.
35-
*
36-
* <p>All expression IDs will be renumbered in a stable manner to ensure there's no ID collision
37-
* between the nodes. The renumbering occurs even if the subtree was not replaced.
38-
*
39-
* @param celExpr Original expression node to rewrite.
40-
* @param newExpr New CelExpr to replace the subtree with.
41-
* @param exprIdToReplace Expression id of the subtree that is getting replaced.
42-
*/
43-
default CelExpr replaceSubtree(CelExpr celExpr, CelExpr newExpr, long exprIdToReplace) {
44-
return MutableAst.replaceSubtree(celExpr, newExpr, exprIdToReplace);
45-
}
46-
47-
/**
48-
* Replaces a subtree in the given AST. This operation is intended for AST optimization purposes.
49-
*
50-
* <p>This is a very dangerous operation. Callers should re-typecheck the mutated AST and
51-
* additionally verify that the resulting AST is semantically valid.
52-
*
53-
* <p>All expression IDs will be renumbered in a stable manner to ensure there's no ID collision
54-
* between the nodes. The renumbering occurs even if the subtree was not replaced.
55-
*
56-
* <p>This will scrub out the description, positions and line offsets from {@code CelSource}. If
57-
* the source contains macro calls, its call IDs will be to be consistent with the renumbered IDs
58-
* in the AST.
59-
*
60-
* @param ast Original ast to mutate.
61-
* @param newExpr New CelExpr to replace the subtree with.
62-
* @param exprIdToReplace Expression id of the subtree that is getting replaced.
63-
*/
64-
default CelAbstractSyntaxTree replaceSubtree(
65-
CelAbstractSyntaxTree ast, CelExpr newExpr, long exprIdToReplace) {
66-
return MutableAst.replaceSubtree(ast, newExpr, exprIdToReplace);
67-
}
68-
69-
/**
70-
* Generates a new bind macro using the provided initialization and result expression, then
71-
* replaces the subtree using the new bind expr at the designated expr ID.
72-
*
73-
* <p>The bind call takes the format of: {@code cel.bind(varInit, varName, resultExpr)}
74-
*
75-
* @param ast Original ast to mutate.
76-
* @param varName New variable name for the bind macro call.
77-
* @param varInit Initialization expression to bind to the local variable.
78-
* @param resultExpr Result expression
79-
* @param exprIdToReplace Expression ID of the subtree that is getting replaced.
80-
*/
81-
default CelAbstractSyntaxTree replaceSubtreeWithNewBindMacro(
82-
CelAbstractSyntaxTree ast,
83-
String varName,
84-
CelExpr varInit,
85-
CelExpr resultExpr,
86-
long exprIdToReplace) {
87-
return MutableAst.replaceSubtreeWithNewBindMacro(
88-
ast, varName, varInit, resultExpr, exprIdToReplace);
89-
}
90-
91-
/**
92-
* Replaces all comprehension identifier names with a unique name based on the given prefix.
93-
*
94-
* <p>The purpose of this is to avoid errors that can be caused by shadowed variables while
95-
* augmenting an AST. As an example: {@code [2, 3].exists(x, x - 1 > 3) || x - 1 > 3}. Note that
96-
* the scoping of `x - 1` is different between th two LOGICAL_OR branches. Iteration variable `x`
97-
* in `exists` will be mangled to {@code [2, 3].exists(@c0, @c0 - 1 > 3) || x - 1 > 3} to avoid
98-
* erroneously extracting x - 1 as common subexpression.
99-
*
100-
* <p>The expression IDs are not modified when the identifier names are changed.
101-
*
102-
* <p>Iteration variables in comprehensions are numbered based on their comprehension nesting
103-
* levels. Examples:
104-
*
105-
* <ul>
106-
* <li>{@code [true].exists(i, i) && [true].exists(j, j)} -> {@code [true].exists(@c0, @c0) &&
107-
* [true].exists(@c0, @c0)} // Note that i,j gets replaced to the same @c0 in this example
108-
* <li>{@code [true].exists(i, i && [true].exists(j, j))} -> {@code [true].exists(@c0, @c0 &&
109-
* [true].exists(@c1, @c1))}
110-
* </ul>
111-
*
112-
* @param ast AST to mutate
113-
* @param newIdentPrefix Prefix to use for new identifier names. For example, providing @c will
114-
* produce @c0, @c1, @c2... as new names.
115-
*/
116-
default CelAbstractSyntaxTree mangleComprehensionIdentifierNames(
117-
CelAbstractSyntaxTree ast, String newIdentPrefix) {
118-
return MutableAst.mangleComprehensionIdentifierNames(ast, newIdentPrefix);
119-
}
120-
121-
/** Sets all expr IDs in the expression tree to 0. */
122-
default CelExpr clearExprIds(CelExpr celExpr) {
123-
return MutableAst.clearExprIds(celExpr);
124-
}
125-
126-
/** Renumbers all the expr IDs in the given AST in a consecutive manner starting from 1. */
127-
default CelAbstractSyntaxTree renumberIdsConsecutively(CelAbstractSyntaxTree ast) {
128-
return MutableAst.renumberIdsConsecutively(ast);
129-
}
13027
}

0 commit comments

Comments
 (0)