public static Expr MakeCast(Expr expr, ExprType type, Env env) {
// if two types are equal, return Expr
if (EqualType(expr.Type, type)) {
return expr;
}
// from pointer
if (expr.Type.Kind == ExprTypeKind.POINTER) {
return FromPointer(expr, type, env);
}
// to pointer
if (type.Kind == ExprTypeKind.POINTER) {
return ToPointer(expr, type, env);
}
switch (expr.Type.Kind) {
// from signed integral
case ExprTypeKind.CHAR:
case ExprTypeKind.SHORT:
case ExprTypeKind.LONG:
return SignedIntegralToArith(expr, type);
// from unsigned integral
case ExprTypeKind.UCHAR:
case ExprTypeKind.USHORT:
case ExprTypeKind.ULONG:
return UnsignedIntegralToArith(expr, type);
// from float
case ExprTypeKind.FLOAT:
case ExprTypeKind.DOUBLE:
return FloatToArith(expr, type);
case ExprTypeKind.VOID:
case ExprTypeKind.POINTER:
case ExprTypeKind.FUNCTION:
case ExprTypeKind.ARRAY:
case ExprTypeKind.INCOMPLETE_ARRAY:
case ExprTypeKind.STRUCT_OR_UNION:
default:
throw new InvalidOperationException("Error: expression Type not supported for casting from.");
}
}