2626import com .microsoft .z3 .SeqExpr ;
2727import dev .cel .common .CelAbstractSyntaxTree ;
2828import dev .cel .common .ast .CelExpr ;
29+ import dev .cel .common .ast .CelExpr .ExprKind ;
2930import dev .cel .common .internal .ProtoTimeUtils ;
3031import dev .cel .common .types .CelKind ;
3132import dev .cel .common .types .CelType ;
@@ -536,25 +537,49 @@ private FieldAccess getMsgAccess(Expr<?> operand, String field, BoolExpr typeGua
536537 return new FieldAccess (presence , value );
537538 }
538539
540+ /**
541+ * Translates {@code e.f}, {@code has(e.f)} or {@code e.?f} (an optional of {@code e.f} if {@code
542+ * has(e.f)}, else empty).
543+ */
539544 TranslatedValue translateSelect (CelExpr celExpr , CelAbstractSyntaxTree ast ) {
540- CelExpr .CelSelect select = celExpr .select ();
545+ boolean isOptional = celExpr .getKind () == ExprKind .Kind .CALL ;
546+ CelExpr operandExpr = isOptional ? celExpr .call ().args ().get (0 ) : celExpr .select ().operand ();
547+ String field =
548+ isOptional
549+ ? celExpr .call ().args ().get (1 ).constant ().stringValue ()
550+ : celExpr .select ().field ();
551+ boolean testOnly = !isOptional && celExpr .select ().testOnly ();
541552 long exprId = celExpr .id ();
542- TranslatedValue operandTv = translateExpr (select . operand () , ast );
553+ TranslatedValue operandTv = translateExpr (operandExpr , ast );
543554 Expr <?> operand = operandTv .z3Expr ();
544- String field = select .field ();
545- CelType operandType = extractAstTypeOrDefault (ast , select .operand ().id ());
555+ CelType operandType = extractAstTypeOrDefault (ast , operandExpr .id ());
556+ // Selecting from an optional selects from its value optionally (e.g. `optional.of({}).a` is
557+ // `optional.none()`). Performance only: an operand of a known type other than optional isn't.
558+ boolean mayBeOptional =
559+ operandType instanceof OptionalType
560+ || !CelZ3OperatorTranslator .isStaticallyKnown (operandType );
561+ BoolExpr isOptionalOperand = ctx .mkFalse ();
562+ BoolExpr hasValue = ctx .mkFalse ();
563+ BoolExpr shouldEvaluate = ctx .mkTrue ();
564+ if (mayBeOptional ) {
565+ Expr <?> optRef = typeSystem .getOptionalRef (operand );
566+ isOptionalOperand = typeSystem .isOptional (operand );
567+ hasValue = typeSystem .optHasValue (optRef );
568+ shouldEvaluate = (BoolExpr ) ctx .mkITE (isOptionalOperand , hasValue , ctx .mkTrue ());
569+ operand = ctx .mkITE (isOptionalOperand , typeSystem .getOptionalValue (optRef ), operand );
570+ }
546571
547572 Expr <?> presenceResult ;
548573 Expr <?> valueResult ;
549574 BoolExpr unmodeledMessage = ctx .mkFalse ();
550575 BoolExpr presenceError = ctx .mkFalse ();
551576
552577 if (operandType instanceof MapType ) {
553- FieldAccess mapAcc = getMapAccess (operand , field , ctx . mkTrue () );
578+ FieldAccess mapAcc = getMapAccess (operand , field , shouldEvaluate );
554579 presenceResult = mapAcc .presence ;
555580 valueResult = ctx .mkITE (mapAcc .presence , mapAcc .value , typeSystem .mkError ());
556581 } else if (operandType .kind () == CelKind .STRUCT ) {
557- FieldAccess msgAcc = getMsgAccess (operand , field , ctx . mkTrue () );
582+ FieldAccess msgAcc = getMsgAccess (operand , field , shouldEvaluate );
558583 presenceResult = msgAcc .presence ;
559584 Expr <?> defaultVal = getDefaultValueForType (extractAstTypeOrDefault (ast , exprId ));
560585 valueResult = ctx .mkITE (msgAcc .presence , msgAcc .value , defaultVal );
@@ -563,8 +588,8 @@ TranslatedValue translateSelect(CelExpr celExpr, CelAbstractSyntaxTree ast) {
563588 BoolExpr isMap = typeSystem .isMap (operand );
564589 BoolExpr isMessage = typeSystem .isMessage (operand );
565590
566- FieldAccess mapAcc = getMapAccess (operand , field , isMap );
567- FieldAccess msgAcc = getMsgAccess (operand , field , isMessage );
591+ FieldAccess mapAcc = getMapAccess (operand , field , ctx . mkAnd ( shouldEvaluate , isMap ) );
592+ FieldAccess msgAcc = getMsgAccess (operand , field , ctx . mkAnd ( shouldEvaluate , isMessage ) );
568593
569594 presenceResult =
570595 CelZ3Exprs .SwitchBuilder .newBuilder (ctx )
@@ -590,13 +615,13 @@ TranslatedValue translateSelect(CelExpr celExpr, CelAbstractSyntaxTree ast) {
590615 msgRead .addCase (isType , typeSystem .mkError ());
591616 continue ;
592617 }
593- if (select . testOnly () ) {
618+ if (testOnly ) {
594619 continue ;
595620 }
596621 CelType fieldType = declared .get ().type ();
597622 context .addConstraint (
598623 ctx .mkImplies (
599- ctx .mkAnd (isMessage , isType , msgAcc .presence ),
624+ ctx .mkAnd (shouldEvaluate , isMessage , isType , msgAcc .presence ),
600625 typeConstraintGenerator .createTypeConstraintForType (msgAcc .value , fieldType )));
601626 msgRead .addCase (
602627 isType , ctx .mkITE (msgAcc .presence , msgAcc .value , getDefaultValueForType (fieldType )));
@@ -605,7 +630,10 @@ TranslatedValue translateSelect(CelExpr celExpr, CelAbstractSyntaxTree ast) {
605630 unmodeledMessage =
606631 context .recordApproximation (
607632 "messages of types the type provider doesn't declare" ,
608- ctx .mkAnd (isMessage , ctx .mkNot (CelZ3Exprs .mkOrFlattened (ctx , isModeledType ))));
633+ ctx .mkAnd (
634+ shouldEvaluate ,
635+ isMessage ,
636+ ctx .mkNot (CelZ3Exprs .mkOrFlattened (ctx , isModeledType ))));
609637 Expr <?> mapRead = ctx .mkITE (mapAcc .presence , mapAcc .value , typeSystem .mkError ());
610638 // Testing an undeclared field, or a field of something other than a map or message, errors.
611639 presenceError =
@@ -622,10 +650,27 @@ TranslatedValue translateSelect(CelExpr celExpr, CelAbstractSyntaxTree ast) {
622650 .build (typeSystem .mkError ());
623651 }
624652
653+ // has(e.f), else e.f, or for e.?f and an optional e, an optional of e.f if has(e.f).
625654 Expr <?> fieldAccess =
626- select . testOnly ()
655+ testOnly
627656 ? typeSystem .withRuntimeError (typeSystem .wrapBool (presenceResult ), presenceError )
628657 : valueResult ;
658+ Expr <?> chained = fieldAccess ;
659+ if (!testOnly && (isOptional || mayBeOptional )) {
660+ chained =
661+ ctx .mkITE (
662+ presenceError ,
663+ typeSystem .mkError (),
664+ ctx .mkITE (
665+ (BoolExpr ) presenceResult ,
666+ context .mkOptionalOf (valueResult ),
667+ typeSystem .mkOptionalNone ()));
668+ fieldAccess = isOptional ? chained : valueResult ;
669+ }
670+ if (mayBeOptional ) {
671+ Expr <?> empty = testOnly ? typeSystem .mkBool (false ) : typeSystem .mkOptionalNone ();
672+ fieldAccess = ctx .mkITE (isOptionalOperand , ctx .mkITE (hasValue , chained , empty ), fieldAccess );
673+ }
629674
630675 context .addConstraint (typeConstraintGenerator .createTypeConstraint (fieldAccess , exprId , ast ));
631676
@@ -636,7 +681,8 @@ TranslatedValue translateSelect(CelExpr celExpr, CelAbstractSyntaxTree ast) {
636681 return operandType .kind () == CelKind .STRUCT
637682 ? result
638683 : result .withApproximation (
639- CelZ3Exprs .mkOrFlattened (ctx , context .isTruncatedMap (operand ), unmodeledMessage ));
684+ CelZ3Exprs .mkOrFlattened (
685+ ctx , ctx .mkAnd (shouldEvaluate , context .isTruncatedMap (operand )), unmodeledMessage ));
640686 }
641687
642688 private static CelType extractAstTypeOrDefault (CelAbstractSyntaxTree ast , long id ) {
0 commit comments