Skip to content

Commit c0ffea8

Browse files
committed
Completely remove Painless Type from AnalyzerCaster in favor of Java Class. (#28329)
Second part in a series of PR's to remove Painless Type in favor of Java Class. This completely removes the Painless Type dependency from AnalyzerCaster. Both casting and promotion are now based on Java Class exclusively. This also allows AnalyzerCaster to be decoupled from Definition and make cast checks be static calls again.
1 parent e0a6018 commit c0ffea8

15 files changed

+272
-249
lines changed

modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerCaster.java

Lines changed: 101 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package org.elasticsearch.painless;
2121

2222
import org.elasticsearch.painless.Definition.Cast;
23-
import org.elasticsearch.painless.Definition.Type;
2423
import org.elasticsearch.painless.Definition.def;
2524

2625
import java.util.Objects;
@@ -31,26 +30,9 @@
3130
*/
3231
public final class AnalyzerCaster {
3332

34-
private Definition definition;
35-
36-
public AnalyzerCaster(Definition definition) {
37-
this.definition = definition;
38-
}
39-
40-
public Cast getLegalCast(Location location, Type actualType, Type expectedType, boolean explicit, boolean internal) {
41-
Objects.requireNonNull(actualType);
42-
Objects.requireNonNull(expectedType);
43-
44-
Class<?> actual = actualType.clazz;
45-
Class<?> expected = expectedType.clazz;
46-
47-
if (actualType.dynamic) {
48-
actual = Definition.ObjectClassTodefClass(actual);
49-
}
50-
51-
if (expectedType.dynamic) {
52-
expected = Definition.ObjectClassTodefClass(expected);
53-
}
33+
public static Cast getLegalCast(Location location, Class<?> actual, Class<?> expected, boolean explicit, boolean internal) {
34+
Objects.requireNonNull(actual);
35+
Objects.requireNonNull(expected);
5436

5537
if (actual == expected) {
5638
return null;
@@ -487,7 +469,7 @@ public Cast getLegalCast(Location location, Type actualType, Type expectedType,
487469
}
488470
}
489471

490-
public Object constCast(Location location, final Object constant, final Cast cast) {
472+
public static Object constCast(Location location, Object constant, Cast cast) {
491473
Class<?> fsort = cast.from;
492474
Class<?> tsort = cast.to;
493475

@@ -498,7 +480,7 @@ public Object constCast(Location location, final Object constant, final Cast cas
498480
} else if (fsort == char.class && tsort == String.class) {
499481
return Utility.charToString((char)constant);
500482
} else if (fsort.isPrimitive() && fsort != boolean.class && tsort.isPrimitive() && tsort != boolean.class) {
501-
final Number number;
483+
Number number;
502484

503485
if (fsort == char.class) {
504486
number = (int)(char)constant;
@@ -523,224 +505,201 @@ public Object constCast(Location location, final Object constant, final Cast cas
523505
}
524506
}
525507

526-
public Type promoteNumeric(Type from, boolean decimal) {
527-
Class<?> sort = from.clazz;
528-
529-
if (from.dynamic) {
530-
return definition.DefType;
531-
} else if ((sort == double.class) && decimal) {
532-
return definition.doubleType;
533-
} else if ((sort == float.class) && decimal) {
534-
return definition.floatType;
535-
} else if (sort == long.class) {
536-
return definition.longType;
537-
} else if (sort == int.class || sort == char.class || sort == short.class || sort == byte.class) {
538-
return definition.intType;
508+
public static Class<?> promoteNumeric(Class<?> from, boolean decimal) {
509+
if (from == def.class || from == double.class && decimal || from == float.class && decimal || from == long.class) {
510+
return from;
511+
} else if (from == int.class || from == char.class || from == short.class || from == byte.class) {
512+
return int.class;
539513
}
540514

541515
return null;
542516
}
543517

544-
public Type promoteNumeric(Type from0, Type from1, boolean decimal) {
545-
Class<?> sort0 = from0.clazz;
546-
Class<?> sort1 = from1.clazz;
547-
548-
if (from0.dynamic || from1.dynamic) {
549-
return definition.DefType;
518+
public static Class<?> promoteNumeric(Class<?> from0, Class<?> from1, boolean decimal) {
519+
if (from0 == def.class || from1 == def.class) {
520+
return def.class;
550521
}
551522

552523
if (decimal) {
553-
if (sort0 == double.class || sort1 == double.class) {
554-
return definition.doubleType;
555-
} else if (sort0 == float.class || sort1 == float.class) {
556-
return definition.floatType;
524+
if (from0 == double.class || from1 == double.class) {
525+
return double.class;
526+
} else if (from0 == float.class || from1 == float.class) {
527+
return float.class;
557528
}
558529
}
559530

560-
if (sort0 == long.class || sort1 == long.class) {
561-
return definition.longType;
562-
} else if (sort0 == int.class || sort1 == int.class ||
563-
sort0 == char.class || sort1 == char.class ||
564-
sort0 == short.class || sort1 == short.class ||
565-
sort0 == byte.class || sort1 == byte.class) {
566-
return definition.intType;
531+
if (from0 == long.class || from1 == long.class) {
532+
return long.class;
533+
} else if (from0 == int.class || from1 == int.class ||
534+
from0 == char.class || from1 == char.class ||
535+
from0 == short.class || from1 == short.class ||
536+
from0 == byte.class || from1 == byte.class) {
537+
return int.class;
567538
}
568539

569540
return null;
570541
}
571542

572-
public Type promoteAdd(Type from0, Type from1) {
573-
Class<?> sort0 = from0.clazz;
574-
Class<?> sort1 = from1.clazz;
575-
576-
if (sort0 == String.class || sort1 == String.class) {
577-
return definition.StringType;
543+
public static Class<?> promoteAdd(Class<?> from0, Class<?> from1) {
544+
if (from0 == String.class || from1 == String.class) {
545+
return String.class;
578546
}
579547

580548
return promoteNumeric(from0, from1, true);
581549
}
582550

583-
public Type promoteXor(Type from0, Type from1) {
584-
Class<?> sort0 = from0.clazz;
585-
Class<?> sort1 = from1.clazz;
586-
587-
if (from0.dynamic || from1.dynamic) {
588-
return definition.DefType;
551+
public static Class<?> promoteXor(Class<?> from0, Class<?> from1) {
552+
if (from0 == def.class || from1 == def.class) {
553+
return def.class;
589554
}
590555

591-
if (sort0 == boolean.class || sort1 == boolean.class) {
592-
return definition.booleanType;
556+
if (from0 == boolean.class || from1 == boolean.class) {
557+
return boolean.class;
593558
}
594559

595560
return promoteNumeric(from0, from1, false);
596561
}
597562

598-
public Type promoteEquality(Type from0, Type from1) {
599-
Class<?> sort0 = from0.clazz;
600-
Class<?> sort1 = from1.clazz;
601-
602-
if (from0.dynamic || from1.dynamic) {
603-
return definition.DefType;
563+
public static Class<?> promoteEquality(Class<?> from0, Class<?> from1) {
564+
if (from0 == def.class || from1 == def.class) {
565+
return def.class;
604566
}
605567

606-
if (sort0.isPrimitive() && sort1.isPrimitive()) {
607-
if (sort0 == boolean.class && sort1 == boolean.class) {
608-
return definition.booleanType;
568+
if (from0.isPrimitive() && from1.isPrimitive()) {
569+
if (from0 == boolean.class && from1 == boolean.class) {
570+
return boolean.class;
609571
}
610572

611573
return promoteNumeric(from0, from1, true);
612574
}
613575

614-
return definition.ObjectType;
576+
return Object.class;
615577
}
616578

617-
public Type promoteConditional(Type from0, Type from1, Object const0, Object const1) {
618-
if (from0.equals(from1)) {
579+
public static Class<?> promoteConditional(Class<?> from0, Class<?> from1, Object const0, Object const1) {
580+
if (from0 == from1) {
619581
return from0;
620582
}
621583

622-
Class<?> sort0 = from0.clazz;
623-
Class<?> sort1 = from1.clazz;
624-
625-
if (from0.dynamic || from1.dynamic) {
626-
return definition.DefType;
584+
if (from0 == def.class || from1 == def.class) {
585+
return def.class;
627586
}
628587

629-
if (sort0.isPrimitive() && sort1.isPrimitive()) {
630-
if (sort0 == boolean.class && sort1 == boolean.class) {
631-
return definition.booleanType;
588+
if (from0.isPrimitive() && from1.isPrimitive()) {
589+
if (from0 == boolean.class && from1 == boolean.class) {
590+
return boolean.class;
632591
}
633592

634-
if (sort0 == double.class || sort1 == double.class) {
635-
return definition.doubleType;
636-
} else if (sort0 == float.class || sort1 == float.class) {
637-
return definition.floatType;
638-
} else if (sort0 == long.class || sort1 == long.class) {
639-
return definition.longType;
593+
if (from0 == double.class || from1 == double.class) {
594+
return double.class;
595+
} else if (from0 == float.class || from1 == float.class) {
596+
return float.class;
597+
} else if (from0 == long.class || from1 == long.class) {
598+
return long.class;
640599
} else {
641-
if (sort0 == byte.class) {
642-
if (sort1 == byte.class) {
643-
return definition.byteType;
644-
} else if (sort1 == short.class) {
600+
if (from0 == byte.class) {
601+
if (from1 == byte.class) {
602+
return byte.class;
603+
} else if (from1 == short.class) {
645604
if (const1 != null) {
646605
final short constant = (short)const1;
647606

648607
if (constant <= Byte.MAX_VALUE && constant >= Byte.MIN_VALUE) {
649-
return definition.byteType;
608+
return byte.class;
650609
}
651610
}
652611

653-
return definition.shortType;
654-
} else if (sort1 == char.class) {
655-
return definition.intType;
656-
} else if (sort1 == int.class) {
612+
return short.class;
613+
} else if (from1 == char.class) {
614+
return int.class;
615+
} else if (from1 == int.class) {
657616
if (const1 != null) {
658617
final int constant = (int)const1;
659618

660619
if (constant <= Byte.MAX_VALUE && constant >= Byte.MIN_VALUE) {
661-
return definition.byteType;
620+
return byte.class;
662621
}
663622
}
664623

665-
return definition.intType;
624+
return int.class;
666625
}
667-
} else if (sort0 == short.class) {
668-
if (sort1 == byte.class) {
626+
} else if (from0 == short.class) {
627+
if (from1 == byte.class) {
669628
if (const0 != null) {
670629
final short constant = (short)const0;
671630

672631
if (constant <= Byte.MAX_VALUE && constant >= Byte.MIN_VALUE) {
673-
return definition.byteType;
632+
return byte.class;
674633
}
675634
}
676635

677-
return definition.shortType;
678-
} else if (sort1 == short.class) {
679-
return definition.shortType;
680-
} else if (sort1 == char.class) {
681-
return definition.intType;
682-
} else if (sort1 == int.class) {
636+
return short.class;
637+
} else if (from1 == short.class) {
638+
return short.class;
639+
} else if (from1 == char.class) {
640+
return int.class;
641+
} else if (from1 == int.class) {
683642
if (const1 != null) {
684643
final int constant = (int)const1;
685644

686645
if (constant <= Short.MAX_VALUE && constant >= Short.MIN_VALUE) {
687-
return definition.shortType;
646+
return short.class;
688647
}
689648
}
690649

691-
return definition.intType;
650+
return int.class;
692651
}
693-
} else if (sort0 == char.class) {
694-
if (sort1 == byte.class) {
695-
return definition.intType;
696-
} else if (sort1 == short.class) {
697-
return definition.intType;
698-
} else if (sort1 == char.class) {
699-
return definition.charType;
700-
} else if (sort1 == int.class) {
652+
} else if (from0 == char.class) {
653+
if (from1 == byte.class) {
654+
return int.class;
655+
} else if (from1 == short.class) {
656+
return int.class;
657+
} else if (from1 == char.class) {
658+
return char.class;
659+
} else if (from1 == int.class) {
701660
if (const1 != null) {
702661
final int constant = (int)const1;
703662

704663
if (constant <= Character.MAX_VALUE && constant >= Character.MIN_VALUE) {
705-
return definition.byteType;
664+
return byte.class;
706665
}
707666
}
708667

709-
return definition.intType;
668+
return int.class;
710669
}
711-
} else if (sort0 == int.class) {
712-
if (sort1 == byte.class) {
670+
} else if (from0 == int.class) {
671+
if (from1 == byte.class) {
713672
if (const0 != null) {
714673
final int constant = (int)const0;
715674

716675
if (constant <= Byte.MAX_VALUE && constant >= Byte.MIN_VALUE) {
717-
return definition.byteType;
676+
return byte.class;
718677
}
719678
}
720679

721-
return definition.intType;
722-
} else if (sort1 == short.class) {
680+
return int.class;
681+
} else if (from1 == short.class) {
723682
if (const0 != null) {
724683
final int constant = (int)const0;
725684

726685
if (constant <= Short.MAX_VALUE && constant >= Short.MIN_VALUE) {
727-
return definition.byteType;
686+
return byte.class;
728687
}
729688
}
730689

731-
return definition.intType;
732-
} else if (sort1 == char.class) {
690+
return int.class;
691+
} else if (from1 == char.class) {
733692
if (const0 != null) {
734693
final int constant = (int)const0;
735694

736695
if (constant <= Character.MAX_VALUE && constant >= Character.MIN_VALUE) {
737-
return definition.byteType;
696+
return byte.class;
738697
}
739698
}
740699

741-
return definition.intType;
742-
} else if (sort1 == int.class) {
743-
return definition.intType;
700+
return int.class;
701+
} else if (from1 == int.class) {
702+
return int.class;
744703
}
745704
}
746705
}
@@ -750,6 +709,10 @@ public Type promoteConditional(Type from0, Type from1, Object const0, Object con
750709
// TODO: to calculate the highest upper bound for the two types and return that.
751710
// TODO: However, for now we just return objectType that may require an extra cast.
752711

753-
return definition.ObjectType;
712+
return Object.class;
713+
}
714+
715+
private AnalyzerCaster() {
716+
754717
}
755718
}

0 commit comments

Comments
 (0)