solve tNew generics superclass and interfaces added.

This commit is contained in:
Aldaron7 2018-05-30 12:39:11 +02:00
parent c7e0281d53
commit 7e1d6fad01
4 changed files with 38 additions and 9 deletions

View File

@ -14,6 +14,7 @@ import de.dhbwstuttgart.strucTypes.classorinterface.ClassOrInterfaceWithConstrai
import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint; import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint;
import de.dhbwstuttgart.strucTypes.exception.InconsistentConstraintsException; import de.dhbwstuttgart.strucTypes.exception.InconsistentConstraintsException;
import de.dhbwstuttgart.strucTypes.visitor.InferTypes; import de.dhbwstuttgart.strucTypes.visitor.InferTypes;
import de.dhbwstuttgart.strucTypes.visitor.TypeExtract;
import de.dhbwstuttgart.strucTypes.visitor.TypeVar; import de.dhbwstuttgart.strucTypes.visitor.TypeVar;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.GenericDeclarationList; import de.dhbwstuttgart.syntaxtree.GenericDeclarationList;
@ -61,21 +62,22 @@ public class Solve {
subst.stream().filter(p -> PairOperator.SMALLERDOT.equals(p.getPairOp())).collect(Collectors.toSet()), subst.stream().filter(p -> PairOperator.SMALLERDOT.equals(p.getPairOp())).collect(Collectors.toSet()),
tphs); tphs);
// TODO Tnew
List<GenericTypeVar> values = new ArrayList<>(); List<GenericTypeVar> values = new ArrayList<>();
// extract typeplaceholder (type variables) of clsA // extract typeplaceholder (type variables) of clsA
TypeVar typeVar = new TypeVar(inferredTypes); TypeVar typeVar = new TypeVar(inferredTypes);
clsA.accept(typeVar); clsA.accept(typeVar);
final ArrayList<RefTypeOrTPHOrWildcardOrGeneric> bounds = new ArrayList<>(); final ArrayList<RefTypeOrTPHOrWildcardOrGeneric> bounds = new ArrayList<>();
final NullToken offset = new NullToken(); final NullToken offset = new NullToken();
// add type variables of clsA as GenericTypeVar to values of GenericDeclarationList // add type variables of clsA as GenericTypeVar to values of
// tNew
typeVar.getTypeVars().stream().map(tph -> new GenericTypeVar(tph.getName(), bounds, offset, offset)) typeVar.getTypeVars().stream().map(tph -> new GenericTypeVar(tph.getName(), bounds, offset, offset))
.forEach(gtv -> { .forEach(gtv -> {
if (values.stream().map(GenericTypeVar::getName).noneMatch(gtv.getName()::equals)) { if (values.stream().map(GenericTypeVar::getName).noneMatch(gtv.getName()::equals)) {
values.add(gtv); values.add(gtv);
}; };
}); });
// add type variables of cs as GenericRefType to values of GenericDeclarationList // add type variables of cs as GenericRefType to values of
// tNew
cs.stream().flatMap(c -> Stream.of(c.getSubtype(), c.getSupertype())).filter(t -> t instanceof TypePlaceholder) cs.stream().flatMap(c -> Stream.of(c.getSubtype(), c.getSupertype())).filter(t -> t instanceof TypePlaceholder)
.map(tph -> new GenericTypeVar(((TypePlaceholder) tph).getName(), bounds, offset, offset)) .map(tph -> new GenericTypeVar(((TypePlaceholder) tph).getName(), bounds, offset, offset))
.forEach(gtv -> { .forEach(gtv -> {
@ -83,7 +85,15 @@ public class Solve {
values.add(gtv); values.add(gtv);
}; };
}); });
// TODO typevar(superclass) // add generics of all superclasses and superinterfaces of clsA to
// values of tNew
TypeExtract typeExtract = new TypeExtract();
clsA.accept(typeExtract);
typeExtract.getGenerics().forEach(gtv -> {
if (values.stream().map(GenericTypeVar::getName).noneMatch(gtv.getName()::equals)) {
values.add(gtv);
};
});
GenericDeclarationList tNew = new GenericDeclarationList(values, offset); GenericDeclarationList tNew = new GenericDeclarationList(values, offset);
if (!consistent(cs)) { if (!consistent(cs)) {

View File

@ -189,7 +189,6 @@ public class InferTypes implements ASTReturnVisitor {
@Override @Override
public RefTypeOrTPHOrWildcardOrGeneric visit(TypePlaceholder typePlaceholder) { public RefTypeOrTPHOrWildcardOrGeneric visit(TypePlaceholder typePlaceholder) {
RefTypeOrTPHOrWildcardOrGeneric inferredType = inferredTypes.infer(typePlaceholder); RefTypeOrTPHOrWildcardOrGeneric inferredType = inferredTypes.infer(typePlaceholder);
if (inferredType instanceof TypePlaceholder) { if (inferredType instanceof TypePlaceholder) {
TypePlaceholder inferredTypeTPH = (TypePlaceholder) inferredType; TypePlaceholder inferredTypeTPH = (TypePlaceholder) inferredType;
@ -334,6 +333,7 @@ public class InferTypes implements ASTReturnVisitor {
@Override @Override
public This visit(This aThis) { public This visit(This aThis) {
// als Typ wird immer ein neuer TypePlaceholder zugeordnet.
return aThis; return aThis;
} }

View File

@ -9,6 +9,8 @@ import de.dhbwstuttgart.strucTypes.classorinterface.ClassOrInterfaceFactory;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Constructor; import de.dhbwstuttgart.syntaxtree.Constructor;
import de.dhbwstuttgart.syntaxtree.Field; import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.GenericDeclarationList;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
@ -25,6 +27,7 @@ public class TypeExtract extends DefaultASTVisitor {
private List<Field> fields = new ArrayList<>(); private List<Field> fields = new ArrayList<>();
private List<Method> methods = new ArrayList<>(); private List<Method> methods = new ArrayList<>();
private List<Constructor> constructors = new ArrayList<>(); private List<Constructor> constructors = new ArrayList<>();
private List<GenericTypeVar> generics = new ArrayList<>();
private boolean typeVariable = false; private boolean typeVariable = false;
private boolean initialClass = true; private boolean initialClass = true;
@ -40,6 +43,10 @@ public class TypeExtract extends DefaultASTVisitor {
return constructors; return constructors;
} }
public List<GenericTypeVar> getGenerics() {
return generics;
}
public Optional<Field> getField(String fieldName) { public Optional<Field> getField(String fieldName) {
return this.fields.stream().filter(f -> f.getName().equals(fieldName)) return this.fields.stream().filter(f -> f.getName().equals(fieldName))
// keine statische Polymorphie zugelassen // keine statische Polymorphie zugelassen
@ -66,6 +73,7 @@ public class TypeExtract extends DefaultASTVisitor {
if (c != null && this.initialClass) if (c != null && this.initialClass)
c.accept(this); c.accept(this);
}); });
classOrInterface.getGenerics().accept(this);
this.initialClass = false; this.initialClass = false;
// superClass(Object) -> Object => unendliche Rekursionstiefe! // superClass(Object) -> Object => unendliche Rekursionstiefe!
if (!classOrInterface.getClassName().equals(new JavaClassName("java.lang.Object"))) { if (!classOrInterface.getClassName().equals(new JavaClassName("java.lang.Object"))) {
@ -82,12 +90,12 @@ public class TypeExtract extends DefaultASTVisitor {
@Override @Override
public void visit(Field field) { public void visit(Field field) {
this.fields.add(field); this.fields.add(field);
} }
@Override @Override
public void visit(Method method) { public void visit(Method method) {
this.methods.add(method); this.methods.add(method);
} }
@Override @Override
@ -100,4 +108,15 @@ public class TypeExtract extends DefaultASTVisitor {
this.constructors.add(constructor); this.constructors.add(constructor);
} }
@Override
public void visit(GenericDeclarationList genericTypeVars) {
genericTypeVars.forEach(gtv -> gtv.accept(this));
}
@Override
public void visit(GenericTypeVar genericTypeVar) {
if (!this.generics.contains(genericTypeVar)) {
this.generics.add(genericTypeVar);
}
}
} }

View File

@ -73,8 +73,8 @@ public class TestSolve {
// Alle Klassen aus allen SourceFiles // Alle Klassen aus allen SourceFiles
List<ClassOrInterface> availableClasses = compiler.getAvailableClasses(sourceFile); List<ClassOrInterface> availableClasses = compiler.getAvailableClasses(sourceFile);
FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(availableClasses); FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(availableClasses);
System.out.println("\nFinite Closure:"); // System.out.println("\nFinite Closure:");
System.out.println(finiteClosure); // System.out.println(finiteClosure);
Solve solve = new Solve(subTypeConstraints, sourceFile.getClasses().get(0), finiteClosure, inferredTypesConstruct); Solve solve = new Solve(subTypeConstraints, sourceFile.getClasses().get(0), finiteClosure, inferredTypesConstruct);
ClassOrInterfaceWithConstraints solvedClass = solve.getSolvedClass(); ClassOrInterfaceWithConstraints solvedClass = solve.getSolvedClass();
System.out.println("\nSolved Class:"); System.out.println("\nSolved Class:");