code polishing => green

This commit is contained in:
Alex Tkachman
2011-09-10 10:34:17 +03:00
parent 3befa8ad59
commit 76df19c2b3
@@ -216,6 +216,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
return StackValue.none(); return StackValue.none();
} }
else { else {
assert expressionType != null;
final DeclarationDescriptor descriptor = expressionType.getConstructor().getDeclarationDescriptor(); final DeclarationDescriptor descriptor = expressionType.getConstructor().getDeclarationDescriptor();
if (isClass(descriptor, "IntRange")) { // TODO IntRange subclasses if (isClass(descriptor, "IntRange")) { // TODO IntRange subclasses
new ForInRangeLoopGenerator(expression, loopRangeType).invoke(); new ForInRangeLoopGenerator(expression, loopRangeType).invoke();
@@ -227,6 +228,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
} }
} }
@SuppressWarnings("ConstantConditions")
private void generateForInIterable(JetForExpression expression, Type loopRangeType) { private void generateForInIterable(JetForExpression expression, Type loopRangeType) {
final JetExpression loopRange = expression.getLoopRange(); final JetExpression loopRange = expression.getLoopRange();
@@ -245,7 +247,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
final VariableDescriptor parameterDescriptor = bindingContext.get(BindingContext.VALUE_PARAMETER, loopParameter); final VariableDescriptor parameterDescriptor = bindingContext.get(BindingContext.VALUE_PARAMETER, loopParameter);
JetType iteratorType = parameterDescriptor.getOutType(); JetType iteratorType = parameterDescriptor.getOutType();
Type asmIterType = typeMapper.boxType(typeMapper.mapType(iteratorType)); Type asmIterType = JetTypeMapper.boxType(typeMapper.mapType(iteratorType));
JetType paramType = parameterDescriptor.getOutType(); JetType paramType = parameterDescriptor.getOutType();
Type asmParamType = typeMapper.mapType(paramType); Type asmParamType = typeMapper.mapType(paramType);
@@ -526,6 +528,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
@Override @Override
public StackValue visitFunctionLiteralExpression(JetFunctionLiteralExpression expression, StackValue receiver) { public StackValue visitFunctionLiteralExpression(JetFunctionLiteralExpression expression, StackValue receiver) {
//noinspection ConstantConditions
if (bindingContext.get(BindingContext.BLOCK, expression)) { if (bindingContext.get(BindingContext.BLOCK, expression)) {
//noinspection ConstantConditions //noinspection ConstantConditions
return generateBlock(expression.getFunctionLiteral().getBodyExpression().getStatements()); return generateBlock(expression.getFunctionLiteral().getBodyExpression().getStatements());
@@ -569,6 +572,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
for (JetElement statement : statements) { for (JetElement statement : statements) {
if (statement instanceof JetProperty) { if (statement instanceof JetProperty) {
final VariableDescriptor variableDescriptor = bindingContext.get(BindingContext.VARIABLE, statement); final VariableDescriptor variableDescriptor = bindingContext.get(BindingContext.VARIABLE, statement);
assert variableDescriptor != null;
final Type type = typeMapper.mapType(variableDescriptor.getOutType()); final Type type = typeMapper.mapType(variableDescriptor.getOutType());
myMap.enter(variableDescriptor, type.getSize()); myMap.enter(variableDescriptor, type.getSize());
} }
@@ -592,6 +596,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
if (statement instanceof JetProperty) { if (statement instanceof JetProperty) {
JetProperty var = (JetProperty) statement; JetProperty var = (JetProperty) statement;
VariableDescriptor variableDescriptor = bindingContext.get(BindingContext.VARIABLE, var); VariableDescriptor variableDescriptor = bindingContext.get(BindingContext.VARIABLE, var);
assert variableDescriptor != null;
Type outType = typeMapper.mapType(variableDescriptor.getOutType()); Type outType = typeMapper.mapType(variableDescriptor.getOutType());
int index = myMap.leave(variableDescriptor); int index = myMap.leave(variableDescriptor);
@@ -666,6 +671,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
return intrinsic.generate(this, v, expectedType, expression, Collections.<JetExpression>emptyList(), receiver); return intrinsic.generate(this, v, expectedType, expression, Collections.<JetExpression>emptyList(), receiver);
} }
assert descriptor != null;
final DeclarationDescriptor container = descriptor.getContainingDeclaration(); final DeclarationDescriptor container = descriptor.getContainingDeclaration();
PsiElement declaration = bindingContext.get(BindingContext.DESCRIPTOR_TO_DECLARATION, descriptor); PsiElement declaration = bindingContext.get(BindingContext.DESCRIPTOR_TO_DECLARATION, descriptor);
@@ -693,6 +699,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
if (PsiTreeUtil.getParentOfType(expression, JetDelegationSpecifier.class) != null) { if (PsiTreeUtil.getParentOfType(expression, JetDelegationSpecifier.class) != null) {
JetClass aClass = PsiTreeUtil.getParentOfType(expression, JetClass.class); JetClass aClass = PsiTreeUtil.getParentOfType(expression, JetClass.class);
ConstructorDescriptor constructorDescriptor = bindingContext.get(BindingContext.CONSTRUCTOR, aClass); ConstructorDescriptor constructorDescriptor = bindingContext.get(BindingContext.CONSTRUCTOR, aClass);
assert constructorDescriptor != null;
List<ValueParameterDescriptor> parameters = constructorDescriptor.getValueParameters(); List<ValueParameterDescriptor> parameters = constructorDescriptor.getValueParameters();
for (ValueParameterDescriptor parameter : parameters) { for (ValueParameterDescriptor parameter : parameters) {
if (parameter.getName().equals(descriptor.getName())) { if (parameter.getName().equals(descriptor.getName())) {
@@ -782,6 +789,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
owner = typeMapper.getOwner(functionDescriptor, OwnerKind.INTERFACE); owner = typeMapper.getOwner(functionDescriptor, OwnerKind.INTERFACE);
if(containingDeclaration instanceof JavaClassDescriptor) { if(containingDeclaration instanceof JavaClassDescriptor) {
PsiClass psiElement = (PsiClass) bindingContext.get(BindingContext.DESCRIPTOR_TO_DECLARATION, containingDeclaration); PsiClass psiElement = (PsiClass) bindingContext.get(BindingContext.DESCRIPTOR_TO_DECLARATION, containingDeclaration);
assert psiElement != null;
isInterface = psiElement.isInterface(); isInterface = psiElement.isInterface();
} }
else else
@@ -937,6 +945,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
if (expression.getParent() instanceof JetQualifiedExpression) { if (expression.getParent() instanceof JetQualifiedExpression) {
final JetExpression receiver = ((JetQualifiedExpression) expression.getParent()).getReceiverExpression(); final JetExpression receiver = ((JetQualifiedExpression) expression.getParent()).getReceiverExpression();
JetType expressionType = bindingContext.get(BindingContext.EXPRESSION_TYPE, receiver); JetType expressionType = bindingContext.get(BindingContext.EXPRESSION_TYPE, receiver);
assert expressionType != null;
DeclarationDescriptor declarationDescriptor = expressionType.getConstructor().getDeclarationDescriptor(); DeclarationDescriptor declarationDescriptor = expressionType.getConstructor().getDeclarationDescriptor();
PsiElement ownerDeclaration = bindingContext.get(BindingContext.DESCRIPTOR_TO_DECLARATION, declarationDescriptor); PsiElement ownerDeclaration = bindingContext.get(BindingContext.DESCRIPTOR_TO_DECLARATION, declarationDescriptor);
if (ownerDeclaration instanceof PsiClass) { if (ownerDeclaration instanceof PsiClass) {
@@ -1077,7 +1086,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
propValue.put(type, v); propValue.put(type, v);
if(JetTypeMapper.isPrimitive(type) && !type.equals(Type.VOID_TYPE)) { if(JetTypeMapper.isPrimitive(type) && !type.equals(Type.VOID_TYPE)) {
v.valueOf(type); v.valueOf(type);
type = typeMapper.boxType(type); type = JetTypeMapper.boxType(type);
} }
v.goTo(end); v.goTo(end);
@@ -1257,10 +1266,10 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
if(JetTypeMapper.isPrimitive(leftType) != JetTypeMapper.isPrimitive(rightType)) { if(JetTypeMapper.isPrimitive(leftType) != JetTypeMapper.isPrimitive(rightType)) {
gen(left, leftType); gen(left, leftType);
v.valueOf(leftType); v.valueOf(leftType);
leftType = typeMapper.boxType(leftType); leftType = JetTypeMapper.boxType(leftType);
gen(right, rightType); gen(right, rightType);
v.valueOf(rightType); v.valueOf(rightType);
rightType = typeMapper.boxType(rightType); rightType = JetTypeMapper.boxType(rightType);
} }
else { else {
gen(left, leftType); gen(left, leftType);
@@ -1269,13 +1278,15 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
if(JetTypeMapper.isPrimitive(leftType)) // both are primitive if(JetTypeMapper.isPrimitive(leftType)) // both are primitive
return generateEqualsForExpressionsOnStack(opToken, leftType, rightType, false, false); return generateEqualsForExpressionsOnStack(opToken, leftType, rightType, false, false);
assert leftJetType != null;
assert rightJetType != null;
return generateEqualsForExpressionsOnStack(opToken, leftType, rightType, leftJetType.isNullable(), rightJetType.isNullable()); return generateEqualsForExpressionsOnStack(opToken, leftType, rightType, leftJetType.isNullable(), rightJetType.isNullable());
} }
private StackValue genCmpWithNull(JetExpression exp, Type expType, IElementType opToken) { private StackValue genCmpWithNull(JetExpression exp, Type expType, IElementType opToken) {
v.iconst(1); v.iconst(1);
gen(exp, typeMapper.boxType(expType)); gen(exp, JetTypeMapper.boxType(expType));
Label ok = new Label(); Label ok = new Label();
if(JetTokens.EQEQ == opToken || JetTokens.EQEQEQ == opToken) { if(JetTokens.EQEQ == opToken || JetTokens.EQEQEQ == opToken) {
v.ifnull(ok); v.ifnull(ok);
@@ -1427,6 +1438,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
final Callable callable = resolveToCallable(op); final Callable callable = resolveToCallable(op);
final JetExpression lhs = expression.getLeft(); final JetExpression lhs = expression.getLeft();
Type lhsType = expressionType(lhs); Type lhsType = expressionType(lhs);
//noinspection ConstantConditions
if (bindingContext.get(BindingContext.VARIABLE_REASSIGNMENT, expression)) { if (bindingContext.get(BindingContext.VARIABLE_REASSIGNMENT, expression)) {
if (callable instanceof IntrinsicMethod) { if (callable instanceof IntrinsicMethod) {
StackValue value = gen(lhs); // receiver StackValue value = gen(lhs); // receiver
@@ -1442,6 +1454,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
} }
} }
else { else {
assert op != null;
final boolean keepReturnValue = !((FunctionDescriptor) op).getReturnType().equals(JetStandardClasses.getUnitType()); final boolean keepReturnValue = !((FunctionDescriptor) op).getReturnType().equals(JetStandardClasses.getUnitType());
callAugAssignMethod(expression, (CallableMethod) callable, lhsType, keepReturnValue); callAugAssignMethod(expression, (CallableMethod) callable, lhsType, keepReturnValue);
} }
@@ -1515,6 +1528,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
DeclarationDescriptor cls = op.getContainingDeclaration(); DeclarationDescriptor cls = op.getContainingDeclaration();
if (isNumberPrimitive(cls) && (op.getName().equals("inc") || op.getName().equals("dec"))) { if (isNumberPrimitive(cls) && (op.getName().equals("inc") || op.getName().equals("dec"))) {
receiver.put(receiver.type, v); receiver.put(receiver.type, v);
//noinspection ConstantConditions
if (bindingContext.get(BindingContext.STATEMENT, expression)) { if (bindingContext.get(BindingContext.STATEMENT, expression)) {
generateIncrement(op, asmType, expression.getBaseExpression(), receiver); generateIncrement(op, asmType, expression.getBaseExpression(), receiver);
return StackValue.none(); return StackValue.none();
@@ -1569,6 +1583,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
JetExpression initializer = property.getInitializer(); JetExpression initializer = property.getInitializer();
if (initializer != null) { if (initializer != null) {
assert variableDescriptor != null;
Type type = typeMapper.mapType(variableDescriptor.getOutType()); Type type = typeMapper.mapType(variableDescriptor.getOutType());
gen(initializer, type); gen(initializer, type);
v.store(index, type); v.store(index, type);
@@ -1584,6 +1599,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
type = generateJavaConstructorCall(expression, (PsiMethod) declaration); type = generateJavaConstructorCall(expression, (PsiMethod) declaration);
} }
else if (constructorDescriptor instanceof ConstructorDescriptor) { else if (constructorDescriptor instanceof ConstructorDescriptor) {
//noinspection ConstantConditions
type = typeMapper.mapType(bindingContext.get(BindingContext.EXPRESSION_TYPE, expression), OwnerKind.IMPLEMENTATION); type = typeMapper.mapType(bindingContext.get(BindingContext.EXPRESSION_TYPE, expression), OwnerKind.IMPLEMENTATION);
if (type.getSort() == Type.ARRAY) { if (type.getSort() == Type.ARRAY) {
generateNewArray(expression, type); generateNewArray(expression, type);
@@ -1731,6 +1747,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
v.mark(clauseStart); v.mark(clauseStart);
VariableDescriptor descriptor = bindingContext.get(BindingContext.VALUE_PARAMETER, clause.getCatchParameter()); VariableDescriptor descriptor = bindingContext.get(BindingContext.VALUE_PARAMETER, clause.getCatchParameter());
assert descriptor != null;
Type descriptorType = typeMapper.mapType(descriptor.getOutType()); Type descriptorType = typeMapper.mapType(descriptor.getOutType());
myMap.enter(descriptor, 1); myMap.enter(descriptor, 1);
int index = lookupLocal(descriptor); int index = lookupLocal(descriptor);
@@ -1773,11 +1790,12 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
else { else {
JetTypeReference typeReference = expression.getRight(); JetTypeReference typeReference = expression.getRight();
JetType jetType = bindingContext.get(BindingContext.TYPE, typeReference); JetType jetType = bindingContext.get(BindingContext.TYPE, typeReference);
assert jetType != null;
DeclarationDescriptor descriptor = jetType.getConstructor().getDeclarationDescriptor(); DeclarationDescriptor descriptor = jetType.getConstructor().getDeclarationDescriptor();
if (!(descriptor instanceof ClassDescriptor)) { if (!(descriptor instanceof ClassDescriptor)) {
throw new UnsupportedOperationException("don't know how to handle non-class types in as/as?"); throw new UnsupportedOperationException("don't know how to handle non-class types in as/as?");
} }
Type type = typeMapper.boxType(typeMapper.mapType(jetType, OwnerKind.INTERFACE)); Type type = JetTypeMapper.boxType(typeMapper.mapType(jetType, OwnerKind.INTERFACE));
generateInstanceOf(StackValue.expression(OBJECT_TYPE, expression.getLeft(), this), jetType, true); generateInstanceOf(StackValue.expression(OBJECT_TYPE, expression.getLeft(), this), jetType, true);
Label isInstance = new Label(); Label isInstance = new Label();
v.ifne(isInstance); v.ifne(isInstance);
@@ -1829,6 +1847,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
else if (pattern instanceof JetBindingPattern) { else if (pattern instanceof JetBindingPattern) {
final JetProperty var = ((JetBindingPattern) pattern).getVariableDeclaration(); final JetProperty var = ((JetBindingPattern) pattern).getVariableDeclaration();
final VariableDescriptor variableDescriptor = bindingContext.get(BindingContext.VARIABLE, var); final VariableDescriptor variableDescriptor = bindingContext.get(BindingContext.VARIABLE, var);
assert variableDescriptor != null;
final Type varType = typeMapper.mapType(variableDescriptor.getOutType()); final Type varType = typeMapper.mapType(variableDescriptor.getOutType());
myMap.enter(variableDescriptor, varType.getSize()); myMap.enter(variableDescriptor, varType.getSize());
expressionToMatch.dupReceiver(v, 0); expressionToMatch.dupReceiver(v, 0);
@@ -1902,7 +1921,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
if (leaveExpressionOnStack) { if (leaveExpressionOnStack) {
v.dup(); v.dup();
} }
Type type = typeMapper.boxType(typeMapper.mapType(jetType, OwnerKind.INTERFACE)); Type type = JetTypeMapper.boxType(typeMapper.mapType(jetType, OwnerKind.INTERFACE));
if(jetType.isNullable()) { if(jetType.isNullable()) {
Label nope = new Label(); Label nope = new Label();
Label end = new Label(); Label end = new Label();
@@ -2104,6 +2123,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
JetBinaryExpression binaryExpression = (JetBinaryExpression) rangeExpression; JetBinaryExpression binaryExpression = (JetBinaryExpression) rangeExpression;
if (binaryExpression.getOperationReference().getReferencedNameElementType() == JetTokens.RANGE) { if (binaryExpression.getOperationReference().getReferencedNameElementType() == JetTokens.RANGE) {
JetType jetType = bindingContext.get(BindingContext.EXPRESSION_TYPE, rangeExpression); JetType jetType = bindingContext.get(BindingContext.EXPRESSION_TYPE, rangeExpression);
assert jetType != null;
final DeclarationDescriptor descriptor = jetType.getConstructor().getDeclarationDescriptor(); final DeclarationDescriptor descriptor = jetType.getConstructor().getDeclarationDescriptor();
if (isClass(descriptor, "IntRange")) { // TODO IntRange subclasses if (isClass(descriptor, "IntRange")) { // TODO IntRange subclasses
return true; return true;