]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/TCon.java
Merged changes from feature/scl to master.
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / types / TCon.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.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;
15
16 import gnu.trove.map.hash.THashMap;
17 import gnu.trove.set.hash.THashSet;
18
19 /**
20  * This class represents an SCL type constant with a name given in a module.
21  */
22 public final class TCon extends Type {
23     public final String module;
24     public final String name;
25
26     TCon(String module, String name) {
27         if(NULL_CHECKS) {
28             if(module == null || name == null)
29                 throw new NullPointerException();
30         }
31         this.module = module;
32         this.name = name;
33     }
34
35     @Override
36     public TCon replace(TVar var, Type replacement) {
37         // Constants don't include type variables
38         return this;
39     }
40
41     @Override
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);
47             else
48                 return new TConAst("(" + name + ")");
49         }
50         else
51             return new TConAst(name.length() <= 1 ? simplifiedModuleName(module) + "." + name : name);
52     }
53     
54     private static String simplifiedModuleName(String name) {
55         int p = name.lastIndexOf('/');
56         if(p == -1)
57             return name;
58         else
59             return name.substring(p+1);
60     }
61
62     @Override
63     public boolean equals(Object obj) {
64         while(obj instanceof TMetaVar) {
65             TMetaVar metaVar = (TMetaVar)obj;
66             if(metaVar.ref == null)
67                 return false;
68             else
69                 obj = metaVar.ref;
70         }
71         return this == obj;
72     }
73     
74     @Override
75     public void updateHashCode(TypeHashCodeContext context) {
76         context.append(System.identityHashCode(this));        
77     }
78
79     @Override
80     public void collectFreeVars(ArrayList<TVar> vars) {
81     }
82     
83     @Override
84     public void collectMetaVars(ArrayList<TMetaVar> vars) {
85     }
86     
87     @Override
88     public void collectMetaVars(THashSet<TMetaVar> vars) {
89     }
90     
91     @Override
92     public void collectEffectMetaVars(ArrayList<TMetaVar> vars) {
93     }
94
95     @Override
96     public boolean isGround() {
97         return true;
98     }
99
100         public Kind inferKind(Environment context) throws KindUnificationException {
101         return context.getTypeDescriptor(this).getKind();
102     }
103         
104         public Kind getKind(Environment context) {
105             return context.getTypeDescriptor(this).getKind();
106         }
107
108     @Override
109     public boolean containsMetaVars() {
110         return false;
111     }
112
113     @Override
114     public void toName(TypeUnparsingContext context, StringBuilder b) {
115         b.append(NameMangling.mangle(name));
116     }
117     
118     @Override
119     public int getClassId() {
120         return CON_ID;
121     }
122
123     @Override
124     public boolean contains(TMetaVar other) {
125         return false;
126     }
127     
128     @Override
129     public Type convertMetaVarsToVars() {
130         return this;
131     }
132     
133     @Override
134     public void addPolarity(Polarity polarity) {
135     }
136     
137     @Override
138     public void collectConcreteEffects(ArrayList<TCon> concreteEffects) {
139         concreteEffects.add(this);
140     }
141     
142     @Override
143     public Type head() {
144         return this;
145     }
146
147     @Override
148     public Type copySkeleton(THashMap<TMetaVar, TMetaVar> metaVarMap) {
149         return this;
150     }
151     
152     @Override
153     public int hashCode() {
154         return System.identityHashCode(this);
155     }
156
157     @Override
158     public int hashCode(int hash) {
159         return HashCodeUtils.update(hash, System.identityHashCode(this));
160     }
161     
162     @Override
163     public int hashCode(int hash, TVar[] boundVars) {
164         return HashCodeUtils.update(hash, System.identityHashCode(this));
165     }
166     
167     @Override
168     public int skeletonHashCode() {
169         return System.identityHashCode(this);
170     }
171
172     @Override
173     public int skeletonHashCode(int hash) {
174         return HashCodeUtils.update(hash, System.identityHashCode(this));
175     }
176     
177     @Override
178     public int skeletonHashCode(int hash, TVar[] boundVars) {
179         return HashCodeUtils.update(hash, System.identityHashCode(this));
180     }
181     
182     @Override
183     public boolean equalsCanonical(Type other) {
184         return this == other;
185     }
186
187     @Override
188     public Type[] skeletonCanonicalChildren() {
189         return EMPTY_ARRAY;
190     }
191 }