not yet working code for for..in loop over Java arrays

This commit is contained in:
Dmitry Jemerov
2011-04-15 17:29:22 +02:00
parent b3ceae05e3
commit 3aa9079ea2
4 changed files with 63 additions and 0 deletions
@@ -164,6 +164,55 @@ public class ExpressionCodegen extends JetVisitor {
myLoopStarts.pop();
}
@Override
public void visitForExpression(JetForExpression expression) {
final JetExpression loopRange = expression.getLoopRange();
final JetParameter parameter = expression.getLoopParameter();
final DeclarationDescriptor descriptor = bindingContext.getParameterDescriptor(parameter);
Type loopRangeType = expressionType(loopRange);
if (loopRangeType.getSort() == Type.ARRAY) {
generateForInArray(expression, descriptor, loopRangeType);
}
else {
throw new UnsupportedOperationException("for/in loop currently only supported for arrays");
}
}
private void generateForInArray(JetForExpression expression, DeclarationDescriptor descriptor, Type loopRangeType) {
JetType paramType = bindingContext.getExpressionType(expression.getLoopParameter());
Type asmParamType = typeMapper.mapType(paramType);
int indexVar = myMap.enterTemp();
v.aconst(0);
v.store(indexVar, Type.INT_TYPE);
int paramVar = myMap.getIndex(descriptor);
Label condition = new Label();
Label end = new Label();
v.mark(condition);
myLoopStarts.push(condition);
v.load(indexVar, Type.INT_TYPE);
gen(expression.getLoopRange(), loopRangeType); // index array
v.dup2(); // index array index array
v.arraylength(); // index array index array.length
v.ifge(end); // index array
v.swap(); // array index
v.aload(loopRangeType.getElementType());
StackValue.onStack(loopRangeType.getElementType()).put(asmParamType, v);
v.store(paramVar, asmParamType);
gen(expression.getBody(), Type.VOID_TYPE);
v.goTo(condition);
myLoopEnds.push(end);
myMap.leave(descriptor);
// TODO v.visitLocalVariable(parameter.getName(), );
myMap.leaveTemp();
}
@Override
public void visitBreakExpression(JetBreakExpression expression) {
JetSimpleNameExpression labelElement = expression.getTargetLabel();
@@ -24,6 +24,14 @@ public class FrameMap {
return myVarIndex.remove(descriptor);
}
public int enterTemp() {
return myMaxIndex++;
}
public void leaveTemp() {
myMaxIndex--;
}
public int getIndex(DeclarationDescriptor descriptor) {
return myVarIndex.contains(descriptor) ? myVarIndex.get(descriptor) : -1;
}
@@ -13,6 +13,7 @@ public interface BindingContext {
TypeParameterDescriptor getTypeParameterDescriptor(JetTypeParameter declaration);
FunctionDescriptor getFunctionDescriptor(JetFunction declaration);
PropertyDescriptor getPropertyDescriptor(JetProperty declaration);
DeclarationDescriptor getParameterDescriptor(JetParameter declaration);
JetType getExpressionType(JetExpression expression);
@@ -111,6 +111,11 @@ public class BindingTraceContext extends BindingTrace implements BindingContext
return (PropertyDescriptor) declarationsToDescriptors.get(declaration);
}
@Override
public DeclarationDescriptor getParameterDescriptor(JetParameter declaration) {
return declarationsToDescriptors.get(declaration);
}
@Override
public JetType resolveTypeReference(JetTypeReference typeReference) {
return types.get(typeReference);