3838import dev .cel .common .ast .CelConstant ;
3939import dev .cel .common .ast .CelExpr ;
4040import dev .cel .common .ast .CelExprConverter ;
41+ import dev .cel .common .ast .CelMutableExpr ;
4142import dev .cel .common .ast .CelReference ;
4243import dev .cel .common .internal .Errors ;
4344import 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