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