Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 653f704265 | |||
| b36ba5e128 | |||
| a43f49cadb | |||
| 69292c37fc | |||
| 218e704272 | |||
| e83e9a041c |
@@ -1,19 +0,0 @@
|
||||
import java.util.stream.Stream;
|
||||
import Pair;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class Kombinatoren_After {
|
||||
|
||||
|
||||
after (fst, snd) { //System.out.println("after2");
|
||||
|
||||
return () -> Stream.of(
|
||||
toks -> {
|
||||
return fst.apply().flatMap(x ->
|
||||
x.apply(toks).flatMap(p1 ->
|
||||
snd.apply().flatMap(y -> y.apply(p1.snd()).map(p2 ->
|
||||
new Pair<>(new Pair<>(p1.fst(), p2.fst()), p2.snd()))))); } );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
import java.util.stream.Stream;
|
||||
import Pair;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class Kombinatoren_After_Lazy {
|
||||
|
||||
|
||||
afterP (fst, snd) {
|
||||
return toks -> {
|
||||
return fst.apply(toks).flatMap(p1 ->
|
||||
snd.apply(p1.snd()).map(p2 ->
|
||||
new Pair<>(new Pair<>(p1.fst(), p2.fst()), p2.snd())));
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
import java.util.stream.Stream;
|
||||
import Pair;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class Kombinatoren_Or {
|
||||
|
||||
|
||||
orP (p1, p2) {
|
||||
return ()-> Stream.concat(p1.apply(), p2.apply());
|
||||
}
|
||||
|
||||
/*
|
||||
after (fst, snd) { //System.out.println("after2");
|
||||
|
||||
return () -> Stream.of(
|
||||
toks -> {
|
||||
return fst.apply().flatMap(x ->
|
||||
x.apply(toks).flatMap(p1 ->
|
||||
snd.apply().flatMap(y -> y.apply(p1.snd()).map(p2 ->
|
||||
new Pair<>(new Pair<>(p1.fst(), p2.fst()), p2.snd()))))); } );
|
||||
}
|
||||
|
||||
|
||||
trans (p, f) {
|
||||
return () -> p.apply().map(x -> (toks -> x.apply(toks).map(pr -> new Pair<>(f.apply(pr.fst()), pr.snd() ) ) ) );
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
import java.util.stream.Stream;
|
||||
//import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.io.PrintStream;
|
||||
import java.lang.System;
|
||||
|
||||
//import java.lang.String;
|
||||
import java.lang.Boolean;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import Pair;
|
||||
import Kombinatoren_failure;
|
||||
|
||||
public class Kombinatoren_Satisfy {
|
||||
|
||||
kb = new Kombinatoren_failure();
|
||||
|
||||
satisfy(cond) {
|
||||
// return () -> Stream.of (newToks -> new Kombinatoren_failure().failure().apply().map(x -> x.apply(newToks)));
|
||||
//}
|
||||
return () -> Stream.of (toks -> {
|
||||
if(toks.isEmpty()) {
|
||||
return new ArrayList<>().stream();
|
||||
}
|
||||
else
|
||||
{
|
||||
var fst = toks.getFirst();
|
||||
if (cond.apply(fst))
|
||||
{
|
||||
//var newToks = List.copyOf(toks.subList(1, toks.size()));
|
||||
var newToks = new ArrayList<>(toks);
|
||||
return newToks.removeFirst();
|
||||
kb.getContent(
|
||||
kb.succeed(fst).apply().map(x -> x.apply(newToks))
|
||||
);
|
||||
// ;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
/* () -> Stream.of (toks -> {
|
||||
//System.out.println(toks);
|
||||
if(toks.isEmpty())
|
||||
{
|
||||
return new ArrayList<>().stream();
|
||||
}
|
||||
else {
|
||||
var fst = toks.getFirst();
|
||||
if (cond.apply(fst))
|
||||
{
|
||||
var newToks = List.copyOf(toks.subList(1, toks.size()));
|
||||
//System.out.println("satisfy ok " + fst.toString());
|
||||
//return get(succeed(fst).apply().map(x -> x.apply(newToks)));
|
||||
}
|
||||
else {
|
||||
//System.out.println("satisfy failure " + fst.toString());
|
||||
//return get(failure().apply()).apply(toks);
|
||||
}
|
||||
return new ArrayList<>().stream();
|
||||
} } ) ;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
import java.util.stream.Stream;
|
||||
import Pair;
|
||||
//import java.util.function.Function;
|
||||
|
||||
public class Kombinatoren_Trans {
|
||||
|
||||
|
||||
trans (p, f) {
|
||||
return () -> p.apply().map(x -> (toks -> x.apply(toks).map(pr -> new Pair<>(f.apply(pr.fst()), pr.snd() ) ) ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.io.PrintStream;
|
||||
import java.lang.System;
|
||||
import java.util.Optional;
|
||||
|
||||
import java.lang.String;
|
||||
import java.lang.Boolean;
|
||||
import java.util.function.Function;
|
||||
|
||||
import Pair;
|
||||
|
||||
|
||||
public class Kombinatoren_failure {
|
||||
public getContent(s) {
|
||||
return s.toList().getFirst();
|
||||
}
|
||||
|
||||
public failure() {
|
||||
return () -> Stream.of(
|
||||
toks -> new ArrayList<>().stream());
|
||||
}
|
||||
public succeed( value) {
|
||||
return () -> Stream.of(toks -> {
|
||||
//System.out.println("succeed");
|
||||
var al = new ArrayList<>();
|
||||
al.add(new Pair<>(value, toks));
|
||||
//al.forEach(x -> { System.out.println(x.toString());});
|
||||
return al.stream();});
|
||||
}
|
||||
|
||||
parser(p, inp) {
|
||||
return p.map(y -> y.apply(
|
||||
inp.chars().mapToObj(c -> (char) c)
|
||||
.collect(Collectors.toList())))
|
||||
.flatMap(x -> x)
|
||||
.filter(x -> x.snd().isEmpty())
|
||||
.findFirst()
|
||||
.get()
|
||||
.fst();
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
|
||||
satisfy(cond) {
|
||||
return newToks -> failure().apply().map(x -> x.apply(newToks));
|
||||
/*
|
||||
return () -> Stream.of (toks -> {
|
||||
if(toks.isEmpty()) {
|
||||
return new ArrayList<>().stream();
|
||||
}
|
||||
else {
|
||||
var fst = toks.getFirst();
|
||||
if (cond.apply(fst)) {
|
||||
var newToks = List.copyOf(toks.subList(1, toks.size()));
|
||||
this.getContent(
|
||||
succeed(fst)
|
||||
.apply().map(x -> x.apply(newToks))
|
||||
)
|
||||
;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
);
|
||||
*/
|
||||
}
|
||||
}
|
||||
*/
|
||||
@@ -1,27 +0,0 @@
|
||||
import java.util.*;
|
||||
|
||||
public class Pair<T, U> {
|
||||
T a;
|
||||
U b;
|
||||
|
||||
public Pair() { }
|
||||
public Pair(T a, U b) {
|
||||
System.out.println("Pair a; " + a + " b: " + b);
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
public T fst () {
|
||||
return a;
|
||||
}
|
||||
|
||||
public U snd () {
|
||||
System.out.println("snd b: " + b);
|
||||
return b;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "Pair<"+a.toString()+","+b.toString()+">";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
import java.util.stream.Stream;
|
||||
//import java.util.stream.IntStream;
|
||||
//import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.io.PrintStream;
|
||||
import java.lang.System;
|
||||
import java.util.Optional;
|
||||
import java.lang.Character;
|
||||
import java.lang.String;
|
||||
import java.lang.Boolean;
|
||||
import java.lang.Integer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
//import java.util.function.IntFunction;
|
||||
|
||||
import Pair;
|
||||
|
||||
|
||||
public class Parser {
|
||||
|
||||
|
||||
strToList(s) {
|
||||
var al;
|
||||
al = new ArrayList<>();
|
||||
var i = 0;
|
||||
while (i < s.length()) {
|
||||
al.add(s.charAt(i));
|
||||
i=i+1;
|
||||
}
|
||||
return al;
|
||||
}
|
||||
/*
|
||||
|
||||
parser(p, inp) {
|
||||
return p.map(y -> y.apply(
|
||||
strToList(inp)//chars().mapToObj(c -> (Character) c)
|
||||
//.toList()
|
||||
))
|
||||
|
||||
//.flatMap(x -> x)
|
||||
|
||||
//.filter(x -> x.snd().isEmpty())
|
||||
;
|
||||
/*
|
||||
.findFirst()
|
||||
.get()
|
||||
.fst()
|
||||
|
||||
;
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
satisfy(cond) {
|
||||
return newToks -> failure().apply().map(x -> x.apply(newToks));
|
||||
/*
|
||||
return () -> Stream.of (toks -> {
|
||||
if(toks.isEmpty()) {
|
||||
return new ArrayList<>().stream();
|
||||
}
|
||||
else {
|
||||
var fst = toks.getFirst();
|
||||
if (cond.apply(fst)) {
|
||||
var newToks = List.copyOf(toks.subList(1, toks.size()));
|
||||
this.getContent(
|
||||
succeed(fst)
|
||||
.apply().map(x -> x.apply(newToks))
|
||||
)
|
||||
;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
);
|
||||
*/
|
||||
}
|
||||
}
|
||||
*/
|
||||
@@ -1499,12 +1499,13 @@ public class Codegen {
|
||||
state.exitScope();
|
||||
}
|
||||
|
||||
private void extractField(State state, TargetType type, int i, ClassOrInterface clazz) {
|
||||
private void extractField(State state, TargetType ret, TargetType type, int i, ClassOrInterface clazz) {
|
||||
if (i >= clazz.getFieldDecl().size())
|
||||
throw new CodeGenException("Couldn't find suitable field accessor for '" + type.name() + "'");
|
||||
var field = clazz.getFieldDecl().get(i);
|
||||
var fieldType = converter.convert(field.getType());
|
||||
state.mv.visitMethodInsn(INVOKEVIRTUAL, type.getInternalName(), field.getName(), "()" + fieldType.toDescriptor(), false);
|
||||
if (ret != null) convertTo(state, fieldType, ret);
|
||||
}
|
||||
|
||||
private void bindPattern(State state, TargetType type, TargetPattern pat, Label start, int index, int depth) {
|
||||
@@ -1567,7 +1568,7 @@ public class Codegen {
|
||||
var subPattern = cp.subPatterns().get(i);
|
||||
|
||||
state.mv.visitInsn(DUP);
|
||||
extractField(state, cp.type(), i, clazz);
|
||||
extractField(state, null, cp.type(), i, clazz);
|
||||
|
||||
if (subPattern.type() instanceof TargetRefType || subPattern.type() instanceof TargetExtendsWildcard) {
|
||||
state.mv.visitInsn(DUP);
|
||||
@@ -1669,7 +1670,7 @@ public class Codegen {
|
||||
if (i < cp.subPatterns().size() - 1)
|
||||
state.mv.visitInsn(DUP);
|
||||
|
||||
extractField(state, cp.type(), i, clazz);
|
||||
extractField(state, subPattern.type(), cp.type(), i, clazz);
|
||||
if (subPattern.type() instanceof TargetRefType)
|
||||
state.mv.visitTypeInsn(CHECKCAST, subPattern.type().getInternalName());
|
||||
offset = state.createVariable(subPattern.name(), subPattern.type()).index;
|
||||
|
||||
@@ -4,6 +4,7 @@ import de.dhbwstuttgart.bytecode.FunNGenerator;
|
||||
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||
import de.dhbwstuttgart.environment.IByteArrayClassLoader;
|
||||
import de.dhbwstuttgart.exceptions.DebugException;
|
||||
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
||||
import de.dhbwstuttgart.parser.NullToken;
|
||||
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
||||
import de.dhbwstuttgart.syntaxtree.*;
|
||||
@@ -11,17 +12,17 @@ import de.dhbwstuttgart.syntaxtree.Record;
|
||||
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.*;
|
||||
import de.dhbwstuttgart.syntaxtree.type.*;
|
||||
import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter;
|
||||
import de.dhbwstuttgart.syntaxtree.visual.OutputGenerator;
|
||||
import de.dhbwstuttgart.target.tree.*;
|
||||
import de.dhbwstuttgart.target.tree.expression.*;
|
||||
import de.dhbwstuttgart.target.tree.type.*;
|
||||
import de.dhbwstuttgart.typeinference.result.*;
|
||||
import de.dhbwstuttgart.typeinference.unify.MartelliMontanariUnify;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.*;
|
||||
|
||||
import java.lang.annotation.Target;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
/**
|
||||
* @author dholle
|
||||
@@ -61,7 +62,7 @@ public class ASTToTargetAST {
|
||||
return converter.result;
|
||||
}
|
||||
|
||||
public record Generics(JavaGenerics javaGenerics, TxGenerics txGenerics) {
|
||||
public record Generics(IGenerics javaGenerics, IGenerics txGenerics) {
|
||||
public Generics(JavaTXCompiler compiler, ResultSet set) {
|
||||
this(new JavaGenerics(compiler, set), new TxGenerics(compiler, set));
|
||||
}
|
||||
@@ -156,190 +157,168 @@ public class ASTToTargetAST {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// This finds a common sealed interface type to group together methods that use different records
|
||||
// This function should do matching or unification
|
||||
private List<ClassOrInterface> commonSuperInterfaceTypes(TargetType a, TargetType b) {
|
||||
if (a instanceof TargetGenericType && b instanceof TargetGenericType) return List.of(ASTFactory.createObjectClass());
|
||||
if (a instanceof TargetRefType ta && b instanceof TargetGenericType)
|
||||
return List.of(compiler.getClass(new JavaClassName(ta.name())));
|
||||
if (b instanceof TargetRefType tb && a instanceof TargetGenericType)
|
||||
return List.of(compiler.getClass(new JavaClassName(tb.name())));
|
||||
|
||||
if (a instanceof TargetRefType ta && b instanceof TargetRefType tb) {
|
||||
var res = new HashSet<ClassOrInterface>();
|
||||
|
||||
var cla = compiler.getClass(new JavaClassName(ta.name()));
|
||||
var clb = compiler.getClass(new JavaClassName(tb.name()));
|
||||
|
||||
if (cla.equals(clb)) return List.of(cla);
|
||||
|
||||
while (!cla.equals(ASTFactory.createObjectClass())) {
|
||||
var clb2 = clb;
|
||||
while (!clb2.equals(ASTFactory.createObjectClass())) {
|
||||
for (var intfa : cla.getSuperInterfaces()) {
|
||||
for (var intfb : clb.getSuperInterfaces()) {
|
||||
if (intfa.equals(intfb)) {
|
||||
var clintf = compiler.getClass(intfa.getName());
|
||||
if (clintf.isSealed()) {
|
||||
res.add(clintf);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
clb2 = compiler.getClass(clb2.getSuperClass().getName());
|
||||
}
|
||||
cla = compiler.getClass(cla.getSuperClass().getName());
|
||||
}
|
||||
return res.stream().toList();
|
||||
}
|
||||
return List.of();
|
||||
private static UnifyType toUnifyType(TargetType a) {
|
||||
return switch(a) {
|
||||
case TargetExtendsWildcard targetExtendsWildcard -> new ExtendsType(toUnifyType(targetExtendsWildcard.innerType()));
|
||||
case TargetSuperWildcard targetSuperWildcard -> new SuperType(toUnifyType(targetSuperWildcard.innerType()));
|
||||
case TargetGenericType targetGenericType -> new PlaceholderType(targetGenericType.name(), JavaTXCompiler.defaultClientPlaceholderRegistry);
|
||||
case TargetPrimitiveType _ -> throw new NotImplementedException();
|
||||
case TargetFunNType targetFunNType -> FunNType.getFunNType(new TypeParams(targetFunNType.params().stream().map(ASTToTargetAST::toUnifyType).toList()));
|
||||
case TargetRefType targetRefType -> new ReferenceType(targetRefType.name(), new TypeParams(targetRefType.params().stream().map(ASTToTargetAST::toUnifyType).toList()));
|
||||
};
|
||||
}
|
||||
|
||||
private boolean canCombine(Signature m1, Signature m2) {
|
||||
var pl1 = m1.java.parameters();
|
||||
var pl2 = m2.java.parameters();
|
||||
if (pl1.size() != pl2.size()) return false;
|
||||
if (pl1.isEmpty()) return false;
|
||||
for (var i = 0; i < pl1.size(); i++) {
|
||||
var p1 = pl1.get(i).pattern();
|
||||
var p2 = pl2.get(i).pattern();
|
||||
// TPH <> RefType sind nicht unterscheidbar
|
||||
if (p1.type() instanceof TargetGenericType || p2.type() instanceof TargetGenericType) continue;
|
||||
// Pattern(X) <> Pattern(Y) ist nicht unterscheidbar
|
||||
if (p1 instanceof TargetComplexPattern pc1 && p2 instanceof TargetComplexPattern pc2 &&
|
||||
pc1.type().equals(pc2.type())) continue;
|
||||
if (!p1.equals(p2)) return false;
|
||||
}
|
||||
return true;
|
||||
private static TargetType toTargetType(UnifyType a) {
|
||||
return switch (a) {
|
||||
case ExtendsType extendType -> new TargetExtendsWildcard(toTargetType(extendType.getExtendedType()));
|
||||
case SuperType superType -> new TargetSuperWildcard(toTargetType(superType.getSuperedType()));
|
||||
case PlaceholderType placeholderType -> new TargetGenericType(placeholderType.getName());
|
||||
case FunNType funNType -> TargetFunNType.fromParams(StreamSupport.stream(funNType.getTypeParams().spliterator(), false).map(ASTToTargetAST::toTargetType).toList(), 1); // FIXME How does this work with Fun0??
|
||||
case ReferenceType referenceType -> new TargetRefType(referenceType.getName(), StreamSupport.stream(referenceType.getTypeParams().spliterator(), false).map(ASTToTargetAST::toTargetType).toList());
|
||||
default -> throw new NotImplementedException();
|
||||
};
|
||||
}
|
||||
|
||||
private record Combination(MethodWithTphs a, MethodWithTphs b) {
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof Combination(MethodWithTphs a1, MethodWithTphs b1))) return false;
|
||||
return this.a.equals(a1) && this.b.equals(b1) ||
|
||||
this.a.equals(b1) && this.b.equals(a1);
|
||||
private static Optional<TargetType> unify(TargetType a, TargetType b) {
|
||||
if (a.equals(b)) return Optional.of(a);
|
||||
var unify = new MartelliMontanariUnify();
|
||||
var ua = toUnifyType(a);
|
||||
var unifier = unify.unify(Set.of(ua, toUnifyType(b)));
|
||||
if (unifier.isEmpty()) return Optional.empty();
|
||||
return Optional.of(toTargetType(unifier.get().apply(ua)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hashCode(a) + Objects.hashCode(b);
|
||||
private static Optional<List<MethodParameter>> unify(List<MethodParameter> a, List<MethodParameter> b) {
|
||||
if (a.size() != b.size()) return Optional.empty();
|
||||
var result = new ArrayList<MethodParameter>();
|
||||
for (var i = 0; i < a.size(); i++) {
|
||||
var u = unify(a.get(i).pattern().type(), b.get(i).pattern().type());
|
||||
if (u.isEmpty()) return Optional.empty();
|
||||
// Strip off patterns, we don't need them for merged methods, they do a switch case
|
||||
result.add(new MethodParameter(u.get(), a.get(i).pattern().name()));
|
||||
}
|
||||
|
||||
return Optional.of(result);
|
||||
}
|
||||
|
||||
private static Set<String> findUsedGenerics(TargetType type) {
|
||||
if (type instanceof TargetSpecializedType tspec) {
|
||||
return tspec.params().stream().map(ASTToTargetAST::findUsedGenerics).flatMap(Set::stream).collect(Collectors.toSet());
|
||||
} else if (type instanceof TargetGenericType(String name)) {
|
||||
var set = new HashSet<String>();
|
||||
set.add(name);
|
||||
return set;
|
||||
}
|
||||
return new HashSet<>();
|
||||
}
|
||||
|
||||
// When unifying types we need to keep the generics that are still used in the signature
|
||||
// This is done in an extra step after all generics have been combined into one list
|
||||
private static void correctGenerics(TargetMethod.Signature signature, List<TargetGeneric> generics) {
|
||||
var outGenerics = signature.generics();
|
||||
outGenerics.addAll(generics);
|
||||
var foundGenerics = signature.parameters().stream().map(p -> findUsedGenerics(p.pattern().type()))
|
||||
.flatMap(Set::stream)
|
||||
.collect(Collectors.toSet());
|
||||
foundGenerics.addAll(findUsedGenerics(signature.returnType()));
|
||||
|
||||
for (var generic : new HashSet<>(outGenerics)) {
|
||||
if (!foundGenerics.contains(generic.name())) outGenerics.remove(generic);
|
||||
}
|
||||
}
|
||||
|
||||
private List<List<MethodWithTphs>> groupOverloads(ClassOrInterface input, List<Method> methods) {
|
||||
var mapOfTargetMethods = new HashMap<Generics, MethodWithTphs[]>();
|
||||
for (var gen : all) {
|
||||
mapOfTargetMethods.put(gen, new MethodWithTphs[methods.size()]);
|
||||
private static Optional<TargetMethod.Signature> unify(TargetMethod.Signature a, TargetMethod.Signature b) {
|
||||
if (a.equals(b)) return Optional.empty();
|
||||
var res = unify(a.parameters(), b.parameters());
|
||||
if (res.isEmpty()) return Optional.empty();
|
||||
var returnType = unify(a.returnType(), b.returnType());
|
||||
if (returnType.isEmpty()) return Optional.empty();
|
||||
var generics = new ArrayList<>(a.generics());
|
||||
generics.addAll(b.generics());
|
||||
var signature = new TargetMethod.Signature(new HashSet<>(), res.get(), returnType.get());
|
||||
correctGenerics(signature, generics);
|
||||
return Optional.of(signature);
|
||||
}
|
||||
|
||||
private static Optional<MethodGroup> unify(MethodGroup a, MethodGroup b) {
|
||||
var junified = unify(a.signature.java, b.signature.java);
|
||||
if (junified.isEmpty()) return Optional.empty();
|
||||
var txunified = unify(a.signature.tx, b.signature.tx);
|
||||
assert txunified.isPresent();
|
||||
|
||||
var unifiedSignature = new Signature(junified.get(), txunified.get(), a.signature().generics);
|
||||
|
||||
var concat = new HashSet<>(a.methods);
|
||||
concat.addAll(b.methods);
|
||||
return Optional.of(new MethodGroup(unifiedSignature, concat));
|
||||
}
|
||||
|
||||
private record NameAndParameters(String name, int parameters) {}
|
||||
|
||||
private List<List<Method>> groupMethods(ClassOrInterface input, List<Method> methods) {
|
||||
var groups = new HashMap<NameAndParameters, List<Method>>();
|
||||
for (var m : methods) {
|
||||
var nameAndParameters = new NameAndParameters(m.name, m.getParameterList().getFormalparalist().size());
|
||||
var l = groups.getOrDefault(nameAndParameters, new ArrayList<>());
|
||||
l.add(m);
|
||||
groups.put(nameAndParameters, l);
|
||||
}
|
||||
|
||||
return groups.values().stream().toList();
|
||||
}
|
||||
|
||||
private List<MethodGroup> groupOverloads(ClassOrInterface input, List<Method> methods) {
|
||||
var a = new HashSet<MethodGroup>();
|
||||
|
||||
for (var i = 0; i < methods.size(); i++) {
|
||||
var method = methods.get(i);
|
||||
// Convert all methods
|
||||
var methodsWithTphs = convert(input, method);
|
||||
for (var m : methodsWithTphs) {
|
||||
var resultMethods = mapOfTargetMethods.get(m.generics);
|
||||
resultMethods[i] = new MethodWithTphs(m.method, m.generics, m.signature);
|
||||
var mtph = new MethodWithTphs(m.method, m.generics, m.signature);
|
||||
a.add(new MethodGroup(m.signature, Set.of(mtph)));
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("============== INPUT ==============");
|
||||
for (var m : mapOfTargetMethods.values()) {
|
||||
for (var v : m) if (v != null) System.out.println(v.signature.java.returnType() + " " + v.method.name + " " + v.signature.java().parameters());
|
||||
System.out.println();
|
||||
for (var m : a) {
|
||||
System.out.println(m);
|
||||
}
|
||||
|
||||
var allCombinations = new HashSet<Set<Combination>>();
|
||||
// Combine methods based on their signature and position in the result set
|
||||
for (var g1 : all) {
|
||||
var resMeth1 = mapOfTargetMethods.get(g1);
|
||||
for (var i = 0; i < methods.size(); i++) {
|
||||
var m1 = resMeth1[i];
|
||||
if (m1 == null) continue;
|
||||
// Algorithm
|
||||
|
||||
for (var g2 : all) {
|
||||
if (g1 == g2) continue; // No need to combine the same method
|
||||
var resMeth2 = mapOfTargetMethods.get(g2);
|
||||
var m2 = resMeth2[i];
|
||||
if (m2 == null) continue;
|
||||
var R = new HashSet<MethodGroup>();
|
||||
var i = new HashSet<>(a);
|
||||
|
||||
var combinations = new HashSet<Combination>();
|
||||
|
||||
if (canCombine(m1.signature, m2.signature)) {
|
||||
//System.out.println(" Combining " + m1.signature.java.getSignature() + " and " + m2.signature.java.getSignature());
|
||||
combinations.add(new Combination(m1, m2));
|
||||
for (var j = 0; j < methods.size(); j++) {
|
||||
if (j == i) continue;
|
||||
var m3 = resMeth2[j];
|
||||
if (m3 == null) continue;
|
||||
var m4 = resMeth1[j];
|
||||
if (m4 == null) continue;
|
||||
combinations.add(new Combination(m4, m3));
|
||||
//System.out.println("Also Combining " + m4.signature.java.getSignature() + " and " + m3.signature.java.getSignature());
|
||||
}
|
||||
} else {
|
||||
//System.out.println(" Not Combining " + m1.signature.java.getSignature() + " and " + m2.signature.java.getSignature());
|
||||
}
|
||||
if (!combinations.isEmpty()) allCombinations.add(combinations);
|
||||
while (!i.isEmpty()) {
|
||||
var m = i.iterator().next();
|
||||
i.remove(m);
|
||||
R.add(m);
|
||||
var a1 = new HashSet<>(a);
|
||||
for (var m1 : a1) {
|
||||
if (m1 != m) {
|
||||
var u_opt = unify(m, m1);
|
||||
if (u_opt.isPresent()) {
|
||||
var u = u_opt.get();
|
||||
//System.out.println("Unified " + m + " AND " + m1 + "\n\t" + u);
|
||||
i.remove(m1);
|
||||
R.remove(m);
|
||||
R.remove(m1);
|
||||
R.add(u);
|
||||
a.add(u);
|
||||
} /*else {
|
||||
System.out.println("Couldn't unify " + m + " AND " + m1);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (allCombinations.isEmpty()) allCombinations.add(new HashSet<>());
|
||||
|
||||
// Combine back into output format
|
||||
var r0 = new HashSet<Set<MethodWithTphs>>();
|
||||
for (var combinations : allCombinations) {
|
||||
var r1 = new HashSet<Set<MethodWithTphs>>();
|
||||
// This is used to weed out duplicates
|
||||
var uniqued = new HashSet<MethodWithTphs>();
|
||||
// We go over all methods in the result
|
||||
for (var g : all) for (var i = 0; i < methods.size(); i++) {
|
||||
var r2 = new HashSet<MethodWithTphs>();
|
||||
var m = mapOfTargetMethods.get(g)[i];
|
||||
if (m == null) continue;
|
||||
if (!uniqued.contains(m)) {
|
||||
// Add the method to r2
|
||||
r2.add(m);
|
||||
uniqued.add(m);
|
||||
} else continue;
|
||||
// Find all combinations that contain the method and add them to the result
|
||||
// if not filtered out by uniqued
|
||||
for (var c : combinations) {
|
||||
if (c.a.equals(m) || c.b.equals(m)) {
|
||||
if (!uniqued.contains(c.a)) {
|
||||
r2.add(c.a);
|
||||
uniqued.add(c.a);
|
||||
}
|
||||
if (!uniqued.contains(c.b)) {
|
||||
r2.add(c.b);
|
||||
uniqued.add(c.b);
|
||||
}
|
||||
}
|
||||
}
|
||||
r1.add(r2);
|
||||
}
|
||||
outer: for (var s1 : r1) {
|
||||
for (var s2 : new HashSet<>(r0)) {
|
||||
if (s2.containsAll(s1)) {
|
||||
continue outer;
|
||||
} else if (s1.containsAll(s2)) {
|
||||
r0.remove(s2);
|
||||
r0.add(s1);
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
r0.add(s1);
|
||||
}
|
||||
System.out.println("============== OUTPUT ==============");
|
||||
for (var mg : R) {
|
||||
System.out.println(mg.methods.size() + " " + mg);
|
||||
}
|
||||
|
||||
var result = r0.stream().map(l -> l.stream().toList()).toList();
|
||||
|
||||
//System.out.println("============== OUTPUT ==============");
|
||||
//for (var l : result) {
|
||||
// for (var m : l) System.out.println(m.method.name + " " + m.signature.java.getSignature());
|
||||
// System.out.println();
|
||||
//}
|
||||
return result;
|
||||
return R.stream().toList();
|
||||
}
|
||||
|
||||
public TargetStructure convert(ClassOrInterface input) {
|
||||
@@ -373,13 +352,15 @@ public class ASTToTargetAST {
|
||||
var superInterfaces = input.getSuperInterfaces().stream().map(clazz -> convert(clazz, generics.javaGenerics, compiler)).toList();
|
||||
var constructors = input.getConstructors().stream().map(constructor -> this.convert(input, constructor, finalFieldInitializer, generics)).flatMap(List::stream).toList();
|
||||
var fields = input.getFieldDecl().stream().map(f -> convert(f, generics.javaGenerics)).toList();
|
||||
var methods = groupOverloads(input, input.getMethods()).stream().map(m -> generatePatternOverloads(input, m)).flatMap(List::stream)
|
||||
.collect(Collectors.toSet()).stream().toList(); // Unique generated methods
|
||||
var m0 = groupMethods(input, input.getMethods());
|
||||
var m1 = m0.stream().map(ms ->
|
||||
groupOverloads(input, ms).stream().map(g -> generatePatternOverloads(input, g)).flatMap(List::stream).toList()).toList();
|
||||
var methods = m1.stream().flatMap(List::stream).collect(Collectors.toSet()).stream().toList();
|
||||
|
||||
TargetMethod staticConstructor = null;
|
||||
if (input.getStaticInitializer().isPresent()) {
|
||||
var init = this.convert(input, input.getStaticInitializer().get()).stream().findFirst().orElseThrow();
|
||||
staticConstructor = this.convert(init, init.generics.javaGenerics);
|
||||
staticConstructor = this.convert(init);
|
||||
}
|
||||
|
||||
if (input instanceof Record)
|
||||
@@ -557,38 +538,13 @@ public class ASTToTargetAST {
|
||||
return new TargetSwitch(switchExpr, cases, null, true);
|
||||
}
|
||||
|
||||
private List<TargetMethod> generatePatternOverloads(ClassOrInterface clazz, List<MethodWithTphs> overloadedMethods) {
|
||||
private List<TargetMethod> generatePatternOverloads(ClassOrInterface clazz, MethodGroup group) {
|
||||
var overloadedMethods = group.methods.stream().toList();
|
||||
if (overloadedMethods.isEmpty()) return List.of();
|
||||
// Check if we have a pattern as a parameter
|
||||
var firstMethod = convert(overloadedMethods.getFirst(), overloadedMethods.getFirst().generics.javaGenerics);
|
||||
var firstMethod = convert(overloadedMethods.getFirst());
|
||||
if (overloadedMethods.size() == 1) return List.of(firstMethod);
|
||||
var secondMethod = convert(overloadedMethods.get(1), overloadedMethods.get(1).generics.javaGenerics);
|
||||
if (firstMethod.signature().parameters().stream().noneMatch(mp -> mp.pattern() instanceof TargetComplexPattern))
|
||||
return overloadedMethods.stream().map(m -> convert(m, m.generics.javaGenerics)).toList();
|
||||
|
||||
var signatureParams = new ArrayList<MethodParameter>();
|
||||
for (var i = 0; i < firstMethod.signature().parameters().size(); i++) {
|
||||
var p1 = firstMethod.signature().parameters().get(i).pattern();
|
||||
var t1 = p1.type();
|
||||
var t2 = secondMethod.signature().parameters().get(i).pattern().type();
|
||||
var commonSubTypes = new HashSet<>(commonSuperInterfaceTypes(t1, t2));
|
||||
for (var m : overloadedMethods.subList(2, overloadedMethods.size())) {
|
||||
var t3 = m.signature().java.parameters().get(i).pattern().type();
|
||||
commonSubTypes.retainAll(commonSuperInterfaceTypes(t1, t3));
|
||||
}
|
||||
if (commonSubTypes.size() > 1) throw new DebugException("Invalid overload");
|
||||
// TODO accept multiple types
|
||||
var superType = ASTFactory.createObjectClass();
|
||||
if (!commonSubTypes.isEmpty())
|
||||
superType = commonSubTypes.iterator().next();
|
||||
|
||||
String name;
|
||||
if (p1 instanceof TargetComplexPattern) name = "__var" + i;
|
||||
else name = p1.name();
|
||||
signatureParams.add(new MethodParameter(new TargetRefType(superType.getClassName().toString()), name));
|
||||
}
|
||||
|
||||
// Rename existing methods
|
||||
var signatureParams = group.signature.java.parameters();
|
||||
|
||||
var res = new ArrayList<TargetMethod>();
|
||||
for (var method : overloadedMethods) {
|
||||
@@ -607,12 +563,6 @@ public class ASTToTargetAST {
|
||||
res.add(new TargetMethod(tMethod.access(), name, tMethod.block(), tMethod.signature(), tMethod.txSignature()));
|
||||
}
|
||||
|
||||
var commonSubTypes = new HashSet<>(commonSuperInterfaceTypes(firstMethod.signature().returnType(), secondMethod.signature().returnType()));
|
||||
for (var m : overloadedMethods.subList(2, overloadedMethods.size())) {
|
||||
commonSubTypes.retainAll(commonSuperInterfaceTypes(firstMethod.signature().returnType(), m.signature().java.returnType()));
|
||||
}
|
||||
var returnType = commonSubTypes.isEmpty() ? TargetType.Object : new TargetRefType(commonSubTypes.iterator().next().getClassName().toString());
|
||||
|
||||
var parameters = signatureParams.stream().map( p -> new TargetLocalVar(p.pattern().type(), p.pattern().name())).toList();
|
||||
//var patterns = List.of((TargetComplexPattern) firstMethod.signature().parameters().stream()
|
||||
// .filter(p -> p.pattern() instanceof TargetComplexPattern).findFirst().orElseThrow().pattern());
|
||||
@@ -622,9 +572,7 @@ public class ASTToTargetAST {
|
||||
var stmt = generatePatternOverloadsRec(0, new TargetLocalVar(signatureParams.getFirst().pattern().type(), signatureParams.getFirst().pattern().name()), parameters, List.of(), res, classType);
|
||||
var block = new TargetBlock(List.of(stmt));
|
||||
|
||||
var signature = new TargetMethod.Signature(Set.of(), signatureParams, returnType);
|
||||
var bridgeMethod = new TargetMethod(firstMethod.access(), firstMethod.name(), block, signature, firstMethod.txSignature());
|
||||
|
||||
var bridgeMethod = new TargetMethod(firstMethod.access(), firstMethod.name(), block, group.signature.java, group.signature.tx);
|
||||
res.add(bridgeMethod);
|
||||
return res;
|
||||
}
|
||||
@@ -670,6 +618,15 @@ public class ASTToTargetAST {
|
||||
}).findFirst();
|
||||
}
|
||||
|
||||
record MethodGroup(Signature signature, Set<MethodWithTphs> methods) {
|
||||
@Override
|
||||
public String toString() {
|
||||
assert !methods.isEmpty();
|
||||
var first = methods.iterator().next();
|
||||
return signature.java.returnType() + " " + first.method.name + " " + signature.java.generics() + " " + signature.java.parameters();
|
||||
}
|
||||
}
|
||||
|
||||
record MethodWithTphs(Method method, Generics generics, Signature signature) {
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
@@ -684,14 +641,30 @@ public class ASTToTargetAST {
|
||||
}
|
||||
}
|
||||
|
||||
private TargetMethod convert(MethodWithTphs mtph) {
|
||||
return convert(mtph, mtph.generics.javaGenerics);
|
||||
}
|
||||
|
||||
private TargetMethod convert(MethodWithTphs mtph, IGenerics generics) {
|
||||
return new TargetMethod(mtph.method.modifier, mtph.method.name, convert(mtph.method.block, generics), mtph.signature.java(), mtph.signature.tx());
|
||||
}
|
||||
|
||||
record Signature(TargetMethod.Signature java, TargetMethod.Signature tx, Generics generics) {}
|
||||
record Signature(TargetMethod.Signature java, TargetMethod.Signature tx, Generics generics) {
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (this == other) return true;
|
||||
if (!(other instanceof Signature that)) return false;
|
||||
return Objects.equals(this.java, that.java) && Objects.equals(this.tx, that.tx);
|
||||
}
|
||||
|
||||
private List<MethodWithTphs> convert(ClassOrInterface currentClass, Method method) {
|
||||
List<MethodWithTphs> result = new ArrayList<>();
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(java, tx);
|
||||
}
|
||||
}
|
||||
|
||||
private Set<MethodWithTphs> convert(ClassOrInterface currentClass, Method method) {
|
||||
Set<MethodWithTphs> result = new HashSet<>();
|
||||
this.currentMethod = method;
|
||||
|
||||
List<Signature> signatures = new ArrayList<>();
|
||||
|
||||
@@ -3,4 +3,9 @@ package de.dhbwstuttgart.target.tree;
|
||||
import de.dhbwstuttgart.target.tree.type.TargetType;
|
||||
|
||||
public record TargetGeneric(String name, TargetType bound) {
|
||||
@Override
|
||||
public String toString() {
|
||||
if (bound.equals(TargetType.Object)) return "'" + name;
|
||||
else return "'" + name + " < " + bound;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -736,12 +736,12 @@ public class TYPEStmt implements StatementVisitor {
|
||||
|
||||
for (int i = 0; i < foMethod.arglist.getArguments().size(); i++) {
|
||||
// Zuordnung von MethoCall.signature (Argumenttypen) zu der Argumenttypen der ausgewaehlten Methode (assumption.params)
|
||||
ret.add(new Pair(resolver.resolve(foMethod.signature.get(i)), resolver.resolve(assumption.getArgTypes().get(i)), PairOperator.EQUALSDOT));
|
||||
ret.add(new Pair(foMethod.signature.get(i), assumption.getArgTypes().get(i), PairOperator.EQUALSDOT));
|
||||
|
||||
}
|
||||
|
||||
// Zuordnung von MethodCall.signature(ReturnType) zu dem ReturnType der ausgewaehlten Methode (assumption.returnType)
|
||||
ret.add(new Pair(resolver.resolve(foMethod.signature.getLast()), resolver.resolve(assumption.getReturnType()), PairOperator.EQUALSDOT));
|
||||
ret.add(new Pair(foMethod.signature.getLast(), assumption.getReturnType(), PairOperator.EQUALSDOT));
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -755,7 +755,7 @@ public class RuleSet implements IRuleSet{
|
||||
|
||||
UnifyType lhsType = pair.getLhsType();
|
||||
UnifyType rhsType = pair.getRhsType();
|
||||
if((!(lhsType instanceof ReferenceType) && !(lhsType instanceof FunNType)) || !(rhsType instanceof ExtendsType))
|
||||
if(!(lhsType instanceof ReferenceType) || !(rhsType instanceof ExtendsType))
|
||||
return Optional.empty();
|
||||
|
||||
return Optional.of(new UnifyPair(lhsType, ((ExtendsType) rhsType).getExtendedType(), PairOperator.SMALLERDOT, pair.getSubstitution(), pair.getBasePair()));
|
||||
@@ -781,7 +781,7 @@ public class RuleSet implements IRuleSet{
|
||||
|
||||
UnifyType lhsType = pair.getLhsType();
|
||||
UnifyType rhsType = pair.getRhsType();
|
||||
if((!(lhsType instanceof ReferenceType) && !(lhsType instanceof FunNType)) || !(rhsType instanceof SuperType))
|
||||
if(!(lhsType instanceof ReferenceType) || !(rhsType instanceof SuperType))
|
||||
return Optional.empty();
|
||||
|
||||
return Optional.of(new UnifyPair(((SuperType) rhsType).getSuperedType(), lhsType, PairOperator.SMALLERDOTWC, pair.getSubstitution(), pair.getBasePair()));
|
||||
@@ -904,7 +904,7 @@ public class RuleSet implements IRuleSet{
|
||||
|
||||
var fiArgs = intf.getFunctionalInterfaceTypeArguments(refType);
|
||||
var retType = fiArgs.getFirst();
|
||||
var lhsArgs = intf.getFunctionalInterfaceTypeArguments(lhsType); //FALSCHRUM????
|
||||
var lhsArgs = intf.getFunctionalInterfaceTypeArguments(lhsType);
|
||||
var lhsRet = lhsArgs.getFirst();
|
||||
|
||||
Set<UnifyPair> result = new HashSet<>();
|
||||
@@ -972,66 +972,16 @@ public class RuleSet implements IRuleSet{
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Set<UnifyPair>> smallerFunN(UnifyPair pair, IFiniteClosure fc) {
|
||||
public Optional<Set<UnifyPair>> smallerFunN(UnifyPair pair) {
|
||||
if(pair.getPairOp() != PairOperator.SMALLERDOT)
|
||||
return Optional.empty();
|
||||
|
||||
UnifyType lhsType = pair.getLhsType();
|
||||
UnifyType rhsType = pair.getRhsType();
|
||||
|
||||
if(!(rhsType instanceof FunNType))
|
||||
if(!(lhsType instanceof PlaceholderType) || !(rhsType instanceof FunNType))
|
||||
return Optional.empty();
|
||||
|
||||
/* muss nach angepasst werden */
|
||||
//FunN$$<...> <. FunctinalInterface<...> wird umgewandelt in FunN$$<...> <. FunN$$<... args aus FuntionalInterface ...>
|
||||
if (lhsType instanceof ReferenceType) {
|
||||
UnifyType typeFI = pair.getLhsType();
|
||||
|
||||
Optional<UnifyType> opt = fc.getRightHandedFunctionalInterfaceType(typeFI.getName());
|
||||
if(!opt.isPresent())
|
||||
return Optional.empty();
|
||||
|
||||
if (!(typeFI instanceof ReferenceType refType) || !(refType instanceof FunInterfaceType intf))
|
||||
return Optional.empty();
|
||||
|
||||
var fiArgs = intf.getFunctionalInterfaceTypeArguments(refType);
|
||||
var retType = fiArgs.getFirst();
|
||||
var rhsArgs = intf.getFunctionalInterfaceTypeArguments(rhsType);
|
||||
var rhsRet = rhsArgs.getFirst();
|
||||
|
||||
Set<UnifyPair> result = new HashSet<>();
|
||||
//if (retType instanceof ExtendsType) {
|
||||
result.add(new UnifyPair(retType, rhsRet, PairOperator.SMALLER));
|
||||
//} else if (retType instanceof SuperType) {
|
||||
// return Optional.empty();
|
||||
//} else {
|
||||
// result.add(new UnifyPair(lhsRet, retType, PairOperator.EQUALSDOT));
|
||||
//}
|
||||
|
||||
for (var i = 1; i < fiArgs.size(); i++) {
|
||||
var rh = rhsArgs.get(i);
|
||||
var lh = fiArgs.get(i);
|
||||
|
||||
//if (rh instanceof SuperType) {
|
||||
result.add(new UnifyPair(rh, lh, PairOperator.SMALLER));
|
||||
//} else if (rh instanceof ExtendsType) {
|
||||
// return Optional.empty();
|
||||
//} else {
|
||||
// result.add(new UnifyPair(lh, rh, PairOperator.EQUALSDOT));
|
||||
//}
|
||||
}
|
||||
|
||||
return Optional.of(result);
|
||||
}
|
||||
|
||||
else {
|
||||
if(!(lhsType instanceof PlaceholderType))
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
FunNType funNRhsType = (FunNType) rhsType;
|
||||
|
||||
Set<UnifyPair> result = new HashSet<UnifyPair>();
|
||||
@@ -1068,121 +1018,6 @@ public class RuleSet implements IRuleSet{
|
||||
return Optional.of(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Set<UnifyPair>> reduceFIFunN(UnifyPair pair, IFiniteClosure fc) {
|
||||
if(pair.getPairOp() != PairOperator.EQUALSDOT)
|
||||
return Optional.empty();
|
||||
|
||||
UnifyType lhsType = pair.getLhsType();
|
||||
UnifyType rhsType = pair.getRhsType();
|
||||
|
||||
if(!(rhsType instanceof FunNType))
|
||||
return Optional.empty();
|
||||
|
||||
/* muss nach angepasst werden */
|
||||
//FunN$$<...> <. FunctinalInterface<...> wird umgewandelt in FunN$$<...> <. FunN$$<... args aus FuntionalInterface ...>
|
||||
if (lhsType instanceof ReferenceType) {
|
||||
UnifyType typeFI = pair.getLhsType();
|
||||
|
||||
Optional<UnifyType> opt = fc.getRightHandedFunctionalInterfaceType(typeFI.getName());
|
||||
if(!opt.isPresent())
|
||||
return Optional.empty();
|
||||
|
||||
if (!(typeFI instanceof ReferenceType refType) || !(refType instanceof FunInterfaceType intf))
|
||||
return Optional.empty();
|
||||
|
||||
var fiArgs = intf.getFunctionalInterfaceTypeArguments(refType);
|
||||
var retType = fiArgs.getFirst();
|
||||
var rhsArgs = intf.getFunctionalInterfaceTypeArguments(rhsType);
|
||||
var rhsRet = rhsArgs.getFirst();
|
||||
|
||||
Set<UnifyPair> result = new HashSet<>();
|
||||
//if (retType instanceof ExtendsType) {
|
||||
result.add(new UnifyPair(retType, rhsRet, PairOperator.EQUALSDOT));
|
||||
//} else if (retType instanceof SuperType) {
|
||||
// return Optional.empty();
|
||||
//} else {
|
||||
// result.add(new UnifyPair(lhsRet, retType, PairOperator.EQUALSDOT));
|
||||
//}
|
||||
|
||||
for (var i = 1; i < fiArgs.size(); i++) {
|
||||
var rh = rhsArgs.get(i);
|
||||
var lh = fiArgs.get(i);
|
||||
|
||||
//if (rh instanceof SuperType) {
|
||||
result.add(new UnifyPair(rh, lh, PairOperator.EQUALSDOT));
|
||||
//} else if (rh instanceof ExtendsType) {
|
||||
// return Optional.empty();
|
||||
//} else {
|
||||
// result.add(new UnifyPair(lh, rh, PairOperator.EQUALSDOT));
|
||||
//}
|
||||
}
|
||||
|
||||
return Optional.of(result);
|
||||
}
|
||||
else {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Set<UnifyPair>> reduceFunNFi(UnifyPair pair, IFiniteClosure fc) {
|
||||
if(pair.getPairOp() != PairOperator.EQUALSDOT)
|
||||
return Optional.empty();
|
||||
|
||||
UnifyType lhsType = pair.getLhsType();
|
||||
UnifyType rhsType = pair.getRhsType();
|
||||
|
||||
if(!(lhsType instanceof FunNType))
|
||||
return Optional.empty();
|
||||
|
||||
//FunN$$<...> <. FunctinalInterface<...> wird umgewandelt in FunN$$<...> <. FunN$$<... args aus FuntionalInterface ...>
|
||||
if (rhsType instanceof ReferenceType) {
|
||||
UnifyType typeFI = pair.getRhsType();
|
||||
|
||||
Optional<UnifyType> opt = fc.getRightHandedFunctionalInterfaceType(typeFI.getName());
|
||||
if(!opt.isPresent())
|
||||
return Optional.empty();
|
||||
|
||||
if (!(typeFI instanceof ReferenceType refType) || !(refType instanceof FunInterfaceType intf))
|
||||
return Optional.empty();
|
||||
|
||||
var fiArgs = intf.getFunctionalInterfaceTypeArguments(refType);
|
||||
var retType = fiArgs.getFirst();
|
||||
var lhsArgs = intf.getFunctionalInterfaceTypeArguments(lhsType);
|
||||
var lhsRet = lhsArgs.getFirst();
|
||||
|
||||
Set<UnifyPair> result = new HashSet<>();
|
||||
//if (retType instanceof ExtendsType) {
|
||||
// result.add(new UnifyPair(lhsRet, retType, PairOperator.SMALLERDOTWC));
|
||||
//} else if (retType instanceof SuperType) {
|
||||
// return Optional.empty();
|
||||
//} else {
|
||||
result.add(new UnifyPair(lhsRet, retType, PairOperator.EQUALSDOT));
|
||||
//}
|
||||
|
||||
for (var i = 1; i < fiArgs.size(); i++) {
|
||||
var lh = lhsArgs.get(i);
|
||||
var rh = fiArgs.get(i);
|
||||
|
||||
//if (rh instanceof SuperType) {
|
||||
// result.add(new UnifyPair(lh, rh, PairOperator.SMALLERDOTWC));
|
||||
//} else if (rh instanceof ExtendsType) {
|
||||
// return Optional.empty();
|
||||
//} else {
|
||||
result.add(new UnifyPair(lh, rh, PairOperator.EQUALSDOT));
|
||||
//}
|
||||
}
|
||||
|
||||
return Optional.of(result);
|
||||
}
|
||||
|
||||
else {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Optional<UnifyPair> reduceTph(UnifyPair pair) {
|
||||
if(pair.getPairOp() != PairOperator.SMALLERDOTWC)
|
||||
@@ -1190,7 +1025,7 @@ public class RuleSet implements IRuleSet{
|
||||
|
||||
UnifyType lhsType = pair.getLhsType();
|
||||
UnifyType rhsType = pair.getRhsType();
|
||||
if(!(lhsType instanceof PlaceholderType) || (!(rhsType instanceof ReferenceType) && !(rhsType instanceof FunNType)))
|
||||
if(!(lhsType instanceof PlaceholderType) || !(rhsType instanceof ReferenceType))
|
||||
return Optional.empty();
|
||||
|
||||
return Optional.of(new UnifyPair(lhsType, rhsType, PairOperator.EQUALSDOT, pair.getSubstitution(), pair.getBasePair()));
|
||||
|
||||
@@ -1239,7 +1239,7 @@ public class TypeUnifyTask extends CancellableTask<CompletableFuture<Set<Set<Uni
|
||||
// FunN Rules
|
||||
optSet = optSet.isPresent() ? optSet : rules.reduceFunN(pair);
|
||||
optSet = optSet.isPresent() ? optSet : rules.greaterFunN(pair, fc);
|
||||
optSet = optSet.isPresent() ? optSet : rules.smallerFunN(pair, fc);
|
||||
optSet = optSet.isPresent() ? optSet : rules.smallerFunN(pair);
|
||||
|
||||
// One of the rules has been applied
|
||||
if (optSet.isPresent()) {
|
||||
|
||||
@@ -61,9 +61,7 @@ public interface IRuleSet {
|
||||
*/
|
||||
public Optional<Set<UnifyPair>> reduceFunN(UnifyPair pair);
|
||||
public Optional<Set<UnifyPair>> greaterFunN(UnifyPair pair, IFiniteClosure fc);
|
||||
public Optional<Set<UnifyPair>> smallerFunN(UnifyPair pair, IFiniteClosure fc);
|
||||
public Optional<Set<UnifyPair>> reduceFIFunN(UnifyPair pair, IFiniteClosure fc);
|
||||
public Optional<Set<UnifyPair>> reduceFunNFi(UnifyPair pair, IFiniteClosure fc);
|
||||
public Optional<Set<UnifyPair>> smallerFunN(UnifyPair pair);
|
||||
|
||||
/**
|
||||
* Checks whether the erase1-Rule applies to the pair.
|
||||
|
||||
@@ -685,7 +685,6 @@ public class FiniteClosure //extends Ordering<UnifyType> //entfernt PL 2018-12-1
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Set<UnifyType> getAncestors(UnifyType t) {
|
||||
if (!inheritanceGraph.containsKey(t))
|
||||
|
||||
@@ -14,9 +14,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import de.dhbwstuttgart.core.ConsoleInterface;
|
||||
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||
import de.dhbwstuttgart.util.Logger.LogLevel;
|
||||
|
||||
public class AllgemeinTest {
|
||||
|
||||
@@ -31,7 +29,6 @@ public class AllgemeinTest {
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
ConsoleInterface.logLevel = LogLevel.DEBUG;
|
||||
//String className = "GenTest";
|
||||
//String className = "Overloading_Generics";
|
||||
//String className = "Generics";
|
||||
@@ -66,14 +63,11 @@ public class AllgemeinTest {
|
||||
//String className = "Cycle";
|
||||
//String className = "TripleTest";
|
||||
//String className = "WildcardList";
|
||||
//String className = "List";
|
||||
String className = "List";
|
||||
//String className = "Box";
|
||||
//String className = "GenBox";
|
||||
//String className = "InnerInf";
|
||||
//String className = "Foo";
|
||||
|
||||
String className = "Kombinatoren_failure";
|
||||
//String className = "Parser";
|
||||
//PL 2019-10-24: genutzt fuer unterschiedliche Tests
|
||||
path = System.getProperty("user.dir")+"/resources/AllgemeinTest/" + className + ".jav";
|
||||
//path = System.getProperty("user.dir")+"/src/test/resources/AllgemeinTest/Overloading_Generics.jav";
|
||||
@@ -82,7 +76,7 @@ public class AllgemeinTest {
|
||||
///*
|
||||
compiler = new JavaTXCompiler(
|
||||
Lists.newArrayList(new File(path)),
|
||||
Lists.newArrayList(new File(System.getProperty("user.dir")+"/resources/AllgemeinTest/")),
|
||||
Lists.newArrayList(new File(System.getProperty("user.dir")+"/resources/bytecode/classFiles/")),
|
||||
new File(System.getProperty("user.dir")+"/resources/bytecode/classFiles/"), true);
|
||||
//*/
|
||||
compiler.generateBytecode();
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
|
||||
import de.dhbwstuttgart.core.ConsoleInterface;
|
||||
import de.dhbwstuttgart.util.Logger;
|
||||
import de.dhbwstuttgart.util.Logger.LogLevel;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -961,8 +959,8 @@ public class TestComplete {
|
||||
|
||||
@Test
|
||||
public void testPatternMatchingZip() throws Exception {
|
||||
ConsoleInterface.logLevel = LogLevel.DEBUG;
|
||||
var classFiles = generateClassFiles(createClassLoader(), false,"PatternMatchingJava2.jav", "PatternMatching.jav");
|
||||
//ConsoleInterface.logLevel = Logger.LogLevel.DEBUG;
|
||||
var classFiles = generateClassFiles(createClassLoader(), "PatternMatching.jav");
|
||||
var clazz = classFiles.get("PatternMatching");
|
||||
var instance = clazz.getDeclaredConstructor().newInstance();
|
||||
}
|
||||
@@ -1390,7 +1388,6 @@ public class TestComplete {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("too slow")
|
||||
public void testBug325() throws Exception {
|
||||
var classFiles = generateClassFiles(createClassLoader(), "Bug325.jav");
|
||||
var clazz = classFiles.get("Bug325");
|
||||
@@ -1552,4 +1549,11 @@ public class TestComplete {
|
||||
var clazz = classFiles.get("Bug390");
|
||||
clazz.getDeclaredMethod("main", List.class).invoke(null, List.of());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBug392() throws Exception {
|
||||
var classFiles = generateClassFiles(createClassLoader(), "Bug392.jav");
|
||||
var clazz = classFiles.get("Bug392");
|
||||
clazz.getDeclaredMethod("main", List.class).invoke(null, List.of());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user