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.ClassBuilder;
10 import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
11 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;
12 import org.simantics.scl.compiler.types.Type;
13 import org.simantics.scl.compiler.types.Types;
15 public class JavaMethod extends FunctionValue {
20 TypeDesc returnTypeDesc;
21 TypeDesc[] parameterTypeDescs;
25 public JavaMethod(boolean virtual, String className, String methodName,
27 TypeDesc returnTypeDesc, TypeDesc[] parameterTypeDescs,
28 Type returnType, Type ... parameterTypes) {
29 super(Types.freeVarsArray(
30 Types.functionE(parameterTypes, effect, returnType)),
32 returnType, parameterTypes);
33 if(SCLCompilerConfiguration.DEBUG) {
35 throw new NullPointerException();
36 if(methodName == null)
37 throw new NullPointerException();
38 if(parameterTypeDescs != null)
39 for(TypeDesc td : parameterTypeDescs)
40 if(td.equals(TypeDesc.VOID))
41 throw new InternalCompilerError();
43 ClassBuilder.checkClassName(className);
44 this.virtual = virtual;
45 this.className = className;
46 this.methodName = methodName;
47 this.returnTypeDesc = returnTypeDesc;
48 this.parameterTypeDescs = parameterTypeDescs;
51 public JavaMethod(boolean virtual, String className, String methodName, Type effect,
52 Type returnType, Type ... parameterTypes) {
53 this(virtual, className, methodName, effect, null, null, returnType, parameterTypes);
57 public Type applyExact(MethodBuilder mb, Val[] parameters) {
58 if(returnTypeDesc == null) {
59 JavaTypeTranslator tt = mb.getJavaTypeTranslator();
60 returnTypeDesc = tt.toTypeDesc(returnType);
61 parameterTypeDescs = JavaTypeTranslator.filterVoid(
62 tt.toTypeDescs(Arrays.copyOfRange(parameterTypes, 1, parameterTypes.length)));
65 mb.push(parameters, parameterTypes);
67 mb.invokeVirtual(className, methodName, returnTypeDesc, parameterTypeDescs);
69 mb.invokeInterface(className, methodName, returnTypeDesc, parameterTypeDescs);
71 return getReturnType();
75 public String toString() {
76 return className + "." + methodName;