Search in sources :

Example 11 with Type

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

the class SFunction method generateSignature.

void generateSignature() {
    try {
        rtnType = Definition.getType(rtnTypeStr);
    } catch (IllegalArgumentException exception) {
        throw createError(new IllegalArgumentException("Illegal return type [" + rtnTypeStr + "] for function [" + name + "]."));
    }
    if (paramTypeStrs.size() != paramNameStrs.size()) {
        throw createError(new IllegalStateException("Illegal tree structure."));
    }
    Class<?>[] paramClasses = new Class<?>[this.paramTypeStrs.size()];
    List<Type> paramTypes = new ArrayList<>();
    for (int param = 0; param < this.paramTypeStrs.size(); ++param) {
        try {
            Type paramType = Definition.getType(this.paramTypeStrs.get(param));
            paramClasses[param] = paramType.clazz;
            paramTypes.add(paramType);
            parameters.add(new Parameter(location, paramNameStrs.get(param), paramType));
        } catch (IllegalArgumentException exception) {
            throw createError(new IllegalArgumentException("Illegal parameter type [" + this.paramTypeStrs.get(param) + "] for function [" + name + "]."));
        }
    }
    org.objectweb.asm.commons.Method method = new org.objectweb.asm.commons.Method(name, MethodType.methodType(rtnType.clazz, paramClasses).toMethodDescriptorString());
    this.method = new Method(name, null, false, rtnType, paramTypes, method, Modifier.STATIC | Modifier.PRIVATE, null);
}
Also used : ArrayList(java.util.ArrayList) Method(org.elasticsearch.painless.Definition.Method) Type(org.elasticsearch.painless.Definition.Type) MethodType(java.lang.invoke.MethodType) Parameter(org.elasticsearch.painless.Locals.Parameter)

Example 12 with Type

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

the class EElvis method analyze.

@Override
void analyze(Locals locals) {
    if (expected != null && expected.sort.primitive) {
        throw createError(new IllegalArgumentException("Evlis operator cannot return primitives"));
    }
    lhs.expected = expected;
    lhs.explicit = explicit;
    lhs.internal = internal;
    rhs.expected = expected;
    rhs.explicit = explicit;
    rhs.internal = internal;
    actual = expected;
    lhs.analyze(locals);
    rhs.analyze(locals);
    if (lhs.isNull) {
        throw createError(new IllegalArgumentException("Extraneous elvis operator. LHS is null."));
    }
    if (lhs.constant != null) {
        throw createError(new IllegalArgumentException("Extraneous elvis operator. LHS is a constant."));
    }
    if (lhs.actual.sort.primitive) {
        throw createError(new IllegalArgumentException("Extraneous elvis operator. LHS is a primitive."));
    }
    if (rhs.isNull) {
        throw createError(new IllegalArgumentException("Extraneous elvis operator. RHS is null."));
    }
    if (expected == null) {
        final Type promote = AnalyzerCaster.promoteConditional(lhs.actual, rhs.actual, lhs.constant, rhs.constant);
        lhs.expected = promote;
        rhs.expected = promote;
        actual = promote;
    }
    lhs = lhs.cast(locals);
    rhs = rhs.cast(locals);
}
Also used : Type(org.elasticsearch.painless.Definition.Type)

Example 13 with Type

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

the class EInstanceof method analyze.

@Override
void analyze(Locals locals) {
    final Type type;
    // ensure the specified type is part of the definition
    try {
        type = Definition.getType(this.type);
    } catch (IllegalArgumentException exception) {
        throw createError(new IllegalArgumentException("Not a type [" + this.type + "]."));
    }
    // map to wrapped type for primitive types
    resolvedType = type.sort.primitive ? type.sort.boxed : type.clazz;
    // analyze and cast the expression
    expression.analyze(locals);
    expression.expected = expression.actual;
    expression = expression.cast(locals);
    // record if the expression returns a primitive
    primitiveExpression = expression.actual.sort.primitive;
    // map to wrapped type for primitive types
    expressionType = expression.actual.sort.primitive ? expression.actual.sort.boxed : type.clazz;
    actual = Definition.BOOLEAN_TYPE;
}
Also used : Type(org.elasticsearch.painless.Definition.Type)

Example 14 with Type

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

the class PainlessDocGenerator method main.

public static void main(String[] args) throws IOException {
    Path apiRootPath = PathUtils.get(args[0]);
    // Blow away the last execution and recreate it from scratch
    IOUtils.rm(apiRootPath);
    Files.createDirectories(apiRootPath);
    Path indexPath = apiRootPath.resolve("index.asciidoc");
    logger.info("Starting to write [index.asciidoc]");
    try (PrintStream indexStream = new PrintStream(Files.newOutputStream(indexPath, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), false, StandardCharsets.UTF_8.name())) {
        emitGeneratedWarning(indexStream);
        List<Type> types = Definition.allSimpleTypes().stream().sorted(comparing(t -> t.name)).collect(toList());
        for (Type type : types) {
            if (type.sort.primitive) {
                // Primitives don't have methods to reference
                continue;
            }
            if ("def".equals(type.name)) {
                // def is special but doesn't have any methods all of its own.
                continue;
            }
            indexStream.print("include::");
            indexStream.print(type.struct.name);
            indexStream.println(".asciidoc[]");
            Path typePath = apiRootPath.resolve(type.struct.name + ".asciidoc");
            logger.info("Writing [{}.asciidoc]", type.name);
            try (PrintStream typeStream = new PrintStream(Files.newOutputStream(typePath, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), false, StandardCharsets.UTF_8.name())) {
                emitGeneratedWarning(typeStream);
                typeStream.print("[[");
                emitAnchor(typeStream, type.struct);
                typeStream.print("]]++");
                typeStream.print(type.name);
                typeStream.println("++::");
                Consumer<Field> documentField = field -> PainlessDocGenerator.documentField(typeStream, field);
                Consumer<Method> documentMethod = method -> PainlessDocGenerator.documentMethod(typeStream, method);
                type.struct.staticMembers.values().stream().sorted(FIELD_NAME).forEach(documentField);
                type.struct.members.values().stream().sorted(FIELD_NAME).forEach(documentField);
                type.struct.staticMethods.values().stream().sorted(METHOD_NAME.thenComparing(NUMBER_OF_ARGS)).forEach(documentMethod);
                type.struct.constructors.values().stream().sorted(NUMBER_OF_ARGS).forEach(documentMethod);
                Map<String, Struct> inherited = new TreeMap<>();
                type.struct.methods.values().stream().sorted(METHOD_NAME.thenComparing(NUMBER_OF_ARGS)).forEach(method -> {
                    if (method.owner == type.struct) {
                        documentMethod(typeStream, method);
                    } else {
                        inherited.put(method.owner.name, method.owner);
                    }
                });
                if (false == inherited.isEmpty()) {
                    typeStream.print("* Inherits methods from ");
                    boolean first = true;
                    for (Struct inheritsFrom : inherited.values()) {
                        if (first) {
                            first = false;
                        } else {
                            typeStream.print(", ");
                        }
                        typeStream.print("++");
                        emitStruct(typeStream, inheritsFrom);
                        typeStream.print("++");
                    }
                    typeStream.println();
                }
            }
        }
    }
    logger.info("Done writing [index.asciidoc]");
}
Also used : Path(java.nio.file.Path) PrintStream(java.io.PrintStream) Field(org.elasticsearch.painless.Definition.Field) Augmentation(org.elasticsearch.painless.api.Augmentation) Files(java.nio.file.Files) StandardOpenOption(java.nio.file.StandardOpenOption) IOUtils(org.apache.lucene.util.IOUtils) IOException(java.io.IOException) Type(org.elasticsearch.painless.Definition.Type) StandardCharsets(java.nio.charset.StandardCharsets) Struct(org.elasticsearch.painless.Definition.Struct) Consumer(java.util.function.Consumer) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Logger(org.apache.logging.log4j.Logger) Method(org.elasticsearch.painless.Definition.Method) TreeMap(java.util.TreeMap) ESLoggerFactory(org.elasticsearch.common.logging.ESLoggerFactory) Modifier(java.lang.reflect.Modifier) Map(java.util.Map) Comparator.comparing(java.util.Comparator.comparing) Comparator(java.util.Comparator) Path(java.nio.file.Path) PathUtils(org.elasticsearch.common.io.PathUtils) PrintStream(java.io.PrintStream) Method(org.elasticsearch.painless.Definition.Method) TreeMap(java.util.TreeMap) Struct(org.elasticsearch.painless.Definition.Struct) Field(org.elasticsearch.painless.Definition.Field) Type(org.elasticsearch.painless.Definition.Type)

Example 15 with Type

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

the class PainlessDocGenerator method documentMethod.

/**
     * Document a method.
     */
private static void documentMethod(PrintStream stream, Method method) {
    stream.print("* ++[[");
    emitAnchor(stream, method);
    stream.print("]]");
    if (false == method.augmentation && Modifier.isStatic(method.modifiers)) {
        stream.print("static ");
    }
    if (false == method.name.equals("<init>")) {
        emitType(stream, method.rtn);
        stream.print(' ');
    }
    String javadocRoot = javadocRoot(method);
    emitJavadocLink(stream, javadocRoot, method);
    stream.print('[');
    stream.print(methodName(method));
    stream.print("](");
    boolean first = true;
    for (Type arg : method.arguments) {
        if (first) {
            first = false;
        } else {
            stream.print(", ");
        }
        emitType(stream, arg);
    }
    stream.print(")++");
    if (javadocRoot.equals("java8")) {
        stream.print(" (");
        emitJavadocLink(stream, "java9", method);
        stream.print("[java 9])");
    }
    stream.println();
}
Also used : Type(org.elasticsearch.painless.Definition.Type)

Aggregations

Type (org.elasticsearch.painless.Definition.Type)18 Method (org.elasticsearch.painless.Definition.Method)3 Sort (org.elasticsearch.painless.Definition.Sort)3 ArrayList (java.util.ArrayList)2 Struct (org.elasticsearch.painless.Definition.Struct)2 Variable (org.elasticsearch.painless.Locals.Variable)2 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 MethodType (java.lang.invoke.MethodType)1 Modifier (java.lang.reflect.Modifier)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 StandardOpenOption (java.nio.file.StandardOpenOption)1 Comparator (java.util.Comparator)1 Comparator.comparing (java.util.Comparator.comparing)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1