]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/modules/TypeClassMethod.java
(refs #7250) Merging master, minor CHR bugfixes
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / modules / TypeClassMethod.java
1 package org.simantics.scl.compiler.elaboration.modules;
2
3 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
4 import org.simantics.scl.compiler.common.names.Name;
5 import org.simantics.scl.compiler.constants.Constant;
6 import org.simantics.scl.compiler.constants.JavaTypeClassMethod;
7 import org.simantics.scl.compiler.types.Type;
8 import org.simantics.scl.compiler.types.Types;
9 import org.simantics.scl.compiler.types.util.MultiFunction;
10
11 public class TypeClassMethod {
12     
13     TypeClass typeClass;
14     String name;
15     String javaName;
16     Type baseType;
17     int arity;
18     Name defaultImplementation;
19     public long location;
20     
21     public TypeClassMethod(TypeClass typeClass, String name, String javaName,
22             Type baseType, int arity, long location) {
23         if(typeClass == null) throw new NullPointerException();
24         if(name == null) throw new NullPointerException();
25         if(javaName == null) throw new NullPointerException();
26         if(baseType == null) throw new NullPointerException();
27         this.typeClass = typeClass;
28         this.name = name;
29         this.javaName = javaName;
30         this.baseType = baseType;
31         this.arity = arity;
32         this.location = location;
33     }
34
35     public void setDefaultImplementation(Name defaultImplementation) {
36         if(this.defaultImplementation != null)
37             throw new InternalCompilerError("Default method implementation already defined");
38         this.defaultImplementation = defaultImplementation;
39     }
40     
41     public Name getDefaultImplementation() {
42         return defaultImplementation;
43     }
44     
45     public String getName() {
46         return name;
47     }
48     
49     public String getJavaName() {
50         return javaName;
51     }
52     
53     public Type getBaseType() {
54         return baseType;
55     }
56     
57     public int getArity() {
58         return arity;
59     }
60
61     public Type getType() {
62         return Types.closure(Types.constrained(
63                 typeClass.class_, 
64                 baseType));
65     }
66     
67     public TypeClass getTypeClass() {
68         return typeClass;
69     }
70
71     public SCLValue createValue() {
72         String moduleName = typeClass.name.module;
73         SCLValue value = new SCLValue(Name.create(moduleName, name));
74         MultiFunction mfun = Types.matchFunction(baseType);
75         Type[] parameterTypes = new Type[mfun.parameterTypes.length + 1];
76         System.arraycopy(mfun.parameterTypes, 0, parameterTypes, 1, mfun.parameterTypes.length);
77         parameterTypes[0] = typeClass.class_;
78         // FIXME totally incorrect type for the method
79         Constant constant = new JavaTypeClassMethod(
80                 this,
81                 typeClass.javaName, 
82                 javaName, 
83                 mfun.effect,
84                 mfun.returnType, 
85                 parameterTypes);
86         value.setValue(constant);
87         value.setType(getType());
88         return value;
89     }
90     
91 }