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());
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();

View File

@ -933,22 +933,22 @@ public class ASTToTargetAST {
return true;
}
Set<TargetGeneric> convert(Set<ResultPair<?, ?>> result) {
Set<TargetGeneric> convert(Set<ResultPair<?, ?>> result, Map<TypePlaceholder, TypePlaceholder> 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<TargetGeneric> convert(GenericTypeVar typeVar) {
public List<TargetGeneric> convert(GenericTypeVar typeVar, Map<TypePlaceholder, TypePlaceholder> equality) {
var ret = new ArrayList<TargetGeneric>();
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<MethodParameter> convert(ParameterList input) {
private List<MethodParameter> convert(ParameterList input, Map<TypePlaceholder, TypePlaceholder> 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<TargetGeneric> generics, GenericRefType type) {
@ -998,7 +998,7 @@ public class ASTToTargetAST {
}
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:
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<MethodParameter> params = convert(input.getParameterList());
List<MethodParameter> params = convert(input.getParameterList(), sigma.equality);
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 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<MethodParameter> params = convert(input.getParameterList());
List<MethodParameter> params = convert(input.getParameterList(), sigma.equality);
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 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<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<>() {
@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

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) {
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) {
@ -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) {
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) {

View File

@ -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<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() {
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() {
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.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) {
String ret = "(";
for (var parameterType : parameters) {
@ -35,15 +37,15 @@ public record TargetMethod(int access, String name, Set<TargetGeneric> 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() {