Search in sources :

Example 1 with FunctionType

use of com.google.api.expr.v1alpha1.Type.FunctionType 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)

Example 2 with FunctionType

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

the class Types method notReferencedIn.

/**
 * notReferencedIn checks whether the type doesn't appear directly or transitively within the
 * other type. This is a standard requirement for type unification, commonly referred to as the
 * "occurs check".
 */
static boolean notReferencedIn(Mapping m, Type t, Type withinType) {
    if (t.equals(withinType)) {
        return false;
    }
    Kind withinKind = kindOf(withinType);
    switch(withinKind) {
        case kindTypeParam:
            Type wtSub = m.find(withinType);
            if (wtSub == null) {
                return true;
            }
            return notReferencedIn(m, t, wtSub);
        case kindAbstract:
            for (Type pt : withinType.getAbstractType().getParameterTypesList()) {
                if (!notReferencedIn(m, t, pt)) {
                    return false;
                }
            }
            return true;
        case kindFunction:
            FunctionType fn = withinType.getFunction();
            List<Type> types = flattenFunctionTypes(fn);
            for (Type a : types) {
                if (!notReferencedIn(m, t, a)) {
                    return false;
                }
            }
            return true;
        case kindList:
            return notReferencedIn(m, t, withinType.getListType().getElemType());
        case kindMap:
            MapType mt = withinType.getMapType();
            return notReferencedIn(m, t, mt.getKeyType()) && notReferencedIn(m, t, mt.getValueType());
        case kindWrapper:
            return notReferencedIn(m, t, Decls.newPrimitiveType(withinType.getWrapper()));
        default:
            return true;
    }
}
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) MapType(com.google.api.expr.v1alpha1.Type.MapType)

Aggregations

Type (com.google.api.expr.v1alpha1.Type)2 AbstractType (com.google.api.expr.v1alpha1.Type.AbstractType)2 FunctionType (com.google.api.expr.v1alpha1.Type.FunctionType)2 MapType (com.google.api.expr.v1alpha1.Type.MapType)2 PrimitiveType (com.google.api.expr.v1alpha1.Type.PrimitiveType)2 WellKnownType (com.google.api.expr.v1alpha1.Type.WellKnownType)2 ArrayList (java.util.ArrayList)1