From e8140b31607c41260d3e682fd75c4cf8f7f4ecb7 Mon Sep 17 00:00:00 2001 From: luca9913 Date: Sat, 15 Jul 2023 21:41:50 +0200 Subject: [PATCH] Added method stubs in visitors for new AST nodes --- .../target/generate/GenerateGenerics.java | 187 ++++++++++++------ .../generate/StatementToTargetExpression.java | 70 +++++++ .../typeinference/assumptions/FunNClass.java | 13 +- .../typeinference/typeAlgo/TYPEStmt.java | 108 ++++++++-- 4 files changed, 298 insertions(+), 80 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/target/generate/GenerateGenerics.java b/src/main/java/de/dhbwstuttgart/target/generate/GenerateGenerics.java index 53143235..b18bc663 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/GenerateGenerics.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/GenerateGenerics.java @@ -35,8 +35,10 @@ public abstract class GenerateGenerics { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; TPH tph = (TPH) o; return Objects.equals(resolve(), tph.resolve()); } @@ -72,8 +74,10 @@ public abstract class GenerateGenerics { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; PairLT pairLT = (PairLT) o; return Objects.equals(right, pairLT.right) && Objects.equals(left, pairLT.left); } @@ -103,8 +107,10 @@ public abstract class GenerateGenerics { } public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; PairEQ pairLT = (PairEQ) o; return Objects.equals(right, pairLT.right) && Objects.equals(left, pairLT.left); } @@ -238,12 +244,7 @@ public abstract class GenerateGenerics { return all; } - private void methodFindConstraints( - ClassOrInterface owner, Method method, - Set typeVariables, - Set typeVariablesOfClass, - Set result - ) { + private void methodFindConstraints(ClassOrInterface owner, Method method, Set typeVariables, Set typeVariablesOfClass, Set result) { var userDefinedGenericsOfClass = astToTargetAST.userDefinedGenerics.get(owner); // Type variables with bounds that are also type variables of the method @@ -263,24 +264,15 @@ public abstract class GenerateGenerics { @Override public void visit(MethodCall methodCall) { - //Anfang es werden Paare von TPHs gespeichert, die bei den Generated Generics ueber die Methodengrenzen hinweg - //betrachtet werden muessen - //Definition 7.2 (Family of generated generics). T1 <. R1 <.^∗ R2 <. T2 - Set T1s = - methodCall.getArgumentList() - .getArguments() - .stream() - .map(TypableStatement::getType) - .collect(Collectors.toCollection(HashSet::new)) - .stream().filter(TypePlaceholder.class::isInstance) - .map(TypePlaceholder.class::cast) - .map(TPH::new) - .collect(Collectors.toCollection(HashSet::new)); + // Anfang es werden Paare von TPHs gespeichert, die bei den Generated Generics ueber die Methodengrenzen hinweg + // betrachtet werden muessen + // Definition 7.2 (Family of generated generics). T1 <. R1 <.^∗ R2 <. T2 + Set T1s = methodCall.getArgumentList().getArguments().stream().map(TypableStatement::getType).collect(Collectors.toCollection(HashSet::new)).stream().filter(TypePlaceholder.class::isInstance).map(TypePlaceholder.class::cast).map(TPH::new).collect(Collectors.toCollection(HashSet::new)); Set T2s = new HashSet<>(); findTphs(superType, T2s); System.out.println("T1s: " + T1s + " T2s: " + T2s); - //Ende + // Ende superType = methodCall.receiverType; methodCall.receiver.accept(this); @@ -292,7 +284,8 @@ public abstract class GenerateGenerics { if (methodCall.receiver instanceof ExpressionReceiver expressionReceiver) { if (expressionReceiver.expr instanceof This) { var optMethod = astToTargetAST.findMethod(owner, methodCall.name, methodCall.getArgumentList()); - if (optMethod.isEmpty()) return; + if (optMethod.isEmpty()) + return; var method2 = optMethod.get(); System.out.println("In: " + method.getName() + " Method: " + method2.getName()); var generics = family(owner, method2); @@ -309,9 +302,9 @@ public abstract class GenerateGenerics { HashSet newPairs = new HashSet<>(); // Loop from hell - outer: - for (var R1 : typeVariables) { - if (typeVariablesOfClass.contains(R1)) continue; + outer: for (var R1 : typeVariables) { + if (typeVariablesOfClass.contains(R1)) + continue; for (var generic : all) if (generic instanceof PairLT ptph) { for (var pair : simplifiedConstraints) { @@ -323,8 +316,10 @@ public abstract class GenerateGenerics { if (!(pair2.right.equals(R2) && pair2.left.equals(ptph.right))) continue; - if (R1.equals(R2)) continue; - if (!T1s.contains(R1) || !T2s.contains(R2)) continue; + if (R1.equals(R2)) + continue; + if (!T1s.contains(R1) || !T2s.contains(R2)) + continue; var newPair = new PairLT(R1, R2); System.out.println("New pair: " + newPair); @@ -402,16 +397,53 @@ public abstract class GenerateGenerics { arglist.getArguments().get(i).accept(this); } } + + @Override + public void visit(Switch switchStmt) { + // TODO Auto-generated method stub + } + + @Override + public void visit(SwitchBlock switchBlock) { + // TODO Auto-generated method stub + } + + @Override + public void visit(SwitchLabel switchLabel) { + // TODO Auto-generated method stub + } + + @Override + public void visit(Yield aYield) { + // TODO Auto-generated method stub + } + + @Override + public void visit(Pattern aPattern) { + // TODO Auto-generated method stub + } + + @Override + public void visit(RecordPattern aRecordPattern) { + // TODO Auto-generated method stub + } + + @Override + public void visit(GuardedPattern aGuardedPattern) { + // TODO Auto-generated method stub + } }); var closure = transitiveClosure(simplifiedConstraints); // Type variables with bounds that are also type variables of the class for (var typeVariable : new HashSet<>(typeVariables)) { - if (typeVariablesOfClass.contains(typeVariable)) continue; + if (typeVariablesOfClass.contains(typeVariable)) + continue; var pairs = new HashSet(); for (var pair : closure) { - if (!(pair instanceof PairLT ptph)) continue; + if (!(pair instanceof PairLT ptph)) + continue; if (ptph.left.equals(typeVariable) && typeVariablesOfClass.contains(ptph.right)) { pairs.add(new PairLT(ptph.left, ptph.right)); } @@ -435,7 +467,8 @@ public abstract class GenerateGenerics { break; } } - if (!found) break; + if (!found) + break; } if (steps < minSteps) { minSteps = steps; @@ -448,8 +481,7 @@ public abstract class GenerateGenerics { } // All unbounded type variables (bounds not in method) - outer: - for (var typeVariable : typeVariables) { + outer: for (var typeVariable : typeVariables) { if (classHasGeneric(userDefinedGenericsOfClass, typeVariablesOfClass, typeVariable)) continue; for (var pair : result) { @@ -460,8 +492,7 @@ public abstract class GenerateGenerics { } // All unbounded bounds - outer: - for (var pair : simplifiedConstraints) { + outer: for (var pair : simplifiedConstraints) { for (var pair2 : simplifiedConstraints) { if (pair.right.equals(pair2.left)) continue outer; @@ -476,10 +507,7 @@ public abstract class GenerateGenerics { return typeVariablesOfClass.contains(typeVariable) || userDefinedGenericsOfClass.stream().anyMatch(g -> g.getName().equals(typeVariable.resolve().getName())); } - private void methodFindTypeVariables( - Method method, - Set typeVariables - ) { + private void methodFindTypeVariables(Method method, Set typeVariables) { if (!(method instanceof Constructor)) typeVariables.addAll(findTypeVariables(method.getReturnType())); @@ -498,6 +526,41 @@ public abstract class GenerateGenerics { super.visit(methodCall); typeVariables.addAll(findTypeVariables(methodCall.getType())); } + + @Override + public void visit(Switch switchStmt) { + // TODO Auto-generated method stub + } + + @Override + public void visit(SwitchBlock switchBlock) { + // TODO Auto-generated method stub + } + + @Override + public void visit(SwitchLabel switchLabel) { + // TODO Auto-generated method stub + } + + @Override + public void visit(Yield aYield) { + // TODO Auto-generated method stub + } + + @Override + public void visit(Pattern aPattern) { + // TODO Auto-generated method stub + } + + @Override + public void visit(RecordPattern aRecordPattern) { + // TODO Auto-generated method stub + } + + @Override + public void visit(GuardedPattern aGuardedPattern) { + // TODO Auto-generated method stub + } }); } @@ -567,7 +630,8 @@ public abstract class GenerateGenerics { private void eliminateChain(Set result, List chain) { for (var pair : new HashSet<>(result)) { if (pair instanceof PairLT ptph && chain.get(chain.size() - 1).equals(ptph.left)) { - if (chain.contains(ptph.right)) return; + if (chain.contains(ptph.right)) + return; var copy = new ArrayList<>(chain); copy.add(ptph.right); if (copy.size() > 2) @@ -644,12 +708,12 @@ public abstract class GenerateGenerics { } void normalize(Set result, Set classGenerics, Set usedTphs) { - outer: - for (var tph : usedTphs) { + outer: for (var tph : usedTphs) { for (var p1 : new HashSet<>(result)) { if (p1 instanceof PairLT ptph && ptph.left.equals(ptph.right)) result.remove(p1); // TODO This is a bit strange - if (p1.left.equals(tph)) continue outer; + if (p1.left.equals(tph)) + continue outer; } if (classGenerics == null || classGenerics.stream().noneMatch((pair) -> pair.left.equals(tph))) @@ -664,22 +728,24 @@ public abstract class GenerateGenerics { chain.add(ptph.left); chain.add(ptph.right); - outer: - while (true) { + outer: while (true) { var added = false; for (var pair2 : input) { if (pair2 instanceof PairLT ptph2 && ptph2.left.equals(chain.get(chain.size() - 1))) { - if (chain.contains(ptph2.right)) break outer; + if (chain.contains(ptph2.right)) + break outer; chain.add(ptph2.right); added = true; } } - if (!added) break; + if (!added) + break; } System.out.println(chain + " " + chain.stream().map(e -> e.resolve().getVariance()).toList()); var variance = chain.get(0).resolve().getVariance(); - if (variance != 1) continue; + if (variance != 1) + continue; var index = 0; for (var tph : chain) { if (variance == 1 && tph.resolve().getVariance() == -1) { @@ -690,14 +756,15 @@ public abstract class GenerateGenerics { } index++; } - if (variance == 1) continue; - + if (variance == 1) + continue; var start = chain.get(0); var prev = start; for (var i = 1; i < index; i++) { var cur = chain.get(i); - if (!referenced.contains(cur)) continue; + if (!referenced.contains(cur)) + continue; addToEquality(cur.resolve(), start.resolve(), referenced); TPH finalPrev = prev; input.removeIf(p -> p.equals(new PairLT(finalPrev, cur))); @@ -750,7 +817,7 @@ public abstract class GenerateGenerics { for (var method : classOrInterface.getMethods()) { family(classOrInterface, method); } - } while(!oldFamily.equals(familyOfMethods)); + } while (!oldFamily.equals(familyOfMethods)); for (var method : classOrInterface.getMethods()) { generics(classOrInterface, method); @@ -766,7 +833,8 @@ public abstract class GenerateGenerics { var foundNext = false; for (var pair : input) { if (pair instanceof PairLT ptph && ptph.left.equals(end)) { - if (chain.contains(ptph.right)) return; + if (chain.contains(ptph.right)) + return; chain = new HashSet<>(chain); chain.add(ptph.right); findChain(referenced, input, output, start, ptph.right, chain); @@ -851,7 +919,7 @@ public abstract class GenerateGenerics { } newTph.setVariance(variance); - //referenced.add(newTph); + // referenced.add(newTph); addToPairs(input, new PairLT(left, new TPH(newTph))); input.removeAll(infima); for (var infimum : infima) { @@ -893,7 +961,8 @@ public abstract class GenerateGenerics { return getTargetType(equality.get(tph)); } var type = concreteTypes.get(new TPH(tph)); - if (type == null) return new TargetGenericType(tph.getName()); + if (type == null) + return new TargetGenericType(tph.getName()); return astToTargetAST.convert(type, this); } return astToTargetAST.convert(in, this); diff --git a/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java index f6ebc6ff..2e2b7b09 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java @@ -74,6 +74,41 @@ public class StatementToTargetExpression implements StatementVisitor { @Override public void visit(LambdaExpression lambda) { } // Don't look at lambda expressions + + @Override + public void visit(Switch switchStmt) { + // TODO Auto-generated method stub + } + + @Override + public void visit(SwitchBlock switchBlock) { + // TODO Auto-generated method stub + } + + @Override + public void visit(SwitchLabel switchLabel) { + // TODO Auto-generated method stub + } + + @Override + public void visit(Yield aYield) { + // TODO Auto-generated method stub + } + + @Override + public void visit(Pattern aPattern) { + // TODO Auto-generated method stub + } + + @Override + public void visit(RecordPattern aRecordPattern) { + // TODO Auto-generated method stub + } + + @Override + public void visit(GuardedPattern aGuardedPattern) { + // TODO Auto-generated method stub + } }); result = new TargetLambdaExpression(converter.convert(lambdaExpression.getType()), captures, parameters, converter.convert(lambdaExpression.getReturnType()), converter.convert(lambdaExpression.methodBody)); @@ -340,4 +375,39 @@ public class StatementToTargetExpression implements StatementVisitor { result = new TargetLiteral.BooleanLiteral((boolean) literal.value); } } + + @Override + public void visit(Switch switchStmt) { + // TODO Auto-generated method stub + } + + @Override + public void visit(SwitchBlock switchBlock) { + // TODO Auto-generated method stub + } + + @Override + public void visit(SwitchLabel switchLabel) { + // TODO Auto-generated method stub + } + + @Override + public void visit(Yield aYield) { + // TODO Auto-generated method stub + } + + @Override + public void visit(Pattern aPattern) { + // TODO Auto-generated method stub + } + + @Override + public void visit(RecordPattern aRecordPattern) { + // TODO Auto-generated method stub + } + + @Override + public void visit(GuardedPattern aGuardedPattern) { + // TODO Auto-generated method stub + } } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java b/src/main/java/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java index d41baa06..cf8fa897 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java @@ -21,18 +21,15 @@ import java.util.Optional; public class FunNClass extends ClassOrInterface { public FunNClass(List funNParams) { - super(0, new JavaClassName("Fun"+(funNParams.size()-1)), new ArrayList<>(), Optional.empty() /* eingefuegt PL 2018-11-24 */, - createMethods(funNParams), new ArrayList<>(), createGenerics(funNParams), - ASTFactory.createObjectType(), true, new ArrayList<>(), new NullToken()); + super(0, new JavaClassName("Fun" + (funNParams.size() - 1)), new ArrayList<>(), Optional.empty() /* eingefuegt PL 2018-11-24 */, createMethods(funNParams), new ArrayList<>(), createGenerics(funNParams), ASTFactory.createObjectType(), true, new ArrayList<>(), new ArrayList<>(), new NullToken()); - - } + } private static GenericDeclarationList createGenerics(List funNParams) { - //PL 2018-06-22: so geaendert, dass generierte Generics den Namen der funParams entsprechen. + // PL 2018-06-22: so geaendert, dass generierte Generics den Namen der funParams entsprechen. List generics = new ArrayList<>(); - for(GenericRefType param : funNParams){ - generics.add(new GenericTypeVar(param.getParsedName(),//NameGenerator.makeNewName(), + for (GenericRefType param : funNParams) { + generics.add(new GenericTypeVar(param.getParsedName(), // NameGenerator.makeNewName(), new ArrayList<>(), new NullToken(), new NullToken())); } return new GenericDeclarationList(generics, new NullToken()); diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index be27249d..e54a7f77 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -1,31 +1,78 @@ //PL 2018-12-19: Merge chekcen package de.dhbwstuttgart.typeinference.typeAlgo; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.parser.scope.JavaClassName; -import de.dhbwstuttgart.syntaxtree.*; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.FormalParameter; +import de.dhbwstuttgart.syntaxtree.GenericTypeVar; +import de.dhbwstuttgart.syntaxtree.Method; +import de.dhbwstuttgart.syntaxtree.ParameterList; +import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.TypeScope; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; -import de.dhbwstuttgart.syntaxtree.statement.*; -import de.dhbwstuttgart.syntaxtree.type.*; -import de.dhbwstuttgart.syntaxtree.type.Void; +import de.dhbwstuttgart.syntaxtree.statement.ArgumentList; +import de.dhbwstuttgart.syntaxtree.statement.Assign; +import de.dhbwstuttgart.syntaxtree.statement.AssignToField; +import de.dhbwstuttgart.syntaxtree.statement.BinaryExpr; +import de.dhbwstuttgart.syntaxtree.statement.Block; +import de.dhbwstuttgart.syntaxtree.statement.BoolExpression; +import de.dhbwstuttgart.syntaxtree.statement.Break; +import de.dhbwstuttgart.syntaxtree.statement.CastExpr; +import de.dhbwstuttgart.syntaxtree.statement.DoStmt; +import de.dhbwstuttgart.syntaxtree.statement.EmptyStmt; +import de.dhbwstuttgart.syntaxtree.statement.ExpressionReceiver; +import de.dhbwstuttgart.syntaxtree.statement.FieldVar; +import de.dhbwstuttgart.syntaxtree.statement.ForStmt; +import de.dhbwstuttgart.syntaxtree.statement.GuardedPattern; +import de.dhbwstuttgart.syntaxtree.statement.IfStmt; +import de.dhbwstuttgart.syntaxtree.statement.InstanceOf; +import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; +import de.dhbwstuttgart.syntaxtree.statement.Literal; +import de.dhbwstuttgart.syntaxtree.statement.LocalVar; +import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl; +import de.dhbwstuttgart.syntaxtree.statement.MethodCall; +import de.dhbwstuttgart.syntaxtree.statement.NewArray; +import de.dhbwstuttgart.syntaxtree.statement.NewClass; +import de.dhbwstuttgart.syntaxtree.statement.Pattern; +import de.dhbwstuttgart.syntaxtree.statement.RecordPattern; +import de.dhbwstuttgart.syntaxtree.statement.Return; +import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid; +import de.dhbwstuttgart.syntaxtree.statement.Statement; +import de.dhbwstuttgart.syntaxtree.statement.StaticClassName; +import de.dhbwstuttgart.syntaxtree.statement.Super; +import de.dhbwstuttgart.syntaxtree.statement.SuperCall; +import de.dhbwstuttgart.syntaxtree.statement.Switch; +import de.dhbwstuttgart.syntaxtree.statement.SwitchBlock; +import de.dhbwstuttgart.syntaxtree.statement.SwitchLabel; +import de.dhbwstuttgart.syntaxtree.statement.This; +import de.dhbwstuttgart.syntaxtree.statement.UnaryExpr; +import de.dhbwstuttgart.syntaxtree.statement.WhileStmt; +import de.dhbwstuttgart.syntaxtree.statement.Yield; +import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; +import de.dhbwstuttgart.syntaxtree.type.GenericRefType; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.assumptions.FieldAssumption; import de.dhbwstuttgart.typeinference.assumptions.FunNClass; import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; -import de.dhbwstuttgart.typeinference.constraints.*; -import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; +import de.dhbwstuttgart.typeinference.constraints.Constraint; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.GenericsResolver; +import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; -import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; -import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; -import de.dhbwstuttgart.util.BiRelation; - -import java.util.*; -import java.util.stream.Collectors; -import java.util.stream.Stream; public class TYPEStmt implements StatementVisitor { @@ -686,4 +733,39 @@ public class TYPEStmt implements StatementVisitor { return methodConstraint; } + @Override + public void visit(Switch switchStmt) { + // TODO Auto-generated method stub + } + + @Override + public void visit(SwitchBlock switchBlock) { + // TODO Auto-generated method stub + } + + @Override + public void visit(SwitchLabel switchLabel) { + // TODO Auto-generated method stub + } + + @Override + public void visit(Yield aYield) { + // TODO Auto-generated method stub + } + + @Override + public void visit(Pattern aPattern) { + // TODO Auto-generated method stub + } + + @Override + public void visit(RecordPattern aRecordPattern) { + // TODO Auto-generated method stub + } + + @Override + public void visit(GuardedPattern aGuardedPattern) { + // TODO Auto-generated method stub + } + }