1 package org.simantics.scl.compiler.elaboration.java;
3 import org.cojen.classfile.TypeDesc;
4 import org.simantics.scl.compiler.constants.FunctionValue;
5 import org.simantics.scl.compiler.internal.codegen.references.Val;
6 import org.simantics.scl.compiler.internal.codegen.utils.Constants;
7 import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
8 import org.simantics.scl.compiler.types.TVar;
9 import org.simantics.scl.compiler.types.Type;
10 import org.simantics.scl.compiler.types.Types;
11 import org.simantics.scl.compiler.types.kinds.Kinds;
13 public class HashCodeFunction extends FunctionValue {
14 private static final TVar A = Types.var(Kinds.STAR);
15 public static final HashCodeFunction INSTANCE = new HashCodeFunction();
17 private HashCodeFunction() {
18 super(new TVar[] {A}, Types.NO_EFFECTS, Types.INTEGER, A);
22 public Type applyExact(MethodBuilder mb, Val[] parameters) {
23 TypeDesc parameterType = mb.getJavaTypeTranslator().getTypeDesc(parameters[0]);
24 parameters[0].push(mb);
25 switch(parameterType.getTypeCode()) {
26 case TypeDesc.VOID_CODE:
27 mb.loadStaticField(Constants.TUPLE0, "INSTANCE", Constants.TUPLE0);
28 mb.invokeVirtual("java/lang/Object", "hashCode", TypeDesc.INT, Constants.OBJECTS[1]);
30 case TypeDesc.OBJECT_CODE:
31 mb.invokeStatic("java/util/Objects", "hashCode", TypeDesc.INT, Constants.OBJECTS[1]);
33 case TypeDesc.INT_CODE:
34 mb.invokeStatic("java/lang/Integer", "hashCode", TypeDesc.INT, new TypeDesc[] {TypeDesc.INT});
36 case TypeDesc.LONG_CODE:
37 mb.invokeStatic("java/lang/Long", "hashCode", TypeDesc.LONG, new TypeDesc[] {TypeDesc.LONG});
39 case TypeDesc.DOUBLE_CODE:
40 mb.invokeStatic("java/lang/Double", "hashCode", TypeDesc.DOUBLE, new TypeDesc[] {TypeDesc.DOUBLE});
42 case TypeDesc.FLOAT_CODE:
43 mb.invokeStatic("java/lang/Float", "hashCode", TypeDesc.FLOAT, new TypeDesc[] {TypeDesc.FLOAT});
45 case TypeDesc.SHORT_CODE:
46 mb.invokeStatic("java/lang/Short", "hashCode", TypeDesc.SHORT, new TypeDesc[] {TypeDesc.SHORT});