1 package org.simantics.scl.compiler.constants;
3 import java.util.Arrays;
5 import org.cojen.classfile.TypeDesc;
6 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
7 import org.simantics.scl.compiler.internal.codegen.references.Val;
8 import org.simantics.scl.compiler.internal.codegen.types.JavaTypeTranslator;
9 import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
10 import org.simantics.scl.compiler.types.Type;
11 import org.simantics.scl.compiler.types.Types;
13 public class JavaConstructor extends FunctionValue {
16 boolean hasStaticInstance = false;
18 TypeDesc[] parameterTypeDescs;
20 public JavaConstructor(String className, Type effect, Type returnType, Type ... parameterTypes) {
21 super(Types.freeVarsArray(Types.functionE(parameterTypes,
23 effect, returnType, parameterTypes);
24 this.className = className;
27 public JavaConstructor(String className, Type effect, TypeDesc[] parameterTypeDescs,
28 Type returnType, Type ... parameterTypes) {
29 super(Types.freeVarsArray(Types.functionE(parameterTypes,
32 returnType, parameterTypes);
33 this.className = className;
34 this.parameterTypeDescs = parameterTypeDescs;
38 public Type applyExact(MethodBuilder mb, Val[] parameters) {
39 if(parameterTypeDescs == null) {
40 JavaTypeTranslator tt = mb.getJavaTypeTranslator();
41 parameterTypeDescs = tt.toTypeDescs(parameterTypes);
44 TypeDesc typeDesc = TypeDesc.forClass(className);
45 if(hasStaticInstance) {
46 mb.loadStaticField(typeDesc, "INSTANCE", typeDesc);
49 mb.newObject(typeDesc);
51 mb.push(parameters, parameterTypes);
52 mb.invokeConstructor(className, parameterTypeDescs);
53 // cb.checkCast(tt.toTypeDesc(returnType));
56 return getReturnType();
60 public String toString() {
61 return className + ".<init>";
64 public void setHasStaticInstance(boolean hasStaticInstance) {
65 if(parameterTypes.length > 0)
66 throw new InternalCompilerError();
67 this.hasStaticInstance = hasStaticInstance;
71 public int hashCode() {
74 result = prime * result
75 + ((className == null) ? 0 : className.hashCode());
76 result = prime * result + (hasStaticInstance ? 1231 : 1237);
77 result = prime * result + Arrays.hashCode(parameterTypeDescs);
82 public boolean equals(Object obj) {
87 if (getClass() != obj.getClass())
89 JavaConstructor other = (JavaConstructor) obj;
90 if (className == null) {
91 if (other.className != null)
93 } else if (!className.equals(other.className))
95 if (hasStaticInstance != other.hasStaticInstance)
97 if (!Arrays.equals(parameterTypeDescs, other.parameterTypeDescs))