Search in sources :

Example 21 with Sort

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

the class PField 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 PSubArrayLength(location, prefix.actual.name, value);
    } else if (sort == Sort.DEF) {
        sub = new PSubDefField(location, value);
    } else {
        Struct struct = prefix.actual.struct;
        Field field = prefix instanceof EStatic ? struct.staticMembers.get(value) : struct.members.get(value);
        if (field != null) {
            sub = new PSubField(location, field);
        } else {
            Method getter = struct.methods.get(new Definition.MethodKey("get" + Character.toUpperCase(value.charAt(0)) + value.substring(1), 0));
            if (getter == null) {
                getter = struct.methods.get(new Definition.MethodKey("is" + Character.toUpperCase(value.charAt(0)) + value.substring(1), 0));
            }
            Method setter = struct.methods.get(new Definition.MethodKey("set" + Character.toUpperCase(value.charAt(0)) + value.substring(1), 1));
            if (getter != null || setter != null) {
                sub = new PSubShortcut(location, value, prefix.actual.name, getter, setter);
            } else {
                EConstant index = new EConstant(location, value);
                index.analyze(locals);
                if (Map.class.isAssignableFrom(prefix.actual.clazz)) {
                    sub = new PSubMapShortcut(location, struct, index);
                }
                if (List.class.isAssignableFrom(prefix.actual.clazz)) {
                    sub = new PSubListShortcut(location, struct, index);
                }
            }
        }
    }
    if (sub == null) {
        throw createError(new IllegalArgumentException("Unknown field [" + value + "] for type [" + prefix.actual.name + "]."));
    }
    if (nullSafe) {
        sub = new PSubNullSafeField(location, sub);
    }
    sub.write = write;
    sub.read = read;
    sub.expected = expected;
    sub.explicit = explicit;
    sub.analyze(locals);
    actual = sub.actual;
}
Also used : Definition(org.elasticsearch.painless.Definition) Method(org.elasticsearch.painless.Definition.Method) Struct(org.elasticsearch.painless.Definition.Struct) Field(org.elasticsearch.painless.Definition.Field) Sort(org.elasticsearch.painless.Definition.Sort) List(java.util.List) Map(java.util.Map)

Example 22 with Sort

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

the class AnalyzerCaster method promoteAdd.

public static Type promoteAdd(final Type from0, final Type from1) {
    final Sort sort0 = from0.sort;
    final Sort sort1 = from1.sort;
    if (sort0 == Sort.STRING || sort1 == Sort.STRING) {
        return STRING_TYPE;
    }
    return promoteNumeric(from0, from1, true);
}
Also used : Sort(org.elasticsearch.painless.Definition.Sort)

Example 23 with Sort

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

the class AnalyzerCaster method constCast.

public static Object constCast(Location location, final Object constant, final Cast cast) {
    final Sort fsort = cast.from.sort;
    final Sort tsort = cast.to.sort;
    if (fsort == tsort) {
        return constant;
    } else if (fsort == Sort.STRING && tsort == Sort.CHAR) {
        return Utility.StringTochar((String) constant);
    } else if (fsort == Sort.CHAR && tsort == Sort.STRING) {
        return Utility.charToString((char) constant);
    } else if (fsort.numeric && tsort.numeric) {
        final Number number;
        if (fsort == Sort.CHAR) {
            number = (int) (char) constant;
        } else {
            number = (Number) constant;
        }
        switch(tsort) {
            case BYTE:
                return number.byteValue();
            case SHORT:
                return number.shortValue();
            case CHAR:
                return (char) number.intValue();
            case INT:
                return number.intValue();
            case LONG:
                return number.longValue();
            case FLOAT:
                return number.floatValue();
            case DOUBLE:
                return number.doubleValue();
            default:
                throw location.createError(new IllegalStateException("Cannot cast from " + "[" + cast.from.clazz.getCanonicalName() + "] to [" + cast.to.clazz.getCanonicalName() + "]."));
        }
    } else {
        throw location.createError(new IllegalStateException("Cannot cast from " + "[" + cast.from.clazz.getCanonicalName() + "] to [" + cast.to.clazz.getCanonicalName() + "]."));
    }
}
Also used : Sort(org.elasticsearch.painless.Definition.Sort)

Example 24 with Sort

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

the class AnalyzerCaster method promoteConditional.

public static Type promoteConditional(final Type from0, final Type from1, final Object const0, final Object const1) {
    if (from0.equals(from1)) {
        return from0;
    }
    final Sort sort0 = from0.sort;
    final Sort sort1 = from1.sort;
    if (sort0 == Sort.DEF || sort1 == Sort.DEF) {
        return DEF_TYPE;
    }
    if (sort0.primitive && sort1.primitive) {
        if (sort0.bool && sort1.bool) {
            return BOOLEAN_TYPE;
        }
        if (sort0 == Sort.DOUBLE || sort1 == Sort.DOUBLE) {
            return DOUBLE_TYPE;
        } else if (sort0 == Sort.FLOAT || sort1 == Sort.FLOAT) {
            return FLOAT_TYPE;
        } else if (sort0 == Sort.LONG || sort1 == Sort.LONG) {
            return LONG_TYPE;
        } else {
            if (sort0 == Sort.BYTE) {
                if (sort1 == Sort.BYTE) {
                    return BYTE_TYPE;
                } else if (sort1 == Sort.SHORT) {
                    if (const1 != null) {
                        final short constant = (short) const1;
                        if (constant <= Byte.MAX_VALUE && constant >= Byte.MIN_VALUE) {
                            return BYTE_TYPE;
                        }
                    }
                    return SHORT_TYPE;
                } else if (sort1 == Sort.CHAR) {
                    return INT_TYPE;
                } else if (sort1 == Sort.INT) {
                    if (const1 != null) {
                        final int constant = (int) const1;
                        if (constant <= Byte.MAX_VALUE && constant >= Byte.MIN_VALUE) {
                            return BYTE_TYPE;
                        }
                    }
                    return INT_TYPE;
                }
            } else if (sort0 == Sort.SHORT) {
                if (sort1 == Sort.BYTE) {
                    if (const0 != null) {
                        final short constant = (short) const0;
                        if (constant <= Byte.MAX_VALUE && constant >= Byte.MIN_VALUE) {
                            return BYTE_TYPE;
                        }
                    }
                    return SHORT_TYPE;
                } else if (sort1 == Sort.SHORT) {
                    return SHORT_TYPE;
                } else if (sort1 == Sort.CHAR) {
                    return INT_TYPE;
                } else if (sort1 == Sort.INT) {
                    if (const1 != null) {
                        final int constant = (int) const1;
                        if (constant <= Short.MAX_VALUE && constant >= Short.MIN_VALUE) {
                            return SHORT_TYPE;
                        }
                    }
                    return INT_TYPE;
                }
            } else if (sort0 == Sort.CHAR) {
                if (sort1 == Sort.BYTE) {
                    return INT_TYPE;
                } else if (sort1 == Sort.SHORT) {
                    return INT_TYPE;
                } else if (sort1 == Sort.CHAR) {
                    return CHAR_TYPE;
                } else if (sort1 == Sort.INT) {
                    if (const1 != null) {
                        final int constant = (int) const1;
                        if (constant <= Character.MAX_VALUE && constant >= Character.MIN_VALUE) {
                            return BYTE_TYPE;
                        }
                    }
                    return INT_TYPE;
                }
            } else if (sort0 == Sort.INT) {
                if (sort1 == Sort.BYTE) {
                    if (const0 != null) {
                        final int constant = (int) const0;
                        if (constant <= Byte.MAX_VALUE && constant >= Byte.MIN_VALUE) {
                            return BYTE_TYPE;
                        }
                    }
                    return INT_TYPE;
                } else if (sort1 == Sort.SHORT) {
                    if (const0 != null) {
                        final int constant = (int) const0;
                        if (constant <= Short.MAX_VALUE && constant >= Short.MIN_VALUE) {
                            return BYTE_TYPE;
                        }
                    }
                    return INT_TYPE;
                } else if (sort1 == Sort.CHAR) {
                    if (const0 != null) {
                        final int constant = (int) const0;
                        if (constant <= Character.MAX_VALUE && constant >= Character.MIN_VALUE) {
                            return BYTE_TYPE;
                        }
                    }
                    return INT_TYPE;
                } else if (sort1 == Sort.INT) {
                    return INT_TYPE;
                }
            }
        }
    }
    return OBJECT_TYPE;
}
Also used : Sort(org.elasticsearch.painless.Definition.Sort)

Example 25 with Sort

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

the class EBinary method analyzeMul.

private void analyzeMul(Locals variables) {
    left.analyze(variables);
    right.analyze(variables);
    promote = AnalyzerCaster.promoteNumeric(left.actual, right.actual, true);
    if (promote == null) {
        throw createError(new ClassCastException("Cannot apply multiply [*] to types " + "[" + left.actual.name + "] and [" + right.actual.name + "]."));
    }
    actual = promote;
    if (promote.sort == Sort.DEF) {
        left.expected = left.actual;
        right.expected = right.actual;
        if (expected != null) {
            actual = expected;
        }
    } else {
        left.expected = promote;
        right.expected = promote;
    }
    left = left.cast(variables);
    right = right.cast(variables);
    if (left.constant != null && right.constant != null) {
        Sort sort = promote.sort;
        if (sort == Sort.INT) {
            constant = (int) left.constant * (int) right.constant;
        } else if (sort == Sort.LONG) {
            constant = (long) left.constant * (long) right.constant;
        } else if (sort == Sort.FLOAT) {
            constant = (float) left.constant * (float) right.constant;
        } else if (sort == Sort.DOUBLE) {
            constant = (double) left.constant * (double) right.constant;
        } else {
            throw createError(new IllegalStateException("Illegal tree structure."));
        }
    }
}
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