]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/TCon.java
Added missing parts from SVN org.simantics.root project.
[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 gnu.trove.map.hash.THashMap;
4 import gnu.trove.set.hash.THashSet;
5
6 import java.util.ArrayList;
7
8 import org.simantics.scl.compiler.environment.Environment;
9 import org.simantics.scl.compiler.internal.codegen.utils.NameMangling;
10 import org.simantics.scl.compiler.internal.types.TypeHashCodeContext;
11 import org.simantics.scl.compiler.internal.types.ast.TConAst;
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.util.Polarity;
16 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
17
18 /**
19  * This class represents an SCL type constant with a name given in a module.
20  */
21 public class TCon extends Type {
22     public final String module;
23     public final String name;
24
25     TCon(String module, String name) {
26         if(NULL_CHECKS) {
27             if(module == null || name == null)
28                 throw new NullPointerException();
29         }
30         this.module = module;
31         this.name = name;
32     }
33
34     @Override
35     public TCon replace(TVar var, Type replacement) {
36         // Constants don't include type variables
37         return this;
38     }
39
40     @Override
41     public TypeAst toTypeAst(TypeUnparsingContext context) {
42         if(module == Types.BUILTIN) {
43             char c = name.charAt(0);
44             if(Character.isLetter(c) || c == '(' || name.equals("[]"))
45                 return new TConAst(name);
46             else
47                 return new TConAst("(" + name + ")");
48         }
49         else
50             return new TConAst(name.length() <= 1 ? simplifiedModuleName(module) + "." + name : name);
51     }
52     
53     private static String simplifiedModuleName(String name) {
54         int p = name.lastIndexOf('/');
55         if(p == -1)
56             return name;
57         else
58             return name.substring(p+1);
59     }
60
61     @Override
62     public boolean equals(Object obj) {
63         while(obj instanceof TMetaVar) {
64             TMetaVar metaVar = (TMetaVar)obj;
65             if(metaVar.ref == null)
66                 return false;
67             else
68                 obj = metaVar.ref;
69         }
70         return this == obj;
71     }
72     
73     @Override
74     public int hashCode() {
75         return System.identityHashCode(this);
76     }
77     
78     @Override
79     public void updateHashCode(TypeHashCodeContext context) {
80         context.append(System.identityHashCode(this));        
81     }
82
83     @Override
84     public void collectFreeVars(ArrayList<TVar> vars) {
85     }
86     
87     @Override
88     public void collectMetaVars(ArrayList<TMetaVar> vars) {
89     }
90     
91     @Override
92     public void collectMetaVars(THashSet<TMetaVar> vars) {
93     }
94     
95     @Override
96     public void collectEffectMetaVars(ArrayList<TMetaVar> vars) {
97     }
98
99     @Override
100     public boolean isGround() {
101         return true;
102     }
103
104         public Kind inferKind(Environment context) throws KindUnificationException {
105         return context.getTypeConstructor(this).kind;
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 }