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;
}
}
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;
}
}
Aggregations