package cd.frontend.semantic;
/**
* Thrown by the semantic checker when a semantic error is detected
* in the user's program.
*/
public class SemanticFailure extends RuntimeException {
private static final long serialVersionUID = 5375946759285719123L;
public enum Cause {
/**
* Caused by an assignment to either a final field, {@code this},
* or some other kind of expression which cannot be assigned to.
* Not used for type errors in assignments, which fall
* under {@link #TYPE_ERROR}.
*/
NOT_ASSIGNABLE,
/**
* Two variables, fields, methods, or classes with the same name
* were declared in the same scope
*/
DOUBLE_DECLARATION,
/**
* A field was accessed that does not exist
*/
NO_SUCH_FIELD,
/**
* A method was called that does not exist
*/
NO_SUCH_METHOD,
/**
* A variable or other identifier was used in a method
* body which has no corresponding declaration
*/
NO_SUCH_VARIABLE,
/**
* A method with a return type is missing a return statement among one of its paths
*/
MISSING_RETURN,
/**
* Can occur in many contents:
*
* - Assignment to a variable from an expression of wrong type
*
- A parameter in a method invocation had the wrong type
*
- A condition of a while loop or if statement was not boolean
*
- A non-array was indexed (i.e., a[i] where a is not an array)
*
- The index was not an int (i.e., a[i] where i is not an int)
*
- Arithmetic operators (+,-,etc) used with non-int type
*
- Boolean operators (!,&&,||,etc) used with non-boolean type
*
- Method or field accessed on non-object type
*
- {@code write()} is used with non-int argument
*
- An invalid cast was detected (i.e., a cast to a type that
* is not a super- or sub-type of the original type).
*
- The size of an array in a new expression was not an
* int (i.e., new A[i] where i is not an int)
*
*/
TYPE_ERROR,
/**
* A class is its own super class
*/
CIRCULAR_INHERITANCE,
/**
* One of the following:
*
* - No class {@code Main}
*
- Class {@code Main} does not have a method {@code main()}
*
- The method {@code main} has > 0 parameters.
*
- The method {@code main} has a non-void return type.
*
*/
INVALID_START_POINT,
/**
* A class {@code Object} was defined. This class is implicitly
* defined and cannot be defined explicitly.
*/
OBJECT_CLASS_DEFINED,
/**
* A type name was found for which no class declaration exists.
* This can occur in many contexts:
*
* - The extends clause on a class declaration.
*
- A cast.
*
- A new expression.
*
*/
NO_SUCH_TYPE,
/**
* The parameters of an overridden method have different types
* from the base method, there is a different
* number of parameters, or the return value is different.
*/
INVALID_OVERRIDE,
/**
* A method was called with the wrong number of arguments
*/
WRONG_NUMBER_OF_ARGUMENTS,
/**
* Indicates the use of a local variable that may not have been
* initialized (ACD only).
*/
POSSIBLY_UNINITIALIZED,
}
public final Cause cause;
public SemanticFailure(Cause cause) {
super(cause.name());
this.cause = cause;
}
public SemanticFailure(Cause cause, String format, Object... args) {
super(String.format(format, args));
this.cause = cause;
}
}