solve tph zu grt

This commit is contained in:
Aldaron7 2018-05-30 11:52:38 +02:00
parent 23ae5d5745
commit c7e0281d53
5 changed files with 52 additions and 9 deletions

View File

@ -63,22 +63,33 @@ public class Solve {
// TODO Tnew
List<GenericTypeVar> values = new ArrayList<>();
// extract typeplaceholder (type variables) of clsA
TypeVar typeVar = new TypeVar(inferredTypes);
clsA.accept(typeVar);
final ArrayList<RefTypeOrTPHOrWildcardOrGeneric> bounds = new ArrayList<>();
final NullToken offset = new NullToken();
// add type variables of clsA as GenericTypeVar to values of GenericDeclarationList
typeVar.getTypeVars().stream().map(tph -> new GenericTypeVar(tph.getName(), bounds, offset, offset))
.forEach(values::add);
.forEach(gtv -> {
if (values.stream().map(GenericTypeVar::getName).noneMatch(gtv.getName()::equals)) {
values.add(gtv);
};
});
// add type variables of cs as GenericRefType to values of GenericDeclarationList
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))
.forEach(values::add);
//TODO typevar(superclass)
GenericDeclarationList Tnew = new GenericDeclarationList(values, offset);
.forEach(gtv -> {
if (values.stream().map(GenericTypeVar::getName).noneMatch(gtv.getName()::equals)) {
values.add(gtv);
};
});
// TODO typevar(superclass)
GenericDeclarationList tNew = new GenericDeclarationList(values, offset);
if (!consistent(cs)) {
throw new InconsistentConstraintsException();
}
return new ClassOrInterfaceWithConstraints(this.clsA.accept(new InferTypes(inferredTypes)), Tnew,
return new ClassOrInterfaceWithConstraints(this.clsA.accept(new InferTypes(inferredTypes, tNew)), tNew,
this.constraints);
}

View File

@ -88,7 +88,7 @@ public class TYPEExpr extends DefaultASTVisitor {
* korrespondierenden Feld in der Instanz auf.
*
* @param fieldVar
* @return true, wenn ein passendes Feld in der Instanz gefungen wird.
* @return true, wenn ein passendes Feld in der Instanz gefunden wird.
*/
private boolean inferFieldVarTypes(FieldVar fieldVar) {
if (fieldVar.receiver instanceof This) {
@ -108,6 +108,7 @@ public class TYPEExpr extends DefaultASTVisitor {
// ermittelt den Typ ty0 von methodCall.receiver und mtype(m, ty0)
TypeExtract methodTypeVisitor = new TypeExtract();
// unterscheide methodcall.receiver == this
if (methodCall.receiver instanceof ExpressionReceiver
&& ((ExpressionReceiver) methodCall.receiver).expr instanceof This) {
this.aThis.accept(methodTypeVisitor);

View File

@ -57,14 +57,28 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
/**
* Inferiert alle TypePlaceholder durch inferredTypes und erstzt sie durch
* GenericTypeVar, falls der Name des Typeplaceholder mit dem Namen eines
* generics in der GenericdeclarationList generics übereinstimmt.
*
* @author mvr
*
*/
public class InferTypes implements ASTReturnVisitor {
private InferredTypes inferredTypes = new InferredTypes();
private GenericDeclarationList generics = new GenericDeclarationList(new ArrayList<>(), new NullToken());
public InferTypes(InferredTypes inferredTypes) {
this.inferredTypes = inferredTypes;
}
public InferTypes(InferredTypes inferredTypes, GenericDeclarationList generics) {
this.inferredTypes = inferredTypes;
this.generics = generics;
}
@Override
public SourceFile visit(SourceFile sourceFile) {
return new SourceFile(sourceFile.getPkgName(),
@ -171,12 +185,19 @@ public class InferTypes implements ASTReturnVisitor {
@Override
public SuperWildcardType visit(SuperWildcardType superWildcardType) {
throw new NotImplementedException();
}
@Override
public RefTypeOrTPHOrWildcardOrGeneric visit(TypePlaceholder typePlaceholder) {
return inferredTypes.infer(typePlaceholder);
RefTypeOrTPHOrWildcardOrGeneric inferredType = inferredTypes.infer(typePlaceholder);
if (inferredType instanceof TypePlaceholder) {
TypePlaceholder inferredTypeTPH = (TypePlaceholder) inferredType;
if (generics.contains(inferredTypeTPH.getName())) {
return new GenericRefType(inferredTypeTPH.getName(), typePlaceholder.getOffset());
}
}
return inferredType;
}
@Override
@ -342,7 +363,7 @@ public class InferTypes implements ASTReturnVisitor {
@Override
public SuperCall visit(SuperCall superCall) {
if(superCall.getArgumentList().getArguments() == null){
if (superCall.getArgumentList().getArguments() == null) {
return superCall;
}
return new SuperCall(superCall.getArgumentList().accept(this), superCall.getOffset());

View File

@ -8,6 +8,12 @@ import de.dhbwstuttgart.syntaxtree.AbstractASTWalker;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
/**
* Extrahiert alle TypePlaceholder.
*
* @author mvr
*
*/
public class TypeVar extends AbstractASTWalker {
private Set<TypePlaceholder> typeVars = new HashSet<>();

View File

@ -23,6 +23,10 @@ public class GenericDeclarationList extends SyntaxTreeNode implements Iterable<G
gtvs = values;
this.offsetOfLastElement = endOffset;
}
public boolean contains(String genericTypeVarName){
return gtvs.stream().anyMatch(gtv-> gtv.getName().equals(genericTypeVarName));
}
@Override
public Iterator<GenericTypeVar> iterator() {