X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Fconstants%2FJavaStaticMethod.java;fp=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Fconstants%2FJavaStaticMethod.java;h=9e071e9cb9002c700d9c398fec92a3b4eafca8ff;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/constants/JavaStaticMethod.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/constants/JavaStaticMethod.java new file mode 100644 index 000000000..9e071e9cb --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/constants/JavaStaticMethod.java @@ -0,0 +1,97 @@ +package org.simantics.scl.compiler.constants; + +import org.cojen.classfile.TypeDesc; +import org.simantics.scl.compiler.common.exceptions.InternalCompilerError; +import org.simantics.scl.compiler.internal.codegen.references.Val; +import org.simantics.scl.compiler.internal.codegen.types.JavaTypeTranslator; +import org.simantics.scl.compiler.internal.codegen.utils.ClassBuilder; +import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder; +import org.simantics.scl.compiler.top.SCLCompilerConfiguration; +import org.simantics.scl.compiler.types.TVar; +import org.simantics.scl.compiler.types.Type; +import org.simantics.scl.compiler.types.Types; + +public class JavaStaticMethod extends FunctionValue { + + String className; + String methodName; + + TypeDesc returnTypeDesc; + TypeDesc[] parameterTypeDescs; + + public JavaStaticMethod(String className, String methodName, Type effect, TVar[] typeParameters, Type returnType, Type ... parameterTypes) { + super(typeParameters, effect, returnType, parameterTypes); + if(SCLCompilerConfiguration.DEBUG) { + if(className.contains("/")) + throw new InternalCompilerError(); + } + ClassBuilder.checkClassName(className); + this.className = className; + this.methodName = methodName; + } + + public JavaStaticMethod(String className, String methodName, Type effect, Type returnType, Type ... parameterTypes) { + super(Types.freeVarsArray(Types.functionE(parameterTypes, effect, returnType)), effect, returnType, parameterTypes); + if(SCLCompilerConfiguration.DEBUG) { + if(className.contains("/")) + throw new InternalCompilerError(); + } + ClassBuilder.checkClassName(className); + this.className = className; + this.methodName = methodName; + } + + public JavaStaticMethod(String className, String methodName, + Type effect, + TypeDesc returnTypeDesc, TypeDesc[] parameterTypeDescs, + Type returnType, Type ... parameterTypes) { + super(Types.freeVarsArray(Types.functionE(parameterTypes, effect, returnType)), effect, returnType, parameterTypes); + if(SCLCompilerConfiguration.DEBUG) { + if(className == null) + throw new NullPointerException(); + if(methodName == null) + throw new NullPointerException(); + if(parameterTypeDescs != null) + for(TypeDesc td : parameterTypeDescs) + if(td.equals(TypeDesc.VOID)) + throw new InternalCompilerError(); + } + ClassBuilder.checkClassName(className); + this.className = className; + this.methodName = methodName; + this.returnTypeDesc = returnTypeDesc; + this.parameterTypeDescs = parameterTypeDescs; + } + + public JavaStaticMethod(String className, String methodName, + TypeDesc returnTypeDesc, TypeDesc[] parameterTypeDescs, + Type returnType, Type ... parameterTypes) { + this(className, methodName, Types.NO_EFFECTS, + returnTypeDesc, parameterTypeDescs, + returnType, + parameterTypes); + } + + @Override + public Type applyExact(MethodBuilder mb, Val[] parameters) { + if(returnTypeDesc == null) { + JavaTypeTranslator tt = mb.getJavaTypeTranslator(); + returnTypeDesc = tt.toTypeDesc(returnType); + parameterTypeDescs = JavaTypeTranslator.filterVoid( + tt.toTypeDescs(parameterTypes)); + } + + mb.push(parameters, getParameterTypes()); + mb.invokeStatic(className, methodName, + returnTypeDesc, + parameterTypeDescs); + + return getReturnType(); + } + + @Override + public String toString() { + return className + "#" + methodName; + } + +}