From e3548384916e8a8e0d5013034b844c63afcd8d4b Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Thu, 14 Mar 2024 10:23:19 +0100 Subject: [PATCH] Use wildcard constraint in foreach, fixes #291 and hopefully doesn't break anything --- resources/bytecode/javFiles/ForEach.jav | 10 +++++++++- src/main/java/de/dhbwstuttgart/bytecode/Codegen.java | 1 + .../syntaxtree/visual/OutputGenerator.java | 10 +++++++++- .../dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/resources/bytecode/javFiles/ForEach.jav b/resources/bytecode/javFiles/ForEach.jav index 32b1eb8d..301066ad 100644 --- a/resources/bytecode/javFiles/ForEach.jav +++ b/resources/bytecode/javFiles/ForEach.jav @@ -1,8 +1,9 @@ import java.util.ArrayList; import java.lang.Integer; +import java.lang.Number; public class ForEach { - m() { + public m() { var list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); @@ -12,4 +13,11 @@ public class ForEach { } return sum; } + + public m2() { + var list = new ArrayList(); + + for (Number n : list) { + } + } } \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java index b28fe67d..6ea6e616 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java @@ -1073,6 +1073,7 @@ public class Codegen { mv.visitJumpInsn(IFEQ, end); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "next", "()Ljava/lang/Object;", true); + mv.visitTypeInsn(CHECKCAST, localVar.type().getInternalName()); mv.visitVarInsn(ASTORE, localVar.index); generate(state, forEach.body()); diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index e68adda6..638d13d6 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -246,7 +246,15 @@ public class OutputGenerator implements ASTVisitor { @Override public void visit(ForEachStmt forEachStmt) { - + out.append("for("); + forEachStmt.statement.accept(this); + out.append(" : "); + forEachStmt.expression.accept(this); + out.append(")\n"); + tab(); + out.append(tabs); + forEachStmt.block.accept(this); + untab(); } @Override diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 929b3726..3811c59c 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -138,7 +138,7 @@ public class TYPEStmt implements StatementVisitor { @Override public void visit(ForEachStmt forEachStmt) { - var iterableType = new RefType(ASTFactory.createClass(java.lang.Iterable.class).getClassName(), Arrays.asList(forEachStmt.statement.getType()), new NullToken()); + var iterableType = new RefType(ASTFactory.createClass(java.lang.Iterable.class).getClassName(), Arrays.asList(new ExtendsWildcardType(forEachStmt.statement.getType(), new NullToken())), new NullToken()); constraintsSet.addUndConstraint(new Pair(forEachStmt.expression.getType(), iterableType, PairOperator.SMALLERDOT, loc(forEachStmt.getOffset()))); forEachStmt.statement.accept(this); forEachStmt.expression.accept(this);