Search in sources :

Example 16 with Sort

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

the class EUnary method write.

@Override
void write(MethodWriter writer, Globals globals) {
    writer.writeDebugInfo(location);
    if (operation == Operation.NOT) {
        Label fals = new Label();
        Label end = new Label();
        child.write(writer, globals);
        writer.ifZCmp(Opcodes.IFEQ, fals);
        writer.push(false);
        writer.goTo(end);
        writer.mark(fals);
        writer.push(true);
        writer.mark(end);
    } else {
        Sort sort = promote.sort;
        child.write(writer, globals);
        // Def calls adopt the wanted return value. If there was a narrowing cast,
        // we need to flag that so that it's done at runtime.
        int defFlags = 0;
        if (originallyExplicit) {
            defFlags |= DefBootstrap.OPERATOR_EXPLICIT_CAST;
        }
        if (operation == Operation.BWNOT) {
            if (sort == Sort.DEF) {
                org.objectweb.asm.Type descriptor = org.objectweb.asm.Type.getMethodType(actual.type, child.actual.type);
                writer.invokeDefCall("not", descriptor, DefBootstrap.UNARY_OPERATOR, defFlags);
            } else {
                if (sort == Sort.INT) {
                    writer.push(-1);
                } else if (sort == Sort.LONG) {
                    writer.push(-1L);
                } else {
                    throw createError(new IllegalStateException("Illegal tree structure."));
                }
                writer.math(MethodWriter.XOR, actual.type);
            }
        } else if (operation == Operation.SUB) {
            if (sort == Sort.DEF) {
                org.objectweb.asm.Type descriptor = org.objectweb.asm.Type.getMethodType(actual.type, child.actual.type);
                writer.invokeDefCall("neg", descriptor, DefBootstrap.UNARY_OPERATOR, defFlags);
            } else {
                writer.math(MethodWriter.NEG, actual.type);
            }
        } else if (operation == Operation.ADD) {
            if (sort == Sort.DEF) {
                org.objectweb.asm.Type descriptor = org.objectweb.asm.Type.getMethodType(actual.type, child.actual.type);
                writer.invokeDefCall("plus", descriptor, DefBootstrap.UNARY_OPERATOR, defFlags);
            }
        } else {
            throw createError(new IllegalStateException("Illegal tree structure."));
        }
    }
}
Also used : Type(org.elasticsearch.painless.Definition.Type) Label(org.objectweb.asm.Label) Sort(org.elasticsearch.painless.Definition.Sort)

Example 17 with Sort

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

the class PBrace method analyze.

@Override
void analyze(Locals locals) {
    prefix.analyze(locals);
    prefix.expected = prefix.actual;
    prefix = prefix.cast(locals);
    Sort sort = prefix.actual.sort;
    if (sort == Sort.ARRAY) {
        sub = new PSubBrace(location, prefix.actual, index);
    } else if (sort == Sort.DEF) {
        sub = new PSubDefArray(location, index);
    } else if (Map.class.isAssignableFrom(prefix.actual.clazz)) {
        sub = new PSubMapShortcut(location, prefix.actual.struct, index);
    } else if (List.class.isAssignableFrom(prefix.actual.clazz)) {
        sub = new PSubListShortcut(location, prefix.actual.struct, index);
    } else {
        throw createError(new IllegalArgumentException("Illegal array access on type [" + prefix.actual.name + "]."));
    }
    sub.write = write;
    sub.read = read;
    sub.expected = expected;
    sub.explicit = explicit;
    sub.analyze(locals);
    actual = sub.actual;
}
Also used : Sort(org.elasticsearch.painless.Definition.Sort) List(java.util.List)

Example 18 with Sort

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

the class ENumeric method analyze.

@Override
void analyze(Locals locals) {
    if (!read) {
        throw createError(new IllegalArgumentException("Must read from constant [" + value + "]."));
    }
    if (value.endsWith("d") || value.endsWith("D")) {
        if (radix != 10) {
            throw createError(new IllegalStateException("Illegal tree structure."));
        }
        try {
            constant = Double.parseDouble(value.substring(0, value.length() - 1));
            actual = Definition.DOUBLE_TYPE;
        } catch (NumberFormatException exception) {
            throw createError(new IllegalArgumentException("Invalid double constant [" + value + "]."));
        }
    } else if (value.endsWith("f") || value.endsWith("F")) {
        if (radix != 10) {
            throw createError(new IllegalStateException("Illegal tree structure."));
        }
        try {
            constant = Float.parseFloat(value.substring(0, value.length() - 1));
            actual = Definition.FLOAT_TYPE;
        } catch (NumberFormatException exception) {
            throw createError(new IllegalArgumentException("Invalid float constant [" + value + "]."));
        }
    } else if (value.endsWith("l") || value.endsWith("L")) {
        try {
            constant = Long.parseLong(value.substring(0, value.length() - 1), radix);
            actual = Definition.LONG_TYPE;
        } catch (NumberFormatException exception) {
            throw createError(new IllegalArgumentException("Invalid long constant [" + value + "]."));
        }
    } else {
        try {
            Sort sort = expected == null ? Sort.INT : expected.sort;
            int integer = Integer.parseInt(value, radix);
            if (sort == Sort.BYTE && integer >= Byte.MIN_VALUE && integer <= Byte.MAX_VALUE) {
                constant = (byte) integer;
                actual = Definition.BYTE_TYPE;
            } else if (sort == Sort.CHAR && integer >= Character.MIN_VALUE && integer <= Character.MAX_VALUE) {
                constant = (char) integer;
                actual = Definition.CHAR_TYPE;
            } else if (sort == Sort.SHORT && integer >= Short.MIN_VALUE && integer <= Short.MAX_VALUE) {
                constant = (short) integer;
                actual = Definition.SHORT_TYPE;
            } else {
                constant = integer;
                actual = Definition.INT_TYPE;
            }
        } catch (NumberFormatException exception) {
            try {
                // Check if we can parse as a long. If so then hint that the user might prefer that.
                Long.parseLong(value, radix);
                throw createError(new IllegalArgumentException("Invalid int constant [" + value + "]. If you want a long constant " + "then change it to [" + value + "L]."));
            } catch (NumberFormatException longNoGood) {
            // Ignored
            }
            throw createError(new IllegalArgumentException("Invalid int constant [" + value + "]."));
        }
    }
}
Also used : Sort(org.elasticsearch.painless.Definition.Sort)

Example 19 with Sort

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

the class EUnary method analyzerSub.

void analyzerSub(Locals variables) {
    child.analyze(variables);
    promote = AnalyzerCaster.promoteNumeric(child.actual, true);
    if (promote == null) {
        throw createError(new ClassCastException("Cannot apply negative [-] to type [" + child.actual.name + "]."));
    }
    child.expected = promote;
    child = child.cast(variables);
    if (child.constant != null) {
        Sort sort = promote.sort;
        if (sort == Sort.INT) {
            constant = -(int) child.constant;
        } else if (sort == Sort.LONG) {
            constant = -(long) child.constant;
        } else if (sort == Sort.FLOAT) {
            constant = -(float) child.constant;
        } else if (sort == Sort.DOUBLE) {
            constant = -(double) child.constant;
        } else {
            throw createError(new IllegalStateException("Illegal tree structure."));
        }
    }
    if (promote.sort == Sort.DEF && expected != null) {
        actual = expected;
    } else {
        actual = promote;
    }
}
Also used : Sort(org.elasticsearch.painless.Definition.Sort)

Example 20 with Sort

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

the class EUnary method analyzeBWNot.

void analyzeBWNot(Locals variables) {
    child.analyze(variables);
    promote = AnalyzerCaster.promoteNumeric(child.actual, false);
    if (promote == null) {
        throw createError(new ClassCastException("Cannot apply not [~] to type [" + child.actual.name + "]."));
    }
    child.expected = promote;
    child = child.cast(variables);
    if (child.constant != null) {
        Sort sort = promote.sort;
        if (sort == Sort.INT) {
            constant = ~(int) child.constant;
        } else if (sort == Sort.LONG) {
            constant = ~(long) child.constant;
        } else {
            throw createError(new IllegalStateException("Illegal tree structure."));
        }
    }
    if (promote.sort == Sort.DEF && expected != null) {
        actual = expected;
    } else {
        actual = promote;
    }
}
Also used : Sort(org.elasticsearch.painless.Definition.Sort)

Aggregations

Sort (org.elasticsearch.painless.Definition.Sort)32 Type (org.elasticsearch.painless.Definition.Type)4 List (java.util.List)2 Map (java.util.Map)1 Definition (org.elasticsearch.painless.Definition)1 Field (org.elasticsearch.painless.Definition.Field)1 Method (org.elasticsearch.painless.Definition.Method)1 Struct (org.elasticsearch.painless.Definition.Struct)1 Label (org.objectweb.asm.Label)1