// _declaredType. For structured types, _declaredType and _varType
// are the same reference, need to initialize this type
// before checking compatibility. But if the new token is not
// compatible with the declared type, the current resolved type
// need to be preserved, so make a clone.
Type declaredType;
try {
declaredType = (Type) _declaredType.clone();
} catch (CloneNotSupportedException cnse) {
throw new InternalErrorException("Variable._setToken: "
+ "Cannot clone the declared type of this Variable.");
}
if (declaredType instanceof StructuredType) {
((StructuredType) declaredType).initialize(BaseType.UNKNOWN);
}
if (declaredType.isCompatible(newToken.getType())) {
newToken = declaredType.convert(newToken);
} else {
throw new IllegalActionException(this,
"Variable._setToken: Cannot store a token of type "
+ newToken.getType().toString()
+ ", which is incompatible with type "
+ declaredType.toString());
}
// update _varType to the type of the new token.
if (_declaredType instanceof StructuredType) {
((StructuredType) _varType)
.updateType((StructuredType) newToken.getType());
} else {
// _declaredType is a BaseType
_varType = newToken.getType();
// FIXME: This is Edward's array optimization for Rome.
// Type newTokenType = newToken.getType();
// // FIXME: What about other structured types?
// if (_varType instanceof ArrayType && newTokenType instanceof ArrayType) {
// // Need to do an update instead of a replacement of _varType
// // because inequalities may have been set up that refer to this
// // particular instance _varType.
// // NOTE: updateType() won't update to an incompatible type!
// ((ArrayType)_varType).setType((ArrayType)newToken.getType());
// } else {
// // It is OK now to replace _varType because either the
// // type is not a structured type or it was previously
// // not a structured type.
// _varType = newTokenType;
// }
}
// Check setTypeAtMost constraint.
if (_typeAtMost != BaseType.UNKNOWN) {
// Recalculate this in case the type has changed.
Type tokenType = newToken.getType();
int comparison = TypeLattice.compare(tokenType, _typeAtMost);
if ((comparison == CPO.HIGHER)
|| (comparison == CPO.INCOMPARABLE)) {
// Incompatible type!
throw new IllegalActionException(this,
"Cannot store a token of type "
+ tokenType.toString()
+ ", which is not less than or equal to "
+ _typeAtMost.toString());
}
}