]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/TForAll.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / types / TForAll.java
1 package org.simantics.scl.compiler.types;
2
3 import gnu.trove.map.hash.THashMap;
4 import gnu.trove.map.hash.TObjectIntHashMap;
5 import gnu.trove.set.hash.THashSet;
6
7 import java.util.ArrayList;
8
9 import org.simantics.scl.compiler.environment.Environment;
10 import org.simantics.scl.compiler.internal.types.TypeHashCodeContext;
11 import org.simantics.scl.compiler.internal.types.ast.TForAllAst;
12 import org.simantics.scl.compiler.internal.types.ast.TypeAst;
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 import org.simantics.scl.compiler.types.util.Polarity;
17 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
18
19
20 public class TForAll extends Type {
21     public final TVar var;
22     public final Type type;
23     
24     TForAll(TVar var, Type type) {
25         if(NULL_CHECKS) {
26             if(var == null || type == null)
27                 throw new NullPointerException();
28         }
29         this.var = var;
30         this.type = type;
31     }
32     
33     private TForAll create(Type type) {
34         if(type == this.type)
35             return this;
36         else
37             return new TForAll(var, type);
38     }
39
40     @Override
41     public TForAll replace(TVar var, Type replacement) {
42         if(var == this.var) {
43             if(replacement instanceof TVar) {
44                return new TForAll((TVar)replacement, type.replace(var, replacement));
45             }
46             else
47                 throw new IllegalStateException("Tried to replace a variable that is not free in the type.");
48         }
49         else
50             return create(type.replace(var, replacement));
51     }
52
53     @Override
54     public TypeAst toTypeAst(TypeUnparsingContext context) {
55         ArrayList<String> vars = new ArrayList<String>();
56         vars.add(context.getName(var));
57         Type cur = Types.canonical(type);
58         while(cur instanceof TForAll) {
59             TForAll forAll = (TForAll)cur;
60             vars.add(context.getName(forAll.var));
61             cur = Types.canonical(forAll.type);
62         }
63         return new TForAllAst(
64                 vars.toArray(new String[vars.size()]), 
65                 cur.toTypeAst(context));
66     }
67     
68     @Override
69     public void updateHashCode(TypeHashCodeContext context) {
70         context.append(TypeHashCodeContext.FORALL);
71         TObjectIntHashMap<TVar> varHashCode = context.createVarHashCode();
72         varHashCode.put(var, varHashCode.size());
73         type.updateHashCode(context);
74         varHashCode.remove(var);
75     }
76
77     @Override
78     public void collectFreeVars(ArrayList<TVar> vars) {
79         type.collectFreeVars(vars);
80         vars.remove(var);
81     }
82     
83     @Override
84     public void collectMetaVars(ArrayList<TMetaVar> vars) {
85         type.collectMetaVars(vars);
86     }
87     
88     @Override
89     public void collectMetaVars(THashSet<TMetaVar> vars) {
90         type.collectMetaVars(vars);
91     }
92     
93     @Override
94     public void collectEffectMetaVars(ArrayList<TMetaVar> vars) {
95         type.collectEffectMetaVars(vars);
96     }
97     
98     @Override
99     public boolean contains(TMetaVar other) {
100         return type.contains(other);
101     }
102     
103     @Override
104     public Type convertMetaVarsToVars() {
105         Type newType = type.convertMetaVarsToVars();
106         if(newType == type)
107             return this;
108         else
109             return new TForAll(var, type);
110     }
111
112     @Override
113     public boolean isGround() {
114         return false; // The method is usually not called for quantified types
115                       // so it is unclear what it should do here.
116     }
117
118         public Kind inferKind(Environment context) throws KindUnificationException {
119         type.checkKind(context, Kinds.STAR);
120         return Kinds.STAR;
121     }
122
123     @Override
124     public boolean containsMetaVars() {
125         return type.containsMetaVars();
126     }
127
128     @Override
129     public void toName(TypeUnparsingContext context, StringBuilder b) {
130         type.toName(context, b);
131     }
132     
133     @Override
134     public int getClassId() {
135         return FORALL_ID;
136     }
137
138     @Override
139     public void addPolarity(Polarity polarity) {
140         type.addPolarity(polarity);
141     }
142
143     @Override
144     public Type head() {
145         return this;
146     }
147
148     @Override
149     public Type copySkeleton(THashMap<TMetaVar, TMetaVar> metaVarMap) {
150         // Should never get here
151         return new TMetaVar(Kinds.STAR);
152     }
153 }