Search in sources :

Example 16 with Type

use of com.google.api.expr.v1alpha1.Type in project cel-java by projectnessie.

the class Checker method checkCreateList.

void checkCreateList(Expr.Builder e) {
    CreateList.Builder create = e.getListExprBuilder();
    Type elemType = null;
    for (int i = 0; i < create.getElementsBuilderList().size(); i++) {
        Expr.Builder el = create.getElementsBuilderList().get(i);
        check(el);
        elemType = joinTypes(location(el), elemType, getType(el));
    }
    if (elemType == null) {
        // If the list is empty, assign free type var to elem type.
        elemType = newTypeVar();
    }
    setType(e, Decls.newListType(elemType));
}
Also used : CreateList(com.google.api.expr.v1alpha1.Expr.CreateList) CheckerEnv.getObjectWellKnownType(org.projectnessie.cel.checker.CheckerEnv.getObjectWellKnownType) CheckerEnv.isObjectWellKnownType(org.projectnessie.cel.checker.CheckerEnv.isObjectWellKnownType) CheckerEnv.dynElementType(org.projectnessie.cel.checker.CheckerEnv.dynElementType) Type(com.google.api.expr.v1alpha1.Type) MapType(com.google.api.expr.v1alpha1.Type.MapType) FieldType(org.projectnessie.cel.common.types.ref.FieldType) Expr(com.google.api.expr.v1alpha1.Expr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr)

Example 17 with Type

use of com.google.api.expr.v1alpha1.Type in project cel-java by projectnessie.

the class Checker method setType.

void setType(Expr.Builder e, Type t) {
    Type old = types.get(e.getId());
    if (old != null && !old.equals(t)) {
        throw new IllegalStateException(String.format("(Incompatible) Type already exists for expression: %s(%d) old:%s, new:%s", e, e.getId(), old, t));
    }
    types.put(e.getId(), t);
}
Also used : CheckerEnv.getObjectWellKnownType(org.projectnessie.cel.checker.CheckerEnv.getObjectWellKnownType) CheckerEnv.isObjectWellKnownType(org.projectnessie.cel.checker.CheckerEnv.isObjectWellKnownType) CheckerEnv.dynElementType(org.projectnessie.cel.checker.CheckerEnv.dynElementType) Type(com.google.api.expr.v1alpha1.Type) MapType(com.google.api.expr.v1alpha1.Type.MapType) FieldType(org.projectnessie.cel.common.types.ref.FieldType)

Example 18 with Type

use of com.google.api.expr.v1alpha1.Type in project cel-java by projectnessie.

the class CheckerEnv method sanitizeFunction.

// sanitizeFunction replaces well-known types referenced by message name with their equivalent
// CEL built-in type instances.
Decl sanitizeFunction(Decl decl) {
    FunctionDecl fn = decl.getFunction();
    // Determine whether the declaration requires replacements from proto-based message type
    // references to well-known CEL type references.
    boolean needsSanitizing = false;
    for (Overload o : fn.getOverloadsList()) {
        if (isObjectWellKnownType(o.getResultType())) {
            needsSanitizing = true;
            break;
        }
        for (Type p : o.getParamsList()) {
            if (isObjectWellKnownType(p)) {
                needsSanitizing = true;
                break;
            }
        }
    }
    // Early return if the declaration requires no modification.
    if (!needsSanitizing) {
        return decl;
    }
    // Sanitize all of the overloads if any overload requires an update to its type references.
    List<Overload> overloads = new ArrayList<>(fn.getOverloadsCount());
    for (Overload o : fn.getOverloadsList()) {
        boolean sanitized = false;
        Type rt = o.getResultType();
        if (isObjectWellKnownType(rt)) {
            rt = getObjectWellKnownType(rt);
            sanitized = true;
        }
        List<Type> params = new ArrayList<>(o.getParamsCount());
        for (Type p : o.getParamsList()) {
            if (isObjectWellKnownType(p)) {
                params.add(getObjectWellKnownType(p));
                sanitized = true;
            } else {
                params.add(p);
            }
        }
        // If sanitized, replace the overload definition.
        Overload ov;
        if (sanitized) {
            if (o.getIsInstanceFunction()) {
                ov = Decls.newInstanceOverload(o.getOverloadId(), params, rt);
            } else {
                ov = Decls.newOverload(o.getOverloadId(), params, rt);
            }
        } else {
            // Otherwise, preserve the original overload.
            ov = o;
        }
        overloads.add(ov);
    }
    return Decls.newFunction(decl.getName(), overloads);
}
Also used : Types.formatCheckedType(org.projectnessie.cel.checker.Types.formatCheckedType) Type(com.google.api.expr.v1alpha1.Type) ErrType(org.projectnessie.cel.common.types.Err.ErrType) Overload(com.google.api.expr.v1alpha1.Decl.FunctionDecl.Overload) ArrayList(java.util.ArrayList) FunctionDecl(com.google.api.expr.v1alpha1.Decl.FunctionDecl)

Example 19 with Type

use of com.google.api.expr.v1alpha1.Type in project cel-java by projectnessie.

the class CheckerEnv method addOverload.

/**
 * addOverload adds overload to function declaration f. Returns one or more errorMsg values if the
 * overload overlaps with an existing overload or macro.
 */
Decl addOverload(Decl f, Overload overload, List<String> errMsgs) {
    FunctionDecl function = f.getFunction();
    Mapping emptyMappings = newMapping();
    Type overloadFunction = Decls.newFunctionType(overload.getResultType(), overload.getParamsList());
    Type overloadErased = substitute(emptyMappings, overloadFunction, true);
    boolean hasErr = false;
    for (Overload existing : function.getOverloadsList()) {
        Type existingFunction = Decls.newFunctionType(existing.getResultType(), existing.getParamsList());
        Type existingErased = substitute(emptyMappings, existingFunction, true);
        boolean overlap = isAssignable(emptyMappings, overloadErased, existingErased) != null || isAssignable(emptyMappings, existingErased, overloadErased) != null;
        if (overlap && overload.getIsInstanceFunction() == existing.getIsInstanceFunction()) {
            errMsgs.add(overlappingOverloadError(f.getName(), overload.getOverloadId(), overloadFunction, existing.getOverloadId(), existingFunction));
            hasErr = true;
        }
    }
    for (Macro macro : Macro.AllMacros) {
        if (macro.function().equals(f.getName()) && macro.isReceiverStyle() == overload.getIsInstanceFunction() && macro.argCount() == overload.getParamsCount()) {
            errMsgs.add(overlappingMacroError(f.getName(), macro.argCount()));
            hasErr = true;
        }
    }
    if (hasErr) {
        return f;
    }
    function = function.toBuilder().addOverloads(overload).build();
    f = f.toBuilder().setFunction(function).build();
    return f;
}
Also used : Types.formatCheckedType(org.projectnessie.cel.checker.Types.formatCheckedType) Type(com.google.api.expr.v1alpha1.Type) ErrType(org.projectnessie.cel.common.types.Err.ErrType) Overload(com.google.api.expr.v1alpha1.Decl.FunctionDecl.Overload) Macro(org.projectnessie.cel.parser.Macro) Mapping.newMapping(org.projectnessie.cel.checker.Mapping.newMapping) FunctionDecl(com.google.api.expr.v1alpha1.Decl.FunctionDecl)

Example 20 with Type

use of com.google.api.expr.v1alpha1.Type in project cel-java by projectnessie.

the class CheckerEnv method lookupIdent.

/**
 * LookupIdent returns a Decl proto for typeName as an identifier in the Env. Returns nil if no
 * such identifier is found in the Env.
 */
public Decl lookupIdent(String name) {
    for (String candidate : container.resolveCandidateNames(name)) {
        Decl ident = declarations.findIdent(candidate);
        if (ident != null) {
            return ident;
        }
        // Next try to import the name as a reference to a message type. If found,
        // the declaration is added to the outest (global) scope of the
        // environment, so next time we can access it faster.
        Type t = provider.findType(candidate);
        if (t != null) {
            Decl decl = Decls.newVar(candidate, t);
            declarations.addIdent(decl);
            return decl;
        }
        // Next try to import this as an enum value by splitting the name in a type prefix and
        // the enum inside.
        Val enumValue = provider.enumValue(candidate);
        if (enumValue.type() != ErrType) {
            Decl decl = Decls.newIdent(candidate, Decls.Int, Constant.newBuilder().setInt64Value(enumValue.intValue()).build());
            declarations.addIdent(decl);
            return decl;
        }
    }
    return null;
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Types.formatCheckedType(org.projectnessie.cel.checker.Types.formatCheckedType) Type(com.google.api.expr.v1alpha1.Type) ErrType(org.projectnessie.cel.common.types.Err.ErrType) FunctionDecl(com.google.api.expr.v1alpha1.Decl.FunctionDecl) Decl(com.google.api.expr.v1alpha1.Decl) IdentDecl(com.google.api.expr.v1alpha1.Decl.IdentDecl)

Aggregations

Type (com.google.api.expr.v1alpha1.Type)30 Test (org.junit.Test)16 MapType (com.google.api.expr.v1alpha1.Type.MapType)15 Type (edu.stanford.CVC4.Type)14 Type (com.google.spanner.v1.Type)12 Test (org.junit.jupiter.api.Test)12 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)11 ByteString (com.google.protobuf.ByteString)11 ArrayType (edu.stanford.CVC4.ArrayType)11 BitVectorType (edu.stanford.CVC4.BitVectorType)11 Expr (edu.stanford.CVC4.Expr)11 ArrayList (java.util.ArrayList)11 Type (org.apache.xbean.asm9.Type)10 Expr (com.google.api.expr.v1alpha1.Expr)9 CVC4.vectorExpr (edu.stanford.CVC4.vectorExpr)9 ListValue (com.google.protobuf.ListValue)8 FieldType (org.projectnessie.cel.common.types.ref.FieldType)8 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)7 CheckerEnv.dynElementType (org.projectnessie.cel.checker.CheckerEnv.dynElementType)7 CheckerEnv.getObjectWellKnownType (org.projectnessie.cel.checker.CheckerEnv.getObjectWellKnownType)7