From cf2d612870eed3e284e022fc8dfa20501cf8da45 Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Fri, 24 Mar 2023 14:51:57 +0100 Subject: [PATCH] Fix TX Signature by adding additional prameters to every method and constructor --- .../de/dhbwstuttgart/bytecode/Codegen.java | 22 +++++---- .../target/generate/ASTToTargetAST.java | 47 +++++++++++-------- .../target/tree/TargetClass.java | 4 +- .../target/tree/TargetConstructor.java | 5 +- .../target/tree/TargetMethod.java | 10 ++-- 5 files changed, 51 insertions(+), 37 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java index efa8def2..3d94be1c 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java @@ -717,10 +717,14 @@ public class Codegen { .toList()); impl = new TargetMethod( - 0, name, Set.of(), Set.of(), - parameters, - lambda.returnType() instanceof TargetGenericType ? TargetType.Object : lambda.returnType(), - lambda.block() + 0, name, + lambda.block(), + new TargetMethod.Signature( + Set.of(), + parameters, + lambda.returnType() instanceof TargetGenericType ? TargetType.Object : lambda.returnType() + ), + null ); generateMethod(impl); lambdas.put(lambda, impl); @@ -758,7 +762,7 @@ public class Codegen { mv.visitInvokeDynamicInsn("apply", descriptor, bootstrap, Type.getType(desugared), handle, - Type.getType(TargetMethod.getDescriptor(impl.returnType(), lambda.params().stream().map(MethodParameter::type).toArray(TargetType[]::new))) + Type.getType(TargetMethod.getDescriptor(impl.signature().returnType(), lambda.params().stream().map(MethodParameter::type).toArray(TargetType[]::new))) ); } @@ -1022,17 +1026,17 @@ public class Codegen { private void generateMethod(TargetMethod method) { // TODO The older codegen has set ACC_PUBLIC for all methods, good for testing but bad for everything else MethodVisitor mv = cw.visitMethod(method.access() | ACC_PUBLIC, method.name(), method.getDescriptor(), method.getSignature(), null); - if (!method.txGenerics().isEmpty()) + if (method.txSignature() != null) mv.visitAttribute(new JavaTXSignatureAttribute(method.getTXSignature())); System.out.println(method.getDescriptor()); System.out.println(method.getSignature()); mv.visitCode(); - var state = new State(method.returnType(), mv, method.isStatic() ? 0 : 1); - for (var param: method.parameters()) + var state = new State(method.signature().returnType(), mv, method.isStatic() ? 0 : 1); + for (var param: method.signature().parameters()) state.createVariable(param.name(), param.type()); generate(state, method.block()); - if (method.returnType() == null) + if (method.signature().returnType() == null) mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index 81cb295e..b4f6978b 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -933,22 +933,22 @@ public class ASTToTargetAST { return true; } - Set convert(Set> result) { + Set convert(Set> result, Map equality) { return result.stream().map(p -> { if (p instanceof PairTPHsmallerTPH pair) { - return new TargetGeneric(pair.left.getName(), convert(pair.right)); + return new TargetGeneric(pair.left.getName(), convert(pair.right, equality)); } else if (p instanceof PairTPHequalRefTypeOrWildcardType pair) { - return new TargetGeneric(pair.left.getName(), convert(pair.right)); + return new TargetGeneric(pair.left.getName(), convert(pair.right, equality)); } else { throw new IllegalArgumentException(); } }).collect(Collectors.toSet()); } - public List convert(GenericTypeVar typeVar) { + public List convert(GenericTypeVar typeVar, Map equality) { var ret = new ArrayList(); for (var bound : typeVar.getBounds()) { - ret.add(new TargetGeneric(typeVar.getName(), convert(bound))); + ret.add(new TargetGeneric(typeVar.getName(), convert(bound, equality))); } return ret; } @@ -965,13 +965,13 @@ public class ASTToTargetAST { while (genericsIter.hasNext()) { var next = genericsIter.next(); userDefinedGenerics.add(next); - javaGenerics.addAll(convert(next)); + javaGenerics.addAll(convert(next, sigma.equality)); } } else { // Generate generics only if there are no user defined ones var generics = sigma.generics(input); - javaGenerics = convert(generics.javaGenerics()); - txGenerics = convert(generics.txGenerics()); + javaGenerics = convert(generics.javaGenerics(), sigma.equality); + txGenerics = convert(generics.txGenerics(), sigma.txEquality); } TargetBlock fieldInitializer = null; @@ -988,9 +988,9 @@ public class ASTToTargetAST { ); } - private List convert(ParameterList input) { + private List convert(ParameterList input, Map equality) { return input.getFormalparalist().stream() - .map(param -> new MethodParameter(convert(param.getType()), param.getName())).toList(); + .map(param -> new MethodParameter(convert(param.getType(), equality), param.getName())).toList(); } private boolean hasGeneric(Set generics, GenericRefType type) { @@ -998,7 +998,7 @@ public class ASTToTargetAST { } private Set collectMethodGenerics(Set> generics, Map equality, Method input) { - var convertedGenerics = new HashSet<>(convert(generics)); + var convertedGenerics = new HashSet<>(convert(generics, equality)); outer: for (GenericTypeVar typeVar : input.getGenerics()) { for (var classGeneric : currentClass.getGenerics()) { @@ -1006,7 +1006,7 @@ public class ASTToTargetAST { continue outer; } } - convertedGenerics.addAll(convert(typeVar)); + convertedGenerics.addAll(convert(typeVar, equality)); } /*var returnType = sigma.getType(input.getReturnType(), equality); if ((returnType instanceof GenericRefType refType) && !hasGeneric(convertedGenerics, refType)) { @@ -1030,12 +1030,13 @@ public class ASTToTargetAST { for (var s : all) { sigma = s; var generics = sigma.generics(currentClass, input); - List params = convert(input.getParameterList()); + List params = convert(input.getParameterList(), sigma.equality); if (parameterSet.stream().noneMatch(p -> p.equals(params))) { + List txParams = convert(input.getParameterList(), sigma.txEquality); var javaGenerics = collectMethodGenerics(generics.javaGenerics(), sigma.equality, input); var txGenerics = collectMethodGenerics(generics.txGenerics(), sigma.txEquality, input); - result.add(new TargetConstructor(input.modifier, javaGenerics, txGenerics, params, convert(input.block), fieldInitializer)); + result.add(new TargetConstructor(input.modifier, javaGenerics, txGenerics, params, txParams, convert(input.block), fieldInitializer)); parameterSet.add(params); } } @@ -1051,16 +1052,18 @@ public class ASTToTargetAST { for (var s : all) { sigma = s; var generics = sigma.generics(currentClass, input); - List params = convert(input.getParameterList()); + List params = convert(input.getParameterList(), sigma.equality); if (parameterSet.stream().noneMatch(p -> p.equals(params))) { + List txParams = convert(input.getParameterList(), sigma.txEquality); + var javaGenerics = collectMethodGenerics(generics.javaGenerics(), sigma.equality, input); var txGenerics = collectMethodGenerics(generics.txGenerics(), sigma.txEquality, input); result.add(new TargetMethod( input.modifier, - input.name, javaGenerics, txGenerics, params, - convert(input.getReturnType()), - convert(input.block) + input.name, convert(input.block), + new TargetMethod.Signature(javaGenerics, params, convert(input.getReturnType(), sigma.equality)), + new TargetMethod.Signature(txGenerics, txParams, convert(input.getReturnType(), sigma.txEquality)) )); parameterSet.add(params); } @@ -1092,7 +1095,11 @@ public class ASTToTargetAST { public Map auxiliaries = new HashMap<>(); - protected TargetType convert(RefTypeOrTPHOrWildcardOrGeneric input) { + protected TargetType convert(RefTypeOrTPHOrWildcardOrGeneric input) { + return convert(input, sigma.equality); + } + + protected TargetType convert(RefTypeOrTPHOrWildcardOrGeneric input, Map equality) { return input.acceptTV(new TypeVisitor<>() { @Override public TargetType visit(RefType refType) { @@ -1126,7 +1133,7 @@ public class ASTToTargetAST { @Override public TargetType visit(TypePlaceholder typePlaceholder) { - return sigma.getTargetType(typePlaceholder, sigma.equality); + return sigma.getTargetType(typePlaceholder, equality); } @Override diff --git a/src/main/java/de/dhbwstuttgart/target/tree/TargetClass.java b/src/main/java/de/dhbwstuttgart/target/tree/TargetClass.java index 781df996..3118a1d8 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/TargetClass.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/TargetClass.java @@ -25,7 +25,7 @@ public record TargetClass(int modifiers, String qualifiedName, TargetType superT } public void addMethod(int access, String name, Set generics, List parameterTypes, TargetType returnType, TargetBlock block) { - this.methods.add(new TargetMethod(access, name, generics, Set.of(), parameterTypes, returnType, block)); + this.methods.add(new TargetMethod(access, name, block, new TargetMethod.Signature(generics, parameterTypes, returnType), null)); } public void addMethod(int access, String name, List parameterTypes, TargetType returnType, TargetBlock block) { @@ -33,7 +33,7 @@ public record TargetClass(int modifiers, String qualifiedName, TargetType superT } public void addConstructor(int access, Set generics, List paramterTypes, TargetBlock block) { - this.constructors.add(new TargetConstructor(access, generics, Set.of(), paramterTypes, block, null)); + this.constructors.add(new TargetConstructor(access, generics, Set.of(), paramterTypes, List.of(), block, null)); } public void addConstructor(int access, List paramterTypes, TargetBlock block) { diff --git a/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java b/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java index 7d3aa389..1da5dc93 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java @@ -1,12 +1,13 @@ package de.dhbwstuttgart.target.tree; +import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.target.tree.expression.TargetBlock; import de.dhbwstuttgart.target.tree.type.TargetType; import java.util.List; import java.util.Set; -public record TargetConstructor(int access, Set generics, Set txGenerics, List parameters, TargetBlock block, TargetBlock fieldInitializer) { +public record TargetConstructor(int access, Set generics, Set txGenerics, List parameters, List txParameters, TargetBlock block, TargetBlock fieldInitializer) { public String getDescriptor() { return TargetMethod.getDescriptor(null, parameters.stream().map(MethodParameter::type).toArray(TargetType[]::new)); @@ -17,7 +18,7 @@ public record TargetConstructor(int access, Set generics, Set generics, Set txGenerics, List parameters, TargetType returnType, TargetBlock block) { +public record TargetMethod(int access, String name, TargetBlock block, Signature signature, Signature txSignature) { + public record Signature(Set generics, List parameters, TargetType returnType) { } + public static String getDescriptor(TargetType returnType, TargetType... parameters) { String ret = "("; for (var parameterType : parameters) { @@ -35,15 +37,15 @@ public record TargetMethod(int access, String name, Set generics, } public String getDescriptor() { - return getDescriptor(returnType, parameters.stream().map(MethodParameter::type).toArray(TargetType[]::new)); + return getDescriptor(signature.returnType, signature.parameters.stream().map(MethodParameter::type).toArray(TargetType[]::new)); } public String getSignature() { - return getSignature(generics, parameters, returnType); + return getSignature(signature.generics, signature.parameters, signature.returnType); } public String getTXSignature() { - return getSignature(txGenerics, parameters, returnType); + return getSignature(txSignature.generics, txSignature.parameters, txSignature.returnType); } public boolean isStatic() {