1 package org.simantics.scl.compiler.elaboration.modules;
\r
3 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
\r
4 import org.simantics.scl.compiler.common.names.Name;
\r
5 import org.simantics.scl.compiler.constants.Constant;
\r
6 import org.simantics.scl.compiler.constants.JavaTypeClassMethod;
\r
7 import org.simantics.scl.compiler.types.Type;
\r
8 import org.simantics.scl.compiler.types.Types;
\r
9 import org.simantics.scl.compiler.types.util.MultiFunction;
\r
11 public class TypeClassMethod {
\r
13 TypeClass typeClass;
\r
18 Name defaultImplementation;
\r
19 public long location;
\r
21 public TypeClassMethod(TypeClass typeClass, String name, String javaName,
\r
22 Type baseType, int arity, long location) {
\r
23 if(typeClass == null) throw new NullPointerException();
\r
24 if(name == null) throw new NullPointerException();
\r
25 if(javaName == null) throw new NullPointerException();
\r
26 if(baseType == null) throw new NullPointerException();
\r
27 this.typeClass = typeClass;
\r
29 this.javaName = javaName;
\r
30 this.baseType = baseType;
\r
32 this.location = location;
\r
35 public void setDefaultImplementation(Name defaultImplementation) {
\r
36 if(this.defaultImplementation != null)
\r
37 throw new InternalCompilerError("Default method implementation already defined");
\r
38 this.defaultImplementation = defaultImplementation;
\r
41 public Name getDefaultImplementation() {
\r
42 return defaultImplementation;
\r
45 public String getName() {
\r
49 public String getJavaName() {
\r
53 public Type getBaseType() {
\r
57 public int getArity() {
\r
61 public Type getType() {
\r
62 return Types.closure(Types.constrained(
\r
67 public TypeClass getTypeClass() {
\r
71 public SCLValue createValue() {
\r
72 String moduleName = typeClass.name.module;
\r
73 SCLValue value = new SCLValue(Name.create(moduleName, name));
\r
74 MultiFunction mfun = Types.matchFunction(baseType);
\r
75 Type[] parameterTypes = new Type[mfun.parameterTypes.length + 1];
\r
76 System.arraycopy(mfun.parameterTypes, 0, parameterTypes, 1, mfun.parameterTypes.length);
\r
77 parameterTypes[0] = typeClass.class_;
\r
78 // FIXME totally incorrect type for the method
\r
79 Constant constant = new JavaTypeClassMethod(
\r
81 typeClass.javaName,
\r
86 value.setValue(constant);
\r
87 value.setType(getType());
\r