Fix TX Signature by adding additional prameters to every method and constructor

This commit is contained in:
Victorious3 2023-03-24 14:51:57 +01:00
parent b55fc71811
commit cf2d612870
5 changed files with 51 additions and 37 deletions

View File

@ -717,10 +717,14 @@ public class Codegen {
.toList()); .toList());
impl = new TargetMethod( impl = new TargetMethod(
0, name, Set.of(), Set.of(), 0, name,
parameters, lambda.block(),
lambda.returnType() instanceof TargetGenericType ? TargetType.Object : lambda.returnType(), new TargetMethod.Signature(
lambda.block() Set.of(),
parameters,
lambda.returnType() instanceof TargetGenericType ? TargetType.Object : lambda.returnType()
),
null
); );
generateMethod(impl); generateMethod(impl);
lambdas.put(lambda, impl); lambdas.put(lambda, impl);
@ -758,7 +762,7 @@ public class Codegen {
mv.visitInvokeDynamicInsn("apply", descriptor, mv.visitInvokeDynamicInsn("apply", descriptor,
bootstrap, Type.getType(desugared), handle, 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) { private void generateMethod(TargetMethod method) {
// TODO The older codegen has set ACC_PUBLIC for all methods, good for testing but bad for everything else // 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); 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())); mv.visitAttribute(new JavaTXSignatureAttribute(method.getTXSignature()));
System.out.println(method.getDescriptor()); System.out.println(method.getDescriptor());
System.out.println(method.getSignature()); System.out.println(method.getSignature());
mv.visitCode(); mv.visitCode();
var state = new State(method.returnType(), mv, method.isStatic() ? 0 : 1); var state = new State(method.signature().returnType(), mv, method.isStatic() ? 0 : 1);
for (var param: method.parameters()) for (var param: method.signature().parameters())
state.createVariable(param.name(), param.type()); state.createVariable(param.name(), param.type());
generate(state, method.block()); generate(state, method.block());
if (method.returnType() == null) if (method.signature().returnType() == null)
mv.visitInsn(RETURN); mv.visitInsn(RETURN);
mv.visitMaxs(0, 0); mv.visitMaxs(0, 0);
mv.visitEnd(); mv.visitEnd();

View File

@ -933,22 +933,22 @@ public class ASTToTargetAST {
return true; return true;
} }
Set<TargetGeneric> convert(Set<ResultPair<?, ?>> result) { Set<TargetGeneric> convert(Set<ResultPair<?, ?>> result, Map<TypePlaceholder, TypePlaceholder> equality) {
return result.stream().map(p -> { return result.stream().map(p -> {
if (p instanceof PairTPHsmallerTPH pair) { 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) { } 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 { } else {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
}).collect(Collectors.toSet()); }).collect(Collectors.toSet());
} }
public List<TargetGeneric> convert(GenericTypeVar typeVar) { public List<TargetGeneric> convert(GenericTypeVar typeVar, Map<TypePlaceholder, TypePlaceholder> equality) {
var ret = new ArrayList<TargetGeneric>(); var ret = new ArrayList<TargetGeneric>();
for (var bound : typeVar.getBounds()) { for (var bound : typeVar.getBounds()) {
ret.add(new TargetGeneric(typeVar.getName(), convert(bound))); ret.add(new TargetGeneric(typeVar.getName(), convert(bound, equality)));
} }
return ret; return ret;
} }
@ -965,13 +965,13 @@ public class ASTToTargetAST {
while (genericsIter.hasNext()) { while (genericsIter.hasNext()) {
var next = genericsIter.next(); var next = genericsIter.next();
userDefinedGenerics.add(next); userDefinedGenerics.add(next);
javaGenerics.addAll(convert(next)); javaGenerics.addAll(convert(next, sigma.equality));
} }
} else { } else {
// Generate generics only if there are no user defined ones // Generate generics only if there are no user defined ones
var generics = sigma.generics(input); var generics = sigma.generics(input);
javaGenerics = convert(generics.javaGenerics()); javaGenerics = convert(generics.javaGenerics(), sigma.equality);
txGenerics = convert(generics.txGenerics()); txGenerics = convert(generics.txGenerics(), sigma.txEquality);
} }
TargetBlock fieldInitializer = null; TargetBlock fieldInitializer = null;
@ -988,9 +988,9 @@ public class ASTToTargetAST {
); );
} }
private List<MethodParameter> convert(ParameterList input) { private List<MethodParameter> convert(ParameterList input, Map<TypePlaceholder, TypePlaceholder> equality) {
return input.getFormalparalist().stream() 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<TargetGeneric> generics, GenericRefType type) { private boolean hasGeneric(Set<TargetGeneric> generics, GenericRefType type) {
@ -998,7 +998,7 @@ public class ASTToTargetAST {
} }
private Set<TargetGeneric> collectMethodGenerics(Set<ResultPair<?, ?>> generics, Map<TypePlaceholder, TypePlaceholder> equality, Method input) { private Set<TargetGeneric> collectMethodGenerics(Set<ResultPair<?, ?>> generics, Map<TypePlaceholder, TypePlaceholder> equality, Method input) {
var convertedGenerics = new HashSet<>(convert(generics)); var convertedGenerics = new HashSet<>(convert(generics, equality));
outer: outer:
for (GenericTypeVar typeVar : input.getGenerics()) { for (GenericTypeVar typeVar : input.getGenerics()) {
for (var classGeneric : currentClass.getGenerics()) { for (var classGeneric : currentClass.getGenerics()) {
@ -1006,7 +1006,7 @@ public class ASTToTargetAST {
continue outer; continue outer;
} }
} }
convertedGenerics.addAll(convert(typeVar)); convertedGenerics.addAll(convert(typeVar, equality));
} }
/*var returnType = sigma.getType(input.getReturnType(), equality); /*var returnType = sigma.getType(input.getReturnType(), equality);
if ((returnType instanceof GenericRefType refType) && !hasGeneric(convertedGenerics, refType)) { if ((returnType instanceof GenericRefType refType) && !hasGeneric(convertedGenerics, refType)) {
@ -1030,12 +1030,13 @@ public class ASTToTargetAST {
for (var s : all) { for (var s : all) {
sigma = s; sigma = s;
var generics = sigma.generics(currentClass, input); var generics = sigma.generics(currentClass, input);
List<MethodParameter> params = convert(input.getParameterList()); List<MethodParameter> params = convert(input.getParameterList(), sigma.equality);
if (parameterSet.stream().noneMatch(p -> p.equals(params))) { if (parameterSet.stream().noneMatch(p -> p.equals(params))) {
List<MethodParameter> txParams = convert(input.getParameterList(), sigma.txEquality);
var javaGenerics = collectMethodGenerics(generics.javaGenerics(), sigma.equality, input); var javaGenerics = collectMethodGenerics(generics.javaGenerics(), sigma.equality, input);
var txGenerics = collectMethodGenerics(generics.txGenerics(), sigma.txEquality, 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); parameterSet.add(params);
} }
} }
@ -1051,16 +1052,18 @@ public class ASTToTargetAST {
for (var s : all) { for (var s : all) {
sigma = s; sigma = s;
var generics = sigma.generics(currentClass, input); var generics = sigma.generics(currentClass, input);
List<MethodParameter> params = convert(input.getParameterList()); List<MethodParameter> params = convert(input.getParameterList(), sigma.equality);
if (parameterSet.stream().noneMatch(p -> p.equals(params))) { if (parameterSet.stream().noneMatch(p -> p.equals(params))) {
List<MethodParameter> txParams = convert(input.getParameterList(), sigma.txEquality);
var javaGenerics = collectMethodGenerics(generics.javaGenerics(), sigma.equality, input); var javaGenerics = collectMethodGenerics(generics.javaGenerics(), sigma.equality, input);
var txGenerics = collectMethodGenerics(generics.txGenerics(), sigma.txEquality, input); var txGenerics = collectMethodGenerics(generics.txGenerics(), sigma.txEquality, input);
result.add(new TargetMethod( result.add(new TargetMethod(
input.modifier, input.modifier,
input.name, javaGenerics, txGenerics, params, input.name, convert(input.block),
convert(input.getReturnType()), new TargetMethod.Signature(javaGenerics, params, convert(input.getReturnType(), sigma.equality)),
convert(input.block) new TargetMethod.Signature(txGenerics, txParams, convert(input.getReturnType(), sigma.txEquality))
)); ));
parameterSet.add(params); parameterSet.add(params);
} }
@ -1092,7 +1095,11 @@ public class ASTToTargetAST {
public Map<String, byte[]> auxiliaries = new HashMap<>(); public Map<String, byte[]> auxiliaries = new HashMap<>();
protected TargetType convert(RefTypeOrTPHOrWildcardOrGeneric input) { protected TargetType convert(RefTypeOrTPHOrWildcardOrGeneric input) {
return convert(input, sigma.equality);
}
protected TargetType convert(RefTypeOrTPHOrWildcardOrGeneric input, Map<TypePlaceholder, TypePlaceholder> equality) {
return input.acceptTV(new TypeVisitor<>() { return input.acceptTV(new TypeVisitor<>() {
@Override @Override
public TargetType visit(RefType refType) { public TargetType visit(RefType refType) {
@ -1126,7 +1133,7 @@ public class ASTToTargetAST {
@Override @Override
public TargetType visit(TypePlaceholder typePlaceholder) { public TargetType visit(TypePlaceholder typePlaceholder) {
return sigma.getTargetType(typePlaceholder, sigma.equality); return sigma.getTargetType(typePlaceholder, equality);
} }
@Override @Override

View File

@ -25,7 +25,7 @@ public record TargetClass(int modifiers, String qualifiedName, TargetType superT
} }
public void addMethod(int access, String name, Set<TargetGeneric> generics, List<MethodParameter> parameterTypes, TargetType returnType, TargetBlock block) { public void addMethod(int access, String name, Set<TargetGeneric> generics, List<MethodParameter> 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<MethodParameter> parameterTypes, TargetType returnType, TargetBlock block) { public void addMethod(int access, String name, List<MethodParameter> parameterTypes, TargetType returnType, TargetBlock block) {
@ -33,7 +33,7 @@ public record TargetClass(int modifiers, String qualifiedName, TargetType superT
} }
public void addConstructor(int access, Set<TargetGeneric> generics, List<MethodParameter> paramterTypes, TargetBlock block) { public void addConstructor(int access, Set<TargetGeneric> generics, List<MethodParameter> 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<MethodParameter> paramterTypes, TargetBlock block) { public void addConstructor(int access, List<MethodParameter> paramterTypes, TargetBlock block) {

View File

@ -1,12 +1,13 @@
package de.dhbwstuttgart.target.tree; package de.dhbwstuttgart.target.tree;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.target.tree.expression.TargetBlock; import de.dhbwstuttgart.target.tree.expression.TargetBlock;
import de.dhbwstuttgart.target.tree.type.TargetType; import de.dhbwstuttgart.target.tree.type.TargetType;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
public record TargetConstructor(int access, Set<TargetGeneric> generics, Set<TargetGeneric> txGenerics, List<MethodParameter> parameters, TargetBlock block, TargetBlock fieldInitializer) { public record TargetConstructor(int access, Set<TargetGeneric> generics, Set<TargetGeneric> txGenerics, List<MethodParameter> parameters, List<MethodParameter> txParameters, TargetBlock block, TargetBlock fieldInitializer) {
public String getDescriptor() { public String getDescriptor() {
return TargetMethod.getDescriptor(null, parameters.stream().map(MethodParameter::type).toArray(TargetType[]::new)); return TargetMethod.getDescriptor(null, parameters.stream().map(MethodParameter::type).toArray(TargetType[]::new));
@ -17,7 +18,7 @@ public record TargetConstructor(int access, Set<TargetGeneric> generics, Set<Tar
} }
public String getTXSignature() { public String getTXSignature() {
return TargetMethod.getSignature(txGenerics, parameters, null); return TargetMethod.getSignature(txGenerics, txParameters, null);
} }
} }

View File

@ -7,7 +7,9 @@ import org.objectweb.asm.Opcodes;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
public record TargetMethod(int access, String name, Set<TargetGeneric> generics, Set<TargetGeneric> txGenerics, List<MethodParameter> parameters, TargetType returnType, TargetBlock block) { public record TargetMethod(int access, String name, TargetBlock block, Signature signature, Signature txSignature) {
public record Signature(Set<TargetGeneric> generics, List<MethodParameter> parameters, TargetType returnType) { }
public static String getDescriptor(TargetType returnType, TargetType... parameters) { public static String getDescriptor(TargetType returnType, TargetType... parameters) {
String ret = "("; String ret = "(";
for (var parameterType : parameters) { for (var parameterType : parameters) {
@ -35,15 +37,15 @@ public record TargetMethod(int access, String name, Set<TargetGeneric> generics,
} }
public String getDescriptor() { 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() { public String getSignature() {
return getSignature(generics, parameters, returnType); return getSignature(signature.generics, signature.parameters, signature.returnType);
} }
public String getTXSignature() { public String getTXSignature() {
return getSignature(txGenerics, parameters, returnType); return getSignature(txSignature.generics, txSignature.parameters, txSignature.returnType);
} }
public boolean isStatic() { public boolean isStatic() {