1 package org.simantics.scl.compiler.types;
3 import java.util.ArrayList;
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;
17 import gnu.trove.map.hash.THashMap;
18 import gnu.trove.map.hash.TObjectIntHashMap;
19 import gnu.trove.set.hash.THashSet;
22 public final class TVar extends Type {
23 public static final TVar[] EMPTY_ARRAY = new TVar[0];
31 public Kind getKind() {
32 if(kind instanceof KMetaVar)
33 kind = Kinds.canonical(kind);
38 public Type replace(TVar var, Type replacement) {
46 public TypeAst toTypeAst(TypeUnparsingContext context) {
47 return new TVarAst(context.getName(this));
51 public boolean equals(Object obj) {
52 while(obj instanceof TMetaVar) {
53 TMetaVar metaVar = (TMetaVar)obj;
54 if(metaVar.ref == null)
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));
68 context.append(System.identityHashCode(this));
72 public void collectFreeVars(ArrayList<TVar> vars) {
73 if(!vars.contains(this))
78 public void collectMetaVars(ArrayList<TMetaVar> vars) {
82 public void collectMetaVars(THashSet<TMetaVar> vars) {
86 public void collectEffectMetaVars(ArrayList<TMetaVar> vars) {
90 public boolean isGround() {
94 public Kind inferKind(Environment context) throws KindUnificationException {
96 kind = Kinds.metaVar();
101 public boolean containsMetaVars() {
106 public void toName(TypeUnparsingContext context, StringBuilder b) {
107 b.append(context.getName(this));
111 public int getClassId() {
116 public boolean contains(TMetaVar other) {
121 public Type convertMetaVarsToVars() {
126 public void addPolarity(Polarity polarity) {
135 public Type copySkeleton(THashMap<TMetaVar, TMetaVar> metaVarMap) {
140 public int hashCode() {
141 return System.identityHashCode(this);
145 public int hashCode(int hash) {
146 return HashCodeUtils.update(hash, System.identityHashCode(this));
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);
156 return HashCodeUtils.update(hash, System.identityHashCode(this));
160 public int skeletonHashCode() {
161 return System.identityHashCode(this);
165 public int skeletonHashCode(int hash) {
166 return HashCodeUtils.update(hash, System.identityHashCode(this));
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);
176 return HashCodeUtils.update(hash, System.identityHashCode(this));
180 public boolean equalsCanonical(Type other) {
181 return this == other;
185 public Kind getKind(Environment context) {
190 public Type[] skeletonCanonicalChildren() {