1616
1717import dev .cel .bundle .Cel ;
1818import dev .cel .common .CelAbstractSyntaxTree ;
19- import dev .cel .common .ast .CelExpr ;
2019import 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