001: /**************************************************************************/
002: /* B O S S A */
003: /* A simple imperative object-oriented research language */
004: /* (c) Daniel Bonniot 1999 */
005: /* */
006: /* This program is free software; you can redistribute it and/or modify */
007: /* it under the terms of the GNU General Public License as published by */
008: /* the Free Software Foundation; either version 2 of the License, or */
009: /* (at your option) any later version. */
010: /* */
011: /**************************************************************************/package mlsub.typing;
012:
013: import java.util.*;
014: import mlsub.typing.lowlevel.*;
015:
016: /**
017: * A class. It "needs" type parameters to become a Monotype
018: */
019: public class TypeConstructor implements mlsub.typing.lowlevel.Element,
020: TypeSymbol {
021: /**
022: * Creates a TypeConstructor.
023: *
024: * A concrete TC is a TC that can tag runtime objects.
025: */
026: public TypeConstructor(String name, AtomicKind v, boolean concrete,
027: boolean rigid) {
028: this .name = name;
029: this .concrete = concrete;
030: this .rigid = rigid;
031:
032: if (v != null)
033: setVariance(v);
034: }
035:
036: /**
037: Creates a non rigid type constructor.
038: */
039: public TypeConstructor(String name) {
040: this (name, null, false, false);
041: }
042:
043: /**
044: Creates an anonymous non-concrete TypeConstructor with a known variance.
045: */
046: public TypeConstructor(AtomicKind v) {
047: this (null, v, false, false);
048: }
049:
050: public void setMinimal() {
051: variance.getConstraint().assertMinimal(this );
052: }
053:
054: public boolean isMinimal() {
055: return variance.getConstraint().isMinimal(this );
056: }
057:
058: public TypeSymbol cloneTypeSymbol() {
059: return new TypeConstructor(name, variance, concrete, rigid);
060: }
061:
062: /**
063: @return s if it is a type constructor, null otherwise
064: */
065: public static TypeConstructor fromTypeSymbol(TypeSymbol s) {
066: if (s instanceof TypeConstructor)
067: return (TypeConstructor) s;
068: else
069: return null;
070: }
071:
072: /**
073: * Tell which variance this TypeConstructor has.
074: * Can be called from ImplementsCst.
075: */
076: public void setVariance(AtomicKind v) {
077: setKind(v.getConstraint());
078: }
079:
080: public void discard() {
081: variance.getConstraint().discard(this );
082: }
083:
084: public int arity() {
085: if (variance == null)
086: throw new InternalError("Variance of " + this
087: + " not known in arity()");
088:
089: return variance.arity();
090: }
091:
092: public boolean isConcrete() {
093: return concrete;
094: }
095:
096: public boolean isExistential() {
097: // Only MonotypeVars need to be marked as existential.
098: return false;
099: }
100:
101: /****************************************************************
102: * Kinding
103: ****************************************************************/
104:
105: private int id = -1;
106:
107: public int getId() {
108: return id;
109: }
110:
111: public void setId(int value) {
112: id = value;
113: }
114:
115: private Kind kind;
116:
117: public Kind getKind() {
118: return kind;
119: }
120:
121: public void setKind(Kind value) {
122: if (kind != null)
123: if (kind == value)
124: return;
125: else
126: throw new InternalError(
127: "Variance already set in type constructor "
128: + this );
129:
130: variance = (AtomicKind) ((Engine.Constraint) value).associatedKind;
131: kind = value;
132: }
133:
134: /****************************************************************
135: * Misc
136: ****************************************************************/
137:
138: public String toString() {
139: if (name != null)
140: return name;
141: else
142: return super .toString();
143: }
144:
145: /**
146: Create a string representing the monotype build by application
147: of this type constructor to the given parameters.
148:
149: This default implementation returns "tc<p1, ..., pn>".
150:
151: It should be overriden by type constructors that print differently.
152: For instance, the array tape constructor could return "p1[]".
153:
154: @param parameters the type parameters
155: @return the representation of the monotype
156: */
157: public String toString(Monotype[] parameters) {
158: return toString(parameters, false, null);
159: }
160:
161: /**
162: Print the monotype when it can be null.
163: */
164: public String toString(Monotype[] parameters, boolean isNull,
165: String suffix) {
166: String res = this .toString()
167: + bossa.util.Util.map("<", ", ", ">", parameters);
168: if (isNull)
169: res = "?" + res;
170: if (suffix != null)
171: res = res + suffix;
172: return res;
173: }
174:
175: /****************************************************************
176: * Fields
177: ****************************************************************/
178:
179: public AtomicKind variance;
180: private boolean concrete;
181: private boolean rigid;
182:
183: public final boolean isRigid() {
184: return rigid;
185: };
186:
187: String name;
188: }
|