Skip to content

Commit 904fdf4

Browse files
committed
Use mutable expressions for AST rewrites during type-check
1 parent 83e91cc commit 904fdf4

6 files changed

Lines changed: 197 additions & 240 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,14 @@ java_library(
179179
"//common:cel_ast",
180180
"//common:compiler_common",
181181
"//common:container",
182+
"//common:mutable_ast",
182183
"//common:operator",
183184
"//common:options",
184185
"//common:proto_ast",
185186
"//common/annotations",
186187
"//common/ast",
187188
"//common/ast:expr_converter",
189+
"//common/ast:mutable_expr",
188190
"//common/internal:errors",
189191
"//common/internal:file_descriptor_converter",
190192
"//common/types",

checker/src/main/java/dev/cel/checker/Env.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import dev.cel.common.ast.CelConstant;
3939
import dev.cel.common.ast.CelExpr;
4040
import dev.cel.common.ast.CelExprConverter;
41+
import dev.cel.common.ast.CelMutableExpr;
4142
import dev.cel.common.ast.CelReference;
4243
import dev.cel.common.internal.Errors;
4344
import dev.cel.common.types.CelKind;
@@ -304,6 +305,14 @@ public CelType getType(CelExpr expr) {
304305
return Preconditions.checkNotNull(typeMap.get(expr.id()), "expression has no type");
305306
}
306307

308+
/**
309+
* Returns the type associated with a mutable expression by expression id. It's an error to call this
310+
* method if the type is not present.
311+
*/
312+
CelType getType(CelMutableExpr expr) {
313+
return Preconditions.checkNotNull(typeMap.get(expr.id()), "expression has no type");
314+
}
315+
307316
/**
308317
* Sets the type associated with an expression by id. It's an error if the type is already set and
309318
* is different than the provided one. Returns the expression parameter.
@@ -319,6 +328,21 @@ public CelExpr setType(CelExpr expr, CelType type) {
319328
return expr;
320329
}
321330

331+
/**
332+
* Sets the type associated with a mutable expression by id. It's an error if the type is already set and
333+
* is different than the provided one. Returns the expression parameter.
334+
*/
335+
@CanIgnoreReturnValue
336+
CelMutableExpr setType(CelMutableExpr expr, CelType type) {
337+
CelType oldType = typeMap.put(expr.id(), type);
338+
Preconditions.checkState(
339+
oldType == null || oldType.equals(type),
340+
"expression already has a type which is incompatible.\n old: %s\n new: %s",
341+
oldType,
342+
type);
343+
return expr;
344+
}
345+
322346
/**
323347
* Sets the reference associated with an expression. It's an error if the reference is already set
324348
* and is different.
@@ -330,6 +354,17 @@ public void setRef(CelExpr expr, CelReference reference) {
330354
"expression already has a reference which is incompatible");
331355
}
332356

357+
/**
358+
* Sets the reference associated with a mutable expression. It's an error if the reference is already set
359+
* and is different.
360+
*/
361+
void setRef(CelMutableExpr expr, CelReference reference) {
362+
CelReference oldReference = referenceMap.put(expr.id(), reference);
363+
Preconditions.checkState(
364+
oldReference == null || oldReference.equals(reference),
365+
"expression already has a reference which is incompatible");
366+
}
367+
333368
/**
334369
* Adds a declaration to the environment, based on the Decl proto. Will report errors if the
335370
* declaration overlaps with an existing one, or clashes with a macro.

0 commit comments

Comments
 (0)