Source code: mlsub/typing/TypeConstructor.java
1 /**************************************************************************/
2 /* B O S S A */
3 /* A simple imperative object-oriented research language */
4 /* (c) Daniel Bonniot 1999 */
5 /* */
6 /* This program is free software; you can redistribute it and/or modify */
7 /* it under the terms of the GNU General Public License as published by */
8 /* the Free Software Foundation; either version 2 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /**************************************************************************/
12
13 // File : TypeConstructor.java
14 // Created : Thu Jul 08 11:51:09 1999 by bonniot
15 //$Modified: Thu Aug 31 16:22:28 2000 by Daniel Bonniot $
16
17 package mlsub.typing;
18
19 import java.util.*;
20 import mlsub.typing.lowlevel.*;
21
22 /**
23 * A class. It "needs" type parameters to become a Monotype
24 */
25 public class TypeConstructor
26 implements mlsub.typing.lowlevel.Element, TypeSymbol
27 {
28 /**
29 * Creates a TypeConstructor.
30 *
31 * A concrete TC is a TC that can tag runtime objects.
32 */
33 public TypeConstructor(String name, AtomicKind v, boolean concrete,
34 boolean rigid)
35 {
36 this.name = name;
37 this.concrete = concrete;
38 this.rigid = rigid;
39
40 if (v != null)
41 setVariance(v);
42 }
43
44 /**
45 Creates a non rigid type constructor.
46 */
47 public TypeConstructor(String name)
48 {
49 this(name, null, false, false);
50 }
51
52 /**
53 Creates an anonymous non-concrete TypeConstructor with a known variance.
54 */
55 public TypeConstructor(AtomicKind v)
56 {
57 this(null, v, false, false);
58 }
59
60 public void setMinimal()
61 {
62 variance.getConstraint().assertMinimal(this);
63 }
64
65 public boolean isMinimal()
66 {
67 return variance.getConstraint().isMinimal(this);
68 }
69
70 public TypeSymbol cloneTypeSymbol()
71 {
72 return new TypeConstructor(name, variance, concrete, rigid);
73 }
74
75 /**
76 @return s if it is a type constructor, null otherwise
77 */
78 public static TypeConstructor fromTypeSymbol(TypeSymbol s)
79 {
80 if (s instanceof TypeConstructor)
81 return (TypeConstructor) s;
82 else
83 return null;
84 }
85
86 /**
87 * Tell which variance this TypeConstructor has.
88 * Can be called from ImplementsCst.
89 */
90 public void setVariance(AtomicKind v)
91 {
92 setKind(v.getConstraint());
93 }
94
95 public int arity()
96 {
97 if(variance==null)
98 throw new InternalError("Variance of "+this+" not known in arity()");
99
100 return variance.arity();
101 }
102
103 public boolean isConcrete()
104 {
105 return concrete;
106 }
107
108 /****************************************************************
109 * Kinding
110 ****************************************************************/
111
112 private int id=-1;
113
114 public int getId() { return id; }
115
116 public void setId(int value) { id=value; }
117
118 private Kind kind;
119
120 public Kind getKind() { return kind; }
121
122 public void setKind(Kind value)
123 {
124 if(kind != null)
125 if(kind == value)
126 return;
127 else
128 throw new InternalError
129 ("Variance already set in type constructor " + this);
130
131 variance = (AtomicKind) ((Engine.Constraint) value).associatedKind;
132 kind = value;
133 }
134
135 /****************************************************************
136 * Misc
137 ****************************************************************/
138
139 public String toString()
140 {
141 if (name != null)
142 return name;
143 else
144 return super.toString();
145 }
146
147 /**
148 Create a string representing the monotype build by application
149 of this type constructor to the given parameters.
150
151 This default implementation returns "tc<p1, ..., pn>".
152
153 It should be overriden by type constructors that print differently.
154 For instance, the array tape constructor could return "p1[]".
155
156 @param parameters the type parameters
157 @return the representation of the monotype
158 */
159 public String toString(Monotype[] parameters)
160 {
161 return toString(parameters, false, null);
162 }
163
164 /**
165 Print the monotype when it can be null.
166 */
167 public String toString(Monotype[] parameters, boolean isNull, String suffix)
168 {
169 String res = this.toString() +
170 bossa.util.Util.map("<", ", ", ">", parameters);
171 if (isNull)
172 res = "?" + res;
173 if (suffix != null)
174 res = res + suffix;
175 return res;
176 }
177
178 /****************************************************************
179 * Fields
180 ****************************************************************/
181
182 public int enumerateTagIndex = -1; // used in Typing.enumerate. ugly ! Subclass
183
184 public AtomicKind variance;
185 private boolean concrete;
186 private boolean rigid;
187 public final boolean isRigid() { return rigid; };
188
189 String name;
190 }