Types for jump expressions

This commit is contained in:
Andrey Breslav
2011-01-27 17:03:52 +03:00
parent 991dbd4427
commit 65cf049e73
2 changed files with 64 additions and 8 deletions
@@ -17,9 +17,29 @@ import java.util.Map;
public class JetTypeChecker { public class JetTypeChecker {
public static final JetTypeChecker INSTANCE = new JetTypeChecker(); public static final JetTypeChecker INSTANCE = new JetTypeChecker();
/*
: jump
: if
: when
: try
: functionLiteral
: "this" ("<" type ">")?
: "typeof" "(" expression ")"
: "new" constructorInvocation
: objectLiteral
: declaration
: loop
: SimpleName
: "namespace" // for the root namespace
*/
public Type getType(JetExpression expression) { public Type getType(JetExpression expression) {
final Type[] result = new Type[1]; final Type[] result = new Type[1];
expression.accept(new JetVisitor() { expression.accept(new JetVisitor() {
@Override
public void visitParenthesizedExpression(JetParenthesizedExpression expression) {
result[0] = getType(expression.getExpression());
}
@Override @Override
public void visitConstantExpression(JetConstantExpression expression) { public void visitConstantExpression(JetConstantExpression expression) {
IElementType elementType = expression.getNode().getElementType(); IElementType elementType = expression.getNode().getElementType();
@@ -49,6 +69,31 @@ public class JetTypeChecker {
} }
} }
@Override
public void visitThrowExpression(JetThrowExpression expression) {
result[0] = JetStandardClasses.getNothingType();
}
@Override
public void visitReturnExpression(JetReturnExpression expression) {
result[0] = JetStandardClasses.getNothingType();
}
@Override
public void visitBreakExpression(JetBreakExpression expression) {
result[0] = JetStandardClasses.getNothingType();
}
@Override
public void visitContinueExpression(JetContinueExpression expression) {
result[0] = JetStandardClasses.getNothingType();
}
@Override
public void visitTypeofExpression(JetTypeofExpression expression) {
throw new UnsupportedOperationException("Return some reflection interface"); // TODO
}
@Override @Override
public void visitTupleExpression(JetTupleExpression expression) { public void visitTupleExpression(JetTupleExpression expression) {
List<JetExpression> entries = expression.getEntries(); List<JetExpression> entries = expression.getEntries();
@@ -56,6 +101,7 @@ public class JetTypeChecker {
for (JetExpression entry : entries) { for (JetExpression entry : entries) {
types.add(getType(entry)); types.add(getType(entry));
} }
// TODO : labels
result[0] = JetStandardClasses.getTupleType(types); result[0] = JetStandardClasses.getTupleType(types);
} }
@@ -66,6 +66,16 @@ public class JetTypeCheckerTest extends LightDaemonAnalyzerTestCase {
assertType("(1, 'a')", JetStandardClasses.getTupleType(JetStandardClasses.getIntType(), JetStandardClasses.getCharType())); assertType("(1, 'a')", JetStandardClasses.getTupleType(JetStandardClasses.getIntType(), JetStandardClasses.getCharType()));
} }
public void testJumps() throws Exception {
assertType("throw e", JetStandardClasses.getNothingType());
assertType("return", JetStandardClasses.getNothingType());
assertType("return 1", JetStandardClasses.getNothingType());
assertType("continue", JetStandardClasses.getNothingType());
assertType("continue \"foo\"", JetStandardClasses.getNothingType());
assertType("break", JetStandardClasses.getNothingType());
assertType("break \"foo\"", JetStandardClasses.getNothingType());
}
public void testBasicSubtyping() throws Exception { public void testBasicSubtyping() throws Exception {
assertSubtype("Boolean", "Boolean"); assertSubtype("Boolean", "Boolean");
assertSubtype("Byte", "Byte"); assertSubtype("Byte", "Byte");
@@ -120,17 +130,17 @@ public class JetTypeCheckerTest extends LightDaemonAnalyzerTestCase {
assertSubtype("(Short)", "(Short)"); assertSubtype("(Short)", "(Short)");
assertSubtype("(Int)", "(Int)"); assertSubtype("(Int)", "(Int)");
assertSubtype("(Long)", "(Long)"); assertSubtype("(Long)", "(Long)");
assertSubtype("(Float)", "(Float)"); assertSubtype("(Float)", "(Float)");
assertSubtype("(Double)", "(Double)"); assertSubtype("(Double)", "(Double)");
assertSubtype("(Unit)", "(Unit)"); assertSubtype("(Unit)", "(Unit)");
assertSubtype("(Unit, Unit)", "(Unit, Unit)"); assertSubtype("(Unit, Unit)", "(Unit, Unit)");
assertNotSubtype("(Unit)", "(Int)"); assertNotSubtype("(Unit)", "(Int)");
assertSubtype("(Unit)", "(Any)"); assertSubtype("(Unit)", "(Any)");
assertSubtype("(Unit, Unit)", "(Any, Any)"); assertSubtype("(Unit, Unit)", "(Any, Any)");
assertSubtype("(Unit, Unit)", "(Any, Unit)"); assertSubtype("(Unit, Unit)", "(Any, Unit)");
assertSubtype("(Unit, Unit)", "(Unit, Any)"); assertSubtype("(Unit, Unit)", "(Unit, Any)");
} }
public void testProjections() throws Exception { public void testProjections() throws Exception {