]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/types/JavaTypeTranslator.java
Added info on backup location to documentation backup.
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / codegen / types / JavaTypeTranslator.java
1 package org.simantics.scl.compiler.internal.codegen.types;
2
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;
18
19 public class JavaTypeTranslator {
20
21     Environment environment;
22     Type[] parameters = new Type[32];
23     
24     public JavaTypeTranslator(Environment environment) {
25         this.environment = environment;
26     }
27     
28     private void reverseParameters(int len) {
29         len >>= 1;
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;
34         }
35     }
36         
37     public TypeDesc toTypeDesc(Type type) {
38         while(true) {
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);
45             }
46             else if(type instanceof TApply) {
47                 int i=0;
48                 while(true) {
49                     TApply apply = (TApply)type;
50                     parameters[i++] = Types.canonical(apply.parameter);
51                     type = Types.canonical(apply.function);
52                     if(type instanceof TCon) {
53                         reverseParameters(i);
54                         return environment.getTypeConstructor((TCon)type).construct(this, parameters);
55                     }
56                     else if(type instanceof TApply)
57                         ;
58                     else if(type instanceof TVar)
59                         return TypeDesc.OBJECT;
60                     else
61                         break;
62                 }
63             }
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);
74             }
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;
80             }
81             else {
82                 throw new IllegalArgumentException("Invalid type " + type + ".");
83             }
84         }
85     }
86     
87     public TypeDesc getTypeDesc(Typed typed) {
88         return toTypeDesc(typed.getType());
89     }
90     
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]);
95         return result;
96     }
97     
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);
103         return result;
104     }
105     
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]);
110         return result;
111     }
112     
113     public static TypeDesc[] filterVoid(TypeDesc[] tds) {
114         int length = tds.length;
115         for(TypeDesc td : tds)
116             if(td.equals(TypeDesc.VOID))
117                 --length;
118         if(length == tds.length)
119             return tds;
120         TypeDesc[] result = new TypeDesc[length];
121         int j=0;
122         for(TypeDesc td : tds)
123             if(!td.equals(TypeDesc.VOID))
124                 result[j++] = td;
125         return result;
126     }
127     
128     public static TypeDesc toObjectType(TypeDesc td) {
129         if(td.equals(TypeDesc.VOID))
130             return Constants.TUPLE0;
131         else
132             return td.toObjectType();
133     }
134 }