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 // TODO Tnew
List<GenericTypeVar> values = new ArrayList<>(); List<GenericTypeVar> values = new ArrayList<>();
// 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
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(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) 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(values::add); .forEach(gtv -> {
//TODO typevar(superclass) if (values.stream().map(GenericTypeVar::getName).noneMatch(gtv.getName()::equals)) {
GenericDeclarationList Tnew = new GenericDeclarationList(values, offset); values.add(gtv);
};
});
// TODO typevar(superclass)
GenericDeclarationList tNew = new GenericDeclarationList(values, offset);
if (!consistent(cs)) { if (!consistent(cs)) {
throw new InconsistentConstraintsException(); 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); this.constraints);
} }

View File

@ -88,7 +88,7 @@ public class TYPEExpr extends DefaultASTVisitor {
* korrespondierenden Feld in der Instanz auf. * korrespondierenden Feld in der Instanz auf.
* *
* @param fieldVar * @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) { private boolean inferFieldVarTypes(FieldVar fieldVar) {
if (fieldVar.receiver instanceof This) { 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) // ermittelt den Typ ty0 von methodCall.receiver und mtype(m, ty0)
TypeExtract methodTypeVisitor = new TypeExtract(); TypeExtract methodTypeVisitor = new TypeExtract();
// unterscheide methodcall.receiver == this
if (methodCall.receiver instanceof ExpressionReceiver if (methodCall.receiver instanceof ExpressionReceiver
&& ((ExpressionReceiver) methodCall.receiver).expr instanceof This) { && ((ExpressionReceiver) methodCall.receiver).expr instanceof This) {
this.aThis.accept(methodTypeVisitor); 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.SuperWildcardType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; 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 { public class InferTypes implements ASTReturnVisitor {
private InferredTypes inferredTypes = new InferredTypes(); private InferredTypes inferredTypes = new InferredTypes();
private GenericDeclarationList generics = new GenericDeclarationList(new ArrayList<>(), new NullToken());
public InferTypes(InferredTypes inferredTypes) { public InferTypes(InferredTypes inferredTypes) {
this.inferredTypes = inferredTypes; this.inferredTypes = inferredTypes;
} }
public InferTypes(InferredTypes inferredTypes, GenericDeclarationList generics) {
this.inferredTypes = inferredTypes;
this.generics = generics;
}
@Override @Override
public SourceFile visit(SourceFile sourceFile) { public SourceFile visit(SourceFile sourceFile) {
return new SourceFile(sourceFile.getPkgName(), return new SourceFile(sourceFile.getPkgName(),
@ -171,12 +185,19 @@ public class InferTypes implements ASTReturnVisitor {
@Override @Override
public SuperWildcardType visit(SuperWildcardType superWildcardType) { public SuperWildcardType visit(SuperWildcardType superWildcardType) {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override @Override
public RefTypeOrTPHOrWildcardOrGeneric visit(TypePlaceholder typePlaceholder) { 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 @Override
@ -342,7 +363,7 @@ public class InferTypes implements ASTReturnVisitor {
@Override @Override
public SuperCall visit(SuperCall superCall) { public SuperCall visit(SuperCall superCall) {
if(superCall.getArgumentList().getArguments() == null){ if (superCall.getArgumentList().getArguments() == null) {
return superCall; return superCall;
} }
return new SuperCall(superCall.getArgumentList().accept(this), superCall.getOffset()); 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.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
/**
* Extrahiert alle TypePlaceholder.
*
* @author mvr
*
*/
public class TypeVar extends AbstractASTWalker { public class TypeVar extends AbstractASTWalker {
private Set<TypePlaceholder> typeVars = new HashSet<>(); private Set<TypePlaceholder> typeVars = new HashSet<>();

View File

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