]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/contexts/TypeTranslationContext.java
(refs #7588) Support for existential type variables with syntax ?v
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / contexts / TypeTranslationContext.java
1 package org.simantics.scl.compiler.elaboration.contexts;
2
3 import org.simantics.scl.compiler.compilation.CompilationContext;
4 import org.simantics.scl.compiler.environment.Environment;
5 import org.simantics.scl.compiler.errors.ErrorLog;
6 import org.simantics.scl.compiler.internal.parsing.exceptions.SCLSyntaxErrorException;
7 import org.simantics.scl.compiler.internal.parsing.types.TypeAst;
8 import org.simantics.scl.compiler.types.TCon;
9 import org.simantics.scl.compiler.types.TMetaVar;
10 import org.simantics.scl.compiler.types.TPred;
11 import org.simantics.scl.compiler.types.TVar;
12 import org.simantics.scl.compiler.types.Type;
13 import org.simantics.scl.compiler.types.Types;
14 import org.simantics.scl.compiler.types.exceptions.KindUnificationException;
15 import org.simantics.scl.compiler.types.kinds.Kind;
16 import org.simantics.scl.compiler.types.kinds.Kinds;
17
18 import gnu.trove.map.hash.THashMap;
19
20 public class TypeTranslationContext {
21     
22     CompilationContext compilationContext;
23     
24     Environment environment;
25    
26     ErrorLog errorLog;    
27     
28     THashMap<String, TVar> typeVariables = new THashMap<String, TVar>();
29     THashMap<String, TMetaVar> existentials;
30     
31     public TypeTranslationContext(CompilationContext compilationContext) {
32         this.compilationContext = compilationContext;
33         this.errorLog = compilationContext.errorLog;
34         this.environment = compilationContext.environment;
35     }
36     
37     /**
38      * Convert a type AST node to a Type instance, using STAR as the expected kind.
39      * If the conversion fails with a syntax error exception, the returned value
40      * is a new type meta-variable.
41      * 
42      * @param typeAst  An AST node representing a type
43      * @return  A Type instance.
44      */
45     public Type toType(TypeAst typeAst) {
46         return toType(typeAst, Kinds.STAR);
47     }
48
49     /**
50      * Convert a type AST node to a Type instance.
51      * If the conversion fails with a syntax error exception, the returned value
52      * is a new type meta-variable.
53      * 
54      * @param typeAst  An AST node representing a type
55      * @param kind  A kind as which the node should be interpreted as
56      * @return  A Type instance.
57      */
58     public Type toType(TypeAst typeAst, Kind kind) {
59         Type type;
60         try {
61             type = typeAst.toType(this, kind);
62         } catch(SCLSyntaxErrorException e) {
63             errorLog.log(e.location, e.getMessage());
64             return Types.metaVar(kind);
65         }
66         /*try {
67             type.checkKind(kindingContext, kind);
68         } catch (KindUnificationException e) {
69             errorLog.log(typeAst, "Invalid type " + type + ".");
70         }*/
71         return type;
72     }
73     
74     public TVar resolveTypeVariable(long loc, String name, Kind expectedKind) {
75         TVar var = typeVariables.get(name);
76         if(var == null) {
77             var = Types.var(expectedKind);
78             typeVariables.put(name, var);
79         }        
80         else
81             unify(loc, var.getKind(), expectedKind);
82         return var;
83     }
84
85     public TMetaVar resolveExistential(long loc, String name, Kind expectedKind) {
86         if(existentials == null)
87             existentials = new THashMap<String, TMetaVar>();
88         TMetaVar var = existentials.get(name);
89         if(var == null) {
90             var = Types.metaVar(expectedKind);
91             existentials.put(name, var);
92         }        
93         else
94             unify(loc, var.getKind(), expectedKind);
95         return var;
96     }
97     
98     public TVar pushTypeVar(String name) {
99         return typeVariables.put(name, Types.var(Kinds.metaVar()));
100     }
101     
102     public TVar addTypeVar(String name) {
103         TVar var = Types.var(Kinds.metaVar());
104         typeVariables.put(name, var);
105         return var;
106     }
107     
108     public TVar popTypeVar(String name, TVar var) {
109         if(var == null)
110             return typeVariables.remove(name);
111         else
112             return typeVariables.put(name, var);
113     }
114     
115     public TPred toTFuncApply(TypeAst typeAst) {
116         return typeAst.toTFuncApply(this);
117     }
118
119     public ErrorLog getErrorLog() {
120         return errorLog;
121     }
122     
123     public Environment getEnvironment() {
124         return environment;
125     }
126
127     public Kind getKind(TCon con) {
128         return environment.getTypeDescriptor(con).getKind();
129     }
130
131     /**
132      * Unify the given kinds. An error message is logged, if the unification fails.
133      * @param loc  location identifier
134      * @param provided  actual kind
135      * @param expectedKind  expected kind
136      */
137     public void unify(long loc, Kind provided, Kind expectedKind) {
138         try {
139             Kinds.unify(provided, expectedKind);
140         } catch (KindUnificationException e) {
141             errorLog.log(loc, "Expected a type with kind " + expectedKind + " but got " + provided + ".");
142         }
143     }
144     
145     public CompilationContext getCompilationContext() {
146         return compilationContext;
147     }
148 }