Compare commits

...

4 Commits

Author SHA1 Message Date
9c6372c3ba Thats probably important 2024-02-28 13:22:13 +01:00
688358aa33 Add more debug help 2024-02-28 13:16:32 +01:00
a60282414c Add location information to constraints 2024-02-27 16:25:47 +01:00
75b9020cf9 Fix up Character 2024-02-26 15:02:15 +01:00
13 changed files with 161 additions and 107 deletions

View File

@ -1,3 +1,6 @@
import java.lang.Character;
public class Literal {
m() { return null; }
m2() { return 'C'; }
}

View File

@ -136,7 +136,7 @@ public class Codegen {
} else if (type.equals(TargetType.Short) || type.equals(TargetType.short_)) {
mv.visitMethodInsn(INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;", false);
} else if (type.equals(TargetType.Char) || type.equals(TargetType.char_)) {
mv.visitMethodInsn(INVOKESTATIC, "java/lang/Char", "valueOf", "(C)Ljava/lang/Char;", false);
mv.visitMethodInsn(INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;", false);
}
}
@ -157,7 +157,7 @@ public class Codegen {
} else if (type.equals(TargetType.Short)) {
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S", false);
} else if (type.equals(TargetType.Char)) {
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Char", "charValue", "()C", false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C", false);
}
}

View File

@ -60,7 +60,7 @@ public class FCGenerator {
.collect(Collectors.toList()));
tl.add(m.getReturnType().acceptTV(new TypeExchanger(gtvs)));
return new Pair(new RefType(new JavaClassName("Fun" + (tl.size()-1) + "$$"), tl, new NullToken()),
fIType);
fIType, PairOperator.SMALLER);
}
return null; //kann nicht passieren, da die Methode nur aufgerufen wird wenn cl Functional Interface ist
}

View File

@ -361,7 +361,7 @@ public class ASTFactory {
} else if (type.getTypeName().equals("boolean")) {
return new RefType(new JavaClassName("java.lang.Boolean"), new ArrayList<>(), new NullToken(), true);
} else if (type.getTypeName().equals("char")) {
return new RefType(new JavaClassName("java.lang.Char"), new ArrayList<>(), new NullToken(), true);
return new RefType(new JavaClassName("java.lang.Character"), new ArrayList<>(), new NullToken(), true);
} else if (type.getTypeName().equals("short")) {
return new RefType(new JavaClassName("java.lang.Short"), new ArrayList<>(), new NullToken(), true);
} else if (type.getTypeName().equals("double")) {

View File

@ -1,6 +1,5 @@
package de.dhbwstuttgart.syntaxtree.factory;
import java.io.FileWriter;
import java.io.Writer;
import java.util.*;
import java.util.regex.Matcher;
@ -15,7 +14,6 @@ import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.syntaxtree.type.WildcardType;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.result.PairNoResult;
@ -24,6 +22,7 @@ import de.dhbwstuttgart.typeinference.result.PairTPHequalRefTypeOrWildcardType;
import de.dhbwstuttgart.typeinference.result.PairTPHsmallerTPH;
import de.dhbwstuttgart.typeinference.result.ResultPair;
import de.dhbwstuttgart.typeinference.unify.model.*;
import org.antlr.v4.runtime.Token;
public class UnifyTypeFactory {
@ -43,20 +42,20 @@ public class UnifyTypeFactory {
return new FiniteClosure(FCGenerator.toUnifyFC(fromClasses, classLoader), logFile);
}
public static UnifyPair generateSmallerPair(UnifyType tl, UnifyType tr){
return new UnifyPair(tl, tr, PairOperator.SMALLER);
public static UnifyPair generateSmallerPair(UnifyType tl, UnifyType tr, Token location){
return new UnifyPair(tl, tr, PairOperator.SMALLER, location);
}
public static UnifyPair generateSmallerDotPair(UnifyType tl, UnifyType tr){
return new UnifyPair(tl, tr, PairOperator.SMALLERDOT);
public static UnifyPair generateSmallerDotPair(UnifyType tl, UnifyType tr, Token location){
return new UnifyPair(tl, tr, PairOperator.SMALLERDOT, location);
}
public static UnifyPair generateSmallNotEqualDotPair(UnifyType tl, UnifyType tr){
return new UnifyPair(tl, tr, PairOperator.SMALLERNEQDOT);
public static UnifyPair generateSmallNotEqualDotPair(UnifyType tl, UnifyType tr, Token location){
return new UnifyPair(tl, tr, PairOperator.SMALLERNEQDOT, location);
}
public static UnifyPair generateEqualDotPair(UnifyType tl, UnifyType tr){
return new UnifyPair(tl, tr, PairOperator.EQUALSDOT);
public static UnifyPair generateEqualDotPair(UnifyType tl, UnifyType tr, Token location){
return new UnifyPair(tl, tr, PairOperator.EQUALSDOT, location);
}
/**
@ -164,19 +163,19 @@ public class UnifyTypeFactory {
UnifyPair ret = null;
if(p.GetOperator().equals(PairOperator.SMALLERDOT)) {
ret = generateSmallerDotPair(UnifyTypeFactory.convert(p.TA1, false)
, UnifyTypeFactory.convert(p.TA2, false));
, UnifyTypeFactory.convert(p.TA2, false), p.getLocation());
//return ret;
}else if(p.GetOperator().equals(PairOperator.SMALLERNEQDOT)) {
ret = generateSmallNotEqualDotPair(UnifyTypeFactory.convert(p.TA1, false)
, UnifyTypeFactory.convert(p.TA2, false));
, UnifyTypeFactory.convert(p.TA2, false), p.getLocation());
//return ret;
}else if(p.GetOperator().equals(PairOperator.EQUALSDOT)) {
ret = generateEqualDotPair(UnifyTypeFactory.convert(p.TA1, false)
, UnifyTypeFactory.convert(p.TA2, false));
, UnifyTypeFactory.convert(p.TA2, false), p.getLocation());
//return ret;
}else if(p.GetOperator().equals(PairOperator.SMALLER)){
ret = generateSmallerPair(UnifyTypeFactory.convert(p.TA1, false),
UnifyTypeFactory.convert(p.TA2, false));
UnifyTypeFactory.convert(p.TA2, false), p.getLocation());
}else throw new NotImplementedException();
UnifyType lhs, rhs;
if (((lhs = ret.getLhsType()) instanceof PlaceholderType)

View File

@ -3,9 +3,11 @@ import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import org.antlr.v4.runtime.Token;
public class Pair implements Serializable
@ -13,11 +15,13 @@ public class Pair implements Serializable
public final RefTypeOrTPHOrWildcardOrGeneric TA1;
public final RefTypeOrTPHOrWildcardOrGeneric TA2;
private Token location;
private PairOperator eOperator = PairOperator.SMALLER;
private Boolean noUnification = false;
public Pair(RefTypeOrTPHOrWildcardOrGeneric TA1, RefTypeOrTPHOrWildcardOrGeneric TA2 )
private Pair(RefTypeOrTPHOrWildcardOrGeneric TA1, RefTypeOrTPHOrWildcardOrGeneric TA2 )
{
this.TA1 = TA1;
this.TA2 = TA2;
@ -32,7 +36,11 @@ public class Pair implements Serializable
this(TA1,TA2);
this.eOperator = eOp;
}
public Pair(RefTypeOrTPHOrWildcardOrGeneric TA1, RefTypeOrTPHOrWildcardOrGeneric TA2, PairOperator e0p, Token location) {
this(TA1, TA2, e0p);
this.location = location;
}
public Pair(RefTypeOrTPHOrWildcardOrGeneric TA1, RefTypeOrTPHOrWildcardOrGeneric TA2, PairOperator eOp, Boolean noUnification)
{
@ -41,6 +49,10 @@ public class Pair implements Serializable
this.eOperator = eOp;
this.noUnification = noUnification;
}
public Token getLocation() {
return this.location;
}
public String toString()
{

View File

@ -71,8 +71,8 @@ public class TYPEStmt implements StatementVisitor {
// lambdaParams.add(0,tphRetType);
constraintsSet.addUndConstraint(new Pair(lambdaExpression.getType(), new RefType(new JavaClassName("Fun" + (lambdaParams.size() - 1) + "$$"), lambdaParams, new NullToken()),
// new FunN(lambdaParams),
PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(lambdaExpression.getReturnType(), tphRetType, PairOperator.EQUALSDOT));
PairOperator.EQUALSDOT, lambdaExpression.getOffset()));
constraintsSet.addUndConstraint(new Pair(lambdaExpression.getReturnType(), tphRetType, PairOperator.EQUALSDOT, lambdaExpression.getOffset()));
// Constraints des Bodys generieren:
TYPEStmt lambdaScope = new TYPEStmt(new TypeInferenceBlockInformation(info, lambdaExpression));
@ -84,7 +84,7 @@ public class TYPEStmt implements StatementVisitor {
public void visit(Assign assign) {
assign.lefSide.accept(this);
assign.rightSide.accept(this);
constraintsSet.addUndConstraint(new Pair(assign.rightSide.getType(), assign.lefSide.getType(), PairOperator.SMALLERDOT));
constraintsSet.addUndConstraint(new Pair(assign.rightSide.getType(), assign.lefSide.getType(), PairOperator.SMALLERDOT, assign.getOffset()));
}
@Override
@ -111,8 +111,8 @@ public class TYPEStmt implements StatementVisitor {
for (FieldAssumption fieldAssumption : info.getFields(fieldVar.fieldVarName)) {
Constraint constraint = new Constraint();
GenericsResolver resolver = getResolverInstance();
constraint.add(new Pair(fieldVar.receiver.getType(), fieldAssumption.getReceiverType(resolver), PairOperator.SMALLERDOT)); // PL 2019-12-09: SMALLERDOT eingefuegt, EQUALSDOT entfernt, wenn ds Field privat ist muesste es EQUALSDOT lauten
constraint.add(new Pair(fieldVar.getType(), fieldAssumption.getType(resolver), PairOperator.EQUALSDOT));
constraint.add(new Pair(fieldVar.receiver.getType(), fieldAssumption.getReceiverType(resolver), PairOperator.SMALLERDOT, fieldVar.getOffset())); // PL 2019-12-09: SMALLERDOT eingefuegt, EQUALSDOT entfernt, wenn ds Field privat ist muesste es EQUALSDOT lauten
constraint.add(new Pair(fieldVar.getType(), fieldAssumption.getType(resolver), PairOperator.EQUALSDOT, fieldVar.getOffset()));
oderConstraints.add(constraint);
}
if (oderConstraints.size() == 0)
@ -131,7 +131,7 @@ public class TYPEStmt implements StatementVisitor {
@Override
public void visit(ForEachStmt forEachStmt) {
var iterableType = new RefType(ASTFactory.createClass(java.lang.Iterable.class).getClassName(), Arrays.asList(forEachStmt.statement.getType()), new NullToken());
constraintsSet.addUndConstraint(new Pair(forEachStmt.expression.getType(), iterableType, PairOperator.SMALLERDOT));
constraintsSet.addUndConstraint(new Pair(forEachStmt.expression.getType(), iterableType, PairOperator.SMALLERDOT, forEachStmt.getOffset()));
forEachStmt.statement.accept(this);
forEachStmt.expression.accept(this);
forEachStmt.block.accept(this);
@ -143,14 +143,14 @@ public class TYPEStmt implements StatementVisitor {
// Expression inferieren:
ifStmt.expr.accept(this);
// Expression muss boolean sein:
constraintsSet.addUndConstraint(new Pair(ifStmt.expr.getType(), booleanType, PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(ifStmt.expr.getType(), booleanType, PairOperator.EQUALSDOT, ifStmt.getOffset()));
// Blöcke inferieren:
ifStmt.then_block.accept(this);
// Beide Blöcke müssen den gleichen Supertyp haben, welcher den Rückgabetyp des If-Stmts darstellt
//constraintsSet.addUndConstraint(new Pair(ifStmt.else_block.getType(), ifStmt.getType(), PairOperator.SMALLERDOT));
if (ifStmt.else_block != null) {
ifStmt.else_block.accept(this);
constraintsSet.addUndConstraint(new Pair(ifStmt.else_block.getType(), ifStmt.getType(), PairOperator.SMALLERDOT));
constraintsSet.addUndConstraint(new Pair(ifStmt.else_block.getType(), ifStmt.getType(), PairOperator.SMALLERDOT, ifStmt.getOffset()));
}
}
@ -226,15 +226,17 @@ public class TYPEStmt implements StatementVisitor {
private final RefType doublee = new RefType(ASTFactory.createClass(Double.class).getClassName(), new NullToken());
private final RefType string = new RefType(ASTFactory.createClass(String.class).getClassName(), new NullToken());
private final RefType bool = new RefType(ASTFactory.createClass(Boolean.class).getClassName(), new NullToken());
private final RefType charr = new RefType(ASTFactory.createClass(Character.class).getClassName(), new NullToken());
@Override
public void visit(UnaryExpr unaryExpr) {
if (unaryExpr.operation == UnaryExpr.Operation.POSTDECREMENT || unaryExpr.operation == UnaryExpr.Operation.POSTINCREMENT || unaryExpr.operation == UnaryExpr.Operation.PREDECREMENT || unaryExpr.operation == UnaryExpr.Operation.PREINCREMENT) {
// @see: https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.14.2
// Expression muss zu Numeric Convertierbar sein. also von Numeric erben
constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), number, PairOperator.SMALLERNEQDOT));
constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), number, PairOperator.SMALLERNEQDOT, unaryExpr.getOffset()));
// The type of the postfix increment expression is the type of the variable
constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), unaryExpr.getType(), PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), unaryExpr.getType(), PairOperator.EQUALSDOT, unaryExpr.getOffset()));
} else {
throw new NotImplementedException();
}
@ -261,49 +263,49 @@ public class TYPEStmt implements StatementVisitor {
// PL eingefuegt 2018-07-17
if (info.getAvailableClasses().stream().map(x -> x.getClassName()).collect(Collectors.toCollection(HashSet::new)).contains(bytee.getName())) {
numeric = new Constraint<>();
numeric.add(new Pair(binary.lexpr.getType(), bytee, PairOperator.SMALLERDOT));
numeric.add(new Pair(binary.rexpr.getType(), bytee, PairOperator.SMALLERDOT));
numeric.add(new Pair(binary.getType(), integer, PairOperator.EQUALSDOT));
numeric.add(new Pair(binary.lexpr.getType(), bytee, PairOperator.SMALLERDOT, binary.getOffset()));
numeric.add(new Pair(binary.rexpr.getType(), bytee, PairOperator.SMALLERDOT, binary.getOffset()));
numeric.add(new Pair(binary.getType(), integer, PairOperator.EQUALSDOT, binary.getOffset()));
numericAdditionOrStringConcatenation.add(numeric);
}
// PL eingefuegt 2018-07-17
if (info.getAvailableClasses().stream().map(x -> x.getClassName()).collect(Collectors.toCollection(HashSet::new)).contains(shortt.getName())) {
numeric = new Constraint<>();
numeric.add(new Pair(binary.lexpr.getType(), shortt, PairOperator.SMALLERDOT));
numeric.add(new Pair(binary.rexpr.getType(), shortt, PairOperator.SMALLERDOT));
numeric.add(new Pair(binary.getType(), integer, PairOperator.EQUALSDOT));
numeric.add(new Pair(binary.lexpr.getType(), shortt, PairOperator.SMALLERDOT, binary.getOffset()));
numeric.add(new Pair(binary.rexpr.getType(), shortt, PairOperator.SMALLERDOT, binary.getOffset()));
numeric.add(new Pair(binary.getType(), integer, PairOperator.EQUALSDOT, binary.getOffset()));
numericAdditionOrStringConcatenation.add(numeric);
}
// PL eingefuegt 2018-07-17
if (info.getAvailableClasses().stream().map(x -> x.getClassName()).collect(Collectors.toCollection(HashSet::new)).contains(integer.getName())) {
numeric = new Constraint<>();
numeric.add(new Pair(binary.lexpr.getType(), integer, PairOperator.SMALLERDOT));
numeric.add(new Pair(binary.rexpr.getType(), integer, PairOperator.SMALLERDOT));
numeric.add(new Pair(integer, binary.getType(), PairOperator.EQUALSDOT));
numeric.add(new Pair(binary.lexpr.getType(), integer, PairOperator.SMALLERDOT, binary.getOffset()));
numeric.add(new Pair(binary.rexpr.getType(), integer, PairOperator.SMALLERDOT, binary.getOffset()));
numeric.add(new Pair(integer, binary.getType(), PairOperator.EQUALSDOT, binary.getOffset()));
numericAdditionOrStringConcatenation.add(numeric);
}
// PL eingefuegt 2018-07-17
if (info.getAvailableClasses().stream().map(x -> x.getClassName()).collect(Collectors.toCollection(HashSet::new)).contains(longg.getName())) {
numeric = new Constraint<>();
numeric.add(new Pair(binary.lexpr.getType(), longg, PairOperator.SMALLERDOT));
numeric.add(new Pair(binary.rexpr.getType(), longg, PairOperator.SMALLERDOT));
numeric.add(new Pair(longg, binary.getType(), PairOperator.EQUALSDOT));
numeric.add(new Pair(binary.lexpr.getType(), longg, PairOperator.SMALLERDOT, binary.getOffset()));
numeric.add(new Pair(binary.rexpr.getType(), longg, PairOperator.SMALLERDOT, binary.getOffset()));
numeric.add(new Pair(longg, binary.getType(), PairOperator.EQUALSDOT, binary.getOffset()));
numericAdditionOrStringConcatenation.add(numeric);
}
// PL eingefuegt 2018-07-17
if (info.getAvailableClasses().stream().map(x -> x.getClassName()).collect(Collectors.toCollection(HashSet::new)).contains(floatt.getName())) {
numeric = new Constraint<>();
numeric.add(new Pair(binary.lexpr.getType(), floatt, PairOperator.SMALLERDOT));
numeric.add(new Pair(binary.rexpr.getType(), floatt, PairOperator.SMALLERDOT));
numeric.add(new Pair(floatt, binary.getType(), PairOperator.EQUALSDOT));
numeric.add(new Pair(binary.lexpr.getType(), floatt, PairOperator.SMALLERDOT, binary.getOffset()));
numeric.add(new Pair(binary.rexpr.getType(), floatt, PairOperator.SMALLERDOT, binary.getOffset()));
numeric.add(new Pair(floatt, binary.getType(), PairOperator.EQUALSDOT, binary.getOffset()));
numericAdditionOrStringConcatenation.add(numeric);
}
// PL eingefuegt 2018-07-17
if (info.getAvailableClasses().stream().map(x -> x.getClassName()).collect(Collectors.toCollection(HashSet::new)).contains(doublee.getName())) {
numeric = new Constraint<>();
numeric.add(new Pair(binary.lexpr.getType(), doublee, PairOperator.SMALLERDOT));
numeric.add(new Pair(binary.rexpr.getType(), doublee, PairOperator.SMALLERDOT));
numeric.add(new Pair(doublee, binary.getType(), PairOperator.EQUALSDOT));
numeric.add(new Pair(binary.lexpr.getType(), doublee, PairOperator.SMALLERDOT, binary.getOffset()));
numeric.add(new Pair(binary.rexpr.getType(), doublee, PairOperator.SMALLERDOT, binary.getOffset()));
numeric.add(new Pair(doublee, binary.getType(), PairOperator.EQUALSDOT, binary.getOffset()));
numericAdditionOrStringConcatenation.add(numeric);
}
/*
@ -316,9 +318,9 @@ public class TYPEStmt implements StatementVisitor {
// Dann kann der Ausdruck auch das aneinanderfügen zweier Strings sein: ("a" + "b") oder (1 + 2)
if (info.getAvailableClasses().stream().map(x -> x.getClassName()).collect(Collectors.toCollection(HashSet::new)).contains(string.getName())) {
Constraint<Pair> stringConcat = new Constraint<>();
stringConcat.add(new Pair(binary.lexpr.getType(), string, PairOperator.EQUALSDOT));
stringConcat.add(new Pair(binary.rexpr.getType(), string, PairOperator.EQUALSDOT));
stringConcat.add(new Pair(string, binary.getType(), PairOperator.EQUALSDOT));
stringConcat.add(new Pair(binary.lexpr.getType(), string, PairOperator.EQUALSDOT, binary.getOffset()));
stringConcat.add(new Pair(binary.rexpr.getType(), string, PairOperator.EQUALSDOT, binary.getOffset()));
stringConcat.add(new Pair(string, binary.getType(), PairOperator.EQUALSDOT, binary.getOffset()));
numericAdditionOrStringConcatenation.add(stringConcat);
}
}
@ -337,10 +339,10 @@ public class TYPEStmt implements StatementVisitor {
*/
// Testeise eingefuegt PL 2018-05-24
// Hier sollte evtl. noch importe angefragt werden PL 2019-05-07
constraintsSet.addUndConstraint(new Pair(binary.lexpr.getType(), number, PairOperator.SMALLERNEQDOT));
constraintsSet.addUndConstraint(new Pair(binary.rexpr.getType(), number, PairOperator.SMALLERNEQDOT));
constraintsSet.addUndConstraint(new Pair(binary.lexpr.getType(), number, PairOperator.SMALLERNEQDOT, binary.getOffset()));
constraintsSet.addUndConstraint(new Pair(binary.rexpr.getType(), number, PairOperator.SMALLERNEQDOT, binary.getOffset()));
// Rückgabetyp ist Boolean
constraintsSet.addUndConstraint(new Pair(bool, binary.getType(), PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(bool, binary.getType(), PairOperator.EQUALSDOT, binary.getOffset()));
// auskommentiert PL 2018-05-24
// constraintsSet.addUndConstraint(new Pair(binary.lexpr.getType(), number, PairOperator.SMALLERDOT));
@ -352,7 +354,7 @@ public class TYPEStmt implements StatementVisitor {
* Auszug aus https://docs.oracle.com/javase/specs/jls/se9/html/jls-15.html#jls-15.21 The equality operators may be used to compare two operands that are convertible (§5.1.8) to numeric type, or two operands of type boolean or Boolean, or two operands that are each of either reference type or the null type. All other cases result in a compile-time error.
*/
// Der Equals Operator geht mit fast allen Typen, daher werden hier keine Constraints gesetzt
constraintsSet.addUndConstraint(new Pair(bool, binary.getType(), PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(bool, binary.getType(), PairOperator.EQUALSDOT, binary.getOffset()));
} else {
throw new NotImplementedException();
}
@ -361,9 +363,9 @@ public class TYPEStmt implements StatementVisitor {
@Override
public void visit(BoolExpression expr) {
constraintsSet.addUndConstraint(new Pair(bool, expr.getType(), PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(bool, expr.lexpr.getType(), PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(bool, expr.rexpr.getType(), PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(bool, expr.getType(), PairOperator.EQUALSDOT, expr.getOffset()));
constraintsSet.addUndConstraint(new Pair(bool, expr.lexpr.getType(), PairOperator.EQUALSDOT, expr.getOffset()));
constraintsSet.addUndConstraint(new Pair(bool, expr.rexpr.getType(), PairOperator.EQUALSDOT, expr.getOffset()));
// TODO
return;
@ -376,23 +378,23 @@ public class TYPEStmt implements StatementVisitor {
// wie hier fuer double gezeigt. Im Momment auskommentiert, weil zu wenige Literaltypen
// funktionieren
if (literal.value instanceof Short) {
constraintsSet.addUndConstraint(new Pair(literal.getType(), shortt, PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(literal.getType(), shortt, PairOperator.EQUALSDOT, literal.getOffset()));
return;
}
if (literal.value instanceof Byte) {
constraintsSet.addUndConstraint(new Pair(literal.getType(), bytee, PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(literal.getType(), bytee, PairOperator.EQUALSDOT, literal.getOffset()));
return;
}
if (literal.value instanceof Float) {
constraintsSet.addUndConstraint(new Pair(literal.getType(), floatt, PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(literal.getType(), floatt, PairOperator.EQUALSDOT, literal.getOffset()));
return;
}
if (literal.value instanceof Double) {
constraintsSet.addUndConstraint(new Pair(literal.getType(), doublee, PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(literal.getType(), doublee, PairOperator.EQUALSDOT, literal.getOffset()));
return;
}
if (literal.value instanceof Long) {
constraintsSet.addUndConstraint(new Pair(literal.getType(), longg, PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(literal.getType(), longg, PairOperator.EQUALSDOT, literal.getOffset()));
return;
}
if (literal.value instanceof Integer) {
@ -401,31 +403,31 @@ public class TYPEStmt implements StatementVisitor {
HashSet<JavaClassName> clNames = info.getAvailableClasses().stream().map(x -> x.getClassName()).collect(Collectors.toCollection(HashSet::new));
Set<Constraint> oderConstraints = new HashSet<>();
Constraint constraint = new Constraint();
constraint.add(new Pair(literal.getType(), integer, PairOperator.EQUALSDOT));
constraint.add(new Pair(literal.getType(), integer, PairOperator.EQUALSDOT, literal.getOffset()));
oderConstraints.add(constraint);
if (clNames.stream().filter(x -> x.toString().equals("java.lang.Double")).findAny().isPresent()) {
constraint = new Constraint();
constraint.add(new Pair(literal.getType(), doublee, PairOperator.EQUALSDOT));
constraint.add(new Pair(literal.getType(), doublee, PairOperator.EQUALSDOT, literal.getOffset()));
oderConstraints.add(constraint);
}
if (clNames.stream().filter(x -> x.toString().equals("java.lang.Long")).findAny().isPresent()) {
constraint = new Constraint();
constraint.add(new Pair(literal.getType(), longg, PairOperator.EQUALSDOT));
constraint.add(new Pair(literal.getType(), longg, PairOperator.EQUALSDOT, literal.getOffset()));
oderConstraints.add(constraint);
}
if (clNames.stream().filter(x -> x.toString().equals("java.lang.Float")).findAny().isPresent()) {
constraint = new Constraint();
constraint.add(new Pair(literal.getType(), floatt, PairOperator.EQUALSDOT));
constraint.add(new Pair(literal.getType(), floatt, PairOperator.EQUALSDOT, literal.getOffset()));
oderConstraints.add(constraint);
}
if (clNames.stream().filter(x -> x.toString().equals("java.lang.Short")).findAny().isPresent()) {
constraint = new Constraint();
constraint.add(new Pair(literal.getType(), shortt, PairOperator.EQUALSDOT));
constraint.add(new Pair(literal.getType(), shortt, PairOperator.EQUALSDOT, literal.getOffset()));
oderConstraints.add(constraint);
}
if (clNames.stream().filter(x -> x.toString().equals("java.lang.Byte")).findAny().isPresent()) {
constraint = new Constraint();
constraint.add(new Pair(literal.getType(), bytee, PairOperator.EQUALSDOT));
constraint.add(new Pair(literal.getType(), bytee, PairOperator.EQUALSDOT, literal.getOffset()));
oderConstraints.add(constraint);
}
constraintsSet.addOderConstraint(oderConstraints);
@ -433,23 +435,27 @@ public class TYPEStmt implements StatementVisitor {
return;
}
if (literal.value instanceof Short) {
constraintsSet.addUndConstraint(new Pair(literal.getType(), shortt, PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(literal.getType(), shortt, PairOperator.EQUALSDOT, literal.getOffset()));
return;
}
if (literal.value instanceof Byte) {
constraintsSet.addUndConstraint(new Pair(literal.getType(), bytee, PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(literal.getType(), bytee, PairOperator.EQUALSDOT, literal.getOffset()));
return;
}
if (literal.value instanceof Float) {
constraintsSet.addUndConstraint(new Pair(literal.getType(), floatt, PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(literal.getType(), floatt, PairOperator.EQUALSDOT, literal.getOffset()));
return;
}
if (literal.value instanceof String) {
constraintsSet.addUndConstraint(new Pair(literal.getType(), string, PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(literal.getType(), string, PairOperator.EQUALSDOT, literal.getOffset()));
return;
}
if (literal.value instanceof Boolean) {
constraintsSet.addUndConstraint(new Pair(literal.getType(), bool, PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(literal.getType(), bool, PairOperator.EQUALSDOT, literal.getOffset()));
return;
}
if (literal.value instanceof Character) {
constraintsSet.addUndConstraint(new Pair(literal.getType(), charr, PairOperator.EQUALSDOT, literal.getOffset()));
return;
}
if (literal.value != null) {
@ -465,7 +471,7 @@ public class TYPEStmt implements StatementVisitor {
@Override
public void visit(Return returnExpr) {
returnExpr.retexpr.accept(this);
constraintsSet.addUndConstraint(new Pair(returnExpr.getType(), info.getCurrentTypeScope().getReturnType(), PairOperator.SMALLERDOT));
constraintsSet.addUndConstraint(new Pair(returnExpr.getType(), info.getCurrentTypeScope().getReturnType(), PairOperator.SMALLERDOT, returnExpr.getOffset()));
}
@Override
@ -497,7 +503,7 @@ public class TYPEStmt implements StatementVisitor {
params.add(new GenericRefType(gtv.getName(), aThis.getOffset()));
}
RefType thisType = new RefType(currentClass.getClassName(), params, aThis.getOffset());
constraintsSet.addUndConstraint(new Pair(aThis.getType(), thisType, PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(aThis.getType(), thisType, PairOperator.EQUALSDOT, aThis.getOffset()));
}
private static TypeScope createNullTypeScope() {
@ -520,7 +526,7 @@ public class TYPEStmt implements StatementVisitor {
// Expression inferieren:
whileStmt.expr.accept(this);
// Expression muss boolean sein:
constraintsSet.addUndConstraint(new Pair(whileStmt.expr.getType(), booleanType, PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(whileStmt.expr.getType(), booleanType, PairOperator.EQUALSDOT, whileStmt.expr.getOffset()));
// LoopBlock inferieren:
whileStmt.loopBlock.accept(this);
}
@ -580,10 +586,10 @@ public class TYPEStmt implements StatementVisitor {
RefTypeOrTPHOrWildcardOrGeneric receiverType = assumption.getReceiverType(resolver);
if (receiverType != null) {
methodConstraint.add(new Pair(forMethod.receiver.getType(), receiverType, PairOperator.EQUALSDOT));// PL 2020-03-17 SMALLERDOT in EQUALSDOT umgewandelt, weil alle geerbten Methoden in den jeweilen Klassen enthalten sind.
methodConstraint.add(new Pair(forMethod.receiver.getType(), receiverType, PairOperator.EQUALSDOT, forMethod.getOffset()));// PL 2020-03-17 SMALLERDOT in EQUALSDOT umgewandelt, weil alle geerbten Methoden in den jeweilen Klassen enthalten sind.
// PL 2023-01-24: dafuer ? extends receiverType noch ergaenzt
extendsMethodConstraint.add(new Pair(forMethod.receiver.getType(), new ExtendsWildcardType(receiverType, receiverType.getOffset()), PairOperator.EQUALSDOT));
extendsMethodConstraint.add(new Pair(forMethod.receiver.getType(), new ExtendsWildcardType(receiverType, receiverType.getOffset()), PairOperator.EQUALSDOT, forMethod.getOffset()));
}
// gegenseite Verschraenkung der beiden Mengen von Typannahmen
@ -595,8 +601,8 @@ public class TYPEStmt implements StatementVisitor {
// PairOperator.EQUALSDOT));
// Fuer Bytecodegenerierung PL 2020-03-09 wird derzeit nicht benutzt ENDE
methodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.SMALLERDOT));
extendsMethodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.SMALLERDOT));
methodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.SMALLERDOT, forMethod.getOffset()));
extendsMethodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.SMALLERDOT, forMethod.getOffset()));
// methodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.EQUALSDOT));
// extendsMethodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.EQUALSDOT));
@ -629,7 +635,7 @@ public class TYPEStmt implements StatementVisitor {
RefTypeOrTPHOrWildcardOrGeneric argType = foMethod.arglist.getArguments().get(i).getType();
RefTypeOrTPHOrWildcardOrGeneric assType = assumption.getArgTypes(resolver).get(i);
ret.add(new Pair(argType, assType, PairOperator.SMALLERDOT));
ret.add(new Pair(argType, assType, PairOperator.SMALLERDOT, foMethod.getOffset()));
// Fuer Bytecodegenerierung PL 2020-03-09 wird derzeit nicht benutzt ANFANG
// ret.add(new Pair(foMethod.argTypes.get(i), assType, PairOperator.EQUALSDOT));
@ -723,7 +729,7 @@ public class TYPEStmt implements StatementVisitor {
Constraint methodConstraint = new Constraint();
// WELCHEN SINN MACHT DIESER CONSTRAINT???
// Ist er nicht immer classname <. classname und damit redundant?
methodConstraint.add(new Pair(assumption.getReturnType(resolver), forConstructor.getType(), PairOperator.SMALLERDOT));
methodConstraint.add(new Pair(assumption.getReturnType(resolver), forConstructor.getType(), PairOperator.SMALLERDOT, forConstructor.getOffset()));
// WELCHEN SINN MACHT DIESER CONSTRAINT???
methodConstraint.addAll(generateParameterConstraints(forConstructor, assumption, info, resolver));
return methodConstraint;
@ -747,17 +753,17 @@ public class TYPEStmt implements StatementVisitor {
for (var child : switchStmt.getBlocks()) {
for (var label : child.getLabels()) {
if (label.getPattern() instanceof FormalParameter) {
constraintsSet.addUndConstraint(new Pair(label.getPattern().getType(), switchStmt.getSwitch().getType(), PairOperator.SMALLERDOT));
constraintsSet.addUndConstraint(new Pair(label.getPattern().getType(), switchStmt.getSwitch().getType(), PairOperator.SMALLERDOT, label.getOffset()));
}
}
}
} else {
constraintsSet.addUndConstraint(new Pair(caseExpressionType, switchStmt.getSwitch().getType(), PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(caseExpressionType, switchStmt.getSwitch().getType(), PairOperator.EQUALSDOT, switchStmt.getSwitch().getOffset()));
}
for (var child : switchStmt.getBlocks()) {
child.accept(this);
constraintsSet.addUndConstraint(new Pair(child.getType(), switchStmt.getType(), PairOperator.SMALLERDOT));
constraintsSet.addUndConstraint(new Pair(child.getType(), switchStmt.getType(), PairOperator.SMALLERDOT, switchStmt.getOffset()));
}
switchStack.pop();
@ -778,7 +784,7 @@ public class TYPEStmt implements StatementVisitor {
@Override
public void visit(Yield aYield) {
aYield.retexpr.accept(this);
constraintsSet.addUndConstraint(new Pair(aYield.getType(), switchStack.peek().getType(), PairOperator.EQUALSDOT));
constraintsSet.addUndConstraint(new Pair(aYield.getType(), switchStack.peek().getType(), PairOperator.EQUALSDOT, aYield.getOffset()));
// TODO Auto-generated method stub
}
}

View File

@ -439,7 +439,7 @@ public class RuleSet implements IRuleSet{
if(!(rhsType instanceof ReferenceType) && !(rhsType instanceof PlaceholderType))
return false;
return fc.greater(lhsType, new HashSet<>()).contains(rhsType);
return fc.greater(lhsType, new HashSet<>(), pair.getLocation()).contains(rhsType);
}
@Override

View File

@ -10,6 +10,7 @@ import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typeinference.unify.model.ReferenceType;
import de.dhbwstuttgart.typeinference.unify.model.SuperType;
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
import org.antlr.v4.runtime.Token;
/**
*
@ -22,13 +23,19 @@ public interface IFiniteClosure {
* Returns all types of the finite closure that are subtypes of the argument.
* @return The set of subtypes of the argument.
*/
public Set<UnifyType> smaller(UnifyType type, Set<UnifyType> fBounded);
public Set<UnifyType> smaller(UnifyType type, Set<UnifyType> fBounded, Token position);
public default Set<UnifyType> smaller(UnifyType type, Set<UnifyType> fBounded) {
return this.smaller(type, fBounded, null);
}
/**
* Returns all types of the finite closure that are supertypes of the argument.
* @return The set of supertypes of the argument.
*/
public Set<UnifyType> greater(UnifyType type, Set<UnifyType> fBounded);
public Set<UnifyType> greater(UnifyType type, Set<UnifyType> fBounded, Token position);
public default Set<UnifyType> greater(UnifyType type, Set<UnifyType> fBounded) {
return this.greater(type, fBounded, null);
}
/**
* Wo passt Type rein?

View File

@ -27,6 +27,7 @@ import de.dhbwstuttgart.typeinference.unify.TypeUnifyTask;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify;
import de.dhbwstuttgart.util.Pair;
import org.antlr.v4.runtime.Token;
/**
* The finite closure for the type unification
@ -145,7 +146,7 @@ implements IFiniteClosure {
* @return The set of subtypes of the argument.
*/
@Override
public Set<UnifyType> smaller(UnifyType type, Set<UnifyType> fBounded) {
public Set<UnifyType> smaller(UnifyType type, Set<UnifyType> fBounded, Token position) {
Set<UnifyType> ret;
if ((ret = smallerHash.get(new hashKeyType(type))) != null) {
@ -260,8 +261,7 @@ implements IFiniteClosure {
*/
@Override
//Eingefuegt PL 2018-05-24 F-Bounded Problematik
public Set<UnifyType> greater(UnifyType type, Set<UnifyType> fBounded) {
public Set<UnifyType> greater(UnifyType type, Set<UnifyType> fBounded, Token location) {
Set<UnifyType> ret;
if ((ret = greaterHash.get(new hashKeyType(type))) != null) {
//System.out.println(greaterHash);
@ -302,7 +302,7 @@ implements IFiniteClosure {
//PL 18-04-05 Unifier durch Matcher ersetzt ANFANG
ArrayList<UnifyPair> termList= new ArrayList<UnifyPair>();
termList.add(new UnifyPair(theta1,type, PairOperator.EQUALSDOT));
termList.add(new UnifyPair(theta1,type, PairOperator.EQUALSDOT, location));
Optional<Unifier> optSigma = match.match(termList);
//PL 18-04-05 Unifier durch Matcher ersetzt ENDE
if(!optSigma.isPresent()) {
@ -344,7 +344,7 @@ implements IFiniteClosure {
BiFunction<Boolean,UnifyType,Boolean> f = (x,y) ->
{
ArrayList<UnifyPair> termList = new ArrayList<UnifyPair>();
termList.add(new UnifyPair(y,t.getTypeParams().get(i_ef), PairOperator.EQUALSDOT));
termList.add(new UnifyPair(y,t.getTypeParams().get(i_ef), PairOperator.EQUALSDOT, location));
return ((match.match(termList).isPresent()) || x);
};
//if (parai.getName().equals("java.lang.Integer")) {

View File

@ -102,7 +102,7 @@ public class Unifier implements Function<UnifyType, UnifyType>, Iterable<Entry<P
}
return new UnifyPair(newLhs, newRhs, p.getPairOp(), suniUnifyPair, p);
}
return new UnifyPair(newLhs, newRhs, p.getPairOp(), p.getSubstitution(), p.getBasePair(), p.getfBounded());
return new UnifyPair(newLhs, newRhs, p.getPairOp(), p.getSubstitution(), p.getBasePair(), p.getfBounded(), p.getLocation());
}
/**

View File

@ -1,5 +1,9 @@
package de.dhbwstuttgart.typeinference.unify.model;
import com.google.common.collect.ObjectArrays;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import org.antlr.v4.runtime.Token;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
@ -13,6 +17,8 @@ import java.util.Set;
* @author Florian Steurer
*/
public class UnifyPair {
private Token location;
/**
* The type on the left hand side of the pair.
@ -63,27 +69,41 @@ public class UnifyPair {
this.rhs = rhs;
pairOp = op;
substitution = new HashSet<>();
// Caching hashcode
hashCode = 17 + 31 * lhs.hashCode() + 31 * rhs.hashCode() + 31 * pairOp.hashCode();
}
}
public UnifyPair(UnifyType lhs, UnifyType rhs, PairOperator op, Token location) {
this(lhs, rhs, op);
this.location = location;
}
public UnifyPair(UnifyType lhs, UnifyType rhs, PairOperator op, Set<UnifyPair> uni, UnifyPair base) {
this(lhs, rhs, op, uni, base, null);
}
public UnifyPair(UnifyType lhs, UnifyType rhs, PairOperator op, Set<UnifyPair> uni, UnifyPair base, Set<UnifyType> fBounded) {
this(lhs, rhs, op, uni, base, fBounded, null);
}
public UnifyPair(UnifyType lhs, UnifyType rhs, PairOperator op, Set<UnifyPair> uni, UnifyPair base, Set<UnifyType> fBounded, Token location) {
this.lhs = lhs;
this.rhs = rhs;
pairOp = op;
substitution = uni;
basePair = base;
this.location = location;
this.fBounded = fBounded;
// Caching hashcode
hashCode = 17 + 31 * lhs.hashCode() + 31 * rhs.hashCode() + 31 * pairOp.hashCode();
}
public UnifyPair(UnifyType lhs, UnifyType rhs, PairOperator op, Set<UnifyPair> uni, UnifyPair base, Set<UnifyType> fBounded) {
this(lhs, rhs, op, uni, base);
this.fBounded = fBounded;
}
public Token getLocation() {
if (location != null) return location;
else if (basePair != null) return basePair.getLocation();
return null;
}
/**
@ -231,7 +251,13 @@ public class UnifyPair {
+ "WC: " + ((PlaceholderType)rhs).isWildcardable()
+ ", IT: " + ((PlaceholderType)rhs).isInnerType();
}
return "(" + lhs + " " + pairOp + " " + rhs + ", " + ret + ")"; //+ ", [" + getfBounded().toString()+ "])";
var res = "(" + lhs + " " + pairOp + " " + rhs + ", " + ret + ")"; //+ ", [" + getfBounded().toString()+ "])";
var location = this.getLocation();
if (location != null) {
res += "@" + location.getLine();
}
return res;
}
/*

View File

@ -824,6 +824,7 @@ public class TestComplete {
var instance = clazz.getDeclaredConstructor().newInstance();
assertNull(clazz.getDeclaredMethod("m").invoke(instance));
assertEquals(clazz.getDeclaredMethod("m2").invoke(instance), 'C');
}
@Test