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