1 package org.simantics.scl.compiler.internal.codegen.types;
3 import org.cojen.classfile.TypeDesc;
4 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
5 import org.simantics.scl.compiler.elaboration.modules.TypeConstructor;
6 import org.simantics.scl.compiler.environment.Environment;
7 import org.simantics.scl.compiler.internal.codegen.utils.Constants;
8 import org.simantics.scl.compiler.types.TApply;
9 import org.simantics.scl.compiler.types.TCon;
10 import org.simantics.scl.compiler.types.TForAll;
11 import org.simantics.scl.compiler.types.TFun;
12 import org.simantics.scl.compiler.types.TMetaVar;
13 import org.simantics.scl.compiler.types.TPred;
14 import org.simantics.scl.compiler.types.TVar;
15 import org.simantics.scl.compiler.types.Type;
16 import org.simantics.scl.compiler.types.Types;
17 import org.simantics.scl.compiler.types.util.Typed;
19 public class JavaTypeTranslator {
21 Environment environment;
22 Type[] parameters = new Type[32];
24 public JavaTypeTranslator(Environment environment) {
25 this.environment = environment;
28 private void reverseParameters(int len) {
30 for(int i=0;i<len;++i) {
31 Type temp = parameters[i];
32 parameters[i] = parameters[len-i-1];
33 parameters[len-i-1] = temp;
37 public TypeDesc toTypeDesc(Type type) {
39 if(type instanceof TCon) {
40 TCon con = (TCon)type;
41 TypeConstructor typeConstructor = environment.getTypeConstructor(con);
42 if(typeConstructor == null)
43 throw new InternalCompilerError("Didn't find type constructor " + con.module + "/" + con.name + ".");
44 return typeConstructor.construct(this, Type.EMPTY_ARRAY);
46 else if(type instanceof TApply) {
49 TApply apply = (TApply)type;
50 parameters[i++] = Types.canonical(apply.parameter);
51 type = Types.canonical(apply.function);
52 if(type instanceof TCon) {
54 return environment.getTypeConstructor((TCon)type).construct(this, parameters);
56 else if(type instanceof TApply)
58 else if(type instanceof TVar)
59 return TypeDesc.OBJECT;
64 else if(type instanceof TVar)
65 return TypeDesc.OBJECT;
66 else if(type instanceof TFun)
67 return Constants.FUNCTION;
68 else if(type instanceof TForAll)
69 type = ((TForAll)type).type;
70 else if(type instanceof TPred) {
71 TPred funcApply = (TPred)type;
72 return environment.getTypeClass(funcApply.typeClass)
73 .construct(this, funcApply.parameters);
75 else if(type instanceof TMetaVar) {
76 TMetaVar metaVar = (TMetaVar)type;
77 type = Types.canonical(metaVar);
78 if(type instanceof TMetaVar)
79 return TypeDesc.OBJECT;
82 throw new IllegalArgumentException("Invalid type " + type + ".");
87 public TypeDesc getTypeDesc(Typed typed) {
88 return toTypeDesc(typed.getType());
91 public TypeDesc[] toTypeDescs(Type[] types) {
92 TypeDesc[] result = new TypeDesc[types.length];
93 for(int i=0;i<types.length;++i)
94 result[i] = toTypeDesc(types[i]);
98 public TypeDesc[] toTypeDescs(Type[] types, Type type) {
99 TypeDesc[] result = new TypeDesc[types.length+1];
100 for(int i=0;i<types.length;++i)
101 result[i] = toTypeDesc(types[i]);
102 result[types.length] = toTypeDesc(type);
106 public TypeDesc[] getTypeDescs(Typed[] typeds) {
107 TypeDesc[] result = new TypeDesc[typeds.length];
108 for(int i=0;i<typeds.length;++i)
109 result[i] = getTypeDesc(typeds[i]);
113 public static TypeDesc[] filterVoid(TypeDesc[] tds) {
114 int length = tds.length;
115 for(TypeDesc td : tds)
116 if(td.equals(TypeDesc.VOID))
118 if(length == tds.length)
120 TypeDesc[] result = new TypeDesc[length];
122 for(TypeDesc td : tds)
123 if(!td.equals(TypeDesc.VOID))
128 public static TypeDesc toObjectType(TypeDesc td) {
129 if(td.equals(TypeDesc.VOID))
130 return Constants.TUPLE0;
132 return td.toObjectType();