use of com.google.api.expr.v1alpha1.Decl.FunctionDecl.Overload in project cel-java by projectnessie.
the class CheckerEnv method addFunction.
/**
* addFunction adds the function Decl to the Env. Adds a function decl if one doesn't already
* exist, then adds all overloads from the Decl. If overload overlaps with an existing overload,
* adds to the errors in the Env instead.
*/
void addFunction(Decl decl, List<String> errMsgs) {
Decl current = declarations.findFunction(decl.getName());
if (current == null) {
// Add the function declaration without overloads and check the overloads below.
current = Decls.newFunction(decl.getName(), Collections.emptyList());
declarations.addFunction(current);
}
for (Overload overload : decl.getFunction().getOverloadsList()) {
current = addOverload(current, overload, errMsgs);
}
declarations.updateFunction(decl.getName(), current);
}
use of com.google.api.expr.v1alpha1.Decl.FunctionDecl.Overload in project cel-java by projectnessie.
the class Checker method checkCall.
void checkCall(Expr.Builder e) {
// Note: similar logic exists within the `interpreter/planner.go`. If making changes here
// please consider the impact on planner.go and consolidate implementations or mirror code
// as appropriate.
Call.Builder call = e.getCallExprBuilder();
List<Expr.Builder> args = call.getArgsBuilderList();
String fnName = call.getFunction();
// Traverse arguments.
for (Expr.Builder arg : args) {
check(arg);
}
// Regular static call with simple name.
if (call.getTarget() == Expr.getDefaultInstance()) {
// Check for the existence of the function.
Decl fn = env.lookupFunction(fnName);
if (fn == null) {
errors.undeclaredReference(location(e), env.container.name(), fnName);
setType(e, Decls.Error);
return;
}
// Overwrite the function name with its fully qualified resolved name.
call.setFunction(fn.getName());
// Check to see whether the overload resolves.
resolveOverloadOrError(location(e), e, fn, null, args);
return;
}
// If a receiver 'target' is present, it may either be a receiver function, or a namespaced
// function, but not both. Given a.b.c() either a.b.c is a function or c is a function with
// target a.b.
//
// Check whether the target is a namespaced function name.
Expr.Builder target = call.getTargetBuilder();
String qualifiedPrefix = Container.toQualifiedName(target.build());
if (qualifiedPrefix != null) {
String maybeQualifiedName = qualifiedPrefix + "." + fnName;
Decl fn = env.lookupFunction(maybeQualifiedName);
if (fn != null) {
// The function name is namespaced and so preserving the target operand would
// be an inaccurate representation of the desired evaluation behavior.
// Overwrite with fully-qualified resolved function name sans receiver target.
call.clearTarget().setFunction(fn.getName());
resolveOverloadOrError(location(e), e, fn, null, args);
return;
}
}
// Regular instance call.
check(target);
// Overwrite with fully-qualified resolved function name sans receiver target.
Decl fn = env.lookupFunction(fnName);
// Function found, attempt overload resolution.
if (fn != null) {
resolveOverloadOrError(location(e), e, fn, target, args);
return;
}
// Function name not declared, record error.
errors.undeclaredReference(location(e), env.container.name(), fnName);
}
Aggregations