Merge branch 'targetBytecode' of https://gitea.hb.dhbw-stuttgart.de/JavaTX/JavaCompilerCore into targetBytecode
Some checks failed
Build and Test with Maven / Build-and-test-with-Maven (push) Failing after 47s

This commit is contained in:
Daniel Holle 2024-06-07 12:04:01 +02:00
commit 60a1f3b220
4 changed files with 57 additions and 13 deletions

View File

@ -0,0 +1,11 @@
import java.util.List;
import java.lang.Integer;
import java.lang.String;
import java.lang.Object;
import java.util.List;
public class Bug338 {
public hashCode() {
return List.of(42);
}
}

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.target.generate; package de.dhbwstuttgart.target.generate;
import de.dhbwstuttgart.bytecode.CodeGenException;
import de.dhbwstuttgart.bytecode.FunNGenerator; import de.dhbwstuttgart.bytecode.FunNGenerator;
import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.environment.ByteArrayClassLoader; import de.dhbwstuttgart.environment.ByteArrayClassLoader;
@ -160,11 +161,11 @@ public class ASTToTargetAST {
var superInterfaces = input.getSuperInterfaces().stream().map(clazz -> convert(clazz, generics.javaGenerics)).toList(); var superInterfaces = input.getSuperInterfaces().stream().map(clazz -> convert(clazz, generics.javaGenerics)).toList();
var constructors = input.getConstructors().stream().map(constructor -> this.convert(input, constructor, finalFieldInitializer)).flatMap(List::stream).toList(); var constructors = input.getConstructors().stream().map(constructor -> this.convert(input, constructor, finalFieldInitializer)).flatMap(List::stream).toList();
var fields = input.getFieldDecl().stream().map(this::convert).toList(); var fields = input.getFieldDecl().stream().map(this::convert).toList();
var methods = groupOverloads(input.getMethods()).stream().map(m -> convert(input, m)).flatMap(List::stream).toList(); var methods = groupOverloads(input.getMethods()).stream().map(m -> convert(input, m)).flatMap(Set::stream).toList();
TargetMethod staticConstructor = null; TargetMethod staticConstructor = null;
if (input.getStaticInitializer().isPresent()) if (input.getStaticInitializer().isPresent())
staticConstructor = this.convert(input, input.getStaticInitializer().get()).get(0); staticConstructor = this.convert(input, input.getStaticInitializer().get()).stream().findFirst().orElseThrow();
if (input instanceof Record) if (input instanceof Record)
return new TargetRecord(input.getModifiers(), input.getClassName(), javaGenerics, txGenerics, superInterfaces, constructors, staticConstructor, fields, methods); return new TargetRecord(input.getModifiers(), input.getClassName(), javaGenerics, txGenerics, superInterfaces, constructors, staticConstructor, fields, methods);
@ -289,11 +290,11 @@ public class ASTToTargetAST {
return res.toString(); return res.toString();
} }
private List<TargetMethod> convert(ClassOrInterface clazz, List<Method> overloadedMethods) { private Set<TargetMethod> convert(ClassOrInterface clazz, List<Method> overloadedMethods) {
if (overloadedMethods.size() == 1) { if (overloadedMethods.size() == 1) {
return convert(clazz, overloadedMethods.get(0)); return convert(clazz, overloadedMethods.getFirst());
} }
var res = new ArrayList<Method>(); var methods = new ArrayList<Method>();
for (var method : overloadedMethods) { for (var method : overloadedMethods) {
var newMethod = new Method( var newMethod = new Method(
method.modifier, method.modifier,
@ -305,7 +306,7 @@ public class ASTToTargetAST {
method.getGenerics(), method.getGenerics(),
method.getOffset() method.getOffset()
); );
res.add(newMethod); methods.add(newMethod);
} }
// TODO Record overloading // TODO Record overloading
@ -328,7 +329,15 @@ public class ASTToTargetAST {
var entryPoint = new Method(template.modifier, template.name, template.getReturnType(), params, block, template.getGenerics(), new NullToken()); var entryPoint = new Method(template.modifier, template.name, template.getReturnType(), params, block, template.getGenerics(), new NullToken());
res.add(entryPoint); // TODO*/ res.add(entryPoint); // TODO*/
return res.stream().map(m -> convert(clazz, m)).flatMap(List::stream).toList(); var res = new HashSet<TargetMethod>();
for (var method : methods) {
var overloads = convert(clazz, method);
for (var overload : overloads) {
if (res.contains(overload)) throw new CodeGenException("Duplicate method found: " + overload.name() + " with signature " + overload.signature().getSignature());
res.add(overload);
}
}
return res;
} }
private Expression makeRecordSwitch(RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList params, List<Method> overloadedMethods) { private Expression makeRecordSwitch(RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList params, List<Method> overloadedMethods) {
@ -366,16 +375,15 @@ public class ASTToTargetAST {
for (var i = 0; i < params.size(); i++) { for (var i = 0; i < params.size(); i++) {
var a = TargetType.toPrimitive(params.get(i).pattern().type()); var a = TargetType.toPrimitive(params.get(i).pattern().type());
var b = convert(sParams.getFormalparalist().get(i).getType()); var b = convert(sParams.getFormalparalist().get(i).getType());
System.out.println(a + " " + b);
if (!Objects.equals(a, b)) return false; if (!Objects.equals(a, b)) return false;
} }
return true; return true;
}).findFirst(); }).findFirst();
} }
private List<TargetMethod> convert(ClassOrInterface currentClass, Method method) { private Set<TargetMethod> convert(ClassOrInterface currentClass, Method method) {
generics = all.get(0); generics = all.get(0);
List<TargetMethod> result = new ArrayList<>(); Set<TargetMethod> result = new HashSet<>();
Set<List<MethodParameter>> parameterSet = new HashSet<>(); Set<List<MethodParameter>> parameterSet = new HashSet<>();
for (var s : all) { for (var s : all) {
@ -387,9 +395,12 @@ public class ASTToTargetAST {
var superMethod = findSuperMethodToOverride(currentClass, method.getName(), params); var superMethod = findSuperMethodToOverride(currentClass, method.getName(), params);
if (superMethod.isPresent()) { if (superMethod.isPresent()) {
// If we find a super method to override, use its parameters and return types // If we find a super method to override, use its parameters and return types
returnType = convert(superMethod.get().getReturnType(), this.generics.javaGenerics); var newReturnType = convert(superMethod.get().getReturnType(), this.generics.javaGenerics);
if (newReturnType instanceof TargetPrimitiveType && TargetType.toPrimitive(returnType).equals(newReturnType)) {
returnType = newReturnType;
params = convert(superMethod.get().getParameterList(), method.getParameterList(), this.generics.javaGenerics); params = convert(superMethod.get().getParameterList(), method.getParameterList(), this.generics.javaGenerics);
} }
}
List<MethodParameter> finalParams = params; List<MethodParameter> finalParams = params;
if (parameterSet.stream().noneMatch(p -> p.equals(finalParams))) { if (parameterSet.stream().noneMatch(p -> p.equals(finalParams))) {
@ -400,10 +411,12 @@ public class ASTToTargetAST {
var javaSignature = new TargetMethod.Signature(javaMethodGenerics, params, returnType); var javaSignature = new TargetMethod.Signature(javaMethodGenerics, params, returnType);
var txSignature = new TargetMethod.Signature(txMethodGenerics, txParams, convert(method.getReturnType(), this.generics.txGenerics)); var txSignature = new TargetMethod.Signature(txMethodGenerics, txParams, convert(method.getReturnType(), this.generics.txGenerics));
result.add(new TargetMethod(method.modifier, method.name, convert(method.block), javaSignature, txSignature)); var newMethod = new TargetMethod(method.modifier, method.name, convert(method.block), javaSignature, txSignature);
result.add(newMethod);
parameterSet.add(params); parameterSet.add(params);
} }
} }
return result; return result;
} }

View File

@ -6,6 +6,7 @@ import de.dhbwstuttgart.target.tree.type.TargetType;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Set; import java.util.Set;
public record TargetMethod(int access, String name, TargetBlock block, Signature signature, Signature txSignature) { public record TargetMethod(int access, String name, TargetBlock block, Signature signature, Signature txSignature) {
@ -64,5 +65,16 @@ public record TargetMethod(int access, String name, TargetBlock block, Signature
public boolean isStatic() { public boolean isStatic() {
return (access & Opcodes.ACC_STATIC) != 0; return (access & Opcodes.ACC_STATIC) != 0;
} }
@Override
public boolean equals(Object other) {
if (!(other instanceof TargetMethod otherMethod)) return false;
return otherMethod.signature.equals(this.signature) && otherMethod.name.equals(this.name);
}
@Override
public int hashCode() {
return Objects.hash(name, signature);
}
} }

View File

@ -1101,6 +1101,7 @@ public class TestComplete {
var res = clazz.getDeclaredMethod("convert", List.class).invoke(instance, list); var res = clazz.getDeclaredMethod("convert", List.class).invoke(instance, list);
assertEquals(res, List.of(6, 7, 8)); assertEquals(res, List.of(6, 7, 8));
} }
@Test @Test
public void testBug325() throws Exception { public void testBug325() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug325.jav"); var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug325.jav");
@ -1133,10 +1134,17 @@ public class TestComplete {
} }
@Test @Test
<<<<<<< HEAD
public void testBug337() throws Exception { public void testBug337() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug337.jav"); var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug337.jav");
var clazz = classFiles.get("Bug337"); var clazz = classFiles.get("Bug337");
var instance = clazz.getDeclaredConstructor().newInstance(); var instance = clazz.getDeclaredConstructor().newInstance();
clazz.getDeclaredMethod("main").invoke(instance); clazz.getDeclaredMethod("main").invoke(instance);
=======
public void testBug338() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug338.jav");
var clazz = classFiles.get("Bug338");
var instance = clazz.getDeclaredConstructor().newInstance();
>>>>>>> ea217d16d5a1d635ac7fe961b98c2ad993c14ac5
} }
} }