Search in sources :

Example 6 with Overload

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);
}
Also used : Overload(com.google.api.expr.v1alpha1.Decl.FunctionDecl.Overload) FunctionDecl(com.google.api.expr.v1alpha1.Decl.FunctionDecl) Decl(com.google.api.expr.v1alpha1.Decl) IdentDecl(com.google.api.expr.v1alpha1.Decl.IdentDecl)

Example 7 with Overload

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);
}
Also used : Call(com.google.api.expr.v1alpha1.Expr.Call) Expr(com.google.api.expr.v1alpha1.Expr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) IdentDecl(com.google.api.expr.v1alpha1.Decl.IdentDecl) Decl(com.google.api.expr.v1alpha1.Decl)

Aggregations

Type (com.google.api.expr.v1alpha1.Type)5 Overload (com.google.api.expr.v1alpha1.Decl.FunctionDecl.Overload)4 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)3 FunctionDecl (com.google.api.expr.v1alpha1.Decl.FunctionDecl)3 Expr (com.google.api.expr.v1alpha1.Expr)3 Decl (com.google.api.expr.v1alpha1.Decl)2 IdentDecl (com.google.api.expr.v1alpha1.Decl.IdentDecl)2 Call (com.google.api.expr.v1alpha1.Expr.Call)2 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2 Mapping.newMapping (org.projectnessie.cel.checker.Mapping.newMapping)2 Types.formatCheckedType (org.projectnessie.cel.checker.Types.formatCheckedType)2 ErrType (org.projectnessie.cel.common.types.Err.ErrType)2 Macro (org.projectnessie.cel.parser.Macro)2 Ident (com.google.api.expr.v1alpha1.Expr.Ident)1 ParsedExpr (com.google.api.expr.v1alpha1.ParsedExpr)1 Reference (com.google.api.expr.v1alpha1.Reference)1 MapType (com.google.api.expr.v1alpha1.Type.MapType)1 Arrays.asList (java.util.Arrays.asList)1 Collections.emptyList (java.util.Collections.emptyList)1