]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/constants/JavaStaticMethod.java
Merge "(refs #7621) Fixed handling of $ in application ignoring return value"
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / constants / JavaStaticMethod.java
1 package org.simantics.scl.compiler.constants;
2
3 import org.cojen.classfile.TypeDesc;
4 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
5 import org.simantics.scl.compiler.internal.codegen.references.Val;
6 import org.simantics.scl.compiler.internal.codegen.types.JavaTypeTranslator;
7 import org.simantics.scl.compiler.internal.codegen.utils.ClassBuilder;
8 import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
9 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;
10 import org.simantics.scl.compiler.types.TVar;
11 import org.simantics.scl.compiler.types.Type;
12 import org.simantics.scl.compiler.types.Types;
13
14 public class JavaStaticMethod extends FunctionValue {
15     
16     String className;
17     String methodName;
18     
19     TypeDesc returnTypeDesc;
20     TypeDesc[] parameterTypeDescs;
21     
22     public JavaStaticMethod(String className, String methodName, Type effect, TVar[] typeParameters, Type returnType, Type ... parameterTypes) {
23         super(typeParameters, effect, returnType, parameterTypes);
24         if(SCLCompilerConfiguration.DEBUG) {
25             if(className.contains("/"))
26                 throw new InternalCompilerError();
27         }
28         ClassBuilder.checkClassName(className);
29         this.className = className;
30         this.methodName = methodName;
31     }
32     
33     public JavaStaticMethod(String className, String methodName, Type effect, Type returnType, Type ... parameterTypes) {
34         super(Types.freeVarsArray(Types.functionE(parameterTypes, effect, returnType)), effect, returnType, parameterTypes);
35         if(SCLCompilerConfiguration.DEBUG) {
36             if(className.contains("/"))
37                 throw new InternalCompilerError();
38         }
39         ClassBuilder.checkClassName(className);
40         this.className = className;
41         this.methodName = methodName;
42     }
43    
44     public JavaStaticMethod(String className, String methodName, 
45             Type effect,
46             TypeDesc returnTypeDesc, TypeDesc[] parameterTypeDescs, 
47             Type returnType, Type ... parameterTypes) {
48         super(Types.freeVarsArray(Types.functionE(parameterTypes, effect, returnType)), effect, returnType, parameterTypes);
49         if(SCLCompilerConfiguration.DEBUG) {
50             if(className == null)
51                 throw new NullPointerException();
52             if(methodName == null)
53                 throw new NullPointerException();
54             if(parameterTypeDescs != null)
55                 for(TypeDesc td : parameterTypeDescs)
56                     if(td.equals(TypeDesc.VOID))
57                         throw new InternalCompilerError();
58         }
59         ClassBuilder.checkClassName(className);
60         this.className = className;
61         this.methodName = methodName;
62         this.returnTypeDesc = returnTypeDesc;
63         this.parameterTypeDescs = parameterTypeDescs;
64     }
65     
66     public JavaStaticMethod(String className, String methodName, 
67             TypeDesc returnTypeDesc, TypeDesc[] parameterTypeDescs, 
68             Type returnType, Type ... parameterTypes) {
69         this(className, methodName, Types.NO_EFFECTS, 
70                 returnTypeDesc, parameterTypeDescs, 
71                 returnType, 
72                 parameterTypes);
73     }
74     
75     @Override
76     public Type applyExact(MethodBuilder mb, Val[] parameters) {        
77         if(returnTypeDesc == null) {
78             JavaTypeTranslator tt = mb.getJavaTypeTranslator();
79             returnTypeDesc = tt.toTypeDesc(returnType);
80             parameterTypeDescs = JavaTypeTranslator.filterVoid(
81                     tt.toTypeDescs(parameterTypes));
82         }
83         
84         mb.push(parameters, getParameterTypes());
85         mb.invokeStatic(className, methodName, 
86                 returnTypeDesc, 
87                 parameterTypeDescs);
88         
89         return getReturnType();
90     }
91     
92     @Override
93     public String toString() {
94         return className + "#" + methodName;
95     }
96
97 }