]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/HashCodeFunction.java
Adding Marker-support to Logging-module
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / java / HashCodeFunction.java
1 package org.simantics.scl.compiler.elaboration.java;
2
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;
12
13 public class HashCodeFunction extends FunctionValue {
14     private static final TVar A = Types.var(Kinds.STAR);
15     public static final HashCodeFunction INSTANCE = new HashCodeFunction();
16     
17     private HashCodeFunction() {
18         super(new TVar[] {A}, Types.NO_EFFECTS, Types.INTEGER, A);
19     }
20
21     @Override
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]);
29             break;
30         case TypeDesc.OBJECT_CODE:
31             mb.invokeStatic("java/util/Objects", "hashCode", TypeDesc.INT, Constants.OBJECTS[1]);
32             break;
33         case TypeDesc.INT_CODE:
34             mb.invokeStatic("java/lang/Integer", "hashCode", TypeDesc.INT, new TypeDesc[] {TypeDesc.INT});
35             break;
36         case TypeDesc.LONG_CODE:
37             mb.invokeStatic("java/lang/Long", "hashCode", TypeDesc.LONG, new TypeDesc[] {TypeDesc.LONG});
38             break;
39         case TypeDesc.DOUBLE_CODE:
40             mb.invokeStatic("java/lang/Double", "hashCode", TypeDesc.DOUBLE, new TypeDesc[] {TypeDesc.DOUBLE});
41             break;
42         case TypeDesc.FLOAT_CODE:
43             mb.invokeStatic("java/lang/Float", "hashCode", TypeDesc.FLOAT, new TypeDesc[] {TypeDesc.FLOAT});
44             break;
45         case TypeDesc.SHORT_CODE:
46             mb.invokeStatic("java/lang/Short", "hashCode", TypeDesc.SHORT, new TypeDesc[] {TypeDesc.SHORT});
47             break;
48         }
49         return Types.INTEGER;
50     }
51 }