Unvollständiger Zustand; Verknüpfung zwischen GTVs und TPH geändert. Änderungen in TypeStmt

This commit is contained in:
JanUlrich 2017-11-15 17:58:15 +01:00
parent 48dc76646b
commit b51d8356b7
22 changed files with 162 additions and 71 deletions

View File

@ -6,6 +6,8 @@ import de.dhbwstuttgart.syntaxtree.statement.literal.Null;
import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import java.lang.reflect.Type;
public interface ASTVisitor extends StatementVisitor{
void visit(SourceFile sourceFile);

View File

@ -37,7 +37,7 @@ public abstract class AbstractASTWalker implements ASTVisitor{
@Override
public void visit(FormalParameter formalParameter) {
formalParameter.getType().accept(this);
formalParameter.getType().accept((ASTVisitor) this);
}
@Override
@ -105,7 +105,6 @@ public abstract class AbstractASTWalker implements ASTVisitor{
@Override
public void visit(TypePlaceholder typePlaceholder) {
}
@Override
@ -115,7 +114,6 @@ public abstract class AbstractASTWalker implements ASTVisitor{
@Override
public void visit(GenericRefType genericRefType) {
}
@Override

View File

@ -3,7 +3,6 @@ package de.dhbwstuttgart.syntaxtree.factory;
import java.util.*;
import java.util.stream.Collectors;
import de.dhbwstuttgart.environment.CompilationEnvironment;
import de.dhbwstuttgart.exceptions.DebugException;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.NullToken;
@ -161,7 +160,7 @@ public class UnifyTypeFactory {
}
public static UnifyType convert(GenericRefType t){
return new ReferenceType(t.getUniqueIdentifier());
return new ReferenceType(t.getParsedName());
}
public static UnifyType convert(WildcardType t){

View File

@ -4,7 +4,6 @@ package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.antlr.v4.runtime.Token;

View File

@ -7,7 +7,6 @@ import de.dhbwstuttgart.typeinference.assumptions.FieldAssumption;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.antlr.v4.runtime.Token;

View File

@ -8,7 +8,6 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;

View File

@ -15,7 +15,6 @@ import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.antlr.v4.runtime.Token;

View File

@ -1,9 +1,6 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import org.antlr.v4.runtime.Token;

View File

@ -3,14 +3,7 @@ package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.atn.SemanticContext;
import de.dhbwstuttgart.exceptions.NotImplementedException;
public class This extends Expression

View File

@ -36,8 +36,8 @@ public class ExtendsWildcardType extends WildcardType{
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
public <A> A acceptTV(TypeVisitor<A> visitor) {
return visitor.visit(this);
}
@Override

View File

@ -20,13 +20,13 @@ public class GenericRefType extends RefTypeOrTPHOrWildcardOrGeneric
return name;
}
public String getUniqueIdentifier(){
public String getParsedName(){
return name.toString();
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
public <A> A acceptTV(TypeVisitor<A> visitor) {
return visitor.visit(this);
}
@Override

View File

@ -96,8 +96,8 @@ public class RefType extends RefTypeOrTPHOrWildcardOrGeneric
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
public <A> A acceptTV(TypeVisitor<A> visitor) {
return visitor.visit(this);
}
@Override

View File

@ -11,7 +11,10 @@ public abstract class RefTypeOrTPHOrWildcardOrGeneric extends SyntaxTreeNode{
}
@Override
public abstract void accept(ASTVisitor visitor);
public void accept(ASTVisitor visitor){
this.acceptTV((TypeVisitor)visitor);
}
public abstract <A> A acceptTV(TypeVisitor<A> visitor);
public abstract void accept(ResultSetVisitor visitor);
}

View File

@ -46,8 +46,8 @@ public class SuperWildcardType extends WildcardType{
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
public <A> A acceptTV(TypeVisitor<A> visitor) {
return visitor.visit(this);
}
@Override

View File

@ -69,8 +69,8 @@ public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
public <A> A acceptTV(TypeVisitor<A> visitor) {
return visitor.visit(this);
}
@Override

View File

@ -0,0 +1,13 @@
package de.dhbwstuttgart.syntaxtree.type;
public interface TypeVisitor<A> {
A visit(RefType refType);
A visit(SuperWildcardType superWildcardType);
A visit(TypePlaceholder typePlaceholder);
A visit(ExtendsWildcardType extendsWildcardType);
A visit(GenericRefType genericRefType);
}

View File

@ -11,7 +11,7 @@ import de.dhbwstuttgart.syntaxtree.type.*;
import java.lang.reflect.Modifier;
import java.util.Iterator;
public class OutputGenerator implements ASTVisitor {
public class OutputGenerator implements ASTVisitor{
private static final String TAB = " ";
String tabs = "";
protected final StringBuilder out;

View File

@ -3,6 +3,7 @@ package de.dhbwstuttgart.typeinference.assumptions;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.TypeScope;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.constraints.GenericsResolver;
public class FieldAssumption extends Assumption{
private ClassOrInterface receiverClass;
@ -19,7 +20,12 @@ public class FieldAssumption extends Assumption{
return receiverClass;
}
public RefTypeOrTPHOrWildcardOrGeneric getType() {
return type;
public RefTypeOrTPHOrWildcardOrGeneric getType(GenericsResolver resolver) {
return resolver.resolve(type);
}
public RefTypeOrTPHOrWildcardOrGeneric getReceiverType(GenericsResolver resolver) {
return null;
}
}

View File

@ -1,12 +1,17 @@
package de.dhbwstuttgart.typeinference.assumptions;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.ParameterList;
import de.dhbwstuttgart.syntaxtree.TypeScope;
import de.dhbwstuttgart.syntaxtree.statement.Assign;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.constraints.GenericsResolver;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@ -38,7 +43,30 @@ public class MethodAssumption extends Assumption{
return retType;
}
public List<RefTypeOrTPHOrWildcardOrGeneric> getArgTypes() {
return params;
public List<RefTypeOrTPHOrWildcardOrGeneric> getArgTypes(GenericsResolver resolver) {
List<RefTypeOrTPHOrWildcardOrGeneric> ret = new ArrayList<>();
for(RefTypeOrTPHOrWildcardOrGeneric param : params){
if(param instanceof GenericRefType){ //Generics in den Assumptions müssen umgewandelt werden.
param = resolver.resolve((GenericRefType) param);
}
ret.add(param);
}
return ret;
}
/**
*
* @param resolver
* @return
*/
public RefTypeOrTPHOrWildcardOrGeneric getReceiverType(GenericsResolver resolver) {
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
for(GenericTypeVar gtv : receiver.getGenerics()){
//Die Generics werden alle zu TPHs umgewandelt.
params.add(resolver.resolve(new GenericRefType(gtv.getName(), new NullToken())));
}
RefTypeOrTPHOrWildcardOrGeneric receiverType = new RefType(receiver.getClassName(), params, new NullToken());
return receiverType;
}
}

View File

@ -1,6 +1,8 @@
package de.dhbwstuttgart.typeinference.constraints;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
/**
@ -8,5 +10,5 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
* TODO: Erklörung!
*/
public interface GenericsResolver {
public TypePlaceholder resolve(GenericTypeVar generic);
public RefTypeOrTPHOrWildcardOrGeneric resolve(RefTypeOrTPHOrWildcardOrGeneric generic);
}

View File

@ -0,0 +1,56 @@
package de.dhbwstuttgart.typeinference.typeAlgo;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.typeinference.constraints.GenericsResolver;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Ein GenericsResolver, welcher Generics mit dem selben Namen den selben TPH zuordnet
*/
public class GenericsResolverSameName implements GenericsResolver, TypeVisitor<RefTypeOrTPHOrWildcardOrGeneric>{
HashMap<String, TypePlaceholder> map = new HashMap<>();
@Override
public RefTypeOrTPHOrWildcardOrGeneric resolve(RefTypeOrTPHOrWildcardOrGeneric generic) {
return generic.acceptTV(this);
}
@Override
public RefTypeOrTPHOrWildcardOrGeneric visit(RefType refType) {
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
for(RefTypeOrTPHOrWildcardOrGeneric param : refType.getParaList()){
params.add(param.acceptTV(this));
}
RefType ret = new RefType(refType.getName(), params, refType.getOffset());
return ret;
}
@Override
public RefTypeOrTPHOrWildcardOrGeneric visit(SuperWildcardType superWildcardType) {
return new SuperWildcardType(superWildcardType.getInnerType().acceptTV(this), superWildcardType.getOffset());
}
@Override
public RefTypeOrTPHOrWildcardOrGeneric visit(TypePlaceholder typePlaceholder) {
return typePlaceholder;
}
@Override
public RefTypeOrTPHOrWildcardOrGeneric visit(ExtendsWildcardType extendsWildcardType) {
return new SuperWildcardType(extendsWildcardType.getInnerType().acceptTV(this), extendsWildcardType.getOffset());
}
@Override
public RefTypeOrTPHOrWildcardOrGeneric visit(GenericRefType genericRefType) {
String name = genericRefType.getParsedName();
if(!map.containsKey(name)){
map.put(name, TypePlaceholder.fresh(genericRefType.getOffset()));
}
return map.get(name);
}
}

View File

@ -34,14 +34,15 @@ public class TYPEStmt implements StatementVisitor{
return constraintsSet;
}
/**
* Erstellt einen neuen GenericResolver
* Die Idee dieser Datenstruktur ist es, GTVs einen eindeutigen TPH zuzuweisen.
* Bei Methodenaufrufen oder anderen Zugriffen, bei denen alle benutzten GTVs jeweils einen einheitlichen TPH bekommen müssen
* kann diese Klasse eingesetzt werden. Wichtig ist, dass hierfür jeweils eine frische Instanz benutzt wird.
* @return
*/
private static GenericsResolver getResolverInstance(){
Map<GenericTypeVar, TypePlaceholder> map = new HashMap<>();
return generic -> {
if(map.containsKey(generic))return map.get(generic);
TypePlaceholder ret = TypePlaceholder.fresh(generic.getOffset());
map.put(generic, ret);
return ret;
};
return new GenericsResolverSameName();
}
private static TypeScope createTypeScope(ClassOrInterface cl, Method method) {
@ -62,11 +63,11 @@ public class TYPEStmt implements StatementVisitor{
//lambdaParams.add(tphRetType);
lambdaParams.add(0,tphRetType);
constraintsSet.addUndConstraint(
ConstraintsFactory.createPair(lambdaExpression.getType(),
new FunN(lambdaParams),PairOperator.EQUALSDOT,info.getCurrentTypeScope(), createNullTypeScope(), getResolverInstance()));
new Pair(lambdaExpression.getType(),
new FunN(lambdaParams),PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(
ConstraintsFactory.createPair(lambdaExpression.getReturnType(),
tphRetType,info.getCurrentTypeScope(), createNullTypeScope(), getResolverInstance()));
new Pair(lambdaExpression.getReturnType(),
tphRetType,PairOperator.EQUALSDOT));
//Constraints des Bodys generieren:
TYPEStmt lambdaScope = new TYPEStmt(new TypeInferenceBlockInformation(info, lambdaExpression));
@ -78,8 +79,8 @@ public class TYPEStmt implements StatementVisitor{
public void visit(Assign assign) {
assign.lefSide.accept(this);
assign.rightSide.accept(this);
constraintsSet.addUndConstraint(ConstraintsFactory.createPair(
assign.rightSide.getType(), assign.lefSide.getType(), PairOperator.SMALLERDOT, info.getCurrentTypeScope(), createNullTypeScope(), getResolverInstance()));
constraintsSet.addUndConstraint(new Pair(
assign.rightSide.getType(), assign.lefSide.getType(), PairOperator.SMALLERDOT));
}
@Override
@ -115,8 +116,9 @@ public class TYPEStmt implements StatementVisitor{
constraint.add(ConstraintsFactory.createPair(
fieldVar.receiver.getType(),fieldAssumption.getReceiverType(), info.getCurrentTypeScope(), fieldAssumption.getTypeScope(), resolver));
*/
constraint.add(ConstraintsFactory.createPair(
fieldVar.getType(),fieldAssumption.getType(), info.getCurrentTypeScope(), fieldAssumption.getTypeScope(), resolver));
constraint.add(new Pair(fieldVar.receiver.getType(), fieldAssumption.getReceiverType(resolver), PairOperator.EQUALSDOT));
constraint.add(new Pair(
fieldVar.getType(), fieldAssumption.getType(resolver), PairOperator.EQUALSDOT));
oderConstraints.add(constraint);
}
if(oderConstraints.size() == 0)
@ -159,8 +161,7 @@ public class TYPEStmt implements StatementVisitor{
Set<Constraint> methodConstraints = new HashSet<>();
for(MethodAssumption m : this.getMethods(methodCall.name, methodCall.arglist, info)){
GenericsResolver resolver = getResolverInstance();
TypeScope additionalScope = m.getTypeScope();
methodConstraints.add(generateConstraint(methodCall, m, info, getResolverInstance()));
methodConstraints.add(generateConstraint(methodCall, m, info, resolver));
}
if(methodConstraints.size()<1){
throw new TypeinferenceException("Methode "+methodCall.name+" ist nicht vorhanden!",methodCall.getOffset());
@ -194,9 +195,7 @@ public class TYPEStmt implements StatementVisitor{
@Override
public void visit(Return returnExpr) {
returnExpr.retexpr.accept(this);
constraintsSet.addUndConstraint(ConstraintsFactory.createPair(
returnExpr.getType(),info.getCurrentTypeScope().getReturnType(), PairOperator.EQUALSDOT,
info.getCurrentTypeScope(), createNullTypeScope(), getResolverInstance()));
constraintsSet.addUndConstraint(new Pair(returnExpr.getType(),info.getCurrentTypeScope().getReturnType(), PairOperator.EQUALSDOT));
}
@Override
@ -223,9 +222,8 @@ public class TYPEStmt implements StatementVisitor{
params.add(new GenericRefType(gtv.getName(), aThis.getOffset()));
}
RefType thisType = new RefType(currentClass.getClassName(), params, aThis.getOffset());
constraintsSet.addUndConstraint(ConstraintsFactory.createPair(
aThis.getType(), thisType, PairOperator.EQUALSDOT, info.getCurrentTypeScope(),
createNullTypeScope(), getResolverInstance()));
constraintsSet.addUndConstraint(new Pair(
aThis.getType(), thisType, PairOperator.EQUALSDOT));
}
private static TypeScope createNullTypeScope() {
@ -291,18 +289,17 @@ public class TYPEStmt implements StatementVisitor{
TypeInferenceBlockInformation info, GenericsResolver resolver){
Constraint methodConstraint = new Constraint();
ClassOrInterface receiverCl = assumption.getReceiver();
/*
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
for(GenericTypeVar gtv : receiverCl.getGenerics()){
//if(gtv.definingClass().equals(info.getCurrentClass().getClassName())){
// params.add(new GenericRefType(gtv.getName(), forMethod.getOffset()));
//}else{
//Die Generics werden alle zu TPHs umgewandelt.
params.add(TypePlaceholder.fresh(forMethod.getOffset()));
//}
params.add(resolver.resolve(gtv.getParsedName()));
}
RefTypeOrTPHOrWildcardOrGeneric receiverType = new RefType(assumption.getReceiver().getClassName(), params, forMethod.getOffset());
methodConstraint.add(new Pair(forMethod.receiver.getType(), receiverType,
PairOperator.SMALLERDOT);
*/
methodConstraint.add(new Pair(forMethod.receiver.getType(), assumption.getReceiverType(resolver),
PairOperator.SMALLERDOT));
methodConstraint.add(new Pair(assumption.getReturnType(), forMethod.getType(),
PairOperator.EQUALSDOT));
methodConstraint.addAll(generateParameterConstraints(forMethod, assumption, info, resolver));
@ -314,8 +311,9 @@ public class TYPEStmt implements StatementVisitor{
Set<Pair> ret = new HashSet<>();
for(int i = 0;i<foMethod.arglist.getArguments().size();i++){
foMethod.arglist.getArguments().get(i).accept(this);
ret.add(ConstraintsFactory.createPair(foMethod.arglist.getArguments().get(i).getType(),
assumption.getArgTypes().get(i), PairOperator.SMALLERDOT, info.getCurrentTypeScope(), assumption.getTypeScope(), resolver));
RefTypeOrTPHOrWildcardOrGeneric argType = foMethod.arglist.getArguments().get(i).getType();
RefTypeOrTPHOrWildcardOrGeneric assType = assumption.getArgTypes(resolver).get(i);
ret.add(new Pair(argType, assType, PairOperator.SMALLERDOT));
}
return ret;
}
@ -396,8 +394,8 @@ public class TYPEStmt implements StatementVisitor{
protected Constraint<Pair> generateConstructorConstraint(NewClass forConstructor, MethodAssumption assumption,
TypeInferenceBlockInformation info, GenericsResolver resolver){
Constraint methodConstraint = new Constraint();
methodConstraint.add(ConstraintsFactory.createPair(assumption.getReturnType(), forConstructor.getType(),
PairOperator.SMALLERDOT, info.getCurrentTypeScope(), assumption.getTypeScope(), resolver));
methodConstraint.add(new Pair(assumption.getReturnType(), forConstructor.getType(),
PairOperator.SMALLERDOT));
methodConstraint.addAll(generateParameterConstraints(forConstructor, assumption, info, resolver));
return methodConstraint;
}