bug fix VectorNotObject test

This commit is contained in:
luca9913 2023-06-01 20:59:16 +02:00
parent 52b7040337
commit 30a62a6621
5 changed files with 647 additions and 699 deletions

View File

@ -51,11 +51,10 @@ http://maven.apache.org/maven-v4_0_0.xsd">
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<version>3.11.0</version>
<configuration>
<compilerArgs>--enable-preview</compilerArgs>
<source>19</source>
<target>19</target>
<release>20</release>
</configuration>
</plugin>
<plugin>
@ -112,8 +111,6 @@ http://maven.apache.org/maven-v4_0_0.xsd">
</repository>
</repositories>
<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<mainClass>de.dhbwstuttgart.core.ConsoleInterface</mainClass>
</properties>
<distributionManagement>

View File

@ -28,7 +28,8 @@ public class Codegen {
this.cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
}
private record LocalVar(int index, String name, TargetType type) {}
private record LocalVar(int index, String name, TargetType type) {
}
private static class Scope {
Scope parent;
@ -196,9 +197,7 @@ public class Codegen {
mv.visitInsn(L2F);
else if (dest.equals(TargetType.Double))
mv.visitInsn(L2D);
else if (dest.equals(TargetType.Byte)
|| dest.equals(TargetType.Char)
|| dest.equals(TargetType.Short)) {
else if (dest.equals(TargetType.Byte) || dest.equals(TargetType.Char) || dest.equals(TargetType.Short)) {
mv.visitInsn(L2I);
convertTo(state, TargetType.Integer, dest);
}
@ -209,9 +208,7 @@ public class Codegen {
mv.visitInsn(F2D);
else if (dest.equals(TargetType.Long))
mv.visitInsn(F2L);
else if (dest.equals(TargetType.Byte)
|| dest.equals(TargetType.Char)
|| dest.equals(TargetType.Short)) {
else if (dest.equals(TargetType.Byte) || dest.equals(TargetType.Char) || dest.equals(TargetType.Short)) {
mv.visitInsn(F2I);
convertTo(state, TargetType.Integer, dest);
}
@ -222,16 +219,11 @@ public class Codegen {
mv.visitInsn(D2F);
else if (dest.equals(TargetType.Long))
mv.visitInsn(D2L);
else if (dest.equals(TargetType.Byte)
|| dest.equals(TargetType.Char)
|| dest.equals(TargetType.Short)) {
else if (dest.equals(TargetType.Byte) || dest.equals(TargetType.Char) || dest.equals(TargetType.Short)) {
mv.visitInsn(D2I);
convertTo(state, TargetType.Integer, dest);
}
} else if (source.equals(TargetType.Byte)
|| source.equals(TargetType.Char)
|| source.equals(TargetType.Short)
|| source.equals(TargetType.Integer)) {
} else if (source.equals(TargetType.Byte) || source.equals(TargetType.Char) || source.equals(TargetType.Short) || source.equals(TargetType.Integer)) {
if (dest.equals(TargetType.Byte))
mv.visitInsn(I2B);
else if (dest.equals(TargetType.Char))
@ -281,10 +273,7 @@ public class Codegen {
generate(state, add.right());
convertTo(state, add.right().type(), add.type());
var type = add.type();
if (type.equals(TargetType.Byte)
|| type.equals(TargetType.Char)
|| type.equals(TargetType.Integer)
|| type.equals(TargetType.Short)) {
if (type.equals(TargetType.Byte) || type.equals(TargetType.Char) || type.equals(TargetType.Integer) || type.equals(TargetType.Short)) {
mv.visitInsn(IADD);
} else if (type.equals(TargetType.Long)) {
mv.visitInsn(LADD);
@ -310,10 +299,7 @@ public class Codegen {
generate(state, sub.right());
convertTo(state, sub.right().type(), op.type());
var type = sub.type();
if (type.equals(TargetType.Byte)
|| type.equals(TargetType.Char)
|| type.equals(TargetType.Integer)
|| type.equals(TargetType.Short)) {
if (type.equals(TargetType.Byte) || type.equals(TargetType.Char) || type.equals(TargetType.Integer) || type.equals(TargetType.Short)) {
mv.visitInsn(ISUB);
} else if (type.equals(TargetType.Long)) {
mv.visitInsn(LSUB);
@ -332,10 +318,7 @@ public class Codegen {
generate(state, div.right());
convertTo(state, div.right().type(), op.type());
var type = div.type();
if (type.equals(TargetType.Byte)
|| type.equals(TargetType.Char)
|| type.equals(TargetType.Integer)
|| type.equals(TargetType.Short)) {
if (type.equals(TargetType.Byte) || type.equals(TargetType.Char) || type.equals(TargetType.Integer) || type.equals(TargetType.Short)) {
mv.visitInsn(IDIV);
} else if (type.equals(TargetType.Long)) {
mv.visitInsn(LDIV);
@ -354,10 +337,7 @@ public class Codegen {
generate(state, mul.right());
convertTo(state, mul.right().type(), op.type());
var type = mul.type();
if (type.equals(TargetType.Byte)
|| type.equals(TargetType.Char)
|| type.equals(TargetType.Integer)
|| type.equals(TargetType.Short)) {
if (type.equals(TargetType.Byte) || type.equals(TargetType.Char) || type.equals(TargetType.Integer) || type.equals(TargetType.Short)) {
mv.visitInsn(IMUL);
} else if (type.equals(TargetType.Long)) {
mv.visitInsn(LMUL);
@ -376,10 +356,7 @@ public class Codegen {
generate(state, rem.right());
convertTo(state, rem.right().type(), op.type());
var type = rem.type();
if (type.equals(TargetType.Byte)
|| type.equals(TargetType.Char)
|| type.equals(TargetType.Integer)
|| type.equals(TargetType.Short)) {
if (type.equals(TargetType.Byte) || type.equals(TargetType.Char) || type.equals(TargetType.Integer) || type.equals(TargetType.Short)) {
mv.visitInsn(IREM);
} else if (type.equals(TargetType.Long)) {
mv.visitInsn(LREM);
@ -429,7 +406,8 @@ public class Codegen {
convertTo(state, band.right().type(), op.type());
if (band.type().equals(TargetType.Long))
mv.visitInsn(LAND);
else mv.visitInsn(IAND);
else
mv.visitInsn(IAND);
break;
}
case BOr bor: {
@ -439,7 +417,8 @@ public class Codegen {
convertTo(state, bor.right().type(), op.type());
if (bor.type().equals(TargetType.Long))
mv.visitInsn(LOR);
else mv.visitInsn(IOR);
else
mv.visitInsn(IOR);
break;
}
case XOr xor: {
@ -449,7 +428,8 @@ public class Codegen {
convertTo(state, xor.right().type(), op.type());
if (xor.type().equals(TargetType.Long))
mv.visitInsn(LXOR);
else mv.visitInsn(IXOR);
else
mv.visitInsn(IXOR);
break;
}
case Shl shl: {
@ -459,7 +439,8 @@ public class Codegen {
convertTo(state, shl.right().type(), op.type());
if (shl.type().equals(TargetType.Long))
mv.visitInsn(LSHL);
else mv.visitInsn(ISHL);
else
mv.visitInsn(ISHL);
break;
}
case Shr shr: {
@ -469,7 +450,8 @@ public class Codegen {
convertTo(state, shr.right().type(), op.type());
if (shr.type().equals(TargetType.Long))
mv.visitInsn(LSHR);
else mv.visitInsn(ISHR);
else
mv.visitInsn(ISHR);
break;
}
case UShr ushr: {
@ -479,7 +461,8 @@ public class Codegen {
convertTo(state, ushr.right().type(), op.type());
if (ushr.type().equals(TargetType.Long))
mv.visitInsn(LUSHR);
else mv.visitInsn(IUSHR);
else
mv.visitInsn(IUSHR);
break;
}
case Greater greater: {
@ -542,11 +525,7 @@ public class Codegen {
generateRelationalOperator(state, equal, type, FCMPL, IFEQ);
} else if (type.equals(TargetType.Double)) {
generateRelationalOperator(state, equal, type, DCMPL, IFEQ);
} else if (type.equals(TargetType.Char)
|| type.equals(TargetType.Short)
|| type.equals(TargetType.Byte)
|| type.equals(TargetType.Integer)
|| type.equals(TargetType.Boolean)) {
} else if (type.equals(TargetType.Char) || type.equals(TargetType.Short) || type.equals(TargetType.Byte) || type.equals(TargetType.Integer) || type.equals(TargetType.Boolean)) {
generateRelationalOperator(state, equal, type, IF_ICMPEQ);
} else {
generateRelationalOperator(state, equal, type, IF_ACMPEQ);
@ -561,10 +540,7 @@ public class Codegen {
generateRelationalOperator(state, notEqual, type, FCMPL, IFNE);
} else if (type.equals(TargetType.Double)) {
generateRelationalOperator(state, notEqual, type, DCMPL, IFNE);
} else if (type.equals(TargetType.Char)
|| type.equals(TargetType.Short)
|| type.equals(TargetType.Byte)
|| type.equals(TargetType.Integer)) {
} else if (type.equals(TargetType.Char) || type.equals(TargetType.Short) || type.equals(TargetType.Byte) || type.equals(TargetType.Integer)) {
generateRelationalOperator(state, notEqual, type, IF_ICMPNE);
} else {
generateRelationalOperator(state, notEqual, type, IF_ACMPNE);
@ -600,7 +576,8 @@ public class Codegen {
mv.visitInsn(FNEG);
else if (negate.type().equals(TargetType.Long))
mv.visitInsn(LNEG);
else mv.visitInsn(INEG);
else
mv.visitInsn(INEG);
break;
case TargetUnaryOp.Not not:
generate(state, not.expr());
@ -712,29 +689,17 @@ public class Codegen {
} else {
var name = "lambda$" + lambdaCounter++;
var parameters = new ArrayList<>(lambda.captures());
parameters.addAll(lambda.params().stream()
.map(param -> param.type() instanceof TargetGenericType ? new MethodParameter(TargetType.Object, param.name()) : param)
.toList());
parameters.addAll(lambda.params().stream().map(param -> param.type() instanceof TargetGenericType ? new MethodParameter(TargetType.Object, param.name()) : param).toList());
impl = new TargetMethod(
0, name, Set.of(), Set.of(),
parameters,
lambda.returnType() instanceof TargetGenericType ? TargetType.Object : lambda.returnType(),
lambda.block()
);
impl = new TargetMethod(0, name, Set.of(), Set.of(), parameters, lambda.returnType() instanceof TargetGenericType ? TargetType.Object : lambda.returnType(), lambda.block());
generateMethod(impl);
lambdas.put(lambda, impl);
}
var mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class,
MethodType.class, MethodType.class, MethodHandle.class, MethodType.class);
var mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class, MethodType.class, MethodHandle.class, MethodType.class);
var bootstrap = new Handle(H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory",
mt.toMethodDescriptorString(), false);
var handle = new Handle(
H_INVOKEVIRTUAL, clazz.getName(), impl.name(),
impl.getDescriptor(), false
);
var bootstrap = new Handle(H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory", mt.toMethodDescriptorString(), false);
var handle = new Handle(H_INVOKEVIRTUAL, clazz.getName(), impl.name(), impl.getDescriptor(), false);
// TODO maybe make this a function?
var desugared = "(";
@ -743,23 +708,19 @@ public class Codegen {
desugared += ")";
if (lambda.returnType() != null)
desugared += "Ljava/lang/Object;";
else desugared += "V";
else
desugared += "V";
var params = new ArrayList<TargetType>();
params.add(new TargetRefType(clazz.qualifiedName()));
params.addAll(lambda.captures().stream()
.map(MethodParameter::type)
.toList());
params.addAll(lambda.captures().stream().map(MethodParameter::type).toList());
var descriptor = TargetMethod.getDescriptor(lambda.type(), params.toArray(TargetType[]::new));
mv.visitVarInsn(ALOAD, 0);
for (var capture : lambda.captures())
mv.visitVarInsn(ALOAD, state.scope.get(capture.name()).index);
mv.visitInvokeDynamicInsn("apply", descriptor,
bootstrap, Type.getType(desugared), handle,
Type.getType(TargetMethod.getDescriptor(impl.returnType(), lambda.params().stream().map(MethodParameter::type).toArray(TargetType[]::new)))
);
mv.visitInvokeDynamicInsn("apply", descriptor, bootstrap, Type.getType(desugared), handle, Type.getType(TargetMethod.getDescriptor(impl.returnType(), lambda.params().stream().map(MethodParameter::type).toArray(TargetType[]::new))));
}
private void generate(State state, TargetExpression expr) {
@ -773,7 +734,8 @@ public class Codegen {
for (var e : block.statements()) {
generate(state, e);
if (e instanceof TargetMethodCall) {
if (e.type() != null) popValue(state, e.type());
if (e.type() != null)
popValue(state, e.type());
} else if (e instanceof TargetAssign) {
mv.visitInsn(POP); // TODO Nasty fix, we don't know if it is a primitive double or long
} else if (e instanceof TargetStatementExpression se) {
@ -856,7 +818,8 @@ public class Codegen {
boxPrimitive(state, fieldType);
if (dot.isStatic())
mv.visitInsn(DUP);
else mv.visitInsn(DUP_X1);
else
mv.visitInsn(DUP_X1);
mv.visitFieldInsn(dot.isStatic() ? PUTSTATIC : PUTFIELD, dot.owner().getInternalName(), dot.right(), fieldType.toSignature());
break;
}
@ -888,7 +851,8 @@ public class Codegen {
mv.visitLabel(start);
if (_for.termination() != null)
generate(state, _for.termination());
else mv.visitInsn(ICONST_1);
else
mv.visitInsn(ICONST_1);
mv.visitJumpInsn(IFEQ, end);
generate(state, _for.body());
if (_for.increment() != null) {
@ -934,7 +898,8 @@ public class Codegen {
convertTo(state, ret.expression().type(), state.returnType);
boxPrimitive(state, state.returnType);
mv.visitInsn(ARETURN);
} else mv.visitInsn(RETURN);
} else
mv.visitInsn(RETURN);
break;
}
case TargetThis _this: {
@ -956,13 +921,9 @@ public class Codegen {
}
var descriptor = call.getDescriptor();
if (call.owner() instanceof TargetFunNType) // Decay FunN
descriptor = TargetMethod.getDescriptor(
call.returnType() == null ? null : TargetType.Object,
call.parameterTypes().stream().map(x -> TargetType.Object).toArray(TargetType[]::new)
);
descriptor = TargetMethod.getDescriptor(call.returnType() == null ? null : TargetType.Object, call.parameterTypes().stream().map(x -> TargetType.Object).toArray(TargetType[]::new));
mv.visitMethodInsn(call.isInterface() ? INVOKEINTERFACE : call.isStatic() ? INVOKESTATIC: call.name().equals("<init>") ? INVOKESPECIAL : INVOKEVIRTUAL,
call.owner().getInternalName(), call.name(), descriptor, call.isInterface());
mv.visitMethodInsn(call.isInterface() ? INVOKEINTERFACE : call.isStatic() ? INVOKESTATIC : call.name().equals("<init>") ? INVOKESPECIAL : INVOKEVIRTUAL, call.owner().getInternalName(), call.name(), descriptor, call.isInterface());
if (call.type() != null && call.returnType() != null && !(call.returnType() instanceof TargetPrimitiveType)) {
if (!call.returnType().equals(call.type()) && !(call.type() instanceof TargetGenericType))
@ -1048,10 +1009,7 @@ public class Codegen {
}
public byte[] generate() {
cw.visit(V1_8, clazz.modifiers() | ACC_PUBLIC | ACC_SUPER, clazz.qualifiedName(),
generateSignature(clazz, clazz.generics()), clazz.superType() != null ? clazz.superType().getInternalName(): "java/lang/Object",
clazz.implementingInterfaces().stream().map(TargetType::toSignature).toArray(String[]::new)
);
cw.visit(V1_8, clazz.modifiers() | ACC_PUBLIC | ACC_SUPER, clazz.qualifiedName(), generateSignature(clazz, clazz.generics()), clazz.superType() != null ? clazz.superType().getInternalName() : "java/lang/Object", clazz.implementingInterfaces().stream().map(TargetType::toSignature).toArray(String[]::new));
if (!clazz.txGenerics().isEmpty())
cw.visitAttribute(new JavaTXSignatureAttribute(generateSignature(clazz, clazz.txGenerics())));

View File

@ -122,17 +122,15 @@ public class StatementGenerator {
public ParameterList convert(Java17Parser.FormalParameterListContext formalParameterListContext) {
List<FormalParameter> ret = new ArrayList<>();
List<Java17Parser.FormalParameterContext> fps = new ArrayList<>();
if (formalParameterListContext == null || formalParameterListContext.lastFormalParameter() == null)
return new ParameterList(ret, new NullToken()); // Dann ist die Parameterliste leer
if (!Objects.isNull(formalParameterListContext.lastFormalParameter())) {
/*
* Restrukturierung Java17-Grammatik Zeile wird nicht mehr benötigt, da Regel für Parameter-Liste nicht mehr rekursiv ist. "lastFormalParameter" hat kein Kind "formalParameter" mehr.
*
* if(formalParameterListContext.lastFormalParameter().formalParameter() == null) throw new NotImplementedException();
* Der '...' Operator wird noch nicht unterstützt, da dafür benötigte Typen (List oder Array) nicht vom Typinferenzalgo. verarbeitet werden können
*/
throw new NotImplementedException("Formale Parameter variabler Länge nicht unterstützt.");
}
if (formalParameterListContext.children.size() == 0)
return new ParameterList(ret, new NullToken()); // Dann ist die Parameterliste leer
fps = formalParameterListContext.formalParameter();
for (Java17Parser.FormalParameterContext fp : fps) {
String paramName = SyntaxTreeGenerator.convert(fp.variableDeclaratorId());
RefTypeOrTPHOrWildcardOrGeneric type;

View File

@ -206,7 +206,7 @@ public class StatementToTargetExpression implements StatementVisitor {
var returnType = isFunNType ? TargetType.Object : converter.convert(methodCall.signature.get(methodCall.signature.size() - 1));
var receiverName = new JavaClassName(converter.convert(methodCall.receiver.getType()).name());
var argList = methodCall.signature.stream().map(converter::convert).toList();
argList = argList.subList(0, argList.size() - 1);
// argList = argList.subList(0, argList.size() - 1);
Method foundMethod = null;
if (methodCall.receiver instanceof ExpressionReceiver expressionReceiver && expressionReceiver.expr instanceof This) {

View File

@ -487,7 +487,6 @@ public class TestComplete {
assertEquals(W, m2.getGenericReturnType());
}
@Test
public void Tph7Test() throws Exception {
var classFiles = TestCodegen.generateClassFiles("Tph7.jav", new ByteArrayClassLoader());
@ -509,15 +508,13 @@ public class TestComplete {
// Conditions for the extracted typeparameters are set
// paraTypes[0] = O
var boundFstArg = Arrays.stream(typeParaTypes)
.filter(x -> x.equals(paraTypes[0])).findFirst().get().getBounds();
var boundFstArg = Arrays.stream(typeParaTypes).filter(x -> x.equals(paraTypes[0])).findFirst().get().getBounds();
// Bound of O has to be Object
assertEquals(Object.class, Arrays.stream(boundFstArg).findFirst().get());
// paraTypes[1] = N
var N = Arrays.stream(typeParaTypes)
.filter(x -> x.equals(paraTypes[1])).findFirst().get();
var N = Arrays.stream(typeParaTypes).filter(x -> x.equals(paraTypes[1])).findFirst().get();
var boundSndArg = N.getBounds();
// Bound of H has to be Object
@ -539,8 +536,7 @@ public class TestComplete {
// Conditions for the extracted typeparameters are set
// paraTypes[0] = DZU
var fstArgm2 = Arrays.stream(typeParaTypesm2)
.filter(x -> x.equals(paraTypesm2[0])).findFirst().get();
var fstArgm2 = Arrays.stream(typeParaTypesm2).filter(x -> x.equals(paraTypesm2[0])).findFirst().get();
// Bound of DZU has to be Object
assertEquals(Object.class, Arrays.stream(fstArgm2.getBounds()).findFirst().get());
@ -549,7 +545,6 @@ public class TestComplete {
assertEquals(fstArgm2, m2.getGenericReturnType());
}
@Test
public void typedIdTest() throws Exception {
var classFiles = generateClassFiles("TypedID.jav", new ByteArrayClassLoader());