Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -55,7 +55,7 @@ public final class CelStandardDeclarations {
private final ImmutableSet<CelIdentDecl> celIdentDecls;

/** Enumeration of Standard Functions. */
public enum StandardFunction {
public enum StandardFunction implements CelFunctionDecl.Declarer {
// Deprecated - use {@link #IN}
OLD_IN(
true,
Expand DownExpand Up@@ -1504,6 +1504,7 @@ private CelFunctionDecl withOverloads(Iterable<StandardOverload> overloads) {
return newCelFunctionDecl(functionName, ImmutableSet.copyOf(overloads));
}

@Override
public CelFunctionDecl functionDecl() {
return celFunctionDecl;
}
Expand DownExpand Up@@ -1579,8 +1580,14 @@ public CelIdentDecl identDecl() {

/** General interface for defining a standard function overload. */
@Immutable
public interface StandardOverload {
public interface StandardOverload extends CelFunctionDecl.Declarer {
CelOverloadDecl celOverloadDecl();

@Override
default CelFunctionDecl functionDecl() {
// TODO: Remove default keyword by implementing this for all standard overloads
throw new UnsupportedOperationException("Unimplemented");
}
}

/** Set of all standard function names. */
Expand Down
6 changes: 6 additions & 0 deletions common/src/main/java/dev/cel/common/CelFunctionDecl.java
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,6 +38,12 @@ public abstract class CelFunctionDecl {
/** Required. List of function overloads. Must contain at least one overload. */
public abstract ImmutableSet<CelOverloadDecl> overloads();

/** General interface for defining an extension function overload or standard declaration. */
@Immutable
public interface Declarer {
CelFunctionDecl functionDecl();
}

/** Builder for configuring the {@link CelFunctionDecl}. */
@AutoValue.Builder
public abstract static class Builder {
Expand Down
4 changes: 3 additions & 1 deletion extensions/src/main/java/dev/cel/extensions/BUILD.bazel
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,7 +13,9 @@ package(

java_library(
name = "extension_library",
srcs = ["CelExtensionLibrary.java"],
srcs = [
"CelExtensionLibrary.java",
],
tags = [
],
deps = [
Expand Down
207 changes: 130 additions & 77 deletions extensions/src/main/java/dev/cel/extensions/CelOptionalLibrary.java
Original file line numberDiff line numberDiff line change
Expand Up@@ -97,95 +97,148 @@ public String getFunction() {
}
}

private static final class Types {
private static final TypeParamType PARAM_TYPE_K = TypeParamType.create("K");
private static final TypeParamType PARAM_TYPE_V = TypeParamType.create("V");
private static final OptionalType OPTIONAL_TYPE_V = OptionalType.create(PARAM_TYPE_V);
private static final ListType LIST_TYPE_V = ListType.create(PARAM_TYPE_V);
private static final MapType MAP_TYPE_KV = MapType.create(PARAM_TYPE_K, PARAM_TYPE_V);
}

/** Declarations for the optional extension library. */
public enum OptionalDeclaration implements CelFunctionDecl.Declarer {
OPTIONAL_OF(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_OF.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_of", Types.OPTIONAL_TYPE_V, Types.PARAM_TYPE_V))),
OPTIONAL_OF_NON_ZERO_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_OF_NON_ZERO_VALUE.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_ofNonZeroValue", Types.OPTIONAL_TYPE_V, Types.PARAM_TYPE_V))),
OPTIONAL_NONE(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_NONE.getFunction(),
CelOverloadDecl.newGlobalOverload("optional_none", Types.OPTIONAL_TYPE_V))),
OPTIONAL_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_value", Types.PARAM_TYPE_V, Types.OPTIONAL_TYPE_V))),
OPTIONAL_HAS_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.HAS_VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_hasValue", SimpleType.BOOL, Types.OPTIONAL_TYPE_V))),
OPTIONAL_UNWRAP(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_UNWRAP.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_unwrap_list",
Types.LIST_TYPE_V,
ListType.create(Types.OPTIONAL_TYPE_V)))),
OPTIONAL_OR(
CelFunctionDecl.newFunctionDeclaration(
"or",
CelOverloadDecl.newMemberOverload(
"optional_or_optional",
Types.OPTIONAL_TYPE_V,
Types.OPTIONAL_TYPE_V,
Types.OPTIONAL_TYPE_V))),
OPTIONAL_OR_VALUE(
CelFunctionDecl.newFunctionDeclaration(
"orValue",
CelOverloadDecl.newMemberOverload(
"optional_orValue_value",
Types.PARAM_TYPE_V,
Types.OPTIONAL_TYPE_V,
Types.PARAM_TYPE_V))),
OPTIONAL_SELECT(
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_SELECT.getFunction(),
CelOverloadDecl.newGlobalOverload(
"select_optional_field",
Types.OPTIONAL_TYPE_V,
SimpleType.DYN,
SimpleType.STRING))),
OPTIONAL_INDEX(
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"list_optindex_optional_int",
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V,
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_list_optindex_optional_int",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.LIST_TYPE_V),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"map_optindex_optional_value",
Types.OPTIONAL_TYPE_V,
Types.MAP_TYPE_KV,
Types.PARAM_TYPE_K),
CelOverloadDecl.newGlobalOverload(
"optional_map_optindex_optional_value",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.MAP_TYPE_KV),
Types.PARAM_TYPE_K))),
OPTIONAL_INDEX_OPERAND(
CelFunctionDecl.newFunctionDeclaration(
Operator.INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_list_index_int",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.LIST_TYPE_V),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_map_index_value",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.MAP_TYPE_KV),
Types.PARAM_TYPE_K)));

private final CelFunctionDecl celFunctionDecl;

OptionalDeclaration(CelFunctionDecl celFunctionDecl) {
this.celFunctionDecl = celFunctionDecl;
}

@Override
public CelFunctionDecl functionDecl() {
return celFunctionDecl;
}
}

private static final CelExtensionLibrary<CelOptionalLibrary> LIBRARY =
new CelExtensionLibrary<CelOptionalLibrary>() {
final TypeParamType paramTypeK = TypeParamType.create("K");
final TypeParamType paramTypeV = TypeParamType.create("V");
final OptionalType optionalTypeV = OptionalType.create(paramTypeV);
final ListType listTypeV = ListType.create(paramTypeV);
final MapType mapTypeKv = MapType.create(paramTypeK, paramTypeV);

private final CelOptionalLibrary version0 =
new CelOptionalLibrary(
0,
ImmutableSet.of(
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_OF.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_of", optionalTypeV, paramTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_OF_NON_ZERO_VALUE.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_ofNonZeroValue", optionalTypeV, paramTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_NONE.getFunction(),
CelOverloadDecl.newGlobalOverload("optional_none", optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_value", paramTypeV, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
HAS_VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_hasValue", SimpleType.BOOL, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_UNWRAP.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_unwrap_list", listTypeV, ListType.create(optionalTypeV))),
OptionalDeclaration.OPTIONAL_OF.functionDecl(),
OptionalDeclaration.OPTIONAL_OF_NON_ZERO_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_NONE.functionDecl(),
OptionalDeclaration.OPTIONAL_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_HAS_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_UNWRAP.functionDecl(),
// Note: Implementation of "or" and "orValue" are special-cased inside the
// interpreter. Hence, their bindings are not provided here.
CelFunctionDecl.newFunctionDeclaration(
"or",
CelOverloadDecl.newMemberOverload(
"optional_or_optional", optionalTypeV, optionalTypeV, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
"orValue",
CelOverloadDecl.newMemberOverload(
"optional_orValue_value", paramTypeV, optionalTypeV, paramTypeV)),
OptionalDeclaration.OPTIONAL_OR.functionDecl(),
OptionalDeclaration.OPTIONAL_OR_VALUE.functionDecl(),
// Note: Function bindings for optional field selection and indexer is defined
// in {@code StandardFunctions}.
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_SELECT.getFunction(),
CelOverloadDecl.newGlobalOverload(
"select_optional_field",
optionalTypeV,
SimpleType.DYN,
SimpleType.STRING)),
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"list_optindex_optional_int", optionalTypeV, listTypeV, SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_list_optindex_optional_int",
optionalTypeV,
OptionalType.create(listTypeV),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"map_optindex_optional_value", optionalTypeV, mapTypeKv, paramTypeK),
CelOverloadDecl.newGlobalOverload(
"optional_map_optindex_optional_value",
optionalTypeV,
OptionalType.create(mapTypeKv),
paramTypeK)),
OptionalDeclaration.OPTIONAL_SELECT.functionDecl(),
OptionalDeclaration.OPTIONAL_INDEX.functionDecl(),
// Index overloads to accommodate using an optional value as the operand
CelFunctionDecl.newFunctionDeclaration(
Operator.INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_list_index_int",
optionalTypeV,
OptionalType.create(listTypeV),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_map_index_value",
optionalTypeV,
OptionalType.create(mapTypeKv),
paramTypeK))),
OptionalDeclaration.OPTIONAL_INDEX_OPERAND.functionDecl()),
ImmutableSet.of(
CelMacro.newReceiverMacro("optMap", 2, CelOptionalLibrary::expandOptMap)),
ImmutableSet.of(
// Type declaration for optional_type -> type(optional_type(V))
CelVarDecl.newVarDeclaration(
OptionalType.NAME, TypeType.create(optionalTypeV))));
OptionalType.NAME, TypeType.create(Types.OPTIONAL_TYPE_V))));

private final CelOptionalLibrary version1 =
new CelOptionalLibrary(
Expand All@@ -211,16 +264,16 @@ public String getFunction() {
"optional_list_first",
"Return the first value in a list if present, otherwise"
+ " optional.none()",
optionalTypeV,
listTypeV)),
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V)),
CelFunctionDecl.newFunctionDeclaration(
LAST.functionName,
CelOverloadDecl.newMemberOverload(
"optional_list_last",
"Return the last value in a list if present, otherwise"
+ " optional.none()",
optionalTypeV,
listTypeV)))
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V)))
.build(),
version1.macros,
version1.variables);
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -243,7 +243,9 @@ private TranslatedValue translateOperatorCall(
// by our axioms
return TranslatedValue.propagateStrict(ctx, typeSystem, typeSystem.mkError(), args);
case INDEX:
return translateIndex(args, ast);
return translateIndex(args, ast, false);
case OPTIONAL_INDEX:
return translateIndex(args, ast, true);
case CONDITIONAL:
return translateConditional(args, ast);
case NOT_STRICTLY_FALSE:
Expand DownExpand Up@@ -600,7 +602,8 @@ private TranslatedValue translateEquality(
.withApproximation(ctx.mkFalse());
}

private Expr<?> buildListIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard) {
private Expr<?> buildListIndex(
Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard, boolean isOptional) {
Expr<?> listRef = typeSystem.getListRef(lhsTrans);
SeqExpr<?> seq = typeSystem.getSeq(listRef);
Expr<?> index = typeSystem.getInt(rhsTrans);
Expand All@@ -617,6 +620,14 @@ private Expr<?> buildListIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr type
constraintSink.accept(ctx.mkImplies(ctx.mkAnd(typeGuard, inBounds), valNotUnknown));
}

if (isOptional) {
Expr<?> resultOptRef = ctx.mkApp(typeSystem.optionalOfRefFunc(), val);
constraintSink.accept(ctx.mkEq(typeSystem.getOptionalValue(resultOptRef), val));
constraintSink.accept(typeSystem.optHasValue(resultOptRef));
return ctx.mkITE(
inBounds, typeSystem.mkOptionalOf(resultOptRef), typeSystem.mkOptionalNone());
}

return ctx.mkITE(inBounds, val, typeSystem.mkError());
}

Expand DownExpand Up@@ -677,7 +688,8 @@ private ProbeResult createProbeResult(
return new ProbeResult(altInMap, altVal);
}

private Expr<?> buildMapIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard) {
private Expr<?> buildMapIndex(
Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard, boolean isOptional) {
Expr<?> mapRef = typeSystem.getMapRef(lhsTrans);
ArrayExpr mapValues = (ArrayExpr) typeSystem.getMapValues(mapRef);
ArrayExpr mapPresence = (ArrayExpr) typeSystem.getMapPresence(mapRef);
Expand DownExpand Up@@ -780,10 +792,19 @@ private Expr<?> buildMapIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeG
constraintSink.accept(ctx.mkImplies(ctx.mkAnd(typeGuard, finalInMap), valNotUnknown));
}

if (isOptional) {
Expr<?> resultOptRef = ctx.mkApp(typeSystem.optionalOfRefFunc(), finalVal);
constraintSink.accept(ctx.mkEq(typeSystem.getOptionalValue(resultOptRef), finalVal));
constraintSink.accept(typeSystem.optHasValue(resultOptRef));
return ctx.mkITE(
finalInMap, typeSystem.mkOptionalOf(resultOptRef), typeSystem.mkOptionalNone());
}

return ctx.mkITE(finalInMap, finalVal, typeSystem.mkError());
}

private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSyntaxTree ast) {
private TranslatedValue translateIndex(
List<TranslatedValue> args, CelAbstractSyntaxTree ast, boolean isOptional) {
Expr<?> lhsTrans = args.get(0).z3Expr();
Expr<?> rhsTrans = args.get(1).z3Expr();

Expand All@@ -794,13 +815,13 @@ private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSy

Expr<?> actualValue;
if (lhsType.kind() == CelKind.LIST && rhsType.kind() == CelKind.INT) {
actualValue = buildListIndex(lhsTrans, rhsTrans, ctx.mkTrue());
actualValue = buildListIndex(lhsTrans, rhsTrans, ctx.mkTrue(), isOptional);
constraintSink.accept(
ctx.mkImplies(
ctx.mkNot(typeSystem.isError(actualValue)),
typeConstraintGenerator.apply(actualValue, ((ListType) lhsType).elemType())));
} else if (lhsType.kind() == CelKind.MAP) {
actualValue = buildMapIndex(lhsTrans, rhsTrans, ctx.mkTrue());
actualValue = buildMapIndex(lhsTrans, rhsTrans, ctx.mkTrue(), isOptional);
constraintSink.accept(
ctx.mkImplies(
ctx.mkNot(typeSystem.isError(actualValue)),
Expand All@@ -810,8 +831,8 @@ private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSy
BoolExpr isMapGuard = typeSystem.isMap(lhsTrans);
actualValue =
CelZ3TypeSystem.SwitchBuilder.newBuilder(ctx)
.addCase(isListGuard, buildListIndex(lhsTrans, rhsTrans, isListGuard))
.addCase(isMapGuard, buildMapIndex(lhsTrans, rhsTrans, isMapGuard))
.addCase(isListGuard, buildListIndex(lhsTrans, rhsTrans, isListGuard, isOptional))
.addCase(isMapGuard, buildMapIndex(lhsTrans, rhsTrans, isMapGuard, isOptional))
.build(typeSystem.mkError());
}

Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -55,7 +55,7 @@ public final class CelStandardDeclarations {
private final ImmutableSet<CelIdentDecl> celIdentDecls;

/** Enumeration of Standard Functions. */
public enum StandardFunction {
public enum StandardFunction implements CelFunctionDecl.Declarer {
// Deprecated - use {@link #IN}
OLD_IN(
true,
Expand DownExpand Up@@ -1504,6 +1504,7 @@ private CelFunctionDecl withOverloads(Iterable<StandardOverload> overloads) {
return newCelFunctionDecl(functionName, ImmutableSet.copyOf(overloads));
}

@Override
public CelFunctionDecl functionDecl() {
return celFunctionDecl;
}
Expand DownExpand Up@@ -1579,8 +1580,14 @@ public CelIdentDecl identDecl() {

/** General interface for defining a standard function overload. */
@Immutable
public interface StandardOverload {
public interface StandardOverload extends CelFunctionDecl.Declarer {
CelOverloadDecl celOverloadDecl();

@Override
default CelFunctionDecl functionDecl() {
// TODO: Remove default keyword by implementing this for all standard overloads
throw new UnsupportedOperationException("Unimplemented");
}
}

/** Set of all standard function names. */
Expand Down
6 changes: 6 additions & 0 deletions common/src/main/java/dev/cel/common/CelFunctionDecl.java
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,6 +38,12 @@ public abstract class CelFunctionDecl {
/** Required. List of function overloads. Must contain at least one overload. */
public abstract ImmutableSet<CelOverloadDecl> overloads();

/** General interface for defining an extension function overload or standard declaration. */
@Immutable
public interface Declarer {
CelFunctionDecl functionDecl();
}

/** Builder for configuring the {@link CelFunctionDecl}. */
@AutoValue.Builder
public abstract static class Builder {
Expand Down
4 changes: 3 additions & 1 deletion extensions/src/main/java/dev/cel/extensions/BUILD.bazel
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,7 +13,9 @@ package(

java_library(
name = "extension_library",
srcs = ["CelExtensionLibrary.java"],
srcs = [
"CelExtensionLibrary.java",
],
tags = [
],
deps = [
Expand Down
207 changes: 130 additions & 77 deletions extensions/src/main/java/dev/cel/extensions/CelOptionalLibrary.java
Original file line numberDiff line numberDiff line change
Expand Up@@ -97,95 +97,148 @@ public String getFunction() {
}
}

private static final class Types {
private static final TypeParamType PARAM_TYPE_K = TypeParamType.create("K");
private static final TypeParamType PARAM_TYPE_V = TypeParamType.create("V");
private static final OptionalType OPTIONAL_TYPE_V = OptionalType.create(PARAM_TYPE_V);
private static final ListType LIST_TYPE_V = ListType.create(PARAM_TYPE_V);
private static final MapType MAP_TYPE_KV = MapType.create(PARAM_TYPE_K, PARAM_TYPE_V);
}

/** Declarations for the optional extension library. */
public enum OptionalDeclaration implements CelFunctionDecl.Declarer {
OPTIONAL_OF(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_OF.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_of", Types.OPTIONAL_TYPE_V, Types.PARAM_TYPE_V))),
OPTIONAL_OF_NON_ZERO_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_OF_NON_ZERO_VALUE.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_ofNonZeroValue", Types.OPTIONAL_TYPE_V, Types.PARAM_TYPE_V))),
OPTIONAL_NONE(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_NONE.getFunction(),
CelOverloadDecl.newGlobalOverload("optional_none", Types.OPTIONAL_TYPE_V))),
OPTIONAL_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_value", Types.PARAM_TYPE_V, Types.OPTIONAL_TYPE_V))),
OPTIONAL_HAS_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.HAS_VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_hasValue", SimpleType.BOOL, Types.OPTIONAL_TYPE_V))),
OPTIONAL_UNWRAP(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_UNWRAP.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_unwrap_list",
Types.LIST_TYPE_V,
ListType.create(Types.OPTIONAL_TYPE_V)))),
OPTIONAL_OR(
CelFunctionDecl.newFunctionDeclaration(
"or",
CelOverloadDecl.newMemberOverload(
"optional_or_optional",
Types.OPTIONAL_TYPE_V,
Types.OPTIONAL_TYPE_V,
Types.OPTIONAL_TYPE_V))),
OPTIONAL_OR_VALUE(
CelFunctionDecl.newFunctionDeclaration(
"orValue",
CelOverloadDecl.newMemberOverload(
"optional_orValue_value",
Types.PARAM_TYPE_V,
Types.OPTIONAL_TYPE_V,
Types.PARAM_TYPE_V))),
OPTIONAL_SELECT(
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_SELECT.getFunction(),
CelOverloadDecl.newGlobalOverload(
"select_optional_field",
Types.OPTIONAL_TYPE_V,
SimpleType.DYN,
SimpleType.STRING))),
OPTIONAL_INDEX(
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"list_optindex_optional_int",
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V,
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_list_optindex_optional_int",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.LIST_TYPE_V),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"map_optindex_optional_value",
Types.OPTIONAL_TYPE_V,
Types.MAP_TYPE_KV,
Types.PARAM_TYPE_K),
CelOverloadDecl.newGlobalOverload(
"optional_map_optindex_optional_value",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.MAP_TYPE_KV),
Types.PARAM_TYPE_K))),
OPTIONAL_INDEX_OPERAND(
CelFunctionDecl.newFunctionDeclaration(
Operator.INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_list_index_int",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.LIST_TYPE_V),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_map_index_value",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.MAP_TYPE_KV),
Types.PARAM_TYPE_K)));

private final CelFunctionDecl celFunctionDecl;

OptionalDeclaration(CelFunctionDecl celFunctionDecl) {
this.celFunctionDecl = celFunctionDecl;
}

@Override
public CelFunctionDecl functionDecl() {
return celFunctionDecl;
}
}

private static final CelExtensionLibrary<CelOptionalLibrary> LIBRARY =
new CelExtensionLibrary<CelOptionalLibrary>() {
final TypeParamType paramTypeK = TypeParamType.create("K");
final TypeParamType paramTypeV = TypeParamType.create("V");
final OptionalType optionalTypeV = OptionalType.create(paramTypeV);
final ListType listTypeV = ListType.create(paramTypeV);
final MapType mapTypeKv = MapType.create(paramTypeK, paramTypeV);

private final CelOptionalLibrary version0 =
new CelOptionalLibrary(
0,
ImmutableSet.of(
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_OF.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_of", optionalTypeV, paramTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_OF_NON_ZERO_VALUE.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_ofNonZeroValue", optionalTypeV, paramTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_NONE.getFunction(),
CelOverloadDecl.newGlobalOverload("optional_none", optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_value", paramTypeV, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
HAS_VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_hasValue", SimpleType.BOOL, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_UNWRAP.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_unwrap_list", listTypeV, ListType.create(optionalTypeV))),
OptionalDeclaration.OPTIONAL_OF.functionDecl(),
OptionalDeclaration.OPTIONAL_OF_NON_ZERO_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_NONE.functionDecl(),
OptionalDeclaration.OPTIONAL_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_HAS_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_UNWRAP.functionDecl(),
// Note: Implementation of "or" and "orValue" are special-cased inside the
// interpreter. Hence, their bindings are not provided here.
CelFunctionDecl.newFunctionDeclaration(
"or",
CelOverloadDecl.newMemberOverload(
"optional_or_optional", optionalTypeV, optionalTypeV, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
"orValue",
CelOverloadDecl.newMemberOverload(
"optional_orValue_value", paramTypeV, optionalTypeV, paramTypeV)),
OptionalDeclaration.OPTIONAL_OR.functionDecl(),
OptionalDeclaration.OPTIONAL_OR_VALUE.functionDecl(),
// Note: Function bindings for optional field selection and indexer is defined
// in {@code StandardFunctions}.
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_SELECT.getFunction(),
CelOverloadDecl.newGlobalOverload(
"select_optional_field",
optionalTypeV,
SimpleType.DYN,
SimpleType.STRING)),
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"list_optindex_optional_int", optionalTypeV, listTypeV, SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_list_optindex_optional_int",
optionalTypeV,
OptionalType.create(listTypeV),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"map_optindex_optional_value", optionalTypeV, mapTypeKv, paramTypeK),
CelOverloadDecl.newGlobalOverload(
"optional_map_optindex_optional_value",
optionalTypeV,
OptionalType.create(mapTypeKv),
paramTypeK)),
OptionalDeclaration.OPTIONAL_SELECT.functionDecl(),
OptionalDeclaration.OPTIONAL_INDEX.functionDecl(),
// Index overloads to accommodate using an optional value as the operand
CelFunctionDecl.newFunctionDeclaration(
Operator.INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_list_index_int",
optionalTypeV,
OptionalType.create(listTypeV),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_map_index_value",
optionalTypeV,
OptionalType.create(mapTypeKv),
paramTypeK))),
OptionalDeclaration.OPTIONAL_INDEX_OPERAND.functionDecl()),
ImmutableSet.of(
CelMacro.newReceiverMacro("optMap", 2, CelOptionalLibrary::expandOptMap)),
ImmutableSet.of(
// Type declaration for optional_type -> type(optional_type(V))
CelVarDecl.newVarDeclaration(
OptionalType.NAME, TypeType.create(optionalTypeV))));
OptionalType.NAME, TypeType.create(Types.OPTIONAL_TYPE_V))));

private final CelOptionalLibrary version1 =
new CelOptionalLibrary(
Expand All@@ -211,16 +264,16 @@ public String getFunction() {
"optional_list_first",
"Return the first value in a list if present, otherwise"
+ " optional.none()",
optionalTypeV,
listTypeV)),
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V)),
CelFunctionDecl.newFunctionDeclaration(
LAST.functionName,
CelOverloadDecl.newMemberOverload(
"optional_list_last",
"Return the last value in a list if present, otherwise"
+ " optional.none()",
optionalTypeV,
listTypeV)))
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V)))
.build(),
version1.macros,
version1.variables);
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -243,7 +243,9 @@ private TranslatedValue translateOperatorCall(
// by our axioms
return TranslatedValue.propagateStrict(ctx, typeSystem, typeSystem.mkError(), args);
case INDEX:
return translateIndex(args, ast);
return translateIndex(args, ast, false);
case OPTIONAL_INDEX:
return translateIndex(args, ast, true);
case CONDITIONAL:
return translateConditional(args, ast);
case NOT_STRICTLY_FALSE:
Expand DownExpand Up@@ -600,7 +602,8 @@ private TranslatedValue translateEquality(
.withApproximation(ctx.mkFalse());
}

private Expr<?> buildListIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard) {
private Expr<?> buildListIndex(
Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard, boolean isOptional) {
Expr<?> listRef = typeSystem.getListRef(lhsTrans);
SeqExpr<?> seq = typeSystem.getSeq(listRef);
Expr<?> index = typeSystem.getInt(rhsTrans);
Expand All@@ -617,6 +620,14 @@ private Expr<?> buildListIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr type
constraintSink.accept(ctx.mkImplies(ctx.mkAnd(typeGuard, inBounds), valNotUnknown));
}

if (isOptional) {
Expr<?> resultOptRef = ctx.mkApp(typeSystem.optionalOfRefFunc(), val);
constraintSink.accept(ctx.mkEq(typeSystem.getOptionalValue(resultOptRef), val));
constraintSink.accept(typeSystem.optHasValue(resultOptRef));
return ctx.mkITE(
inBounds, typeSystem.mkOptionalOf(resultOptRef), typeSystem.mkOptionalNone());
}

return ctx.mkITE(inBounds, val, typeSystem.mkError());
}

Expand DownExpand Up@@ -677,7 +688,8 @@ private ProbeResult createProbeResult(
return new ProbeResult(altInMap, altVal);
}

private Expr<?> buildMapIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard) {
private Expr<?> buildMapIndex(
Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard, boolean isOptional) {
Expr<?> mapRef = typeSystem.getMapRef(lhsTrans);
ArrayExpr mapValues = (ArrayExpr) typeSystem.getMapValues(mapRef);
ArrayExpr mapPresence = (ArrayExpr) typeSystem.getMapPresence(mapRef);
Expand DownExpand Up@@ -780,10 +792,19 @@ private Expr<?> buildMapIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeG
constraintSink.accept(ctx.mkImplies(ctx.mkAnd(typeGuard, finalInMap), valNotUnknown));
}

if (isOptional) {
Expr<?> resultOptRef = ctx.mkApp(typeSystem.optionalOfRefFunc(), finalVal);
constraintSink.accept(ctx.mkEq(typeSystem.getOptionalValue(resultOptRef), finalVal));
constraintSink.accept(typeSystem.optHasValue(resultOptRef));
return ctx.mkITE(
finalInMap, typeSystem.mkOptionalOf(resultOptRef), typeSystem.mkOptionalNone());
}

return ctx.mkITE(finalInMap, finalVal, typeSystem.mkError());
}

private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSyntaxTree ast) {
private TranslatedValue translateIndex(
List<TranslatedValue> args, CelAbstractSyntaxTree ast, boolean isOptional) {
Expr<?> lhsTrans = args.get(0).z3Expr();
Expr<?> rhsTrans = args.get(1).z3Expr();

Expand All@@ -794,13 +815,13 @@ private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSy

Expr<?> actualValue;
if (lhsType.kind() == CelKind.LIST && rhsType.kind() == CelKind.INT) {
actualValue = buildListIndex(lhsTrans, rhsTrans, ctx.mkTrue());
actualValue = buildListIndex(lhsTrans, rhsTrans, ctx.mkTrue(), isOptional);
constraintSink.accept(
ctx.mkImplies(
ctx.mkNot(typeSystem.isError(actualValue)),
typeConstraintGenerator.apply(actualValue, ((ListType) lhsType).elemType())));
} else if (lhsType.kind() == CelKind.MAP) {
actualValue = buildMapIndex(lhsTrans, rhsTrans, ctx.mkTrue());
actualValue = buildMapIndex(lhsTrans, rhsTrans, ctx.mkTrue(), isOptional);
constraintSink.accept(
ctx.mkImplies(
ctx.mkNot(typeSystem.isError(actualValue)),
Expand All@@ -810,8 +831,8 @@ private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSy
BoolExpr isMapGuard = typeSystem.isMap(lhsTrans);
actualValue =
CelZ3TypeSystem.SwitchBuilder.newBuilder(ctx)
.addCase(isListGuard, buildListIndex(lhsTrans, rhsTrans, isListGuard))
.addCase(isMapGuard, buildMapIndex(lhsTrans, rhsTrans, isMapGuard))
.addCase(isListGuard, buildListIndex(lhsTrans, rhsTrans, isListGuard, isOptional))
.addCase(isMapGuard, buildMapIndex(lhsTrans, rhsTrans, isMapGuard, isOptional))
.build(typeSystem.mkError());
}

Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -55,7 +55,7 @@ public final class CelStandardDeclarations {
private final ImmutableSet<CelIdentDecl> celIdentDecls;

/** Enumeration of Standard Functions. */
public enum StandardFunction {
public enum StandardFunction implements CelFunctionDecl.Declarer {
// Deprecated - use {@link #IN}
OLD_IN(
true,
Expand DownExpand Up@@ -1504,6 +1504,7 @@ private CelFunctionDecl withOverloads(Iterable<StandardOverload> overloads) {
return newCelFunctionDecl(functionName, ImmutableSet.copyOf(overloads));
}

@Override
public CelFunctionDecl functionDecl() {
return celFunctionDecl;
}
Expand DownExpand Up@@ -1579,8 +1580,14 @@ public CelIdentDecl identDecl() {

/** General interface for defining a standard function overload. */
@Immutable
public interface StandardOverload {
public interface StandardOverload extends CelFunctionDecl.Declarer {
CelOverloadDecl celOverloadDecl();

@Override
default CelFunctionDecl functionDecl() {
// TODO: Remove default keyword by implementing this for all standard overloads
throw new UnsupportedOperationException("Unimplemented");
}
}

/** Set of all standard function names. */
Expand Down
6 changes: 6 additions & 0 deletions common/src/main/java/dev/cel/common/CelFunctionDecl.java
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,6 +38,12 @@ public abstract class CelFunctionDecl {
/** Required. List of function overloads. Must contain at least one overload. */
public abstract ImmutableSet<CelOverloadDecl> overloads();

/** General interface for defining an extension function overload or standard declaration. */
@Immutable
public interface Declarer {
CelFunctionDecl functionDecl();
}

/** Builder for configuring the {@link CelFunctionDecl}. */
@AutoValue.Builder
public abstract static class Builder {
Expand Down
4 changes: 3 additions & 1 deletion extensions/src/main/java/dev/cel/extensions/BUILD.bazel
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,7 +13,9 @@ package(

java_library(
name = "extension_library",
srcs = ["CelExtensionLibrary.java"],
srcs = [
"CelExtensionLibrary.java",
],
tags = [
],
deps = [
Expand Down
207 changes: 130 additions & 77 deletions extensions/src/main/java/dev/cel/extensions/CelOptionalLibrary.java
Original file line numberDiff line numberDiff line change
Expand Up@@ -97,95 +97,148 @@ public String getFunction() {
}
}

private static final class Types {
private static final TypeParamType PARAM_TYPE_K = TypeParamType.create("K");
private static final TypeParamType PARAM_TYPE_V = TypeParamType.create("V");
private static final OptionalType OPTIONAL_TYPE_V = OptionalType.create(PARAM_TYPE_V);
private static final ListType LIST_TYPE_V = ListType.create(PARAM_TYPE_V);
private static final MapType MAP_TYPE_KV = MapType.create(PARAM_TYPE_K, PARAM_TYPE_V);
}

/** Declarations for the optional extension library. */
public enum OptionalDeclaration implements CelFunctionDecl.Declarer {
OPTIONAL_OF(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_OF.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_of", Types.OPTIONAL_TYPE_V, Types.PARAM_TYPE_V))),
OPTIONAL_OF_NON_ZERO_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_OF_NON_ZERO_VALUE.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_ofNonZeroValue", Types.OPTIONAL_TYPE_V, Types.PARAM_TYPE_V))),
OPTIONAL_NONE(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_NONE.getFunction(),
CelOverloadDecl.newGlobalOverload("optional_none", Types.OPTIONAL_TYPE_V))),
OPTIONAL_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_value", Types.PARAM_TYPE_V, Types.OPTIONAL_TYPE_V))),
OPTIONAL_HAS_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.HAS_VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_hasValue", SimpleType.BOOL, Types.OPTIONAL_TYPE_V))),
OPTIONAL_UNWRAP(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_UNWRAP.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_unwrap_list",
Types.LIST_TYPE_V,
ListType.create(Types.OPTIONAL_TYPE_V)))),
OPTIONAL_OR(
CelFunctionDecl.newFunctionDeclaration(
"or",
CelOverloadDecl.newMemberOverload(
"optional_or_optional",
Types.OPTIONAL_TYPE_V,
Types.OPTIONAL_TYPE_V,
Types.OPTIONAL_TYPE_V))),
OPTIONAL_OR_VALUE(
CelFunctionDecl.newFunctionDeclaration(
"orValue",
CelOverloadDecl.newMemberOverload(
"optional_orValue_value",
Types.PARAM_TYPE_V,
Types.OPTIONAL_TYPE_V,
Types.PARAM_TYPE_V))),
OPTIONAL_SELECT(
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_SELECT.getFunction(),
CelOverloadDecl.newGlobalOverload(
"select_optional_field",
Types.OPTIONAL_TYPE_V,
SimpleType.DYN,
SimpleType.STRING))),
OPTIONAL_INDEX(
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"list_optindex_optional_int",
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V,
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_list_optindex_optional_int",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.LIST_TYPE_V),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"map_optindex_optional_value",
Types.OPTIONAL_TYPE_V,
Types.MAP_TYPE_KV,
Types.PARAM_TYPE_K),
CelOverloadDecl.newGlobalOverload(
"optional_map_optindex_optional_value",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.MAP_TYPE_KV),
Types.PARAM_TYPE_K))),
OPTIONAL_INDEX_OPERAND(
CelFunctionDecl.newFunctionDeclaration(
Operator.INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_list_index_int",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.LIST_TYPE_V),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_map_index_value",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.MAP_TYPE_KV),
Types.PARAM_TYPE_K)));

private final CelFunctionDecl celFunctionDecl;

OptionalDeclaration(CelFunctionDecl celFunctionDecl) {
this.celFunctionDecl = celFunctionDecl;
}

@Override
public CelFunctionDecl functionDecl() {
return celFunctionDecl;
}
}

private static final CelExtensionLibrary<CelOptionalLibrary> LIBRARY =
new CelExtensionLibrary<CelOptionalLibrary>() {
final TypeParamType paramTypeK = TypeParamType.create("K");
final TypeParamType paramTypeV = TypeParamType.create("V");
final OptionalType optionalTypeV = OptionalType.create(paramTypeV);
final ListType listTypeV = ListType.create(paramTypeV);
final MapType mapTypeKv = MapType.create(paramTypeK, paramTypeV);

private final CelOptionalLibrary version0 =
new CelOptionalLibrary(
0,
ImmutableSet.of(
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_OF.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_of", optionalTypeV, paramTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_OF_NON_ZERO_VALUE.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_ofNonZeroValue", optionalTypeV, paramTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_NONE.getFunction(),
CelOverloadDecl.newGlobalOverload("optional_none", optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_value", paramTypeV, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
HAS_VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_hasValue", SimpleType.BOOL, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_UNWRAP.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_unwrap_list", listTypeV, ListType.create(optionalTypeV))),
OptionalDeclaration.OPTIONAL_OF.functionDecl(),
OptionalDeclaration.OPTIONAL_OF_NON_ZERO_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_NONE.functionDecl(),
OptionalDeclaration.OPTIONAL_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_HAS_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_UNWRAP.functionDecl(),
// Note: Implementation of "or" and "orValue" are special-cased inside the
// interpreter. Hence, their bindings are not provided here.
CelFunctionDecl.newFunctionDeclaration(
"or",
CelOverloadDecl.newMemberOverload(
"optional_or_optional", optionalTypeV, optionalTypeV, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
"orValue",
CelOverloadDecl.newMemberOverload(
"optional_orValue_value", paramTypeV, optionalTypeV, paramTypeV)),
OptionalDeclaration.OPTIONAL_OR.functionDecl(),
OptionalDeclaration.OPTIONAL_OR_VALUE.functionDecl(),
// Note: Function bindings for optional field selection and indexer is defined
// in {@code StandardFunctions}.
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_SELECT.getFunction(),
CelOverloadDecl.newGlobalOverload(
"select_optional_field",
optionalTypeV,
SimpleType.DYN,
SimpleType.STRING)),
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"list_optindex_optional_int", optionalTypeV, listTypeV, SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_list_optindex_optional_int",
optionalTypeV,
OptionalType.create(listTypeV),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"map_optindex_optional_value", optionalTypeV, mapTypeKv, paramTypeK),
CelOverloadDecl.newGlobalOverload(
"optional_map_optindex_optional_value",
optionalTypeV,
OptionalType.create(mapTypeKv),
paramTypeK)),
OptionalDeclaration.OPTIONAL_SELECT.functionDecl(),
OptionalDeclaration.OPTIONAL_INDEX.functionDecl(),
// Index overloads to accommodate using an optional value as the operand
CelFunctionDecl.newFunctionDeclaration(
Operator.INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_list_index_int",
optionalTypeV,
OptionalType.create(listTypeV),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_map_index_value",
optionalTypeV,
OptionalType.create(mapTypeKv),
paramTypeK))),
OptionalDeclaration.OPTIONAL_INDEX_OPERAND.functionDecl()),
ImmutableSet.of(
CelMacro.newReceiverMacro("optMap", 2, CelOptionalLibrary::expandOptMap)),
ImmutableSet.of(
// Type declaration for optional_type -> type(optional_type(V))
CelVarDecl.newVarDeclaration(
OptionalType.NAME, TypeType.create(optionalTypeV))));
OptionalType.NAME, TypeType.create(Types.OPTIONAL_TYPE_V))));

private final CelOptionalLibrary version1 =
new CelOptionalLibrary(
Expand All@@ -211,16 +264,16 @@ public String getFunction() {
"optional_list_first",
"Return the first value in a list if present, otherwise"
+ " optional.none()",
optionalTypeV,
listTypeV)),
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V)),
CelFunctionDecl.newFunctionDeclaration(
LAST.functionName,
CelOverloadDecl.newMemberOverload(
"optional_list_last",
"Return the last value in a list if present, otherwise"
+ " optional.none()",
optionalTypeV,
listTypeV)))
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V)))
.build(),
version1.macros,
version1.variables);
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -243,7 +243,9 @@ private TranslatedValue translateOperatorCall(
// by our axioms
return TranslatedValue.propagateStrict(ctx, typeSystem, typeSystem.mkError(), args);
case INDEX:
return translateIndex(args, ast);
return translateIndex(args, ast, false);
case OPTIONAL_INDEX:
return translateIndex(args, ast, true);
case CONDITIONAL:
return translateConditional(args, ast);
case NOT_STRICTLY_FALSE:
Expand DownExpand Up@@ -600,7 +602,8 @@ private TranslatedValue translateEquality(
.withApproximation(ctx.mkFalse());
}

private Expr<?> buildListIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard) {
private Expr<?> buildListIndex(
Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard, boolean isOptional) {
Expr<?> listRef = typeSystem.getListRef(lhsTrans);
SeqExpr<?> seq = typeSystem.getSeq(listRef);
Expr<?> index = typeSystem.getInt(rhsTrans);
Expand All@@ -617,6 +620,14 @@ private Expr<?> buildListIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr type
constraintSink.accept(ctx.mkImplies(ctx.mkAnd(typeGuard, inBounds), valNotUnknown));
}

if (isOptional) {
Expr<?> resultOptRef = ctx.mkApp(typeSystem.optionalOfRefFunc(), val);
constraintSink.accept(ctx.mkEq(typeSystem.getOptionalValue(resultOptRef), val));
constraintSink.accept(typeSystem.optHasValue(resultOptRef));
return ctx.mkITE(
inBounds, typeSystem.mkOptionalOf(resultOptRef), typeSystem.mkOptionalNone());
}

return ctx.mkITE(inBounds, val, typeSystem.mkError());
}

Expand DownExpand Up@@ -677,7 +688,8 @@ private ProbeResult createProbeResult(
return new ProbeResult(altInMap, altVal);
}

private Expr<?> buildMapIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard) {
private Expr<?> buildMapIndex(
Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard, boolean isOptional) {
Expr<?> mapRef = typeSystem.getMapRef(lhsTrans);
ArrayExpr mapValues = (ArrayExpr) typeSystem.getMapValues(mapRef);
ArrayExpr mapPresence = (ArrayExpr) typeSystem.getMapPresence(mapRef);
Expand DownExpand Up@@ -780,10 +792,19 @@ private Expr<?> buildMapIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeG
constraintSink.accept(ctx.mkImplies(ctx.mkAnd(typeGuard, finalInMap), valNotUnknown));
}

if (isOptional) {
Expr<?> resultOptRef = ctx.mkApp(typeSystem.optionalOfRefFunc(), finalVal);
constraintSink.accept(ctx.mkEq(typeSystem.getOptionalValue(resultOptRef), finalVal));
constraintSink.accept(typeSystem.optHasValue(resultOptRef));
return ctx.mkITE(
finalInMap, typeSystem.mkOptionalOf(resultOptRef), typeSystem.mkOptionalNone());
}

return ctx.mkITE(finalInMap, finalVal, typeSystem.mkError());
}

private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSyntaxTree ast) {
private TranslatedValue translateIndex(
List<TranslatedValue> args, CelAbstractSyntaxTree ast, boolean isOptional) {
Expr<?> lhsTrans = args.get(0).z3Expr();
Expr<?> rhsTrans = args.get(1).z3Expr();

Expand All@@ -794,13 +815,13 @@ private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSy

Expr<?> actualValue;
if (lhsType.kind() == CelKind.LIST && rhsType.kind() == CelKind.INT) {
actualValue = buildListIndex(lhsTrans, rhsTrans, ctx.mkTrue());
actualValue = buildListIndex(lhsTrans, rhsTrans, ctx.mkTrue(), isOptional);
constraintSink.accept(
ctx.mkImplies(
ctx.mkNot(typeSystem.isError(actualValue)),
typeConstraintGenerator.apply(actualValue, ((ListType) lhsType).elemType())));
} else if (lhsType.kind() == CelKind.MAP) {
actualValue = buildMapIndex(lhsTrans, rhsTrans, ctx.mkTrue());
actualValue = buildMapIndex(lhsTrans, rhsTrans, ctx.mkTrue(), isOptional);
constraintSink.accept(
ctx.mkImplies(
ctx.mkNot(typeSystem.isError(actualValue)),
Expand All@@ -810,8 +831,8 @@ private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSy
BoolExpr isMapGuard = typeSystem.isMap(lhsTrans);
actualValue =
CelZ3TypeSystem.SwitchBuilder.newBuilder(ctx)
.addCase(isListGuard, buildListIndex(lhsTrans, rhsTrans, isListGuard))
.addCase(isMapGuard, buildMapIndex(lhsTrans, rhsTrans, isMapGuard))
.addCase(isListGuard, buildListIndex(lhsTrans, rhsTrans, isListGuard, isOptional))
.addCase(isMapGuard, buildMapIndex(lhsTrans, rhsTrans, isMapGuard, isOptional))
.build(typeSystem.mkError());
}

Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -55,7 +55,7 @@ public final class CelStandardDeclarations {
private final ImmutableSet<CelIdentDecl> celIdentDecls;

/** Enumeration of Standard Functions. */
public enum StandardFunction {
public enum StandardFunction implements CelFunctionDecl.Declarer {
// Deprecated - use {@link #IN}
OLD_IN(
true,
Expand DownExpand Up@@ -1504,6 +1504,7 @@ private CelFunctionDecl withOverloads(Iterable<StandardOverload> overloads) {
return newCelFunctionDecl(functionName, ImmutableSet.copyOf(overloads));
}

@Override
public CelFunctionDecl functionDecl() {
return celFunctionDecl;
}
Expand DownExpand Up@@ -1579,8 +1580,14 @@ public CelIdentDecl identDecl() {

/** General interface for defining a standard function overload. */
@Immutable
public interface StandardOverload {
public interface StandardOverload extends CelFunctionDecl.Declarer {
CelOverloadDecl celOverloadDecl();

@Override
default CelFunctionDecl functionDecl() {
// TODO: Remove default keyword by implementing this for all standard overloads
throw new UnsupportedOperationException("Unimplemented");
}
}

/** Set of all standard function names. */
Expand Down
6 changes: 6 additions & 0 deletions common/src/main/java/dev/cel/common/CelFunctionDecl.java
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,6 +38,12 @@ public abstract class CelFunctionDecl {
/** Required. List of function overloads. Must contain at least one overload. */
public abstract ImmutableSet<CelOverloadDecl> overloads();

/** General interface for defining an extension function overload or standard declaration. */
@Immutable
public interface Declarer {
CelFunctionDecl functionDecl();
}

/** Builder for configuring the {@link CelFunctionDecl}. */
@AutoValue.Builder
public abstract static class Builder {
Expand Down
4 changes: 3 additions & 1 deletion extensions/src/main/java/dev/cel/extensions/BUILD.bazel
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,7 +13,9 @@ package(

java_library(
name = "extension_library",
srcs = ["CelExtensionLibrary.java"],
srcs = [
"CelExtensionLibrary.java",
],
tags = [
],
deps = [
Expand Down
207 changes: 130 additions & 77 deletions extensions/src/main/java/dev/cel/extensions/CelOptionalLibrary.java
Original file line numberDiff line numberDiff line change
Expand Up@@ -97,95 +97,148 @@ public String getFunction() {
}
}

private static final class Types {
private static final TypeParamType PARAM_TYPE_K = TypeParamType.create("K");
private static final TypeParamType PARAM_TYPE_V = TypeParamType.create("V");
private static final OptionalType OPTIONAL_TYPE_V = OptionalType.create(PARAM_TYPE_V);
private static final ListType LIST_TYPE_V = ListType.create(PARAM_TYPE_V);
private static final MapType MAP_TYPE_KV = MapType.create(PARAM_TYPE_K, PARAM_TYPE_V);
}

/** Declarations for the optional extension library. */
public enum OptionalDeclaration implements CelFunctionDecl.Declarer {
OPTIONAL_OF(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_OF.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_of", Types.OPTIONAL_TYPE_V, Types.PARAM_TYPE_V))),
OPTIONAL_OF_NON_ZERO_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_OF_NON_ZERO_VALUE.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_ofNonZeroValue", Types.OPTIONAL_TYPE_V, Types.PARAM_TYPE_V))),
OPTIONAL_NONE(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_NONE.getFunction(),
CelOverloadDecl.newGlobalOverload("optional_none", Types.OPTIONAL_TYPE_V))),
OPTIONAL_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_value", Types.PARAM_TYPE_V, Types.OPTIONAL_TYPE_V))),
OPTIONAL_HAS_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.HAS_VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_hasValue", SimpleType.BOOL, Types.OPTIONAL_TYPE_V))),
OPTIONAL_UNWRAP(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_UNWRAP.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_unwrap_list",
Types.LIST_TYPE_V,
ListType.create(Types.OPTIONAL_TYPE_V)))),
OPTIONAL_OR(
CelFunctionDecl.newFunctionDeclaration(
"or",
CelOverloadDecl.newMemberOverload(
"optional_or_optional",
Types.OPTIONAL_TYPE_V,
Types.OPTIONAL_TYPE_V,
Types.OPTIONAL_TYPE_V))),
OPTIONAL_OR_VALUE(
CelFunctionDecl.newFunctionDeclaration(
"orValue",
CelOverloadDecl.newMemberOverload(
"optional_orValue_value",
Types.PARAM_TYPE_V,
Types.OPTIONAL_TYPE_V,
Types.PARAM_TYPE_V))),
OPTIONAL_SELECT(
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_SELECT.getFunction(),
CelOverloadDecl.newGlobalOverload(
"select_optional_field",
Types.OPTIONAL_TYPE_V,
SimpleType.DYN,
SimpleType.STRING))),
OPTIONAL_INDEX(
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"list_optindex_optional_int",
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V,
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_list_optindex_optional_int",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.LIST_TYPE_V),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"map_optindex_optional_value",
Types.OPTIONAL_TYPE_V,
Types.MAP_TYPE_KV,
Types.PARAM_TYPE_K),
CelOverloadDecl.newGlobalOverload(
"optional_map_optindex_optional_value",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.MAP_TYPE_KV),
Types.PARAM_TYPE_K))),
OPTIONAL_INDEX_OPERAND(
CelFunctionDecl.newFunctionDeclaration(
Operator.INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_list_index_int",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.LIST_TYPE_V),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_map_index_value",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.MAP_TYPE_KV),
Types.PARAM_TYPE_K)));

private final CelFunctionDecl celFunctionDecl;

OptionalDeclaration(CelFunctionDecl celFunctionDecl) {
this.celFunctionDecl = celFunctionDecl;
}

@Override
public CelFunctionDecl functionDecl() {
return celFunctionDecl;
}
}

private static final CelExtensionLibrary<CelOptionalLibrary> LIBRARY =
new CelExtensionLibrary<CelOptionalLibrary>() {
final TypeParamType paramTypeK = TypeParamType.create("K");
final TypeParamType paramTypeV = TypeParamType.create("V");
final OptionalType optionalTypeV = OptionalType.create(paramTypeV);
final ListType listTypeV = ListType.create(paramTypeV);
final MapType mapTypeKv = MapType.create(paramTypeK, paramTypeV);

private final CelOptionalLibrary version0 =
new CelOptionalLibrary(
0,
ImmutableSet.of(
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_OF.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_of", optionalTypeV, paramTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_OF_NON_ZERO_VALUE.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_ofNonZeroValue", optionalTypeV, paramTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_NONE.getFunction(),
CelOverloadDecl.newGlobalOverload("optional_none", optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_value", paramTypeV, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
HAS_VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_hasValue", SimpleType.BOOL, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_UNWRAP.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_unwrap_list", listTypeV, ListType.create(optionalTypeV))),
OptionalDeclaration.OPTIONAL_OF.functionDecl(),
OptionalDeclaration.OPTIONAL_OF_NON_ZERO_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_NONE.functionDecl(),
OptionalDeclaration.OPTIONAL_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_HAS_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_UNWRAP.functionDecl(),
// Note: Implementation of "or" and "orValue" are special-cased inside the
// interpreter. Hence, their bindings are not provided here.
CelFunctionDecl.newFunctionDeclaration(
"or",
CelOverloadDecl.newMemberOverload(
"optional_or_optional", optionalTypeV, optionalTypeV, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
"orValue",
CelOverloadDecl.newMemberOverload(
"optional_orValue_value", paramTypeV, optionalTypeV, paramTypeV)),
OptionalDeclaration.OPTIONAL_OR.functionDecl(),
OptionalDeclaration.OPTIONAL_OR_VALUE.functionDecl(),
// Note: Function bindings for optional field selection and indexer is defined
// in {@code StandardFunctions}.
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_SELECT.getFunction(),
CelOverloadDecl.newGlobalOverload(
"select_optional_field",
optionalTypeV,
SimpleType.DYN,
SimpleType.STRING)),
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"list_optindex_optional_int", optionalTypeV, listTypeV, SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_list_optindex_optional_int",
optionalTypeV,
OptionalType.create(listTypeV),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"map_optindex_optional_value", optionalTypeV, mapTypeKv, paramTypeK),
CelOverloadDecl.newGlobalOverload(
"optional_map_optindex_optional_value",
optionalTypeV,
OptionalType.create(mapTypeKv),
paramTypeK)),
OptionalDeclaration.OPTIONAL_SELECT.functionDecl(),
OptionalDeclaration.OPTIONAL_INDEX.functionDecl(),
// Index overloads to accommodate using an optional value as the operand
CelFunctionDecl.newFunctionDeclaration(
Operator.INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_list_index_int",
optionalTypeV,
OptionalType.create(listTypeV),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_map_index_value",
optionalTypeV,
OptionalType.create(mapTypeKv),
paramTypeK))),
OptionalDeclaration.OPTIONAL_INDEX_OPERAND.functionDecl()),
ImmutableSet.of(
CelMacro.newReceiverMacro("optMap", 2, CelOptionalLibrary::expandOptMap)),
ImmutableSet.of(
// Type declaration for optional_type -> type(optional_type(V))
CelVarDecl.newVarDeclaration(
OptionalType.NAME, TypeType.create(optionalTypeV))));
OptionalType.NAME, TypeType.create(Types.OPTIONAL_TYPE_V))));

private final CelOptionalLibrary version1 =
new CelOptionalLibrary(
Expand All@@ -211,16 +264,16 @@ public String getFunction() {
"optional_list_first",
"Return the first value in a list if present, otherwise"
+ " optional.none()",
optionalTypeV,
listTypeV)),
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V)),
CelFunctionDecl.newFunctionDeclaration(
LAST.functionName,
CelOverloadDecl.newMemberOverload(
"optional_list_last",
"Return the last value in a list if present, otherwise"
+ " optional.none()",
optionalTypeV,
listTypeV)))
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V)))
.build(),
version1.macros,
version1.variables);
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -243,7 +243,9 @@ private TranslatedValue translateOperatorCall(
// by our axioms
return TranslatedValue.propagateStrict(ctx, typeSystem, typeSystem.mkError(), args);
case INDEX:
return translateIndex(args, ast);
return translateIndex(args, ast, false);
case OPTIONAL_INDEX:
return translateIndex(args, ast, true);
case CONDITIONAL:
return translateConditional(args, ast);
case NOT_STRICTLY_FALSE:
Expand DownExpand Up@@ -600,7 +602,8 @@ private TranslatedValue translateEquality(
.withApproximation(ctx.mkFalse());
}

private Expr<?> buildListIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard) {
private Expr<?> buildListIndex(
Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard, boolean isOptional) {
Expr<?> listRef = typeSystem.getListRef(lhsTrans);
SeqExpr<?> seq = typeSystem.getSeq(listRef);
Expr<?> index = typeSystem.getInt(rhsTrans);
Expand All@@ -617,6 +620,14 @@ private Expr<?> buildListIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr type
constraintSink.accept(ctx.mkImplies(ctx.mkAnd(typeGuard, inBounds), valNotUnknown));
}

if (isOptional) {
Expr<?> resultOptRef = ctx.mkApp(typeSystem.optionalOfRefFunc(), val);
constraintSink.accept(ctx.mkEq(typeSystem.getOptionalValue(resultOptRef), val));
constraintSink.accept(typeSystem.optHasValue(resultOptRef));
return ctx.mkITE(
inBounds, typeSystem.mkOptionalOf(resultOptRef), typeSystem.mkOptionalNone());
}

return ctx.mkITE(inBounds, val, typeSystem.mkError());
}

Expand DownExpand Up@@ -677,7 +688,8 @@ private ProbeResult createProbeResult(
return new ProbeResult(altInMap, altVal);
}

private Expr<?> buildMapIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard) {
private Expr<?> buildMapIndex(
Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard, boolean isOptional) {
Expr<?> mapRef = typeSystem.getMapRef(lhsTrans);
ArrayExpr mapValues = (ArrayExpr) typeSystem.getMapValues(mapRef);
ArrayExpr mapPresence = (ArrayExpr) typeSystem.getMapPresence(mapRef);
Expand DownExpand Up@@ -780,10 +792,19 @@ private Expr<?> buildMapIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeG
constraintSink.accept(ctx.mkImplies(ctx.mkAnd(typeGuard, finalInMap), valNotUnknown));
}

if (isOptional) {
Expr<?> resultOptRef = ctx.mkApp(typeSystem.optionalOfRefFunc(), finalVal);
constraintSink.accept(ctx.mkEq(typeSystem.getOptionalValue(resultOptRef), finalVal));
constraintSink.accept(typeSystem.optHasValue(resultOptRef));
return ctx.mkITE(
finalInMap, typeSystem.mkOptionalOf(resultOptRef), typeSystem.mkOptionalNone());
}

return ctx.mkITE(finalInMap, finalVal, typeSystem.mkError());
}

private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSyntaxTree ast) {
private TranslatedValue translateIndex(
List<TranslatedValue> args, CelAbstractSyntaxTree ast, boolean isOptional) {
Expr<?> lhsTrans = args.get(0).z3Expr();
Expr<?> rhsTrans = args.get(1).z3Expr();

Expand All@@ -794,13 +815,13 @@ private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSy

Expr<?> actualValue;
if (lhsType.kind() == CelKind.LIST && rhsType.kind() == CelKind.INT) {
actualValue = buildListIndex(lhsTrans, rhsTrans, ctx.mkTrue());
actualValue = buildListIndex(lhsTrans, rhsTrans, ctx.mkTrue(), isOptional);
constraintSink.accept(
ctx.mkImplies(
ctx.mkNot(typeSystem.isError(actualValue)),
typeConstraintGenerator.apply(actualValue, ((ListType) lhsType).elemType())));
} else if (lhsType.kind() == CelKind.MAP) {
actualValue = buildMapIndex(lhsTrans, rhsTrans, ctx.mkTrue());
actualValue = buildMapIndex(lhsTrans, rhsTrans, ctx.mkTrue(), isOptional);
constraintSink.accept(
ctx.mkImplies(
ctx.mkNot(typeSystem.isError(actualValue)),
Expand All@@ -810,8 +831,8 @@ private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSy
BoolExpr isMapGuard = typeSystem.isMap(lhsTrans);
actualValue =
CelZ3TypeSystem.SwitchBuilder.newBuilder(ctx)
.addCase(isListGuard, buildListIndex(lhsTrans, rhsTrans, isListGuard))
.addCase(isMapGuard, buildMapIndex(lhsTrans, rhsTrans, isMapGuard))
.addCase(isListGuard, buildListIndex(lhsTrans, rhsTrans, isListGuard, isOptional))
.addCase(isMapGuard, buildMapIndex(lhsTrans, rhsTrans, isMapGuard, isOptional))
.build(typeSystem.mkError());
}

Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -55,7 +55,7 @@ public final class CelStandardDeclarations {
private final ImmutableSet<CelIdentDecl> celIdentDecls;

/** Enumeration of Standard Functions. */
public enum StandardFunction {
public enum StandardFunction implements CelFunctionDecl.Declarer {
// Deprecated - use {@link #IN}
OLD_IN(
true,
Expand DownExpand Up@@ -1504,6 +1504,7 @@ private CelFunctionDecl withOverloads(Iterable<StandardOverload> overloads) {
return newCelFunctionDecl(functionName, ImmutableSet.copyOf(overloads));
}

@Override
public CelFunctionDecl functionDecl() {
return celFunctionDecl;
}
Expand DownExpand Up@@ -1579,8 +1580,14 @@ public CelIdentDecl identDecl() {

/** General interface for defining a standard function overload. */
@Immutable
public interface StandardOverload {
public interface StandardOverload extends CelFunctionDecl.Declarer {
CelOverloadDecl celOverloadDecl();

@Override
default CelFunctionDecl functionDecl() {
// TODO: Remove default keyword by implementing this for all standard overloads
throw new UnsupportedOperationException("Unimplemented");
}
}

/** Set of all standard function names. */
Expand Down
6 changes: 6 additions & 0 deletions common/src/main/java/dev/cel/common/CelFunctionDecl.java
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,6 +38,12 @@ public abstract class CelFunctionDecl {
/** Required. List of function overloads. Must contain at least one overload. */
public abstract ImmutableSet<CelOverloadDecl> overloads();

/** General interface for defining an extension function overload or standard declaration. */
@Immutable
public interface Declarer {
CelFunctionDecl functionDecl();
}

/** Builder for configuring the {@link CelFunctionDecl}. */
@AutoValue.Builder
public abstract static class Builder {
Expand Down
4 changes: 3 additions & 1 deletion extensions/src/main/java/dev/cel/extensions/BUILD.bazel
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,7 +13,9 @@ package(

java_library(
name = "extension_library",
srcs = ["CelExtensionLibrary.java"],
srcs = [
"CelExtensionLibrary.java",
],
tags = [
],
deps = [
Expand Down
207 changes: 130 additions & 77 deletions extensions/src/main/java/dev/cel/extensions/CelOptionalLibrary.java
Original file line numberDiff line numberDiff line change
Expand Up@@ -97,95 +97,148 @@ public String getFunction() {
}
}

private static final class Types {
private static final TypeParamType PARAM_TYPE_K = TypeParamType.create("K");
private static final TypeParamType PARAM_TYPE_V = TypeParamType.create("V");
private static final OptionalType OPTIONAL_TYPE_V = OptionalType.create(PARAM_TYPE_V);
private static final ListType LIST_TYPE_V = ListType.create(PARAM_TYPE_V);
private static final MapType MAP_TYPE_KV = MapType.create(PARAM_TYPE_K, PARAM_TYPE_V);
}

/** Declarations for the optional extension library. */
public enum OptionalDeclaration implements CelFunctionDecl.Declarer {
OPTIONAL_OF(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_OF.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_of", Types.OPTIONAL_TYPE_V, Types.PARAM_TYPE_V))),
OPTIONAL_OF_NON_ZERO_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_OF_NON_ZERO_VALUE.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_ofNonZeroValue", Types.OPTIONAL_TYPE_V, Types.PARAM_TYPE_V))),
OPTIONAL_NONE(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_NONE.getFunction(),
CelOverloadDecl.newGlobalOverload("optional_none", Types.OPTIONAL_TYPE_V))),
OPTIONAL_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_value", Types.PARAM_TYPE_V, Types.OPTIONAL_TYPE_V))),
OPTIONAL_HAS_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.HAS_VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_hasValue", SimpleType.BOOL, Types.OPTIONAL_TYPE_V))),
OPTIONAL_UNWRAP(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_UNWRAP.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_unwrap_list",
Types.LIST_TYPE_V,
ListType.create(Types.OPTIONAL_TYPE_V)))),
OPTIONAL_OR(
CelFunctionDecl.newFunctionDeclaration(
"or",
CelOverloadDecl.newMemberOverload(
"optional_or_optional",
Types.OPTIONAL_TYPE_V,
Types.OPTIONAL_TYPE_V,
Types.OPTIONAL_TYPE_V))),
OPTIONAL_OR_VALUE(
CelFunctionDecl.newFunctionDeclaration(
"orValue",
CelOverloadDecl.newMemberOverload(
"optional_orValue_value",
Types.PARAM_TYPE_V,
Types.OPTIONAL_TYPE_V,
Types.PARAM_TYPE_V))),
OPTIONAL_SELECT(
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_SELECT.getFunction(),
CelOverloadDecl.newGlobalOverload(
"select_optional_field",
Types.OPTIONAL_TYPE_V,
SimpleType.DYN,
SimpleType.STRING))),
OPTIONAL_INDEX(
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"list_optindex_optional_int",
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V,
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_list_optindex_optional_int",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.LIST_TYPE_V),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"map_optindex_optional_value",
Types.OPTIONAL_TYPE_V,
Types.MAP_TYPE_KV,
Types.PARAM_TYPE_K),
CelOverloadDecl.newGlobalOverload(
"optional_map_optindex_optional_value",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.MAP_TYPE_KV),
Types.PARAM_TYPE_K))),
OPTIONAL_INDEX_OPERAND(
CelFunctionDecl.newFunctionDeclaration(
Operator.INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_list_index_int",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.LIST_TYPE_V),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_map_index_value",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.MAP_TYPE_KV),
Types.PARAM_TYPE_K)));

private final CelFunctionDecl celFunctionDecl;

OptionalDeclaration(CelFunctionDecl celFunctionDecl) {
this.celFunctionDecl = celFunctionDecl;
}

@Override
public CelFunctionDecl functionDecl() {
return celFunctionDecl;
}
}

private static final CelExtensionLibrary<CelOptionalLibrary> LIBRARY =
new CelExtensionLibrary<CelOptionalLibrary>() {
final TypeParamType paramTypeK = TypeParamType.create("K");
final TypeParamType paramTypeV = TypeParamType.create("V");
final OptionalType optionalTypeV = OptionalType.create(paramTypeV);
final ListType listTypeV = ListType.create(paramTypeV);
final MapType mapTypeKv = MapType.create(paramTypeK, paramTypeV);

private final CelOptionalLibrary version0 =
new CelOptionalLibrary(
0,
ImmutableSet.of(
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_OF.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_of", optionalTypeV, paramTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_OF_NON_ZERO_VALUE.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_ofNonZeroValue", optionalTypeV, paramTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_NONE.getFunction(),
CelOverloadDecl.newGlobalOverload("optional_none", optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_value", paramTypeV, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
HAS_VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_hasValue", SimpleType.BOOL, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_UNWRAP.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_unwrap_list", listTypeV, ListType.create(optionalTypeV))),
OptionalDeclaration.OPTIONAL_OF.functionDecl(),
OptionalDeclaration.OPTIONAL_OF_NON_ZERO_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_NONE.functionDecl(),
OptionalDeclaration.OPTIONAL_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_HAS_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_UNWRAP.functionDecl(),
// Note: Implementation of "or" and "orValue" are special-cased inside the
// interpreter. Hence, their bindings are not provided here.
CelFunctionDecl.newFunctionDeclaration(
"or",
CelOverloadDecl.newMemberOverload(
"optional_or_optional", optionalTypeV, optionalTypeV, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
"orValue",
CelOverloadDecl.newMemberOverload(
"optional_orValue_value", paramTypeV, optionalTypeV, paramTypeV)),
OptionalDeclaration.OPTIONAL_OR.functionDecl(),
OptionalDeclaration.OPTIONAL_OR_VALUE.functionDecl(),
// Note: Function bindings for optional field selection and indexer is defined
// in {@code StandardFunctions}.
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_SELECT.getFunction(),
CelOverloadDecl.newGlobalOverload(
"select_optional_field",
optionalTypeV,
SimpleType.DYN,
SimpleType.STRING)),
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"list_optindex_optional_int", optionalTypeV, listTypeV, SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_list_optindex_optional_int",
optionalTypeV,
OptionalType.create(listTypeV),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"map_optindex_optional_value", optionalTypeV, mapTypeKv, paramTypeK),
CelOverloadDecl.newGlobalOverload(
"optional_map_optindex_optional_value",
optionalTypeV,
OptionalType.create(mapTypeKv),
paramTypeK)),
OptionalDeclaration.OPTIONAL_SELECT.functionDecl(),
OptionalDeclaration.OPTIONAL_INDEX.functionDecl(),
// Index overloads to accommodate using an optional value as the operand
CelFunctionDecl.newFunctionDeclaration(
Operator.INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_list_index_int",
optionalTypeV,
OptionalType.create(listTypeV),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_map_index_value",
optionalTypeV,
OptionalType.create(mapTypeKv),
paramTypeK))),
OptionalDeclaration.OPTIONAL_INDEX_OPERAND.functionDecl()),
ImmutableSet.of(
CelMacro.newReceiverMacro("optMap", 2, CelOptionalLibrary::expandOptMap)),
ImmutableSet.of(
// Type declaration for optional_type -> type(optional_type(V))
CelVarDecl.newVarDeclaration(
OptionalType.NAME, TypeType.create(optionalTypeV))));
OptionalType.NAME, TypeType.create(Types.OPTIONAL_TYPE_V))));

private final CelOptionalLibrary version1 =
new CelOptionalLibrary(
Expand All@@ -211,16 +264,16 @@ public String getFunction() {
"optional_list_first",
"Return the first value in a list if present, otherwise"
+ " optional.none()",
optionalTypeV,
listTypeV)),
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V)),
CelFunctionDecl.newFunctionDeclaration(
LAST.functionName,
CelOverloadDecl.newMemberOverload(
"optional_list_last",
"Return the last value in a list if present, otherwise"
+ " optional.none()",
optionalTypeV,
listTypeV)))
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V)))
.build(),
version1.macros,
version1.variables);
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -243,7 +243,9 @@ private TranslatedValue translateOperatorCall(
// by our axioms
return TranslatedValue.propagateStrict(ctx, typeSystem, typeSystem.mkError(), args);
case INDEX:
return translateIndex(args, ast);
return translateIndex(args, ast, false);
case OPTIONAL_INDEX:
return translateIndex(args, ast, true);
case CONDITIONAL:
return translateConditional(args, ast);
case NOT_STRICTLY_FALSE:
Expand DownExpand Up@@ -600,7 +602,8 @@ private TranslatedValue translateEquality(
.withApproximation(ctx.mkFalse());
}

private Expr<?> buildListIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard) {
private Expr<?> buildListIndex(
Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard, boolean isOptional) {
Expr<?> listRef = typeSystem.getListRef(lhsTrans);
SeqExpr<?> seq = typeSystem.getSeq(listRef);
Expr<?> index = typeSystem.getInt(rhsTrans);
Expand All@@ -617,6 +620,14 @@ private Expr<?> buildListIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr type
constraintSink.accept(ctx.mkImplies(ctx.mkAnd(typeGuard, inBounds), valNotUnknown));
}

if (isOptional) {
Expr<?> resultOptRef = ctx.mkApp(typeSystem.optionalOfRefFunc(), val);
constraintSink.accept(ctx.mkEq(typeSystem.getOptionalValue(resultOptRef), val));
constraintSink.accept(typeSystem.optHasValue(resultOptRef));
return ctx.mkITE(
inBounds, typeSystem.mkOptionalOf(resultOptRef), typeSystem.mkOptionalNone());
}

return ctx.mkITE(inBounds, val, typeSystem.mkError());
}

Expand DownExpand Up@@ -677,7 +688,8 @@ private ProbeResult createProbeResult(
return new ProbeResult(altInMap, altVal);
}

private Expr<?> buildMapIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard) {
private Expr<?> buildMapIndex(
Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard, boolean isOptional) {
Expr<?> mapRef = typeSystem.getMapRef(lhsTrans);
ArrayExpr mapValues = (ArrayExpr) typeSystem.getMapValues(mapRef);
ArrayExpr mapPresence = (ArrayExpr) typeSystem.getMapPresence(mapRef);
Expand DownExpand Up@@ -780,10 +792,19 @@ private Expr<?> buildMapIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeG
constraintSink.accept(ctx.mkImplies(ctx.mkAnd(typeGuard, finalInMap), valNotUnknown));
}

if (isOptional) {
Expr<?> resultOptRef = ctx.mkApp(typeSystem.optionalOfRefFunc(), finalVal);
constraintSink.accept(ctx.mkEq(typeSystem.getOptionalValue(resultOptRef), finalVal));
constraintSink.accept(typeSystem.optHasValue(resultOptRef));
return ctx.mkITE(
finalInMap, typeSystem.mkOptionalOf(resultOptRef), typeSystem.mkOptionalNone());
}

return ctx.mkITE(finalInMap, finalVal, typeSystem.mkError());
}

private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSyntaxTree ast) {
private TranslatedValue translateIndex(
List<TranslatedValue> args, CelAbstractSyntaxTree ast, boolean isOptional) {
Expr<?> lhsTrans = args.get(0).z3Expr();
Expr<?> rhsTrans = args.get(1).z3Expr();

Expand All@@ -794,13 +815,13 @@ private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSy

Expr<?> actualValue;
if (lhsType.kind() == CelKind.LIST && rhsType.kind() == CelKind.INT) {
actualValue = buildListIndex(lhsTrans, rhsTrans, ctx.mkTrue());
actualValue = buildListIndex(lhsTrans, rhsTrans, ctx.mkTrue(), isOptional);
constraintSink.accept(
ctx.mkImplies(
ctx.mkNot(typeSystem.isError(actualValue)),
typeConstraintGenerator.apply(actualValue, ((ListType) lhsType).elemType())));
} else if (lhsType.kind() == CelKind.MAP) {
actualValue = buildMapIndex(lhsTrans, rhsTrans, ctx.mkTrue());
actualValue = buildMapIndex(lhsTrans, rhsTrans, ctx.mkTrue(), isOptional);
constraintSink.accept(
ctx.mkImplies(
ctx.mkNot(typeSystem.isError(actualValue)),
Expand All@@ -810,8 +831,8 @@ private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSy
BoolExpr isMapGuard = typeSystem.isMap(lhsTrans);
actualValue =
CelZ3TypeSystem.SwitchBuilder.newBuilder(ctx)
.addCase(isListGuard, buildListIndex(lhsTrans, rhsTrans, isListGuard))
.addCase(isMapGuard, buildMapIndex(lhsTrans, rhsTrans, isMapGuard))
.addCase(isListGuard, buildListIndex(lhsTrans, rhsTrans, isListGuard, isOptional))
.addCase(isMapGuard, buildMapIndex(lhsTrans, rhsTrans, isMapGuard, isOptional))
.build(typeSystem.mkError());
}

Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -55,7 +55,7 @@ public final class CelStandardDeclarations {
private final ImmutableSet<CelIdentDecl> celIdentDecls;

/** Enumeration of Standard Functions. */
public enum StandardFunction {
public enum StandardFunction implements CelFunctionDecl.Declarer {
// Deprecated - use {@link #IN}
OLD_IN(
true,
Expand DownExpand Up@@ -1504,6 +1504,7 @@ private CelFunctionDecl withOverloads(Iterable<StandardOverload> overloads) {
return newCelFunctionDecl(functionName, ImmutableSet.copyOf(overloads));
}

@Override
public CelFunctionDecl functionDecl() {
return celFunctionDecl;
}
Expand DownExpand Up@@ -1579,8 +1580,14 @@ public CelIdentDecl identDecl() {

/** General interface for defining a standard function overload. */
@Immutable
public interface StandardOverload {
public interface StandardOverload extends CelFunctionDecl.Declarer {
CelOverloadDecl celOverloadDecl();

@Override
default CelFunctionDecl functionDecl() {
// TODO: Remove default keyword by implementing this for all standard overloads
throw new UnsupportedOperationException("Unimplemented");
}
}

/** Set of all standard function names. */
Expand Down
6 changes: 6 additions & 0 deletions common/src/main/java/dev/cel/common/CelFunctionDecl.java
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,6 +38,12 @@ public abstract class CelFunctionDecl {
/** Required. List of function overloads. Must contain at least one overload. */
public abstract ImmutableSet<CelOverloadDecl> overloads();

/** General interface for defining an extension function overload or standard declaration. */
@Immutable
public interface Declarer {
CelFunctionDecl functionDecl();
}

/** Builder for configuring the {@link CelFunctionDecl}. */
@AutoValue.Builder
public abstract static class Builder {
Expand Down
4 changes: 3 additions & 1 deletion extensions/src/main/java/dev/cel/extensions/BUILD.bazel
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,7 +13,9 @@ package(

java_library(
name = "extension_library",
srcs = ["CelExtensionLibrary.java"],
srcs = [
"CelExtensionLibrary.java",
],
tags = [
],
deps = [
Expand Down
207 changes: 130 additions & 77 deletions extensions/src/main/java/dev/cel/extensions/CelOptionalLibrary.java
Original file line numberDiff line numberDiff line change
Expand Up@@ -97,95 +97,148 @@ public String getFunction() {
}
}

private static final class Types {
private static final TypeParamType PARAM_TYPE_K = TypeParamType.create("K");
private static final TypeParamType PARAM_TYPE_V = TypeParamType.create("V");
private static final OptionalType OPTIONAL_TYPE_V = OptionalType.create(PARAM_TYPE_V);
private static final ListType LIST_TYPE_V = ListType.create(PARAM_TYPE_V);
private static final MapType MAP_TYPE_KV = MapType.create(PARAM_TYPE_K, PARAM_TYPE_V);
}

/** Declarations for the optional extension library. */
public enum OptionalDeclaration implements CelFunctionDecl.Declarer {
OPTIONAL_OF(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_OF.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_of", Types.OPTIONAL_TYPE_V, Types.PARAM_TYPE_V))),
OPTIONAL_OF_NON_ZERO_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_OF_NON_ZERO_VALUE.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_ofNonZeroValue", Types.OPTIONAL_TYPE_V, Types.PARAM_TYPE_V))),
OPTIONAL_NONE(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_NONE.getFunction(),
CelOverloadDecl.newGlobalOverload("optional_none", Types.OPTIONAL_TYPE_V))),
OPTIONAL_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_value", Types.PARAM_TYPE_V, Types.OPTIONAL_TYPE_V))),
OPTIONAL_HAS_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.HAS_VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_hasValue", SimpleType.BOOL, Types.OPTIONAL_TYPE_V))),
OPTIONAL_UNWRAP(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_UNWRAP.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_unwrap_list",
Types.LIST_TYPE_V,
ListType.create(Types.OPTIONAL_TYPE_V)))),
OPTIONAL_OR(
CelFunctionDecl.newFunctionDeclaration(
"or",
CelOverloadDecl.newMemberOverload(
"optional_or_optional",
Types.OPTIONAL_TYPE_V,
Types.OPTIONAL_TYPE_V,
Types.OPTIONAL_TYPE_V))),
OPTIONAL_OR_VALUE(
CelFunctionDecl.newFunctionDeclaration(
"orValue",
CelOverloadDecl.newMemberOverload(
"optional_orValue_value",
Types.PARAM_TYPE_V,
Types.OPTIONAL_TYPE_V,
Types.PARAM_TYPE_V))),
OPTIONAL_SELECT(
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_SELECT.getFunction(),
CelOverloadDecl.newGlobalOverload(
"select_optional_field",
Types.OPTIONAL_TYPE_V,
SimpleType.DYN,
SimpleType.STRING))),
OPTIONAL_INDEX(
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"list_optindex_optional_int",
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V,
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_list_optindex_optional_int",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.LIST_TYPE_V),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"map_optindex_optional_value",
Types.OPTIONAL_TYPE_V,
Types.MAP_TYPE_KV,
Types.PARAM_TYPE_K),
CelOverloadDecl.newGlobalOverload(
"optional_map_optindex_optional_value",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.MAP_TYPE_KV),
Types.PARAM_TYPE_K))),
OPTIONAL_INDEX_OPERAND(
CelFunctionDecl.newFunctionDeclaration(
Operator.INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_list_index_int",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.LIST_TYPE_V),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_map_index_value",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.MAP_TYPE_KV),
Types.PARAM_TYPE_K)));

private final CelFunctionDecl celFunctionDecl;

OptionalDeclaration(CelFunctionDecl celFunctionDecl) {
this.celFunctionDecl = celFunctionDecl;
}

@Override
public CelFunctionDecl functionDecl() {
return celFunctionDecl;
}
}

private static final CelExtensionLibrary<CelOptionalLibrary> LIBRARY =
new CelExtensionLibrary<CelOptionalLibrary>() {
final TypeParamType paramTypeK = TypeParamType.create("K");
final TypeParamType paramTypeV = TypeParamType.create("V");
final OptionalType optionalTypeV = OptionalType.create(paramTypeV);
final ListType listTypeV = ListType.create(paramTypeV);
final MapType mapTypeKv = MapType.create(paramTypeK, paramTypeV);

private final CelOptionalLibrary version0 =
new CelOptionalLibrary(
0,
ImmutableSet.of(
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_OF.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_of", optionalTypeV, paramTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_OF_NON_ZERO_VALUE.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_ofNonZeroValue", optionalTypeV, paramTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_NONE.getFunction(),
CelOverloadDecl.newGlobalOverload("optional_none", optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_value", paramTypeV, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
HAS_VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_hasValue", SimpleType.BOOL, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_UNWRAP.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_unwrap_list", listTypeV, ListType.create(optionalTypeV))),
OptionalDeclaration.OPTIONAL_OF.functionDecl(),
OptionalDeclaration.OPTIONAL_OF_NON_ZERO_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_NONE.functionDecl(),
OptionalDeclaration.OPTIONAL_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_HAS_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_UNWRAP.functionDecl(),
// Note: Implementation of "or" and "orValue" are special-cased inside the
// interpreter. Hence, their bindings are not provided here.
CelFunctionDecl.newFunctionDeclaration(
"or",
CelOverloadDecl.newMemberOverload(
"optional_or_optional", optionalTypeV, optionalTypeV, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
"orValue",
CelOverloadDecl.newMemberOverload(
"optional_orValue_value", paramTypeV, optionalTypeV, paramTypeV)),
OptionalDeclaration.OPTIONAL_OR.functionDecl(),
OptionalDeclaration.OPTIONAL_OR_VALUE.functionDecl(),
// Note: Function bindings for optional field selection and indexer is defined
// in {@code StandardFunctions}.
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_SELECT.getFunction(),
CelOverloadDecl.newGlobalOverload(
"select_optional_field",
optionalTypeV,
SimpleType.DYN,
SimpleType.STRING)),
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"list_optindex_optional_int", optionalTypeV, listTypeV, SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_list_optindex_optional_int",
optionalTypeV,
OptionalType.create(listTypeV),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"map_optindex_optional_value", optionalTypeV, mapTypeKv, paramTypeK),
CelOverloadDecl.newGlobalOverload(
"optional_map_optindex_optional_value",
optionalTypeV,
OptionalType.create(mapTypeKv),
paramTypeK)),
OptionalDeclaration.OPTIONAL_SELECT.functionDecl(),
OptionalDeclaration.OPTIONAL_INDEX.functionDecl(),
// Index overloads to accommodate using an optional value as the operand
CelFunctionDecl.newFunctionDeclaration(
Operator.INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_list_index_int",
optionalTypeV,
OptionalType.create(listTypeV),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_map_index_value",
optionalTypeV,
OptionalType.create(mapTypeKv),
paramTypeK))),
OptionalDeclaration.OPTIONAL_INDEX_OPERAND.functionDecl()),
ImmutableSet.of(
CelMacro.newReceiverMacro("optMap", 2, CelOptionalLibrary::expandOptMap)),
ImmutableSet.of(
// Type declaration for optional_type -> type(optional_type(V))
CelVarDecl.newVarDeclaration(
OptionalType.NAME, TypeType.create(optionalTypeV))));
OptionalType.NAME, TypeType.create(Types.OPTIONAL_TYPE_V))));

private final CelOptionalLibrary version1 =
new CelOptionalLibrary(
Expand All@@ -211,16 +264,16 @@ public String getFunction() {
"optional_list_first",
"Return the first value in a list if present, otherwise"
+ " optional.none()",
optionalTypeV,
listTypeV)),
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V)),
CelFunctionDecl.newFunctionDeclaration(
LAST.functionName,
CelOverloadDecl.newMemberOverload(
"optional_list_last",
"Return the last value in a list if present, otherwise"
+ " optional.none()",
optionalTypeV,
listTypeV)))
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V)))
.build(),
version1.macros,
version1.variables);
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -243,7 +243,9 @@ private TranslatedValue translateOperatorCall(
// by our axioms
return TranslatedValue.propagateStrict(ctx, typeSystem, typeSystem.mkError(), args);
case INDEX:
return translateIndex(args, ast);
return translateIndex(args, ast, false);
case OPTIONAL_INDEX:
return translateIndex(args, ast, true);
case CONDITIONAL:
return translateConditional(args, ast);
case NOT_STRICTLY_FALSE:
Expand DownExpand Up@@ -600,7 +602,8 @@ private TranslatedValue translateEquality(
.withApproximation(ctx.mkFalse());
}

private Expr<?> buildListIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard) {
private Expr<?> buildListIndex(
Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard, boolean isOptional) {
Expr<?> listRef = typeSystem.getListRef(lhsTrans);
SeqExpr<?> seq = typeSystem.getSeq(listRef);
Expr<?> index = typeSystem.getInt(rhsTrans);
Expand All@@ -617,6 +620,14 @@ private Expr<?> buildListIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr type
constraintSink.accept(ctx.mkImplies(ctx.mkAnd(typeGuard, inBounds), valNotUnknown));
}

if (isOptional) {
Expr<?> resultOptRef = ctx.mkApp(typeSystem.optionalOfRefFunc(), val);
constraintSink.accept(ctx.mkEq(typeSystem.getOptionalValue(resultOptRef), val));
constraintSink.accept(typeSystem.optHasValue(resultOptRef));
return ctx.mkITE(
inBounds, typeSystem.mkOptionalOf(resultOptRef), typeSystem.mkOptionalNone());
}

return ctx.mkITE(inBounds, val, typeSystem.mkError());
}

Expand DownExpand Up@@ -677,7 +688,8 @@ private ProbeResult createProbeResult(
return new ProbeResult(altInMap, altVal);
}

private Expr<?> buildMapIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard) {
private Expr<?> buildMapIndex(
Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard, boolean isOptional) {
Expr<?> mapRef = typeSystem.getMapRef(lhsTrans);
ArrayExpr mapValues = (ArrayExpr) typeSystem.getMapValues(mapRef);
ArrayExpr mapPresence = (ArrayExpr) typeSystem.getMapPresence(mapRef);
Expand DownExpand Up@@ -780,10 +792,19 @@ private Expr<?> buildMapIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeG
constraintSink.accept(ctx.mkImplies(ctx.mkAnd(typeGuard, finalInMap), valNotUnknown));
}

if (isOptional) {
Expr<?> resultOptRef = ctx.mkApp(typeSystem.optionalOfRefFunc(), finalVal);
constraintSink.accept(ctx.mkEq(typeSystem.getOptionalValue(resultOptRef), finalVal));
constraintSink.accept(typeSystem.optHasValue(resultOptRef));
return ctx.mkITE(
finalInMap, typeSystem.mkOptionalOf(resultOptRef), typeSystem.mkOptionalNone());
}

return ctx.mkITE(finalInMap, finalVal, typeSystem.mkError());
}

private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSyntaxTree ast) {
private TranslatedValue translateIndex(
List<TranslatedValue> args, CelAbstractSyntaxTree ast, boolean isOptional) {
Expr<?> lhsTrans = args.get(0).z3Expr();
Expr<?> rhsTrans = args.get(1).z3Expr();

Expand All@@ -794,13 +815,13 @@ private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSy

Expr<?> actualValue;
if (lhsType.kind() == CelKind.LIST && rhsType.kind() == CelKind.INT) {
actualValue = buildListIndex(lhsTrans, rhsTrans, ctx.mkTrue());
actualValue = buildListIndex(lhsTrans, rhsTrans, ctx.mkTrue(), isOptional);
constraintSink.accept(
ctx.mkImplies(
ctx.mkNot(typeSystem.isError(actualValue)),
typeConstraintGenerator.apply(actualValue, ((ListType) lhsType).elemType())));
} else if (lhsType.kind() == CelKind.MAP) {
actualValue = buildMapIndex(lhsTrans, rhsTrans, ctx.mkTrue());
actualValue = buildMapIndex(lhsTrans, rhsTrans, ctx.mkTrue(), isOptional);
constraintSink.accept(
ctx.mkImplies(
ctx.mkNot(typeSystem.isError(actualValue)),
Expand All@@ -810,8 +831,8 @@ private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSy
BoolExpr isMapGuard = typeSystem.isMap(lhsTrans);
actualValue =
CelZ3TypeSystem.SwitchBuilder.newBuilder(ctx)
.addCase(isListGuard, buildListIndex(lhsTrans, rhsTrans, isListGuard))
.addCase(isMapGuard, buildMapIndex(lhsTrans, rhsTrans, isMapGuard))
.addCase(isListGuard, buildListIndex(lhsTrans, rhsTrans, isListGuard, isOptional))
.addCase(isMapGuard, buildMapIndex(lhsTrans, rhsTrans, isMapGuard, isOptional))
.build(typeSystem.mkError());
}

Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -55,7 +55,7 @@ public final class CelStandardDeclarations {
private final ImmutableSet<CelIdentDecl> celIdentDecls;

/** Enumeration of Standard Functions. */
public enum StandardFunction {
public enum StandardFunction implements CelFunctionDecl.Declarer {
// Deprecated - use {@link #IN}
OLD_IN(
true,
Expand DownExpand Up@@ -1504,6 +1504,7 @@ private CelFunctionDecl withOverloads(Iterable<StandardOverload> overloads) {
return newCelFunctionDecl(functionName, ImmutableSet.copyOf(overloads));
}

@Override
public CelFunctionDecl functionDecl() {
return celFunctionDecl;
}
Expand DownExpand Up@@ -1579,8 +1580,14 @@ public CelIdentDecl identDecl() {

/** General interface for defining a standard function overload. */
@Immutable
public interface StandardOverload {
public interface StandardOverload extends CelFunctionDecl.Declarer {
CelOverloadDecl celOverloadDecl();

@Override
default CelFunctionDecl functionDecl() {
// TODO: Remove default keyword by implementing this for all standard overloads
throw new UnsupportedOperationException("Unimplemented");
}
}

/** Set of all standard function names. */
Expand Down
6 changes: 6 additions & 0 deletions common/src/main/java/dev/cel/common/CelFunctionDecl.java
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,6 +38,12 @@ public abstract class CelFunctionDecl {
/** Required. List of function overloads. Must contain at least one overload. */
public abstract ImmutableSet<CelOverloadDecl> overloads();

/** General interface for defining an extension function overload or standard declaration. */
@Immutable
public interface Declarer {
CelFunctionDecl functionDecl();
}

/** Builder for configuring the {@link CelFunctionDecl}. */
@AutoValue.Builder
public abstract static class Builder {
Expand Down
4 changes: 3 additions & 1 deletion extensions/src/main/java/dev/cel/extensions/BUILD.bazel
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,7 +13,9 @@ package(

java_library(
name = "extension_library",
srcs = ["CelExtensionLibrary.java"],
srcs = [
"CelExtensionLibrary.java",
],
tags = [
],
deps = [
Expand Down
207 changes: 130 additions & 77 deletions extensions/src/main/java/dev/cel/extensions/CelOptionalLibrary.java
Original file line numberDiff line numberDiff line change
Expand Up@@ -97,95 +97,148 @@ public String getFunction() {
}
}

private static final class Types {
private static final TypeParamType PARAM_TYPE_K = TypeParamType.create("K");
private static final TypeParamType PARAM_TYPE_V = TypeParamType.create("V");
private static final OptionalType OPTIONAL_TYPE_V = OptionalType.create(PARAM_TYPE_V);
private static final ListType LIST_TYPE_V = ListType.create(PARAM_TYPE_V);
private static final MapType MAP_TYPE_KV = MapType.create(PARAM_TYPE_K, PARAM_TYPE_V);
}

/** Declarations for the optional extension library. */
public enum OptionalDeclaration implements CelFunctionDecl.Declarer {
OPTIONAL_OF(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_OF.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_of", Types.OPTIONAL_TYPE_V, Types.PARAM_TYPE_V))),
OPTIONAL_OF_NON_ZERO_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_OF_NON_ZERO_VALUE.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_ofNonZeroValue", Types.OPTIONAL_TYPE_V, Types.PARAM_TYPE_V))),
OPTIONAL_NONE(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_NONE.getFunction(),
CelOverloadDecl.newGlobalOverload("optional_none", Types.OPTIONAL_TYPE_V))),
OPTIONAL_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_value", Types.PARAM_TYPE_V, Types.OPTIONAL_TYPE_V))),
OPTIONAL_HAS_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.HAS_VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_hasValue", SimpleType.BOOL, Types.OPTIONAL_TYPE_V))),
OPTIONAL_UNWRAP(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_UNWRAP.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_unwrap_list",
Types.LIST_TYPE_V,
ListType.create(Types.OPTIONAL_TYPE_V)))),
OPTIONAL_OR(
CelFunctionDecl.newFunctionDeclaration(
"or",
CelOverloadDecl.newMemberOverload(
"optional_or_optional",
Types.OPTIONAL_TYPE_V,
Types.OPTIONAL_TYPE_V,
Types.OPTIONAL_TYPE_V))),
OPTIONAL_OR_VALUE(
CelFunctionDecl.newFunctionDeclaration(
"orValue",
CelOverloadDecl.newMemberOverload(
"optional_orValue_value",
Types.PARAM_TYPE_V,
Types.OPTIONAL_TYPE_V,
Types.PARAM_TYPE_V))),
OPTIONAL_SELECT(
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_SELECT.getFunction(),
CelOverloadDecl.newGlobalOverload(
"select_optional_field",
Types.OPTIONAL_TYPE_V,
SimpleType.DYN,
SimpleType.STRING))),
OPTIONAL_INDEX(
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"list_optindex_optional_int",
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V,
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_list_optindex_optional_int",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.LIST_TYPE_V),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"map_optindex_optional_value",
Types.OPTIONAL_TYPE_V,
Types.MAP_TYPE_KV,
Types.PARAM_TYPE_K),
CelOverloadDecl.newGlobalOverload(
"optional_map_optindex_optional_value",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.MAP_TYPE_KV),
Types.PARAM_TYPE_K))),
OPTIONAL_INDEX_OPERAND(
CelFunctionDecl.newFunctionDeclaration(
Operator.INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_list_index_int",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.LIST_TYPE_V),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_map_index_value",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.MAP_TYPE_KV),
Types.PARAM_TYPE_K)));

private final CelFunctionDecl celFunctionDecl;

OptionalDeclaration(CelFunctionDecl celFunctionDecl) {
this.celFunctionDecl = celFunctionDecl;
}

@Override
public CelFunctionDecl functionDecl() {
return celFunctionDecl;
}
}

private static final CelExtensionLibrary<CelOptionalLibrary> LIBRARY =
new CelExtensionLibrary<CelOptionalLibrary>() {
final TypeParamType paramTypeK = TypeParamType.create("K");
final TypeParamType paramTypeV = TypeParamType.create("V");
final OptionalType optionalTypeV = OptionalType.create(paramTypeV);
final ListType listTypeV = ListType.create(paramTypeV);
final MapType mapTypeKv = MapType.create(paramTypeK, paramTypeV);

private final CelOptionalLibrary version0 =
new CelOptionalLibrary(
0,
ImmutableSet.of(
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_OF.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_of", optionalTypeV, paramTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_OF_NON_ZERO_VALUE.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_ofNonZeroValue", optionalTypeV, paramTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_NONE.getFunction(),
CelOverloadDecl.newGlobalOverload("optional_none", optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_value", paramTypeV, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
HAS_VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_hasValue", SimpleType.BOOL, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_UNWRAP.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_unwrap_list", listTypeV, ListType.create(optionalTypeV))),
OptionalDeclaration.OPTIONAL_OF.functionDecl(),
OptionalDeclaration.OPTIONAL_OF_NON_ZERO_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_NONE.functionDecl(),
OptionalDeclaration.OPTIONAL_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_HAS_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_UNWRAP.functionDecl(),
// Note: Implementation of "or" and "orValue" are special-cased inside the
// interpreter. Hence, their bindings are not provided here.
CelFunctionDecl.newFunctionDeclaration(
"or",
CelOverloadDecl.newMemberOverload(
"optional_or_optional", optionalTypeV, optionalTypeV, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
"orValue",
CelOverloadDecl.newMemberOverload(
"optional_orValue_value", paramTypeV, optionalTypeV, paramTypeV)),
OptionalDeclaration.OPTIONAL_OR.functionDecl(),
OptionalDeclaration.OPTIONAL_OR_VALUE.functionDecl(),
// Note: Function bindings for optional field selection and indexer is defined
// in {@code StandardFunctions}.
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_SELECT.getFunction(),
CelOverloadDecl.newGlobalOverload(
"select_optional_field",
optionalTypeV,
SimpleType.DYN,
SimpleType.STRING)),
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"list_optindex_optional_int", optionalTypeV, listTypeV, SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_list_optindex_optional_int",
optionalTypeV,
OptionalType.create(listTypeV),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"map_optindex_optional_value", optionalTypeV, mapTypeKv, paramTypeK),
CelOverloadDecl.newGlobalOverload(
"optional_map_optindex_optional_value",
optionalTypeV,
OptionalType.create(mapTypeKv),
paramTypeK)),
OptionalDeclaration.OPTIONAL_SELECT.functionDecl(),
OptionalDeclaration.OPTIONAL_INDEX.functionDecl(),
// Index overloads to accommodate using an optional value as the operand
CelFunctionDecl.newFunctionDeclaration(
Operator.INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_list_index_int",
optionalTypeV,
OptionalType.create(listTypeV),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_map_index_value",
optionalTypeV,
OptionalType.create(mapTypeKv),
paramTypeK))),
OptionalDeclaration.OPTIONAL_INDEX_OPERAND.functionDecl()),
ImmutableSet.of(
CelMacro.newReceiverMacro("optMap", 2, CelOptionalLibrary::expandOptMap)),
ImmutableSet.of(
// Type declaration for optional_type -> type(optional_type(V))
CelVarDecl.newVarDeclaration(
OptionalType.NAME, TypeType.create(optionalTypeV))));
OptionalType.NAME, TypeType.create(Types.OPTIONAL_TYPE_V))));

private final CelOptionalLibrary version1 =
new CelOptionalLibrary(
Expand All@@ -211,16 +264,16 @@ public String getFunction() {
"optional_list_first",
"Return the first value in a list if present, otherwise"
+ " optional.none()",
optionalTypeV,
listTypeV)),
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V)),
CelFunctionDecl.newFunctionDeclaration(
LAST.functionName,
CelOverloadDecl.newMemberOverload(
"optional_list_last",
"Return the last value in a list if present, otherwise"
+ " optional.none()",
optionalTypeV,
listTypeV)))
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V)))
.build(),
version1.macros,
version1.variables);
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -243,7 +243,9 @@ private TranslatedValue translateOperatorCall(
// by our axioms
return TranslatedValue.propagateStrict(ctx, typeSystem, typeSystem.mkError(), args);
case INDEX:
return translateIndex(args, ast);
return translateIndex(args, ast, false);
case OPTIONAL_INDEX:
return translateIndex(args, ast, true);
case CONDITIONAL:
return translateConditional(args, ast);
case NOT_STRICTLY_FALSE:
Expand DownExpand Up@@ -600,7 +602,8 @@ private TranslatedValue translateEquality(
.withApproximation(ctx.mkFalse());
}

private Expr<?> buildListIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard) {
private Expr<?> buildListIndex(
Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard, boolean isOptional) {
Expr<?> listRef = typeSystem.getListRef(lhsTrans);
SeqExpr<?> seq = typeSystem.getSeq(listRef);
Expr<?> index = typeSystem.getInt(rhsTrans);
Expand All@@ -617,6 +620,14 @@ private Expr<?> buildListIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr type
constraintSink.accept(ctx.mkImplies(ctx.mkAnd(typeGuard, inBounds), valNotUnknown));
}

if (isOptional) {
Expr<?> resultOptRef = ctx.mkApp(typeSystem.optionalOfRefFunc(), val);
constraintSink.accept(ctx.mkEq(typeSystem.getOptionalValue(resultOptRef), val));
constraintSink.accept(typeSystem.optHasValue(resultOptRef));
return ctx.mkITE(
inBounds, typeSystem.mkOptionalOf(resultOptRef), typeSystem.mkOptionalNone());
}

return ctx.mkITE(inBounds, val, typeSystem.mkError());
}

Expand DownExpand Up@@ -677,7 +688,8 @@ private ProbeResult createProbeResult(
return new ProbeResult(altInMap, altVal);
}

private Expr<?> buildMapIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard) {
private Expr<?> buildMapIndex(
Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard, boolean isOptional) {
Expr<?> mapRef = typeSystem.getMapRef(lhsTrans);
ArrayExpr mapValues = (ArrayExpr) typeSystem.getMapValues(mapRef);
ArrayExpr mapPresence = (ArrayExpr) typeSystem.getMapPresence(mapRef);
Expand DownExpand Up@@ -780,10 +792,19 @@ private Expr<?> buildMapIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeG
constraintSink.accept(ctx.mkImplies(ctx.mkAnd(typeGuard, finalInMap), valNotUnknown));
}

if (isOptional) {
Expr<?> resultOptRef = ctx.mkApp(typeSystem.optionalOfRefFunc(), finalVal);
constraintSink.accept(ctx.mkEq(typeSystem.getOptionalValue(resultOptRef), finalVal));
constraintSink.accept(typeSystem.optHasValue(resultOptRef));
return ctx.mkITE(
finalInMap, typeSystem.mkOptionalOf(resultOptRef), typeSystem.mkOptionalNone());
}

return ctx.mkITE(finalInMap, finalVal, typeSystem.mkError());
}

private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSyntaxTree ast) {
private TranslatedValue translateIndex(
List<TranslatedValue> args, CelAbstractSyntaxTree ast, boolean isOptional) {
Expr<?> lhsTrans = args.get(0).z3Expr();
Expr<?> rhsTrans = args.get(1).z3Expr();

Expand All@@ -794,13 +815,13 @@ private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSy

Expr<?> actualValue;
if (lhsType.kind() == CelKind.LIST && rhsType.kind() == CelKind.INT) {
actualValue = buildListIndex(lhsTrans, rhsTrans, ctx.mkTrue());
actualValue = buildListIndex(lhsTrans, rhsTrans, ctx.mkTrue(), isOptional);
constraintSink.accept(
ctx.mkImplies(
ctx.mkNot(typeSystem.isError(actualValue)),
typeConstraintGenerator.apply(actualValue, ((ListType) lhsType).elemType())));
} else if (lhsType.kind() == CelKind.MAP) {
actualValue = buildMapIndex(lhsTrans, rhsTrans, ctx.mkTrue());
actualValue = buildMapIndex(lhsTrans, rhsTrans, ctx.mkTrue(), isOptional);
constraintSink.accept(
ctx.mkImplies(
ctx.mkNot(typeSystem.isError(actualValue)),
Expand All@@ -810,8 +831,8 @@ private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSy
BoolExpr isMapGuard = typeSystem.isMap(lhsTrans);
actualValue =
CelZ3TypeSystem.SwitchBuilder.newBuilder(ctx)
.addCase(isListGuard, buildListIndex(lhsTrans, rhsTrans, isListGuard))
.addCase(isMapGuard, buildMapIndex(lhsTrans, rhsTrans, isMapGuard))
.addCase(isListGuard, buildListIndex(lhsTrans, rhsTrans, isListGuard, isOptional))
.addCase(isMapGuard, buildMapIndex(lhsTrans, rhsTrans, isMapGuard, isOptional))
.build(typeSystem.mkError());
}

Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -55,7 +55,7 @@ public final class CelStandardDeclarations {
private final ImmutableSet<CelIdentDecl> celIdentDecls;

/** Enumeration of Standard Functions. */
public enum StandardFunction {
public enum StandardFunction implements CelFunctionDecl.Declarer {
// Deprecated - use {@link #IN}
OLD_IN(
true,
Expand DownExpand Up@@ -1504,6 +1504,7 @@ private CelFunctionDecl withOverloads(Iterable<StandardOverload> overloads) {
return newCelFunctionDecl(functionName, ImmutableSet.copyOf(overloads));
}

@Override
public CelFunctionDecl functionDecl() {
return celFunctionDecl;
}
Expand DownExpand Up@@ -1579,8 +1580,14 @@ public CelIdentDecl identDecl() {

/** General interface for defining a standard function overload. */
@Immutable
public interface StandardOverload {
public interface StandardOverload extends CelFunctionDecl.Declarer {
CelOverloadDecl celOverloadDecl();

@Override
default CelFunctionDecl functionDecl() {
// TODO: Remove default keyword by implementing this for all standard overloads
throw new UnsupportedOperationException("Unimplemented");
}
}

/** Set of all standard function names. */
Expand Down
6 changes: 6 additions & 0 deletions common/src/main/java/dev/cel/common/CelFunctionDecl.java
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,6 +38,12 @@ public abstract class CelFunctionDecl {
/** Required. List of function overloads. Must contain at least one overload. */
public abstract ImmutableSet<CelOverloadDecl> overloads();

/** General interface for defining an extension function overload or standard declaration. */
@Immutable
public interface Declarer {
CelFunctionDecl functionDecl();
}

/** Builder for configuring the {@link CelFunctionDecl}. */
@AutoValue.Builder
public abstract static class Builder {
Expand Down
4 changes: 3 additions & 1 deletion extensions/src/main/java/dev/cel/extensions/BUILD.bazel
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,7 +13,9 @@ package(

java_library(
name = "extension_library",
srcs = ["CelExtensionLibrary.java"],
srcs = [
"CelExtensionLibrary.java",
],
tags = [
],
deps = [
Expand Down
207 changes: 130 additions & 77 deletions extensions/src/main/java/dev/cel/extensions/CelOptionalLibrary.java
Original file line numberDiff line numberDiff line change
Expand Up@@ -97,95 +97,148 @@ public String getFunction() {
}
}

private static final class Types {
private static final TypeParamType PARAM_TYPE_K = TypeParamType.create("K");
private static final TypeParamType PARAM_TYPE_V = TypeParamType.create("V");
private static final OptionalType OPTIONAL_TYPE_V = OptionalType.create(PARAM_TYPE_V);
private static final ListType LIST_TYPE_V = ListType.create(PARAM_TYPE_V);
private static final MapType MAP_TYPE_KV = MapType.create(PARAM_TYPE_K, PARAM_TYPE_V);
}

/** Declarations for the optional extension library. */
public enum OptionalDeclaration implements CelFunctionDecl.Declarer {
OPTIONAL_OF(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_OF.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_of", Types.OPTIONAL_TYPE_V, Types.PARAM_TYPE_V))),
OPTIONAL_OF_NON_ZERO_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_OF_NON_ZERO_VALUE.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_ofNonZeroValue", Types.OPTIONAL_TYPE_V, Types.PARAM_TYPE_V))),
OPTIONAL_NONE(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_NONE.getFunction(),
CelOverloadDecl.newGlobalOverload("optional_none", Types.OPTIONAL_TYPE_V))),
OPTIONAL_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_value", Types.PARAM_TYPE_V, Types.OPTIONAL_TYPE_V))),
OPTIONAL_HAS_VALUE(
CelFunctionDecl.newFunctionDeclaration(
Function.HAS_VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_hasValue", SimpleType.BOOL, Types.OPTIONAL_TYPE_V))),
OPTIONAL_UNWRAP(
CelFunctionDecl.newFunctionDeclaration(
Function.OPTIONAL_UNWRAP.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_unwrap_list",
Types.LIST_TYPE_V,
ListType.create(Types.OPTIONAL_TYPE_V)))),
OPTIONAL_OR(
CelFunctionDecl.newFunctionDeclaration(
"or",
CelOverloadDecl.newMemberOverload(
"optional_or_optional",
Types.OPTIONAL_TYPE_V,
Types.OPTIONAL_TYPE_V,
Types.OPTIONAL_TYPE_V))),
OPTIONAL_OR_VALUE(
CelFunctionDecl.newFunctionDeclaration(
"orValue",
CelOverloadDecl.newMemberOverload(
"optional_orValue_value",
Types.PARAM_TYPE_V,
Types.OPTIONAL_TYPE_V,
Types.PARAM_TYPE_V))),
OPTIONAL_SELECT(
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_SELECT.getFunction(),
CelOverloadDecl.newGlobalOverload(
"select_optional_field",
Types.OPTIONAL_TYPE_V,
SimpleType.DYN,
SimpleType.STRING))),
OPTIONAL_INDEX(
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"list_optindex_optional_int",
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V,
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_list_optindex_optional_int",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.LIST_TYPE_V),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"map_optindex_optional_value",
Types.OPTIONAL_TYPE_V,
Types.MAP_TYPE_KV,
Types.PARAM_TYPE_K),
CelOverloadDecl.newGlobalOverload(
"optional_map_optindex_optional_value",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.MAP_TYPE_KV),
Types.PARAM_TYPE_K))),
OPTIONAL_INDEX_OPERAND(
CelFunctionDecl.newFunctionDeclaration(
Operator.INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_list_index_int",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.LIST_TYPE_V),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_map_index_value",
Types.OPTIONAL_TYPE_V,
OptionalType.create(Types.MAP_TYPE_KV),
Types.PARAM_TYPE_K)));

private final CelFunctionDecl celFunctionDecl;

OptionalDeclaration(CelFunctionDecl celFunctionDecl) {
this.celFunctionDecl = celFunctionDecl;
}

@Override
public CelFunctionDecl functionDecl() {
return celFunctionDecl;
}
}

private static final CelExtensionLibrary<CelOptionalLibrary> LIBRARY =
new CelExtensionLibrary<CelOptionalLibrary>() {
final TypeParamType paramTypeK = TypeParamType.create("K");
final TypeParamType paramTypeV = TypeParamType.create("V");
final OptionalType optionalTypeV = OptionalType.create(paramTypeV);
final ListType listTypeV = ListType.create(paramTypeV);
final MapType mapTypeKv = MapType.create(paramTypeK, paramTypeV);

private final CelOptionalLibrary version0 =
new CelOptionalLibrary(
0,
ImmutableSet.of(
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_OF.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_of", optionalTypeV, paramTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_OF_NON_ZERO_VALUE.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_ofNonZeroValue", optionalTypeV, paramTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_NONE.getFunction(),
CelOverloadDecl.newGlobalOverload("optional_none", optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_value", paramTypeV, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
HAS_VALUE.getFunction(),
CelOverloadDecl.newMemberOverload(
"optional_hasValue", SimpleType.BOOL, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
OPTIONAL_UNWRAP.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_unwrap_list", listTypeV, ListType.create(optionalTypeV))),
OptionalDeclaration.OPTIONAL_OF.functionDecl(),
OptionalDeclaration.OPTIONAL_OF_NON_ZERO_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_NONE.functionDecl(),
OptionalDeclaration.OPTIONAL_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_HAS_VALUE.functionDecl(),
OptionalDeclaration.OPTIONAL_UNWRAP.functionDecl(),
// Note: Implementation of "or" and "orValue" are special-cased inside the
// interpreter. Hence, their bindings are not provided here.
CelFunctionDecl.newFunctionDeclaration(
"or",
CelOverloadDecl.newMemberOverload(
"optional_or_optional", optionalTypeV, optionalTypeV, optionalTypeV)),
CelFunctionDecl.newFunctionDeclaration(
"orValue",
CelOverloadDecl.newMemberOverload(
"optional_orValue_value", paramTypeV, optionalTypeV, paramTypeV)),
OptionalDeclaration.OPTIONAL_OR.functionDecl(),
OptionalDeclaration.OPTIONAL_OR_VALUE.functionDecl(),
// Note: Function bindings for optional field selection and indexer is defined
// in {@code StandardFunctions}.
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_SELECT.getFunction(),
CelOverloadDecl.newGlobalOverload(
"select_optional_field",
optionalTypeV,
SimpleType.DYN,
SimpleType.STRING)),
CelFunctionDecl.newFunctionDeclaration(
Operator.OPTIONAL_INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"list_optindex_optional_int", optionalTypeV, listTypeV, SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_list_optindex_optional_int",
optionalTypeV,
OptionalType.create(listTypeV),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"map_optindex_optional_value", optionalTypeV, mapTypeKv, paramTypeK),
CelOverloadDecl.newGlobalOverload(
"optional_map_optindex_optional_value",
optionalTypeV,
OptionalType.create(mapTypeKv),
paramTypeK)),
OptionalDeclaration.OPTIONAL_SELECT.functionDecl(),
OptionalDeclaration.OPTIONAL_INDEX.functionDecl(),
// Index overloads to accommodate using an optional value as the operand
CelFunctionDecl.newFunctionDeclaration(
Operator.INDEX.getFunction(),
CelOverloadDecl.newGlobalOverload(
"optional_list_index_int",
optionalTypeV,
OptionalType.create(listTypeV),
SimpleType.INT),
CelOverloadDecl.newGlobalOverload(
"optional_map_index_value",
optionalTypeV,
OptionalType.create(mapTypeKv),
paramTypeK))),
OptionalDeclaration.OPTIONAL_INDEX_OPERAND.functionDecl()),
ImmutableSet.of(
CelMacro.newReceiverMacro("optMap", 2, CelOptionalLibrary::expandOptMap)),
ImmutableSet.of(
// Type declaration for optional_type -> type(optional_type(V))
CelVarDecl.newVarDeclaration(
OptionalType.NAME, TypeType.create(optionalTypeV))));
OptionalType.NAME, TypeType.create(Types.OPTIONAL_TYPE_V))));

private final CelOptionalLibrary version1 =
new CelOptionalLibrary(
Expand All@@ -211,16 +264,16 @@ public String getFunction() {
"optional_list_first",
"Return the first value in a list if present, otherwise"
+ " optional.none()",
optionalTypeV,
listTypeV)),
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V)),
CelFunctionDecl.newFunctionDeclaration(
LAST.functionName,
CelOverloadDecl.newMemberOverload(
"optional_list_last",
"Return the last value in a list if present, otherwise"
+ " optional.none()",
optionalTypeV,
listTypeV)))
Types.OPTIONAL_TYPE_V,
Types.LIST_TYPE_V)))
.build(),
version1.macros,
version1.variables);
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -243,7 +243,9 @@ private TranslatedValue translateOperatorCall(
// by our axioms
return TranslatedValue.propagateStrict(ctx, typeSystem, typeSystem.mkError(), args);
case INDEX:
return translateIndex(args, ast);
return translateIndex(args, ast, false);
case OPTIONAL_INDEX:
return translateIndex(args, ast, true);
case CONDITIONAL:
return translateConditional(args, ast);
case NOT_STRICTLY_FALSE:
Expand DownExpand Up@@ -600,7 +602,8 @@ private TranslatedValue translateEquality(
.withApproximation(ctx.mkFalse());
}

private Expr<?> buildListIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard) {
private Expr<?> buildListIndex(
Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard, boolean isOptional) {
Expr<?> listRef = typeSystem.getListRef(lhsTrans);
SeqExpr<?> seq = typeSystem.getSeq(listRef);
Expr<?> index = typeSystem.getInt(rhsTrans);
Expand All@@ -617,6 +620,14 @@ private Expr<?> buildListIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr type
constraintSink.accept(ctx.mkImplies(ctx.mkAnd(typeGuard, inBounds), valNotUnknown));
}

if (isOptional) {
Expr<?> resultOptRef = ctx.mkApp(typeSystem.optionalOfRefFunc(), val);
constraintSink.accept(ctx.mkEq(typeSystem.getOptionalValue(resultOptRef), val));
constraintSink.accept(typeSystem.optHasValue(resultOptRef));
return ctx.mkITE(
inBounds, typeSystem.mkOptionalOf(resultOptRef), typeSystem.mkOptionalNone());
}

return ctx.mkITE(inBounds, val, typeSystem.mkError());
}

Expand DownExpand Up@@ -677,7 +688,8 @@ private ProbeResult createProbeResult(
return new ProbeResult(altInMap, altVal);
}

private Expr<?> buildMapIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard) {
private Expr<?> buildMapIndex(
Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeGuard, boolean isOptional) {
Expr<?> mapRef = typeSystem.getMapRef(lhsTrans);
ArrayExpr mapValues = (ArrayExpr) typeSystem.getMapValues(mapRef);
ArrayExpr mapPresence = (ArrayExpr) typeSystem.getMapPresence(mapRef);
Expand DownExpand Up@@ -780,10 +792,19 @@ private Expr<?> buildMapIndex(Expr<?> lhsTrans, Expr<?> rhsTrans, BoolExpr typeG
constraintSink.accept(ctx.mkImplies(ctx.mkAnd(typeGuard, finalInMap), valNotUnknown));
}

if (isOptional) {
Expr<?> resultOptRef = ctx.mkApp(typeSystem.optionalOfRefFunc(), finalVal);
constraintSink.accept(ctx.mkEq(typeSystem.getOptionalValue(resultOptRef), finalVal));
constraintSink.accept(typeSystem.optHasValue(resultOptRef));
return ctx.mkITE(
finalInMap, typeSystem.mkOptionalOf(resultOptRef), typeSystem.mkOptionalNone());
}

return ctx.mkITE(finalInMap, finalVal, typeSystem.mkError());
}

private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSyntaxTree ast) {
private TranslatedValue translateIndex(
List<TranslatedValue> args, CelAbstractSyntaxTree ast, boolean isOptional) {
Expr<?> lhsTrans = args.get(0).z3Expr();
Expr<?> rhsTrans = args.get(1).z3Expr();

Expand All@@ -794,13 +815,13 @@ private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSy

Expr<?> actualValue;
if (lhsType.kind() == CelKind.LIST && rhsType.kind() == CelKind.INT) {
actualValue = buildListIndex(lhsTrans, rhsTrans, ctx.mkTrue());
actualValue = buildListIndex(lhsTrans, rhsTrans, ctx.mkTrue(), isOptional);
constraintSink.accept(
ctx.mkImplies(
ctx.mkNot(typeSystem.isError(actualValue)),
typeConstraintGenerator.apply(actualValue, ((ListType) lhsType).elemType())));
} else if (lhsType.kind() == CelKind.MAP) {
actualValue = buildMapIndex(lhsTrans, rhsTrans, ctx.mkTrue());
actualValue = buildMapIndex(lhsTrans, rhsTrans, ctx.mkTrue(), isOptional);
constraintSink.accept(
ctx.mkImplies(
ctx.mkNot(typeSystem.isError(actualValue)),
Expand All@@ -810,8 +831,8 @@ private TranslatedValue translateIndex(List<TranslatedValue> args, CelAbstractSy
BoolExpr isMapGuard = typeSystem.isMap(lhsTrans);
actualValue =
CelZ3TypeSystem.SwitchBuilder.newBuilder(ctx)
.addCase(isListGuard, buildListIndex(lhsTrans, rhsTrans, isListGuard))
.addCase(isMapGuard, buildMapIndex(lhsTrans, rhsTrans, isMapGuard))
.addCase(isListGuard, buildListIndex(lhsTrans, rhsTrans, isListGuard, isOptional))
.addCase(isMapGuard, buildMapIndex(lhsTrans, rhsTrans, isMapGuard, isOptional))
.build(typeSystem.mkError());
}

Expand Down
Loading
Loading