Search in sources :

Example 21 with Type

use of com.google.spanner.v1.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 22 with Type

use of com.google.spanner.v1.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)

Example 23 with Type

use of com.google.spanner.v1.Type in project cel-java by projectnessie.

the class FieldDescription method typeDefToType.

public Type typeDefToType() {
    switch(desc.getJavaType()) {
        case MESSAGE:
            String msgType = desc.getMessageType().getFullName();
            Type wk = Checked.CheckedWellKnowns.get(msgType);
            if (wk != null) {
                return wk;
            }
            return Checked.checkedMessageType(msgType);
        case ENUM:
            return Checked.checkedInt;
        case BOOLEAN:
            return Checked.checkedBool;
        case BYTE_STRING:
            return Checked.checkedBytes;
        case DOUBLE:
        case FLOAT:
            return Checked.checkedDouble;
        case INT:
            if (desc.getType() == FieldDescriptor.Type.UINT32 || desc.getType() == FieldDescriptor.Type.FIXED32) {
                return Checked.checkedUint;
            }
            return Checked.checkedInt;
        case LONG:
            if (desc.getType() == FieldDescriptor.Type.UINT64 || desc.getType() == FieldDescriptor.Type.FIXED64) {
                return Checked.checkedUint;
            }
            return Checked.checkedInt;
        case STRING:
            return Checked.checkedString;
    }
    throw new UnsupportedOperationException("Unknown JavaType " + desc.getJavaType());
}
Also used : ListType(com.google.api.expr.v1alpha1.Type.ListType) JavaType(com.google.protobuf.Descriptors.FieldDescriptor.JavaType) Type(com.google.api.expr.v1alpha1.Type) MapType(com.google.api.expr.v1alpha1.Type.MapType) ByteString(com.google.protobuf.ByteString)

Example 24 with Type

use of com.google.spanner.v1.Type in project cel-java by projectnessie.

the class Types method internalIsAssignable.

/**
 * internalIsAssignable returns true if t1 is assignable to t2.
 */
static boolean internalIsAssignable(Mapping m, Type t1, Type t2) {
    // Early terminate the call to avoid cases of infinite recursion.
    if (t1.equals(t2)) {
        return true;
    }
    // Process type parameters.
    Kind kind1 = kindOf(t1);
    Kind kind2 = kindOf(t2);
    if (kind2 == Kind.kindTypeParam) {
        Type t2Sub = m.find(t2);
        if (t2Sub != null) {
            // If the types are compatible, pick the more general type and return true
            if (internalIsAssignable(m, t1, t2Sub)) {
                m.add(t2, mostGeneral(t1, t2Sub));
                return true;
            }
            return false;
        }
        if (notReferencedIn(m, t2, t1)) {
            m.add(t2, t1);
            return true;
        }
    }
    if (kind1 == Kind.kindTypeParam) {
        // For the lower type bound, we currently do not perform adjustment. The restricted
        // way we use type parameters in lower type bounds, it is not necessary, but may
        // become if we generalize type unification.
        Type t1Sub = m.find(t1);
        if (t1Sub != null) {
            // If the types are compatible, pick the more general type and return true
            if (internalIsAssignable(m, t1Sub, t2)) {
                m.add(t1, mostGeneral(t1Sub, t2));
                return true;
            }
            return false;
        }
        if (notReferencedIn(m, t1, t2)) {
            m.add(t1, t2);
            return true;
        }
    }
    // Next check for wildcard types.
    if (isDynOrError(t1) || isDynOrError(t2)) {
        return true;
    }
    // Test for when the types do not need to agree, but are more specific than dyn.
    switch(kind1) {
        case kindNull:
            return internalIsAssignableNull(t2);
        case kindPrimitive:
            return internalIsAssignablePrimitive(t1.getPrimitive(), t2);
        case kindWrapper:
            return internalIsAssignable(m, Decls.newPrimitiveType(t1.getWrapper()), t2);
        default:
            if (kind1 != kind2) {
                return false;
            }
    }
    // Test for when the types must agree.
    switch(kind1) {
        // ERROR, TYPE_PARAM, and DYN handled above.
        case kindAbstract:
            return internalIsAssignableAbstractType(m, t1.getAbstractType(), t2.getAbstractType());
        case kindFunction:
            return internalIsAssignableFunction(m, t1.getFunction(), t2.getFunction());
        case kindList:
            return internalIsAssignable(m, t1.getListType().getElemType(), t2.getListType().getElemType());
        case kindMap:
            return internalIsAssignableMap(m, t1.getMapType(), t2.getMapType());
        case kindObject:
            return t1.getMessageType().equals(t2.getMessageType());
        case kindType:
            // type cannot affect method resolution or assignability.
            return true;
        case kindWellKnown:
            return t1.getWellKnown() == t2.getWellKnown();
        default:
            return false;
    }
}
Also used : AbstractType(com.google.api.expr.v1alpha1.Type.AbstractType) PrimitiveType(com.google.api.expr.v1alpha1.Type.PrimitiveType) Type(com.google.api.expr.v1alpha1.Type) WellKnownType(com.google.api.expr.v1alpha1.Type.WellKnownType) FunctionType(com.google.api.expr.v1alpha1.Type.FunctionType) MapType(com.google.api.expr.v1alpha1.Type.MapType)

Example 25 with Type

use of com.google.spanner.v1.Type in project cel-java by projectnessie.

the class Types method substitute.

/**
 * substitute replaces all direct and indirect occurrences of bound type parameters. Unbound type
 * parameters are replaced by DYN if typeParamToDyn is true.
 */
static Type substitute(Mapping m, Type t, boolean typeParamToDyn) {
    Type tSub = m.find(t);
    if (tSub != null) {
        return substitute(m, tSub, typeParamToDyn);
    }
    Kind kind = kindOf(t);
    if (typeParamToDyn && kind == Kind.kindTypeParam) {
        return Decls.Dyn;
    }
    switch(kind) {
        case kindAbstract:
            // TODO: implement!
            AbstractType at = t.getAbstractType();
            List<Type> params = new ArrayList<>(at.getParameterTypesCount());
            for (Type p : at.getParameterTypesList()) {
                params.add(substitute(m, p, typeParamToDyn));
            }
            return Decls.newAbstractType(at.getName(), params);
        case kindFunction:
            FunctionType fn = t.getFunction();
            Type rt = substitute(m, fn.getResultType(), typeParamToDyn);
            List<Type> args = new ArrayList<>(fn.getArgTypesCount());
            for (Type a : fn.getArgTypesList()) {
                args.add(substitute(m, a, typeParamToDyn));
            }
            return Decls.newFunctionType(rt, args);
        case kindList:
            return Decls.newListType(substitute(m, t.getListType().getElemType(), typeParamToDyn));
        case kindMap:
            MapType mt = t.getMapType();
            return Decls.newMapType(substitute(m, mt.getKeyType(), typeParamToDyn), substitute(m, mt.getValueType(), typeParamToDyn));
        case kindType:
            if (t.getType() != Type.getDefaultInstance()) {
                return Decls.newTypeType(substitute(m, t.getType(), typeParamToDyn));
            }
            return t;
        default:
            return t;
    }
}
Also used : AbstractType(com.google.api.expr.v1alpha1.Type.AbstractType) PrimitiveType(com.google.api.expr.v1alpha1.Type.PrimitiveType) Type(com.google.api.expr.v1alpha1.Type) WellKnownType(com.google.api.expr.v1alpha1.Type.WellKnownType) FunctionType(com.google.api.expr.v1alpha1.Type.FunctionType) MapType(com.google.api.expr.v1alpha1.Type.MapType) FunctionType(com.google.api.expr.v1alpha1.Type.FunctionType) AbstractType(com.google.api.expr.v1alpha1.Type.AbstractType) ArrayList(java.util.ArrayList) MapType(com.google.api.expr.v1alpha1.Type.MapType)

Aggregations

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