Search in sources :

Example 1 with MethodKey

use of org.elasticsearch.painless.Definition.MethodKey in project elasticsearch by elastic.

the class SSource method analyze.

public void analyze() {
    Map<MethodKey, Method> methods = new HashMap<>();
    for (SFunction function : functions) {
        function.generateSignature();
        MethodKey key = new MethodKey(function.name, function.parameters.size());
        if (methods.put(key, function.method) != null) {
            throw createError(new IllegalArgumentException("Duplicate functions with name [" + function.name + "]."));
        }
    }
    analyze(Locals.newProgramScope(methods.values()));
}
Also used : HashMap(java.util.HashMap) Method(org.elasticsearch.painless.Definition.Method) MethodKey(org.elasticsearch.painless.Definition.MethodKey)

Example 2 with MethodKey

use of org.elasticsearch.painless.Definition.MethodKey in project elasticsearch by elastic.

the class ECallLocal method analyze.

@Override
void analyze(Locals locals) {
    MethodKey methodKey = new MethodKey(name, arguments.size());
    method = locals.getMethod(methodKey);
    if (method == null) {
        throw createError(new IllegalArgumentException("Unknown call [" + name + "] with [" + arguments.size() + "] arguments."));
    }
    for (int argument = 0; argument < arguments.size(); ++argument) {
        AExpression expression = arguments.get(argument);
        expression.expected = method.arguments.get(argument);
        expression.internal = true;
        expression.analyze(locals);
        arguments.set(argument, expression.cast(locals));
    }
    statement = true;
    actual = method.rtn;
}
Also used : MethodKey(org.elasticsearch.painless.Definition.MethodKey)

Example 3 with MethodKey

use of org.elasticsearch.painless.Definition.MethodKey in project elasticsearch by elastic.

the class NodeToStringTests method testPSubCallInvoke.

public void testPSubCallInvoke() {
    Location l = new Location(getTestName(), 0);
    RuntimeClass c = Definition.getRuntimeClass(Integer.class);
    Method m = c.methods.get(new MethodKey("toString", 0));
    PSubCallInvoke node = new PSubCallInvoke(l, m, null, emptyList());
    node.prefix = new EVariable(l, "a");
    assertEquals("(PSubCallInvoke (EVariable a) toString)", node.toString());
    assertEquals("(PSubNullSafeCallInvoke (PSubCallInvoke (EVariable a) toString))", new PSubNullSafeCallInvoke(l, node).toString());
    l = new Location(getTestName(), 1);
    m = c.methods.get(new MethodKey("equals", 1));
    node = new PSubCallInvoke(l, m, null, singletonList(new EVariable(l, "b")));
    node.prefix = new EVariable(l, "a");
    assertEquals("(PSubCallInvoke (EVariable a) equals (Args (EVariable b)))", node.toString());
    assertEquals("(PSubNullSafeCallInvoke (PSubCallInvoke (EVariable a) equals (Args (EVariable b))))", new PSubNullSafeCallInvoke(l, node).toString());
}
Also used : Method(org.elasticsearch.painless.Definition.Method) RuntimeClass(org.elasticsearch.painless.Definition.RuntimeClass) Location(org.elasticsearch.painless.Location) MethodKey(org.elasticsearch.painless.Definition.MethodKey)

Example 4 with MethodKey

use of org.elasticsearch.painless.Definition.MethodKey in project elasticsearch by elastic.

the class EFunctionRef method analyze.

@Override
void analyze(Locals locals) {
    if (expected == null) {
        ref = null;
        actual = Definition.getType("String");
        defPointer = "S" + type + "." + call + ",0";
    } else {
        defPointer = null;
        try {
            if ("this".equals(type)) {
                // user's own function
                Method interfaceMethod = expected.struct.getFunctionalMethod();
                if (interfaceMethod == null) {
                    throw new IllegalArgumentException("Cannot convert function reference [" + type + "::" + call + "] " + "to [" + expected.name + "], not a functional interface");
                }
                Method implMethod = locals.getMethod(new MethodKey(call, interfaceMethod.arguments.size()));
                if (implMethod == null) {
                    throw new IllegalArgumentException("Cannot convert function reference [" + type + "::" + call + "] " + "to [" + expected.name + "], function not found");
                }
                ref = new FunctionRef(expected, interfaceMethod, implMethod, 0);
            } else {
                // whitelist lookup
                ref = new FunctionRef(expected, type, call, 0);
            }
        } catch (IllegalArgumentException e) {
            throw createError(e);
        }
        actual = expected;
    }
}
Also used : Method(org.elasticsearch.painless.Definition.Method) MethodKey(org.elasticsearch.painless.Definition.MethodKey) FunctionRef(org.elasticsearch.painless.FunctionRef)

Example 5 with MethodKey

use of org.elasticsearch.painless.Definition.MethodKey in project elasticsearch by elastic.

the class PCallInvoke method analyze.

@Override
void analyze(Locals locals) {
    prefix.analyze(locals);
    prefix.expected = prefix.actual;
    prefix = prefix.cast(locals);
    if (prefix.actual.sort == Sort.ARRAY) {
        throw createError(new IllegalArgumentException("Illegal call [" + name + "] on array type."));
    }
    Struct struct = prefix.actual.struct;
    if (prefix.actual.sort.primitive) {
        struct = Definition.getType(prefix.actual.sort.boxed.getSimpleName()).struct;
    }
    MethodKey methodKey = new MethodKey(name, arguments.size());
    Method method = prefix instanceof EStatic ? struct.staticMethods.get(methodKey) : struct.methods.get(methodKey);
    if (method != null) {
        sub = new PSubCallInvoke(location, method, prefix.actual, arguments);
    } else if (prefix.actual.sort == Sort.DEF) {
        sub = new PSubDefCall(location, name, arguments);
    } else {
        throw createError(new IllegalArgumentException("Unknown call [" + name + "] with [" + arguments.size() + "] arguments on type [" + struct.name + "]."));
    }
    if (nullSafe) {
        sub = new PSubNullSafeCallInvoke(location, sub);
    }
    sub.expected = expected;
    sub.explicit = explicit;
    sub.analyze(locals);
    actual = sub.actual;
    statement = true;
}
Also used : Method(org.elasticsearch.painless.Definition.Method) Struct(org.elasticsearch.painless.Definition.Struct) MethodKey(org.elasticsearch.painless.Definition.MethodKey)

Aggregations

MethodKey (org.elasticsearch.painless.Definition.MethodKey)6 Method (org.elasticsearch.painless.Definition.Method)5 Struct (org.elasticsearch.painless.Definition.Struct)2 Location (org.elasticsearch.painless.Location)2 HashMap (java.util.HashMap)1 RuntimeClass (org.elasticsearch.painless.Definition.RuntimeClass)1 FeatureTest (org.elasticsearch.painless.FeatureTest)1 FunctionRef (org.elasticsearch.painless.FunctionRef)1