]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/TVar.java
Merge "Re-enabled Acorn transaction cancellation support for testing"
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / types / TVar.java
1 package org.simantics.scl.compiler.types;
2
3 import java.util.ArrayList;
4
5 import org.simantics.scl.compiler.environment.Environment;
6 import org.simantics.scl.compiler.internal.types.HashCodeUtils;
7 import org.simantics.scl.compiler.internal.types.TypeHashCodeContext;
8 import org.simantics.scl.compiler.internal.types.ast.TVarAst;
9 import org.simantics.scl.compiler.internal.types.ast.TypeAst;
10 import org.simantics.scl.compiler.types.exceptions.KindUnificationException;
11 import org.simantics.scl.compiler.types.kinds.KMetaVar;
12 import org.simantics.scl.compiler.types.kinds.Kind;
13 import org.simantics.scl.compiler.types.kinds.Kinds;
14 import org.simantics.scl.compiler.types.util.Polarity;
15 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
16
17 import gnu.trove.map.hash.THashMap;
18 import gnu.trove.map.hash.TObjectIntHashMap;
19 import gnu.trove.set.hash.THashSet;
20
21
22 public final class TVar extends Type {
23     public static final TVar[] EMPTY_ARRAY = new TVar[0];
24     
25     private Kind kind;
26     
27     TVar(Kind kind) {
28         this.kind = kind;
29     }
30     
31     public Kind getKind() {
32         if(kind instanceof KMetaVar)
33             kind = Kinds.canonical(kind);
34         return kind;
35     }
36
37     @Override
38     public Type replace(TVar var, Type replacement) {
39         if(this == var)
40             return replacement;
41         else
42             return this;
43     }
44
45     @Override
46     public TypeAst toTypeAst(TypeUnparsingContext context) {
47         return new TVarAst(context.getName(this));
48     }
49     
50     @Override
51     public boolean equals(Object obj) {
52         while(obj instanceof TMetaVar) {
53             TMetaVar metaVar = (TMetaVar)obj;
54             if(metaVar.ref == null)
55                 return false;
56             else
57                 obj = metaVar.ref;
58         }
59         return this == obj;
60     }
61     
62     @Override
63     public void updateHashCode(TypeHashCodeContext context) {
64         TObjectIntHashMap<TVar> varHashCode = context.getVarHashCode();
65         if(varHashCode != null && varHashCode.containsKey(this))
66             context.append(varHashCode.get(this));
67         else
68             context.append(System.identityHashCode(this));
69     }
70
71     @Override
72     public void collectFreeVars(ArrayList<TVar> vars) {
73         if(!vars.contains(this))
74             vars.add(this);
75     }
76     
77     @Override
78     public void collectMetaVars(ArrayList<TMetaVar> vars) {
79     }
80     
81     @Override
82     public void collectMetaVars(THashSet<TMetaVar> vars) {
83     }
84     
85     @Override
86     public void collectEffectMetaVars(ArrayList<TMetaVar> vars) {
87     }
88
89     @Override
90     public boolean isGround() {
91         return false;
92     }
93
94         public Kind inferKind(Environment context) throws KindUnificationException {
95             if(kind == null)
96                 kind = Kinds.metaVar();
97         return kind;
98     }
99
100     @Override
101     public boolean containsMetaVars() {
102         return false;
103     }
104
105     @Override
106     public void toName(TypeUnparsingContext context, StringBuilder b) {
107         b.append(context.getName(this));
108     }
109     
110     @Override
111     public int getClassId() {
112         return VAR_ID;
113     }
114     
115     @Override
116     public boolean contains(TMetaVar other) {
117         return false;
118     }
119
120     @Override
121     public Type convertMetaVarsToVars() {
122         return this;
123     }
124
125     @Override
126     public void addPolarity(Polarity polarity) {
127     }
128
129     @Override
130     public Type head() {
131         return this;
132     }
133
134     @Override
135     public Type copySkeleton(THashMap<TMetaVar, TMetaVar> metaVarMap) {
136         return this;
137     }
138     
139     @Override
140     public int hashCode() {
141         return System.identityHashCode(this);
142     }
143     
144     @Override
145     public int hashCode(int hash) {
146         return HashCodeUtils.update(hash, System.identityHashCode(this));
147     }
148     
149     @Override
150     public int hashCode(int hash, TVar[] boundVars) {
151         for(int i=0;i<boundVars.length;++i)
152             if(boundVars[i] == this) {
153                 hash = HashCodeUtils.updateWithPreprocessedValue(hash, BOUND_VAR_HASH);
154                 return HashCodeUtils.update(hash, i);
155             }
156         return HashCodeUtils.update(hash, System.identityHashCode(this));
157     }
158     
159     @Override
160     public int skeletonHashCode() {
161         return System.identityHashCode(this);
162     }
163     
164     @Override
165     public int skeletonHashCode(int hash) {
166         return HashCodeUtils.update(hash, System.identityHashCode(this));
167     }
168     
169     @Override
170     public int skeletonHashCode(int hash, TVar[] boundVars) {
171         for(int i=0;i<boundVars.length;++i)
172             if(boundVars[i] == this) {
173                 hash = HashCodeUtils.updateWithPreprocessedValue(hash, BOUND_VAR_HASH);
174                 return HashCodeUtils.update(hash, i);
175             }
176         return HashCodeUtils.update(hash, System.identityHashCode(this));
177     }
178     
179     @Override
180     public boolean equalsCanonical(Type other) {
181         return this == other;
182     }
183
184     @Override
185     public Kind getKind(Environment context) {
186         return kind;
187     }
188     
189     @Override
190     public Type[] skeletonCanonicalChildren() {
191         return EMPTY_ARRAY;
192     }
193 }