1 package org.simantics.scl.compiler.elaboration.contexts;
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;
18 import gnu.trove.map.hash.THashMap;
20 public class TypeTranslationContext {
22 CompilationContext compilationContext;
24 Environment environment;
28 THashMap<String, TVar> typeVariables = new THashMap<String, TVar>();
29 THashMap<String, TMetaVar> existentials;
31 public TypeTranslationContext(CompilationContext compilationContext) {
32 this.compilationContext = compilationContext;
33 this.errorLog = compilationContext.errorLog;
34 this.environment = compilationContext.environment;
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.
42 * @param typeAst An AST node representing a type
43 * @return A Type instance.
45 public Type toType(TypeAst typeAst) {
46 return toType(typeAst, Kinds.STAR);
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.
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.
58 public Type toType(TypeAst typeAst, Kind kind) {
61 type = typeAst.toType(this, kind);
62 } catch(SCLSyntaxErrorException e) {
63 errorLog.log(e.location, e.getMessage());
64 return Types.metaVar(kind);
67 type.checkKind(kindingContext, kind);
68 } catch (KindUnificationException e) {
69 errorLog.log(typeAst, "Invalid type " + type + ".");
74 public TVar resolveTypeVariable(long loc, String name, Kind expectedKind) {
75 TVar var = typeVariables.get(name);
77 var = Types.var(expectedKind);
78 typeVariables.put(name, var);
81 unify(loc, var.getKind(), expectedKind);
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);
90 var = Types.metaVar(expectedKind);
91 existentials.put(name, var);
94 unify(loc, var.getKind(), expectedKind);
98 public TVar pushTypeVar(String name) {
99 return typeVariables.put(name, Types.var(Kinds.metaVar()));
102 public TVar addTypeVar(String name) {
103 TVar var = Types.var(Kinds.metaVar());
104 typeVariables.put(name, var);
108 public TVar popTypeVar(String name, TVar var) {
110 return typeVariables.remove(name);
112 return typeVariables.put(name, var);
115 public TPred toTFuncApply(TypeAst typeAst) {
116 return typeAst.toTFuncApply(this);
119 public ErrorLog getErrorLog() {
123 public Environment getEnvironment() {
127 public Kind getKind(TCon con) {
128 return environment.getTypeDescriptor(con).getKind();
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
137 public void unify(long loc, Kind provided, Kind expectedKind) {
139 Kinds.unify(provided, expectedKind);
140 } catch (KindUnificationException e) {
141 errorLog.log(loc, "Expected a type with kind " + expectedKind + " but got " + provided + ".");
145 public CompilationContext getCompilationContext() {
146 return compilationContext;