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.codegen.utils.NameMangling;
7 import org.simantics.scl.compiler.internal.types.HashCodeUtils;
8 import org.simantics.scl.compiler.internal.types.TypeHashCodeContext;
9 import org.simantics.scl.compiler.internal.types.ast.TConAst;
10 import org.simantics.scl.compiler.internal.types.ast.TypeAst;
11 import org.simantics.scl.compiler.types.exceptions.KindUnificationException;
12 import org.simantics.scl.compiler.types.kinds.Kind;
13 import org.simantics.scl.compiler.types.util.Polarity;
14 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
16 import gnu.trove.map.hash.THashMap;
17 import gnu.trove.set.hash.THashSet;
20 * This class represents an SCL type constant with a name given in a module.
22 public final class TCon extends Type {
23 public final String module;
24 public final String name;
26 TCon(String module, String name) {
28 if(module == null || name == null)
29 throw new NullPointerException();
36 public TCon replace(TVar var, Type replacement) {
37 // Constants don't include type variables
42 public TypeAst toTypeAst(TypeUnparsingContext context) {
43 if(module == Types.BUILTIN) {
44 char c = name.charAt(0);
45 if(Character.isLetter(c) || c == '(' || name.equals("[]"))
46 return new TConAst(name);
48 return new TConAst("(" + name + ")");
51 return new TConAst(name.length() <= 1 ? simplifiedModuleName(module) + "." + name : name);
54 private static String simplifiedModuleName(String name) {
55 int p = name.lastIndexOf('/');
59 return name.substring(p+1);
63 public boolean equals(Object obj) {
64 while(obj instanceof TMetaVar) {
65 TMetaVar metaVar = (TMetaVar)obj;
66 if(metaVar.ref == null)
75 public void updateHashCode(TypeHashCodeContext context) {
76 context.append(System.identityHashCode(this));
80 public void collectFreeVars(ArrayList<TVar> vars) {
84 public void collectMetaVars(ArrayList<TMetaVar> vars) {
88 public void collectMetaVars(THashSet<TMetaVar> vars) {
92 public void collectEffectMetaVars(ArrayList<TMetaVar> vars) {
96 public boolean isGround() {
100 public Kind inferKind(Environment context) throws KindUnificationException {
101 return context.getTypeDescriptor(this).getKind();
104 public Kind getKind(Environment context) {
105 return context.getTypeDescriptor(this).getKind();
109 public boolean containsMetaVars() {
114 public void toName(TypeUnparsingContext context, StringBuilder b) {
115 b.append(NameMangling.mangle(name));
119 public int getClassId() {
124 public boolean contains(TMetaVar other) {
129 public Type convertMetaVarsToVars() {
134 public void addPolarity(Polarity polarity) {
138 public void collectConcreteEffects(ArrayList<TCon> concreteEffects) {
139 concreteEffects.add(this);
148 public Type copySkeleton(THashMap<TMetaVar, TMetaVar> metaVarMap) {
153 public int hashCode() {
154 return System.identityHashCode(this);
158 public int hashCode(int hash) {
159 return HashCodeUtils.update(hash, System.identityHashCode(this));
163 public int hashCode(int hash, TVar[] boundVars) {
164 return HashCodeUtils.update(hash, System.identityHashCode(this));
168 public int skeletonHashCode() {
169 return System.identityHashCode(this);
173 public int skeletonHashCode(int hash) {
174 return HashCodeUtils.update(hash, System.identityHashCode(this));
178 public int skeletonHashCode(int hash, TVar[] boundVars) {
179 return HashCodeUtils.update(hash, System.identityHashCode(this));
183 public boolean equalsCanonical(Type other) {
184 return this == other;
188 public Type[] skeletonCanonicalChildren() {