From 427a87598c14661d9b8cc4df9c1c0aac0a10a5b9 Mon Sep 17 00:00:00 2001 From: Aldaron7 Date: Thu, 17 May 2018 21:35:14 +0200 Subject: [PATCH] solve added --- .../SyntaxTreeGenerator/AssignToLocal.java | 6 + .../strucTypes/ASTReturnVisitor.java | 138 +++++++ .../ClassOrInterfaceWithConstraints.java | 50 +++ ...lassOrInterfaceWithConstraintsFactory.java | 82 ++++ .../dhbwstuttgart/strucTypes/Construct.java | 16 +- .../strucTypes/InferTypesVisitor.java | 368 ++++++++++++++++++ .../strucTypes/InferredTypes.java | 4 + src/de/dhbwstuttgart/strucTypes/Solve.java | 102 +++++ .../strucTypes/StrucTypeUnifyUtils.java | 29 ++ src/de/dhbwstuttgart/strucTypes/TypeVar.java | 31 ++ .../strucTypes/constraint/ConstraintsSet.java | 22 +- .../InconsistentConstraintsException.java | 28 ++ .../printutils/PrintConstraints.java | 9 +- .../printutils/SyntaxTreePrinter.java | 1 + .../syntaxtree/ClassOrInterface.java | 12 +- .../dhbwstuttgart/syntaxtree/Constructor.java | 6 + src/de/dhbwstuttgart/syntaxtree/Field.java | 6 + .../syntaxtree/FormalParameter.java | 6 + .../syntaxtree/GenericDeclarationList.java | 7 + .../syntaxtree/GenericTypeVar.java | 6 + src/de/dhbwstuttgart/syntaxtree/Method.java | 6 + .../syntaxtree/ParameterList.java | 7 + .../dhbwstuttgart/syntaxtree/SourceFile.java | 6 + .../syntaxtree/SyntaxTreeNode.java | 3 + .../syntaxtree/statement/ArgumentList.java | 6 + .../syntaxtree/statement/Assign.java | 6 + .../syntaxtree/statement/AssignToField.java | 6 + .../syntaxtree/statement/BinaryExpr.java | 6 + .../syntaxtree/statement/Block.java | 6 + .../syntaxtree/statement/CastExpr.java | 6 + .../syntaxtree/statement/DoStmt.java | 6 + .../syntaxtree/statement/EmptyStmt.java | 6 + .../statement/ExpressionReceiver.java | 6 + .../syntaxtree/statement/FieldVar.java | 6 + .../syntaxtree/statement/ForStmt.java | 6 + .../syntaxtree/statement/IfStmt.java | 6 + .../syntaxtree/statement/InstanceOf.java | 6 + .../statement/LambdaExpression.java | 6 + .../syntaxtree/statement/Literal.java | 6 + .../syntaxtree/statement/LocalVar.java | 6 + .../syntaxtree/statement/LocalVarDecl.java | 6 + .../syntaxtree/statement/MethodCall.java | 6 + .../syntaxtree/statement/NewArray.java | 6 + .../syntaxtree/statement/NewClass.java | 6 + .../syntaxtree/statement/Return.java | 6 + .../syntaxtree/statement/ReturnVoid.java | 6 + .../syntaxtree/statement/StaticClassName.java | 6 + .../syntaxtree/statement/Super.java | 6 + .../syntaxtree/statement/SuperCall.java | 6 + .../syntaxtree/statement/This.java | 6 + .../syntaxtree/statement/UnaryExpr.java | 6 + .../syntaxtree/statement/WhileStmt.java | 42 +- .../syntaxtree/type/ExtendsWildcardType.java | 6 + .../syntaxtree/type/GenericRefType.java | 6 + .../syntaxtree/type/RefType.java | 6 + .../syntaxtree/type/SuperWildcardType.java | 6 + .../syntaxtree/type/TypePlaceholder.java | 6 + .../syntaxtree/visual/OutputGenerator.java | 2 +- test/strucType/TestConstruct.java | 6 +- test/strucType/TestInferTypesVisitor.java | 102 +++++ test/strucType/TestPaperExample.java | 89 +++++ test/strucType/TestSolve.java | 88 +++++ test/strucType/constructed/A.java | 12 + test/strucType/constructed/CO.java | 6 + test/strucType/constructed/DE.java | 7 + test/strucType/constructed/DG.java | 8 + test/strucType/constructed/EK.java | 7 + test/strucType/constructed/FF.java | 7 + test/strucType/constructed/FI.java | 7 + test/strucType/constructed/L.java | 7 + test/strucType/constructed/O.java | 7 + test/strucType/javFiles/testMain.jav | 8 + .../strucType/typedtestclasses/MyInteger.java | 24 ++ 73 files changed, 1520 insertions(+), 52 deletions(-) create mode 100644 src/de/dhbwstuttgart/strucTypes/ASTReturnVisitor.java create mode 100644 src/de/dhbwstuttgart/strucTypes/ClassOrInterfaceWithConstraints.java create mode 100644 src/de/dhbwstuttgart/strucTypes/ClassOrInterfaceWithConstraintsFactory.java create mode 100644 src/de/dhbwstuttgart/strucTypes/InferTypesVisitor.java create mode 100644 src/de/dhbwstuttgart/strucTypes/Solve.java create mode 100644 src/de/dhbwstuttgart/strucTypes/TypeVar.java create mode 100644 src/de/dhbwstuttgart/strucTypes/exception/InconsistentConstraintsException.java create mode 100644 test/strucType/TestInferTypesVisitor.java create mode 100644 test/strucType/TestPaperExample.java create mode 100644 test/strucType/TestSolve.java create mode 100644 test/strucType/constructed/A.java create mode 100644 test/strucType/constructed/CO.java create mode 100644 test/strucType/constructed/DE.java create mode 100644 test/strucType/constructed/DG.java create mode 100644 test/strucType/constructed/EK.java create mode 100644 test/strucType/constructed/FF.java create mode 100644 test/strucType/constructed/FI.java create mode 100644 test/strucType/constructed/L.java create mode 100644 test/strucType/constructed/O.java create mode 100644 test/strucType/javFiles/testMain.jav create mode 100644 test/strucType/typedtestclasses/MyInteger.java diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/AssignToLocal.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/AssignToLocal.java index c505bfb6..eae9ee90 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/AssignToLocal.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/AssignToLocal.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.statement.AssignLeftSide; import de.dhbwstuttgart.syntaxtree.statement.Expression; @@ -17,4 +18,9 @@ public class AssignToLocal extends AssignLeftSide { public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public AssignToLocal accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/strucTypes/ASTReturnVisitor.java b/src/de/dhbwstuttgart/strucTypes/ASTReturnVisitor.java new file mode 100644 index 00000000..d097b1ff --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/ASTReturnVisitor.java @@ -0,0 +1,138 @@ +package de.dhbwstuttgart.strucTypes; + +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.Constructor; +import de.dhbwstuttgart.syntaxtree.Field; +import de.dhbwstuttgart.syntaxtree.FormalParameter; +import de.dhbwstuttgart.syntaxtree.GenericDeclarationList; +import de.dhbwstuttgart.syntaxtree.GenericTypeVar; +import de.dhbwstuttgart.syntaxtree.Method; +import de.dhbwstuttgart.syntaxtree.ParameterList; +import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; +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.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.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.Return; +import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid; +import de.dhbwstuttgart.syntaxtree.statement.StaticClassName; +import de.dhbwstuttgart.syntaxtree.statement.Super; +import de.dhbwstuttgart.syntaxtree.statement.SuperCall; +import de.dhbwstuttgart.syntaxtree.statement.This; +import de.dhbwstuttgart.syntaxtree.statement.UnaryExpr; +import de.dhbwstuttgart.syntaxtree.statement.WhileStmt; +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.SuperWildcardType; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; + +public interface ASTReturnVisitor { + + // ASTVisitor + SourceFile visit(SourceFile sourceFile); + + GenericTypeVar visit(GenericTypeVar genericTypeVar); + + FormalParameter visit(FormalParameter formalParameter); + + GenericDeclarationList visit(GenericDeclarationList genericTypeVars); + + Field visit(Field field); + + Method visit(Method method); + + Constructor visit(Constructor constructor); + + ParameterList visit(ParameterList formalParameters); + + ClassOrInterface visit(ClassOrInterface classOrInterface); + + RefTypeOrTPHOrWildcardOrGeneric visit(RefType refType); + + RefTypeOrTPHOrWildcardOrGeneric visit(SuperWildcardType superWildcardType); + + RefTypeOrTPHOrWildcardOrGeneric visit(TypePlaceholder typePlaceholder); + + RefTypeOrTPHOrWildcardOrGeneric visit(ExtendsWildcardType extendsWildcardType); + + RefTypeOrTPHOrWildcardOrGeneric visit(GenericRefType genericRefType); + //StatementVisitor + ArgumentList visit(ArgumentList argumentList); + + LambdaExpression visit(LambdaExpression lambdaExpression); + + Assign visit(Assign assign); + + BinaryExpr visit(BinaryExpr binary); + + Block visit(Block block); + + CastExpr visit(CastExpr castExpr); + + EmptyStmt visit(EmptyStmt emptyStmt); + + FieldVar visit(FieldVar fieldVar); + + ForStmt visit(ForStmt forStmt); + + IfStmt visit(IfStmt ifStmt); + + InstanceOf visit(InstanceOf instanceOf); + + LocalVar visit(LocalVar localVar); + + LocalVarDecl visit(LocalVarDecl localVarDecl); + + MethodCall visit(MethodCall methodCall); + + NewClass visit(NewClass newClass); + + NewArray visit(NewArray newArray); + + Return visit(Return aReturn); + + ReturnVoid visit(ReturnVoid aReturn); + + StaticClassName visit(StaticClassName staticClassName); + + Super visit(Super aSuper); + + This visit(This aThis); + + WhileStmt visit(WhileStmt whileStmt); + + DoStmt visit(DoStmt doStmt); + + AssignToField visit(AssignToField assignLeftSide); + + AssignToLocal visit(AssignToLocal assignLeftSide); + + SuperCall visit(SuperCall superCall); + + ExpressionReceiver visit(ExpressionReceiver expressionReceiver); + + UnaryExpr visit(UnaryExpr unaryExpr); + + Literal visit(Literal literal); + +} diff --git a/src/de/dhbwstuttgart/strucTypes/ClassOrInterfaceWithConstraints.java b/src/de/dhbwstuttgart/strucTypes/ClassOrInterfaceWithConstraints.java new file mode 100644 index 00000000..fb875ee7 --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/ClassOrInterfaceWithConstraints.java @@ -0,0 +1,50 @@ +package de.dhbwstuttgart.strucTypes; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.antlr.v4.runtime.Token; + +import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.Constructor; +import de.dhbwstuttgart.syntaxtree.Field; +import de.dhbwstuttgart.syntaxtree.GenericDeclarationList; +import de.dhbwstuttgart.syntaxtree.Method; +import de.dhbwstuttgart.syntaxtree.type.RefType; + +public class ClassOrInterfaceWithConstraints extends ClassOrInterface { + + private Set constraints = new HashSet<>(); + + public ClassOrInterfaceWithConstraints(int modifiers, JavaClassName name, List fielddecl, + List methods, List constructors, GenericDeclarationList genericClassParameters, + RefType superClass, Boolean isInterface, List implementedInterfaces, Token offset, + Set constraints) { + super(modifiers, name, fielddecl, methods, constructors, genericClassParameters, superClass, isInterface, + implementedInterfaces, offset); + this.constraints = constraints; + } + + public ClassOrInterfaceWithConstraints(ClassOrInterface classOrInterface, Set constraints) { + this(classOrInterface.getModifiers(), classOrInterface.getClassName(), classOrInterface.getFieldDecl(), + classOrInterface.getMethods(), classOrInterface.getConstructors(), classOrInterface.getGenerics(), + classOrInterface.getSuperClass(), classOrInterface.isInterface, classOrInterface.getSuperInterfaces(), + classOrInterface.getOffset(), constraints); + } + + public ClassOrInterfaceWithConstraints(ClassOrInterface classOrInterface, GenericDeclarationList generics, + Set constraints) { + this(classOrInterface.getModifiers(), classOrInterface.getClassName(), classOrInterface.getFieldDecl(), + classOrInterface.getMethods(), classOrInterface.getConstructors(), generics, + classOrInterface.getSuperClass(), classOrInterface.isInterface, classOrInterface.getSuperInterfaces(), + classOrInterface.getOffset(), constraints); + } + + public Set getConstraints() { + return this.constraints; + } + +} diff --git a/src/de/dhbwstuttgart/strucTypes/ClassOrInterfaceWithConstraintsFactory.java b/src/de/dhbwstuttgart/strucTypes/ClassOrInterfaceWithConstraintsFactory.java new file mode 100644 index 00000000..e9c78348 --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/ClassOrInterfaceWithConstraintsFactory.java @@ -0,0 +1,82 @@ +package de.dhbwstuttgart.strucTypes; + +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import org.antlr.v4.runtime.Token; + +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.Constructor; +import de.dhbwstuttgart.syntaxtree.Field; +import de.dhbwstuttgart.syntaxtree.GenericDeclarationList; +import de.dhbwstuttgart.syntaxtree.GenericTypeVar; +import de.dhbwstuttgart.syntaxtree.Method; +import de.dhbwstuttgart.syntaxtree.ParameterList; +import de.dhbwstuttgart.syntaxtree.statement.Block; +import de.dhbwstuttgart.syntaxtree.type.RefType; + +public class ClassOrInterfaceWithConstraintsFactory { + + public ClassOrInterfaceWithConstraintsFactory() { + } + + public static ClassOrInterfaceWithConstraints inferTypes(ClassOrInterface cls, Set constraints, InferredTypes inferredTypes) { + int modifiers = Modifier.PUBLIC; + JavaClassName name = cls.getClassName(); + List fielddecl = inferFields(cls.getFieldDecl(), inferredTypes); + List methods = inferMethods(cls.getMethods(), inferredTypes); + List constructors = inferConstructors(cls.getConstructors(), inferredTypes); + Boolean isInterface = false; + List implementedInterfaces = inferInterfaces((List) cls.getSuperInterfaces(), inferredTypes); + RefType superClass = inferSuperClass(cls.getSuperClass(), inferredTypes); + Token offset = cls.getOffset(); + GenericDeclarationList genericClassParameters = inferGenerics(cls.getGenerics(), inferredTypes); + return new ClassOrInterfaceWithConstraints(modifiers, name, fielddecl, methods, constructors, + genericClassParameters, superClass, isInterface, implementedInterfaces, offset, constraints); + } + + public static List inferFields(List fields, InferredTypes inferredTypes){ + List result = new ArrayList<>(); + for (Field field : fields) { + result.add(new Field(field.getName(), inferredTypes.infer(field.getType()), field.modifier, field.getOffset())); + } + return result; + } + + public static List inferMethods(List methods, InferredTypes inferredTypes){ + List result = new ArrayList<>(); + for (Method method : methods) { + ParameterList parameterList = null; + Block block = null; + GenericDeclarationList gtvDeclarations = null; + result.add(new Method(method.modifier, method.name, inferredTypes.infer(method.getReturnType()), parameterList, block, gtvDeclarations, method.getOffset())); + } + return result; + } + + public static List inferConstructors(List constructors, InferredTypes inferredTypes){ + List result = new ArrayList<>(); + return result; + } + + public static List inferInterfaces(List interfaces, InferredTypes inferredTypes){ + List result = new ArrayList<>(); + return result; + } + + public static RefType inferSuperClass(RefType superclass, InferredTypes inferredTypes){ + return null; + } + + public static GenericDeclarationList inferGenerics(GenericDeclarationList generics, InferredTypes inferredTypes){ + List values = new ArrayList<>(); + return new GenericDeclarationList(values, new NullToken()); + } + + +} diff --git a/src/de/dhbwstuttgart/strucTypes/Construct.java b/src/de/dhbwstuttgart/strucTypes/Construct.java index 6e8565a4..ba4fa547 100644 --- a/src/de/dhbwstuttgart/strucTypes/Construct.java +++ b/src/de/dhbwstuttgart/strucTypes/Construct.java @@ -5,7 +5,6 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; -import java.util.stream.Collectors; import org.antlr.v4.runtime.Token; @@ -28,11 +27,10 @@ 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.unify.model.UnifyPair; public class Construct extends DefaultASTVisitor { - private List subTypeConstraints = new ArrayList<>(); + private Set subTypeConstraints = new HashSet<>(); private ConstraintsSet constraintsSet = new ConstraintsSet(); private Set newInterf = new HashSet<>(); private InferredTypes inferredTypes = new InferredTypes(); @@ -56,13 +54,13 @@ public class Construct extends DefaultASTVisitor { return inferredTypes; } - public List getSubTypeConstraints() { + public Set getSubTypeConstraints() { return subTypeConstraints; } - public Set getSubTypeConstraintsAsUnifyPairs() { - return subTypeConstraints.stream().map(SubTypeConstraint::getAsUnifyPair).collect(Collectors.toSet()); - } +// public Set getSubTypeConstraintsAsUnifyPairs() { +// return subTypeConstraints.stream().map(SubTypeConstraint::getAsUnifyPair).collect(Collectors.toSet()); +// } private void gatherSubTypeConstraints(ConstraintsSet constraintsSet) { this.subTypeConstraints.addAll(constraintsSet.getSubTypeConstraints()); @@ -75,7 +73,7 @@ public class Construct extends DefaultASTVisitor { @Override public void visit(TypePlaceholder typePlaceholder) { - JavaClassName name = new JavaClassName("constructedinterface." + typePlaceholder.getName()); + JavaClassName name = new JavaClassName( typePlaceholder.getName()); this.constructedInterfaces.add(this.constructInterface(typePlaceholder, name)); } @@ -116,7 +114,7 @@ public class Construct extends DefaultASTVisitor { // Typ für argument: GenericRefType TypePlaceholder tph = TypePlaceholder.fresh(offset); GenericRefType grt = new GenericRefType(tph.getName(), offset); - params.add(new FormalParameter(tph.getName(), grt, offset)); + params.add(new FormalParameter("arg" + tph.getName(), grt, offset)); parameterInhTyterm.add(supertype); generics.add(new GenericTypeVar(tph.getName(), new ArrayList<>(), offset, offset)); }); diff --git a/src/de/dhbwstuttgart/strucTypes/InferTypesVisitor.java b/src/de/dhbwstuttgart/strucTypes/InferTypesVisitor.java new file mode 100644 index 00000000..3f57115f --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/InferTypesVisitor.java @@ -0,0 +1,368 @@ +package de.dhbwstuttgart.strucTypes; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +import org.antlr.v4.runtime.Token; + +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; +import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.Constructor; +import de.dhbwstuttgart.syntaxtree.Field; +import de.dhbwstuttgart.syntaxtree.FormalParameter; +import de.dhbwstuttgart.syntaxtree.GenericDeclarationList; +import de.dhbwstuttgart.syntaxtree.GenericTypeVar; +import de.dhbwstuttgart.syntaxtree.Method; +import de.dhbwstuttgart.syntaxtree.ParameterList; +import de.dhbwstuttgart.syntaxtree.SourceFile; +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.CastExpr; +import de.dhbwstuttgart.syntaxtree.statement.DoStmt; +import de.dhbwstuttgart.syntaxtree.statement.EmptyStmt; +import de.dhbwstuttgart.syntaxtree.statement.Expression; +import de.dhbwstuttgart.syntaxtree.statement.ExpressionReceiver; +import de.dhbwstuttgart.syntaxtree.statement.FieldVar; +import de.dhbwstuttgart.syntaxtree.statement.ForStmt; +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.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.This; +import de.dhbwstuttgart.syntaxtree.statement.UnaryExpr; +import de.dhbwstuttgart.syntaxtree.statement.WhileStmt; +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.SuperWildcardType; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; + +public class InferTypesVisitor implements ASTReturnVisitor { + + private InferredTypes inferredTypes = new InferredTypes(); + + public InferTypesVisitor(InferredTypes inferredTypes) { + this.inferredTypes = inferredTypes; + } + + @Override + public SourceFile visit(SourceFile sourceFile) { + return new SourceFile(sourceFile.getPkgName(), + sourceFile.getClasses().stream().map(c -> c.accept(this)).collect(Collectors.toList()), + sourceFile.getImports()); + + } + + @Override + public Constructor visit(Constructor constructor) { + int modifier = constructor.modifier; + String name = constructor.getName(); + RefTypeOrTPHOrWildcardOrGeneric returnType = constructor.getReturnType().accept(this); + ParameterList parameterList = constructor.getParameterList().accept(this); + Block codeInsideConstructor = constructor.block.accept(this); + GenericDeclarationList gtvDeclarations = ((GenericDeclarationList) constructor.getGenerics()).accept(this); + Token offset = constructor.getOffset(); + List fieldInitializations = new ArrayList<>(); + return new Constructor(modifier, name, returnType, parameterList, codeInsideConstructor, gtvDeclarations, + offset, fieldInitializations); + + } + + @Override + public GenericTypeVar visit(GenericTypeVar genericTypeVar) { + return new GenericTypeVar( + genericTypeVar.getName(), genericTypeVar.getBounds().stream() + .map(b -> (RefTypeOrTPHOrWildcardOrGeneric) b.accept(this)).collect(Collectors.toList()), + genericTypeVar.getOffset(), new NullToken()); + } + + @Override + public FormalParameter visit(FormalParameter formalParameter) { + return new FormalParameter(formalParameter.getName(), formalParameter.getType().accept(this), + formalParameter.getOffset()); + } + + @Override + public GenericDeclarationList visit(GenericDeclarationList genericTypeVars) { + List values = new ArrayList<>(); + genericTypeVars.forEach(gtv -> values.add(gtv.accept(this))); + return new GenericDeclarationList(values, genericTypeVars.getOffset()); + + } + + @Override + public Field visit(Field field) { + return new Field(field.getName(), field.getType().accept(this), field.modifier, field.getOffset()); + + } + + @Override + public Method visit(Method method) { + int modifier = method.modifier; + String name = method.getName(); + RefTypeOrTPHOrWildcardOrGeneric returnType = method.getReturnType().accept(this); + ParameterList parameterList = method.getParameterList().accept(this); + Block block = method.block.accept(this); + GenericDeclarationList gtvDeclarations = ((GenericDeclarationList) method.getGenerics()).accept(this); + Token offset = method.getOffset(); + return new Method(modifier, name, returnType, parameterList, block, gtvDeclarations, offset); + + } + + @Override + public ParameterList visit(ParameterList formalParameters) { + for (FormalParameter formalParameter : formalParameters) { + formalParameter.accept(this); + } + return new ParameterList( + formalParameters.getFormalparalist().stream().map(p -> p.accept(this)).collect(Collectors.toList()), + formalParameters.getOffset()); + } + + @Override + public ClassOrInterface visit(ClassOrInterface classOrInterface) { + int modifiers = classOrInterface.getModifiers(); + JavaClassName name = classOrInterface.getClassName(); + List fielddecl = classOrInterface.getFieldDecl().stream().map(f -> f.accept(this)) + .collect(Collectors.toList()); + List methods = classOrInterface.getMethods().stream().map(m -> m.accept(this)) + .collect(Collectors.toList()); + List constructors = classOrInterface.getConstructors().stream() + .map(c -> (Constructor) c.accept(this)).collect(Collectors.toList()); + GenericDeclarationList genericClassParameters = classOrInterface.getGenerics().accept(this); + RefType superClass = (RefType) classOrInterface.getSuperClass().accept(this); + Boolean isInterface = classOrInterface.isInterface; + List implementedInterfaces = classOrInterface.getSuperInterfaces().stream() + .map(i -> (RefType) i.accept(this)).collect(Collectors.toList()); + Token offset = classOrInterface.getOffset(); + return new ClassOrInterface(modifiers, name, fielddecl, methods, constructors, genericClassParameters, + superClass, isInterface, implementedInterfaces, offset); + } + + @Override + public RefType visit(RefType refType) { + + return refType.getParaList() == null ? refType + : new RefType(refType.getName(), refType.getParaList().stream() + .map(p -> (RefTypeOrTPHOrWildcardOrGeneric) p.accept(this)).collect(Collectors.toList()), + refType.getOffset()); + } + + @Override + public SuperWildcardType visit(SuperWildcardType superWildcardType) { + throw new NotImplementedException(); + + } + + @Override + public RefTypeOrTPHOrWildcardOrGeneric visit(TypePlaceholder typePlaceholder) { + return inferredTypes.infer(typePlaceholder); + } + + @Override + public ExtendsWildcardType visit(ExtendsWildcardType extendsWildcardType) { + throw new NotImplementedException(); + + } + + @Override + public GenericRefType visit(GenericRefType genericRefType) { + return genericRefType; + } + + @Override + public ArgumentList visit(ArgumentList argumentList) { + return new ArgumentList(argumentList.getArguments().stream().map(expr -> (Expression) expr.accept(this)) + .collect(Collectors.toList()), argumentList.getOffset()); + } + + @Override + public LambdaExpression visit(LambdaExpression lambdaExpression) { + throw new NotImplementedException(); + + } + + @Override + public Assign visit(Assign assign) { + return assign; + } + + @Override + public BinaryExpr visit(BinaryExpr binary) { + throw new NotImplementedException(); + + } + + @Override + public Block visit(Block block) { + return new Block( + block.getStatements().stream().map(s -> (Statement) s.accept(this)).collect(Collectors.toList()), + block.getOffset()); + } + + @Override + public CastExpr visit(CastExpr castExpr) { + return new CastExpr(castExpr.getType().accept(this), castExpr.expr == null ? null : castExpr.expr.accept(this), + castExpr.getOffset()); + } + + @Override + public EmptyStmt visit(EmptyStmt emptyStmt) { + return emptyStmt; + } + + @Override + public FieldVar visit(FieldVar fieldVar) { + return new FieldVar(fieldVar.receiver == null ? null : fieldVar.receiver.accept(this), fieldVar.fieldVarName, + fieldVar.getType().accept(this), fieldVar.getOffset()); + } + + @Override + public ForStmt visit(ForStmt forStmt) { + throw new NotImplementedException(); + + } + + @Override + public IfStmt visit(IfStmt ifStmt) { + throw new NotImplementedException(); + + } + + @Override + public InstanceOf visit(InstanceOf instanceOf) { + throw new NotImplementedException(); + + } + + @Override + public LocalVar visit(LocalVar localVar) { + return new LocalVar(localVar.name, localVar.getType().accept(this), localVar.getOffset()); + } + + @Override + public LocalVarDecl visit(LocalVarDecl localVarDecl) { + throw new NotImplementedException(); + + } + + @Override + public MethodCall visit(MethodCall methodCall) { + methodCall.getType().accept(this); + methodCall.getArgumentList().accept(this); + return new MethodCall(methodCall.getType().accept(this), + methodCall.receiver == null ? null : methodCall.receiver.accept(this), methodCall.name, + methodCall.getArgumentList().accept(this), methodCall.getOffset()); + + } + + @Override + public NewClass visit(NewClass newClass) { + return new NewClass(newClass.getType().accept(this), newClass.getArgumentList().accept(this), + newClass.getOffset()); + } + + @Override + public NewArray visit(NewArray newArray) { + throw new NotImplementedException(); + + } + + @Override + public Return visit(Return aReturn) { + return new Return(aReturn.retexpr == null ? null : aReturn.retexpr.accept(this), aReturn.getOffset()); + } + + @Override + public ReturnVoid visit(ReturnVoid aReturn) { + return aReturn; + + } + + @Override + public StaticClassName visit(StaticClassName staticClassName) { + throw new NotImplementedException(); + + } + + @Override + public Super visit(Super aSuper) { + throw new NotImplementedException(); + + } + + @Override + public This visit(This aThis) { + return aThis; + } + + @Override + public WhileStmt visit(WhileStmt whileStmt) { + throw new NotImplementedException(); + + } + + @Override + public DoStmt visit(DoStmt whileStmt) { + throw new NotImplementedException(); + + } + + @Override + public AssignToField visit(AssignToField assignLeftSide) { + throw new NotImplementedException(); + + } + + @Override + public AssignToLocal visit(AssignToLocal assignLeftSide) { + throw new NotImplementedException(); + + } + + @Override + public SuperCall visit(SuperCall superCall) { + if(superCall.getArgumentList().getArguments() == null){ + return superCall; + } + return new SuperCall(superCall.getArgumentList().accept(this), superCall.getOffset()); + + } + + @Override + public ExpressionReceiver visit(ExpressionReceiver expressionReceiver) { + return new ExpressionReceiver(expressionReceiver.expr == null ? null : expressionReceiver.expr.accept(this)); + } + + @Override + public UnaryExpr visit(UnaryExpr unaryExpr) { + throw new NotImplementedException(); + + } + + @Override + public Literal visit(Literal literal) { + throw new NotImplementedException(); + + } + +} diff --git a/src/de/dhbwstuttgart/strucTypes/InferredTypes.java b/src/de/dhbwstuttgart/strucTypes/InferredTypes.java index 3523e459..fb9621cd 100644 --- a/src/de/dhbwstuttgart/strucTypes/InferredTypes.java +++ b/src/de/dhbwstuttgart/strucTypes/InferredTypes.java @@ -84,6 +84,10 @@ public class InferredTypes implements Map> entrySet() { return inferredTypes.entrySet(); } + + public RefTypeOrTPHOrWildcardOrGeneric infer(RefTypeOrTPHOrWildcardOrGeneric type){ + return inferredTypes.get(type) != null ? inferredTypes.get(type) : type; + } @Override public String toString() { diff --git a/src/de/dhbwstuttgart/strucTypes/Solve.java b/src/de/dhbwstuttgart/strucTypes/Solve.java new file mode 100644 index 00000000..be5daf4f --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/Solve.java @@ -0,0 +1,102 @@ +package de.dhbwstuttgart.strucTypes; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint; +import de.dhbwstuttgart.strucTypes.exception.InconsistentConstraintsException; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.GenericDeclarationList; +import de.dhbwstuttgart.syntaxtree.GenericTypeVar; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; +import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; + +public class Solve { + + private Set constraints = new HashSet<>(); + private IFiniteClosure fc; + private InferredTypes inferredTypes = new InferredTypes(); + private ClassOrInterface clsA; + + public Solve(Set constraints, ClassOrInterface clsA, IFiniteClosure fc, + InferredTypes inferredTypes) { + this.constraints = constraints; + this.fc = fc; + this.inferredTypes = inferredTypes; + this.clsA = clsA; + } + + public ClassOrInterfaceWithConstraints getSolvedClass() throws InconsistentConstraintsException { + Set constraintsUnifyPair = this.constraints.stream().map(SubTypeConstraint::getAsUnifyPair) + .collect(Collectors.toSet()); + StrucTypeUnify strucTypeUnify = new StrucTypeUnify(constraintsUnifyPair, this.fc); + Set subst = strucTypeUnify.unify(); + + Map tphs = this.generateTPHMap(constraints); + InferredTypes sigma = new InferredTypes(); + sigma.putAll(inferredTypes); + sigma.putAll( + StrucTypeUnifyUtils.convertEqualsdot( + subst.stream() + .filter(p -> p.getLhsType() instanceof PlaceholderType + && p.getPairOp().equals(PairOperator.EQUALSDOT)) + .collect(Collectors.toSet()), + tphs)); + + Set cs = StrucTypeUnifyUtils.convertSmallerdot( + subst.stream().filter(p -> PairOperator.SMALLERDOT.equals(p.getPairOp())).collect(Collectors.toSet()), + tphs); + + // TODO Tnew + List values = new ArrayList<>(); + TypeVar typeVar = new TypeVar(inferredTypes); + clsA.accept(typeVar); + final ArrayList bounds = new ArrayList<>(); + final NullToken offset = new NullToken(); + typeVar.getTypeVars().stream().map(tph -> new GenericTypeVar(tph.getName(), bounds, offset, offset)) + .forEach(values::add); + 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); + + if (!consistent(cs)) { + throw new InconsistentConstraintsException(); + } + return new ClassOrInterfaceWithConstraints(this.clsA.accept(new InferTypesVisitor(inferredTypes)), Tnew, + this.constraints); + + } + + private Map generateTPHMap(Set constraints) { + Map tphs = new HashMap<>(); + constraints.stream().forEach(c -> { + if (c.getSubtype() instanceof TypePlaceholder) { + TypePlaceholder subtype = (TypePlaceholder) c.getSubtype(); + tphs.put(subtype.getName(), subtype); + } + if (c.getSupertype() instanceof TypePlaceholder) { + TypePlaceholder supertype = (TypePlaceholder) c.getSupertype(); + tphs.put(supertype.getName(), supertype); + } + }); + return tphs; + } + + private boolean consistent(Set cs) { + // TODO update inconsistencies + return true; + } +} diff --git a/src/de/dhbwstuttgart/strucTypes/StrucTypeUnifyUtils.java b/src/de/dhbwstuttgart/strucTypes/StrucTypeUnifyUtils.java index 2ed5b3ed..b1b27a84 100644 --- a/src/de/dhbwstuttgart/strucTypes/StrucTypeUnifyUtils.java +++ b/src/de/dhbwstuttgart/strucTypes/StrucTypeUnifyUtils.java @@ -3,10 +3,17 @@ package de.dhbwstuttgart.strucTypes; import java.util.Collection; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint; +import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; +import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; public class StrucTypeUnifyUtils { @@ -33,4 +40,26 @@ public class StrucTypeUnifyUtils { } return Optional.empty(); } + + public static InferredTypes convertEqualsdot(Set pairs, Map tphs) { + InferredTypes inferredTypes = new InferredTypes(); + if (!pairs.stream().allMatch( + p -> PairOperator.EQUALSDOT.equals(p.getPairOp()) && p.getLhsType() instanceof PlaceholderType)) { + throw new IllegalArgumentException("Not all UnifyPairs were EQUALSDOT pairs."); + } + for (UnifyPair pair : pairs) { + TypePlaceholder lhs = (TypePlaceholder) UnifyTypeFactory.convert(pair.getLhsType(), tphs); + RefTypeOrTPHOrWildcardOrGeneric rhs = UnifyTypeFactory.convert(pair.getRhsType(), tphs); + inferredTypes.put(lhs, rhs); + } + return inferredTypes; + } + + public static Set convertSmallerdot(Set pairs, Map tphs) { + if (!pairs.stream().allMatch(p -> PairOperator.SMALLERDOT.equals(p.getPairOp()))) { + throw new IllegalArgumentException("Not all UnifyPairs were SMALLERDOT pairs."); + } + return pairs.stream().map(p -> new SubTypeConstraint(UnifyTypeFactory.convert(p.getLhsType(), tphs), + UnifyTypeFactory.convert(p.getRhsType(), tphs))).collect(Collectors.toSet()); + } } diff --git a/src/de/dhbwstuttgart/strucTypes/TypeVar.java b/src/de/dhbwstuttgart/strucTypes/TypeVar.java new file mode 100644 index 00000000..900e63a5 --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/TypeVar.java @@ -0,0 +1,31 @@ +package de.dhbwstuttgart.strucTypes; + +import java.util.HashSet; +import java.util.Set; + +import de.dhbwstuttgart.syntaxtree.AbstractASTWalker; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; + +public class TypeVar extends AbstractASTWalker { + + private Set typeVars = new HashSet<>(); + private InferredTypes inferredTypes = new InferredTypes(); + + public TypeVar(InferredTypes inferredTypes) { + this.inferredTypes = inferredTypes; + } + + @Override + public void visit(TypePlaceholder typePlaceholder) { + super.visit(typePlaceholder); + RefTypeOrTPHOrWildcardOrGeneric inferredType = this.inferredTypes.infer(typePlaceholder); + if (inferredType instanceof TypePlaceholder) { + this.typeVars.add((TypePlaceholder) inferredType); + } + } + + public Set getTypeVars() { + return typeVars; + } +} diff --git a/src/de/dhbwstuttgart/strucTypes/constraint/ConstraintsSet.java b/src/de/dhbwstuttgart/strucTypes/constraint/ConstraintsSet.java index c9347627..87f27450 100644 --- a/src/de/dhbwstuttgart/strucTypes/constraint/ConstraintsSet.java +++ b/src/de/dhbwstuttgart/strucTypes/constraint/ConstraintsSet.java @@ -1,31 +1,31 @@ package de.dhbwstuttgart.strucTypes.constraint; -import java.util.ArrayList; -import java.util.List; +import java.util.HashSet; +import java.util.Set; import de.dhbwstuttgart.strucTypes.InferredTypes; public class ConstraintsSet { - private List subTypeConstraints; - private List fieldConstraints; - private List methodConstraints; + private Set subTypeConstraints; + private Set fieldConstraints; + private Set methodConstraints; public ConstraintsSet() { - this.subTypeConstraints = new ArrayList<>(); - this.fieldConstraints = new ArrayList<>(); - this.methodConstraints = new ArrayList<>(); + this.subTypeConstraints = new HashSet<>(); + this.fieldConstraints = new HashSet<>(); + this.methodConstraints = new HashSet<>(); } - public List getSubTypeConstraints() { + public Set getSubTypeConstraints() { return subTypeConstraints; } - public List getFieldConstraints() { + public Set getFieldConstraints() { return fieldConstraints; } - public List getMethodConstraints() { + public Set getMethodConstraints() { return methodConstraints; } diff --git a/src/de/dhbwstuttgart/strucTypes/exception/InconsistentConstraintsException.java b/src/de/dhbwstuttgart/strucTypes/exception/InconsistentConstraintsException.java new file mode 100644 index 00000000..484c0e76 --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/exception/InconsistentConstraintsException.java @@ -0,0 +1,28 @@ +package de.dhbwstuttgart.strucTypes.exception; + +public class InconsistentConstraintsException extends Exception { + + private static final long serialVersionUID = 1L; + + public InconsistentConstraintsException() { + super(); + } + + public InconsistentConstraintsException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + + public InconsistentConstraintsException(String message, Throwable cause) { + super(message, cause); + } + + public InconsistentConstraintsException(String message) { + super(message); + } + + public InconsistentConstraintsException(Throwable cause) { + super(cause); + } + +} diff --git a/src/de/dhbwstuttgart/strucTypes/printutils/PrintConstraints.java b/src/de/dhbwstuttgart/strucTypes/printutils/PrintConstraints.java index 26ace5fa..05fcbec7 100644 --- a/src/de/dhbwstuttgart/strucTypes/printutils/PrintConstraints.java +++ b/src/de/dhbwstuttgart/strucTypes/printutils/PrintConstraints.java @@ -1,6 +1,5 @@ package de.dhbwstuttgart.strucTypes.printutils; -import java.util.List; import java.util.Set; import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet; @@ -17,7 +16,7 @@ public class PrintConstraints { printMethodConstraints(constraintsSet.getMethodConstraints()); } - public void printSubTypeConstraints(List constraints) { + public void printSubTypeConstraints(Set constraints) { System.out.println("\n SubTypeConstraints:"); constraints.forEach(System.out::println); // constraints.forEach(c -> System.out @@ -25,12 +24,12 @@ public class PrintConstraints { // c.getSupertype().toString()))); } - public void printSubTypeConstraints(Set constraints){ + public void printSubTypeConstraintsAsUnifyPair(Set constraints){ System.out.println("\n SubTypeConstraints:"); constraints.forEach(System.out::println); } - public void printFieldConstraints(List constraints) { + public void printFieldConstraints(Set constraints) { System.out.println("\n FieldConstraints:"); constraints.forEach(System.out::println); // constraints.forEach(c -> System.out.println(String.format("F(%s, %s, @@ -38,7 +37,7 @@ public class PrintConstraints { // c.getFieldName(), c.getFieldType().toString()))); } - public void printMethodConstraints(List constraints) { + public void printMethodConstraints(Set constraints) { System.out.println("\n MethodConstraints:"); constraints.forEach(System.out::println); // constraints.forEach(c -> { diff --git a/src/de/dhbwstuttgart/strucTypes/printutils/SyntaxTreePrinter.java b/src/de/dhbwstuttgart/strucTypes/printutils/SyntaxTreePrinter.java index 02034465..19972fc1 100644 --- a/src/de/dhbwstuttgart/strucTypes/printutils/SyntaxTreePrinter.java +++ b/src/de/dhbwstuttgart/strucTypes/printutils/SyntaxTreePrinter.java @@ -330,6 +330,7 @@ public class SyntaxTreePrinter implements ASTVisitor { RefTypeOrTPHOrWildcardOrGeneric type = literal.getType(); System.out.print("literal: "); type.accept(this); + System.out.println(); } @Override diff --git a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java index 72097fc0..c8078774 100644 --- a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java +++ b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java @@ -3,6 +3,7 @@ package de.dhbwstuttgart.syntaxtree; import de.dhbwstuttgart.core.IItemWithOffset; import de.dhbwstuttgart.exceptions.DebugException; import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter; @@ -12,6 +13,7 @@ import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; import org.antlr.v4.runtime.Token; +import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -26,7 +28,7 @@ public class ClassOrInterface extends SyntaxTreeNode implements TypeScope{ private List methods = new ArrayList<>(); private GenericDeclarationList genericClassParameters; private RefType superClass; - protected boolean isInterface; + public boolean isInterface; private List implementedInterfaces; private List constructors; @@ -34,6 +36,7 @@ public class ClassOrInterface extends SyntaxTreeNode implements TypeScope{ RefType superClass, Boolean isInterface, List implementedInterfaces, Token offset){ super(offset); this.modifiers = modifiers; + if(isInterface)this.modifiers += Modifier.INTERFACE; this.name = name; this.fields = fielddecl; this.genericClassParameters = genericClassParameters; @@ -102,8 +105,13 @@ public class ClassOrInterface extends SyntaxTreeNode implements TypeScope{ public void accept(ASTVisitor visitor) { visitor.visit(this); } + + @Override + public ClassOrInterface accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } - public Collection getSuperInterfaces() { + public List getSuperInterfaces() { return implementedInterfaces; } } diff --git a/src/de/dhbwstuttgart/syntaxtree/Constructor.java b/src/de/dhbwstuttgart/syntaxtree/Constructor.java index a99692bb..22297e5e 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Constructor.java +++ b/src/de/dhbwstuttgart/syntaxtree/Constructor.java @@ -6,6 +6,7 @@ import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import org.antlr.v4.runtime.Token; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.statement.Block; import java.util.List; @@ -35,4 +36,9 @@ public class Constructor extends Method { public void accept(ASTVisitor visitor) { visitor.visit(this); } + + @Override + public Constructor accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/Field.java b/src/de/dhbwstuttgart/syntaxtree/Field.java index 8a4230e3..5fd1951d 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Field.java +++ b/src/de/dhbwstuttgart/syntaxtree/Field.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.syntaxtree; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import org.antlr.v4.runtime.Token; @@ -40,5 +41,10 @@ public class Field extends SyntaxTreeNode implements TypeScope{ public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { return type; } + + @Override + public Field accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/FormalParameter.java b/src/de/dhbwstuttgart/syntaxtree/FormalParameter.java index 8e7e7983..19bb47e8 100644 --- a/src/de/dhbwstuttgart/syntaxtree/FormalParameter.java +++ b/src/de/dhbwstuttgart/syntaxtree/FormalParameter.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.syntaxtree; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import org.antlr.v4.runtime.Token; @@ -26,4 +27,9 @@ public class FormalParameter extends SyntaxTreeNode public void accept(ASTVisitor visitor) { visitor.visit(this); } + + @Override + public FormalParameter accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java b/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java index e316aa6b..8e5336ac 100644 --- a/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java +++ b/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java @@ -2,6 +2,8 @@ package de.dhbwstuttgart.syntaxtree; import org.antlr.v4.runtime.Token; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; + import java.util.*; @@ -31,4 +33,9 @@ public class GenericDeclarationList extends SyntaxTreeNode implements Iterable T accept(ASTReturnVisitor visitor); } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/ArgumentList.java b/src/de/dhbwstuttgart/syntaxtree/statement/ArgumentList.java index ed5a082b..eb1a6241 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/ArgumentList.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/ArgumentList.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.ASTVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; @@ -30,4 +31,9 @@ public class ArgumentList extends SyntaxTreeNode public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public ArgumentList accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java index 3af2de6f..dea6c809 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; @@ -26,4 +27,9 @@ public class Assign extends Statement public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public Assign accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/AssignToField.java b/src/de/dhbwstuttgart/syntaxtree/statement/AssignToField.java index 931443d1..c2ef8444 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/AssignToField.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/AssignToField.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; public class AssignToField extends AssignLeftSide{ @@ -13,4 +14,9 @@ public class AssignToField extends AssignLeftSide{ public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public AssignToField accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java index bb631e04..97331ec0 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import org.antlr.v4.runtime.Token; @@ -11,6 +12,11 @@ public class BinaryExpr extends Expression public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public BinaryExpr accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } public enum Operator{ ADD, // + diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Block.java b/src/de/dhbwstuttgart/syntaxtree/statement/Block.java index 73a92c7e..e6a619c1 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Block.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Block.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree.statement; import java.util.*; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; @@ -28,6 +29,11 @@ public class Block extends Statement public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public Block accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/CastExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/CastExpr.java index 15fedbbc..1d3c681c 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/CastExpr.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/CastExpr.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree.statement; import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; @@ -22,4 +23,9 @@ public class CastExpr extends Expression public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public CastExpr accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/DoStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/DoStmt.java index b24396bb..ac08ce96 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/DoStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/DoStmt.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import org.antlr.v4.runtime.Token; @@ -15,4 +16,9 @@ public class DoStmt extends WhileStmt public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public DoStmt accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/EmptyStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/EmptyStmt.java index ff80e47f..dba32f45 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/EmptyStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/EmptyStmt.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.Void; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; @@ -19,4 +20,9 @@ public class EmptyStmt extends Statement public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public EmptyStmt accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/ExpressionReceiver.java b/src/de/dhbwstuttgart/syntaxtree/statement/ExpressionReceiver.java index 409cfe68..07af8192 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/ExpressionReceiver.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/ExpressionReceiver.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import org.antlr.v4.runtime.Token; @@ -18,4 +19,9 @@ public class ExpressionReceiver extends Receiver public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public ExpressionReceiver accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/FieldVar.java b/src/de/dhbwstuttgart/syntaxtree/statement/FieldVar.java index 9380e951..8d783cfc 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/FieldVar.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/FieldVar.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree.statement; import de.dhbwstuttgart.exceptions.TypeinferenceException; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.assumptions.FieldAssumption; @@ -28,4 +29,9 @@ public class FieldVar extends Expression { public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public FieldVar accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java index 1f933366..12bd0832 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.syntaxtree.statement; import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; @@ -26,4 +27,9 @@ public class ForStmt extends Statement public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public ForStmt accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } \ No newline at end of file diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java index 4af1e770..4f714ec6 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; @@ -27,4 +28,9 @@ public class IfStmt extends Statement public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public IfStmt accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java b/src/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java index 02fd667b..6909bd88 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree.statement; import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; @@ -21,4 +22,9 @@ public class InstanceOf extends BinaryExpr public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public InstanceOf accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java b/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java index 40e65958..c5d26dc1 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LambdaExpression.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; @@ -39,4 +40,9 @@ public class LambdaExpression extends Expression implements TypeScope { //return type.getParaList().get(0); return methodBody.getType(); } + + @Override + public LambdaExpression accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Literal.java b/src/de/dhbwstuttgart/syntaxtree/statement/Literal.java index 8bbaae0a..97fc1115 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Literal.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Literal.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.statement.Expression; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; @@ -21,4 +22,9 @@ public class Literal extends Expression public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public Literal accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java index 91094a7a..4b24d48f 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import org.antlr.v4.runtime.Token; @@ -24,4 +25,9 @@ public class LocalVar extends Statement{ public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public LocalVar accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarDecl.java b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarDecl.java index fde48e51..81c97c4b 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarDecl.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVarDecl.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.syntaxtree.statement; import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; @@ -28,4 +29,9 @@ public class LocalVarDecl extends Statement public String getName() { return name; } + + @Override + public LocalVarDecl accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java b/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java index f6307f1e..ce848820 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree.statement; import de.dhbwstuttgart.exceptions.TypeinferenceException; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; @@ -38,4 +39,9 @@ public class MethodCall extends Statement public ArgumentList getArgumentList() { return arglist; } + + @Override + public MethodCall accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/NewArray.java b/src/de/dhbwstuttgart/syntaxtree/statement/NewArray.java index 0f699161..e3e79dbf 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/NewArray.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/NewArray.java @@ -6,6 +6,7 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; public class NewArray extends Expression @@ -21,4 +22,9 @@ public class NewArray extends Expression public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public NewArray accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java b/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java index d29f3d53..86e234c9 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java @@ -6,6 +6,7 @@ import java.util.Set; import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.StatementVisitor; @@ -37,4 +38,9 @@ public class NewClass extends MethodCall public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public NewClass accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Return.java b/src/de/dhbwstuttgart/syntaxtree/statement/Return.java index 5a6a5312..f9d019d6 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Return.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Return.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import org.antlr.v4.runtime.Token; @@ -18,4 +19,9 @@ public class Return extends Statement public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public Return accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/ReturnVoid.java b/src/de/dhbwstuttgart/syntaxtree/statement/ReturnVoid.java index ca64b2c5..02b97331 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/ReturnVoid.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/ReturnVoid.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import org.antlr.v4.runtime.Token; @@ -12,4 +13,9 @@ public class ReturnVoid extends Return{ public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public ReturnVoid accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/StaticClassName.java b/src/de/dhbwstuttgart/syntaxtree/statement/StaticClassName.java index a7089d8c..e02c4b1a 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/StaticClassName.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/StaticClassName.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree.statement; import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; @@ -17,4 +18,9 @@ public class StaticClassName extends Receiver { public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public StaticClassName accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Super.java b/src/de/dhbwstuttgart/syntaxtree/statement/Super.java index fbeba99b..5e943903 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Super.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Super.java @@ -6,6 +6,7 @@ import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; import org.antlr.v4.runtime.Token; import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; public class Super extends Expression { @@ -18,4 +19,9 @@ public class Super extends Expression public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public Super accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/SuperCall.java b/src/de/dhbwstuttgart/syntaxtree/statement/SuperCall.java index 42cfb8ee..52724672 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/SuperCall.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/SuperCall.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.Void; @@ -25,4 +26,9 @@ public class SuperCall extends MethodCall public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public SuperCall accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/This.java b/src/de/dhbwstuttgart/syntaxtree/statement/This.java index d35e7797..73ff3b3f 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/This.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/This.java @@ -5,6 +5,7 @@ import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import org.antlr.v4.runtime.Token; import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; public class This extends Expression { @@ -19,4 +20,9 @@ public class This extends Expression public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public This accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/UnaryExpr.java index ca06146a..199e7934 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryExpr.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/UnaryExpr.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.statement.Expression; import de.dhbwstuttgart.syntaxtree.statement.JavaInternalExpression; @@ -32,6 +33,11 @@ public class UnaryExpr extends JavaInternalExpression public void accept(StatementVisitor visitor) { visitor.visit(this); } + + @Override + public UnaryExpr accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java index 0127a505..203fc46e 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java @@ -1,28 +1,28 @@ package de.dhbwstuttgart.syntaxtree.statement; - -import de.dhbwstuttgart.syntaxtree.StatementVisitor; -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.exceptions.NotImplementedException; import org.antlr.v4.runtime.Token; -public class WhileStmt extends Statement -{ - public final Expression expr; - public final Statement loopBlock; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; +import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; - public WhileStmt(Expression expr, Statement loopBlock, Token offset) - { - super(TypePlaceholder.fresh(offset), offset); - this.expr = expr; - this.loopBlock = loopBlock; - } +public class WhileStmt extends Statement { + public final Expression expr; + public final Statement loopBlock; - @Override - public void accept(StatementVisitor visitor) { - visitor.visit(this); - } + public WhileStmt(Expression expr, Statement loopBlock, Token offset) { + super(TypePlaceholder.fresh(offset), offset); + this.expr = expr; + this.loopBlock = loopBlock; + } + + @Override + public void accept(StatementVisitor visitor) { + visitor.visit(this); + } + + @Override + public WhileStmt accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java index d5abd93b..cd594cd2 100644 --- a/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree.type; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.ASTVisitor; import de.dhbwstuttgart.typeinference.result.ResultSetVisitor; import org.antlr.v4.runtime.Token; @@ -49,4 +50,9 @@ public class ExtendsWildcardType extends WildcardType{ public void accept(ResultSetVisitor visitor) { visitor.visit(this); } + + @Override + public RefTypeOrTPHOrWildcardOrGeneric accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java b/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java index a14813e7..1d318375 100644 --- a/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.syntaxtree.type; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.ASTVisitor; import de.dhbwstuttgart.typeinference.result.ResultSetVisitor; import org.antlr.v4.runtime.Token; @@ -32,5 +33,10 @@ public class GenericRefType extends RefTypeOrTPHOrWildcardOrGeneric public void accept(ResultSetVisitor visitor) { visitor.visit(this); } + + @Override + public RefTypeOrTPHOrWildcardOrGeneric accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 3c9a5907..48fdb16d 100644 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree.type; import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.ASTVisitor; import de.dhbwstuttgart.typeinference.result.ResultSetVisitor; import org.antlr.v4.runtime.Token; @@ -121,5 +122,10 @@ public class RefType extends RefTypeOrTPHOrWildcardOrGeneric public void accept(ResultSetVisitor visitor) { visitor.visit(this); } + + @Override + public RefTypeOrTPHOrWildcardOrGeneric accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java index 740c80e5..77d6df27 100644 --- a/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree.type; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.ASTVisitor; import de.dhbwstuttgart.typeinference.result.ResultSetVisitor; import org.antlr.v4.runtime.Token; @@ -59,4 +60,9 @@ public class SuperWildcardType extends WildcardType{ public void accept(ResultSetVisitor visitor) { visitor.visit(this); } + + @Override + public RefTypeOrTPHOrWildcardOrGeneric accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java b/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java index ab4414fd..cc602c6f 100644 --- a/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree.type; import java.util.Hashtable; +import de.dhbwstuttgart.strucTypes.ASTReturnVisitor; import de.dhbwstuttgart.syntaxtree.ASTVisitor; import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; @@ -82,4 +83,9 @@ public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric public void accept(ResultSetVisitor visitor) { visitor.visit(this); } + + @Override + public RefTypeOrTPHOrWildcardOrGeneric accept(ASTReturnVisitor visitor) { + return visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index 0c623548..c2fc3d24 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -109,7 +109,7 @@ public class OutputGenerator implements ASTVisitor{ @Override public void visit(ClassOrInterface classOrInterface) { - if((Modifier.INTERFACE & classOrInterface.getModifiers()) == 1){ + if(Modifier.isInterface(classOrInterface.getModifiers())){ out.append("interface "); }else{ out.append("class "); diff --git a/test/strucType/TestConstruct.java b/test/strucType/TestConstruct.java index cbdb0316..512805d6 100644 --- a/test/strucType/TestConstruct.java +++ b/test/strucType/TestConstruct.java @@ -11,13 +11,13 @@ import de.dhbwstuttgart.strucTypes.Construct; import de.dhbwstuttgart.strucTypes.InferredTypes; import de.dhbwstuttgart.strucTypes.StrucTYPE; import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet; +import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint; import de.dhbwstuttgart.strucTypes.exception.ImpossibleSubTypeException; import de.dhbwstuttgart.strucTypes.printutils.PrintConstraints; import de.dhbwstuttgart.strucTypes.printutils.PrintInferredTypes; import de.dhbwstuttgart.strucTypes.printutils.SyntaxTreePrinter; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.SourceFile; -import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; public class TestConstruct { public static final String rootDirectory = System.getProperty("user.dir") + "/test/strucType/javFiles/"; @@ -53,8 +53,8 @@ public class TestConstruct { Construct construct = new Construct(constraints, inferredTypesType); final List constructedInterfaces = construct.getConstructedInterfaces(); -// final List subTypeConstraints = construct.getSubTypeConstraints(); - Set subTypeConstraints = construct.getSubTypeConstraintsAsUnifyPairs(); + final Set subTypeConstraints = construct.getSubTypeConstraints(); +// Set subTypeConstraints = construct.getSubTypeConstraintsAsUnifyPairs(); final InferredTypes inferredTypesConstruct = construct.getInferredTypes(); final SyntaxTreePrinter syntaxTreePrinterInferred = new SyntaxTreePrinter(inferredTypesConstruct); diff --git a/test/strucType/TestInferTypesVisitor.java b/test/strucType/TestInferTypesVisitor.java new file mode 100644 index 00000000..8b459e9b --- /dev/null +++ b/test/strucType/TestInferTypesVisitor.java @@ -0,0 +1,102 @@ +package strucType; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import org.junit.Ignore; +import org.junit.Test; + +import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.strucTypes.Construct; +import de.dhbwstuttgart.strucTypes.InferTypesVisitor; +import de.dhbwstuttgart.strucTypes.InferredTypes; +import de.dhbwstuttgart.strucTypes.StrucTYPE; +import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet; +import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint; +import de.dhbwstuttgart.strucTypes.exception.ImpossibleSubTypeException; +import de.dhbwstuttgart.strucTypes.printutils.PrintConstraints; +import de.dhbwstuttgart.strucTypes.printutils.PrintInferredTypes; +import de.dhbwstuttgart.strucTypes.printutils.SyntaxTreePrinter; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.SourceFile; + +public class TestInferTypesVisitor { + + public static final String rootDirectory = System.getProperty("user.dir") + "/test/strucType/javFiles/"; + public final PrintConstraints printConstraints = new PrintConstraints(); + + @Test + @Ignore + public void test() throws ClassNotFoundException, IOException { + ArrayList files = new ArrayList<>(); + files.add(new File(rootDirectory + "testLocalVar.jav")); + files.add(new File(rootDirectory + "testCast.jav")); + files.add(new File(rootDirectory + "testNew.jav")); + files.add(new File(rootDirectory + "testFieldVar.jav")); + files.add(new File(rootDirectory + "testFieldMethod.jav")); + files.add(new File(rootDirectory + "testMethod.jav")); + files.add(new File(rootDirectory + "testPaperExample.jav")); + JavaTXCompiler compiler = new JavaTXCompiler(files); + for (File f : compiler.sourceFiles.keySet()) { + String name = f.getName(); + System.out.println("Filename: " + name); + SourceFile sourceFile = compiler.sourceFiles.get(f); + //Print SourceFile Infos + sourceFile.accept(new InferTypesVisitor(new InferredTypes())); + + + System.out.println("____________________________________________________________________________"); + } + } + + @Test + public void test2() throws ClassNotFoundException, IOException, ImpossibleSubTypeException { + ArrayList files = new ArrayList<>(); + files.add(new File(rootDirectory + "testLocalVar.jav")); + files.add(new File(rootDirectory + "testCast.jav")); + files.add(new File(rootDirectory + "testNew.jav")); + files.add(new File(rootDirectory + "testFieldVar.jav")); + files.add(new File(rootDirectory + "testFieldMethod.jav")); + files.add(new File(rootDirectory + "testMethod.jav")); + files.add(new File(rootDirectory + "testPaperExample.jav")); + JavaTXCompiler compiler = new JavaTXCompiler(files); + for (File f : compiler.sourceFiles.keySet()) { + String name = f.getName(); + System.out.println("Filename: " + name); + SourceFile sourceFile = compiler.sourceFiles.get(f); + // Print SourceFile Infos + SyntaxTreePrinter syntaxTreePrinter = new SyntaxTreePrinter(); + sourceFile.accept(syntaxTreePrinter); + + StrucTYPE strucTYPE = new StrucTYPE(sourceFile); + + final ConstraintsSet constraints = strucTYPE.getConstraints(); + final InferredTypes inferredTypesType = strucTYPE.getInferredTypes(); + + System.out.println("\n--StrucTYPE--"); + printConstraints.print(constraints); + PrintInferredTypes.print(inferredTypesType); + + Construct construct = new Construct(constraints, inferredTypesType); + + final List constructedInterfaces = construct.getConstructedInterfaces(); + final Set subTypeConstraints = construct.getSubTypeConstraints(); + final InferredTypes inferredTypesConstruct = construct.getInferredTypes(); + + System.out.println("\n--Construct--"); + System.out.println("\nConstructed Interfaces:"); + constructedInterfaces.forEach(i -> i.accept(syntaxTreePrinter)); + printConstraints.printSubTypeConstraints(subTypeConstraints); + PrintInferredTypes.print(inferredTypesConstruct); + System.out.println("\n--Inferred SysntaxTree--"); + sourceFile = sourceFile.accept(new InferTypesVisitor(inferredTypesConstruct)); + sourceFile.accept(syntaxTreePrinter); + + System.out.println("____________________________________________________________________________"); + } + } + +} diff --git a/test/strucType/TestPaperExample.java b/test/strucType/TestPaperExample.java new file mode 100644 index 00000000..ef503470 --- /dev/null +++ b/test/strucType/TestPaperExample.java @@ -0,0 +1,89 @@ +package strucType; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.strucTypes.ClassOrInterfaceWithConstraints; +import de.dhbwstuttgart.strucTypes.Construct; +import de.dhbwstuttgart.strucTypes.InferredTypes; +import de.dhbwstuttgart.strucTypes.Solve; +import de.dhbwstuttgart.strucTypes.StrucTYPE; +import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet; +import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint; +import de.dhbwstuttgart.strucTypes.exception.ImpossibleSubTypeException; +import de.dhbwstuttgart.strucTypes.exception.InconsistentConstraintsException; +import de.dhbwstuttgart.strucTypes.printutils.PrintConstraints; +import de.dhbwstuttgart.strucTypes.printutils.PrintInferredTypes; +import de.dhbwstuttgart.strucTypes.printutils.SyntaxTreePrinter; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; +import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; + +public class TestPaperExample { + public static final String rootDirectory = System.getProperty("user.dir") + "/test/strucType/javFiles/"; + public final PrintConstraints printConstraints = new PrintConstraints(); + + @org.junit.Test + public void test() throws ClassNotFoundException, IOException, ImpossibleSubTypeException, InconsistentConstraintsException { + ArrayList files = new ArrayList<>(); + files.add(new File(rootDirectory + "testPaperExample.jav")); + trans(files); + files.clear(); + files.add(new File(rootDirectory + "testMain.jav")); + trans(files); + } + + private void trans(ArrayList files) + throws IOException, ClassNotFoundException, ImpossibleSubTypeException, InconsistentConstraintsException { + JavaTXCompiler compiler = new JavaTXCompiler(files); + for (File f : compiler.sourceFiles.keySet()) { + String name = f.getName(); + System.out.println("Filename: " + name); + SourceFile sourceFile = compiler.sourceFiles.get(f); + // Print SourceFile Infos + SyntaxTreePrinter syntaxtreeprinter = new SyntaxTreePrinter(); + sourceFile.accept(syntaxtreeprinter); + + StrucTYPE strucTYPE = new StrucTYPE(sourceFile); + + final ConstraintsSet constraints = strucTYPE.getConstraints(); + final InferredTypes inferredTypesType = strucTYPE.getInferredTypes(); + + System.out.println("\n--StrucTYPE--"); + printConstraints.print(constraints); + PrintInferredTypes.print(inferredTypesType); + + Construct construct = new Construct(constraints, inferredTypesType); + + final List constructedInterfaces = construct.getConstructedInterfaces(); + final Set subTypeConstraints = construct.getSubTypeConstraints(); + final InferredTypes inferredTypesConstruct = construct.getInferredTypes(); + + System.out.println("\n--Construct--"); + System.out.println("\nConstructed Interfaces:"); + constructedInterfaces.forEach(i -> i.accept(syntaxtreeprinter)); + printConstraints.printSubTypeConstraints(subTypeConstraints); + PrintInferredTypes.print(inferredTypesConstruct); + + System.out.println("\n--Solve--"); + // Alle Klassen aus allen SourceFiles + List availableClasses = compiler.getAvailableClasses(sourceFile); + FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(availableClasses); + System.out.println("\nFinite Closure:"); + System.out.println(finiteClosure); + Solve solve = new Solve(subTypeConstraints, sourceFile.getClasses().get(0), finiteClosure, inferredTypesConstruct); + ClassOrInterfaceWithConstraints solvedClass = solve.getSolvedClass(); + System.out.println("\nSolved Class:"); + solvedClass.accept(syntaxtreeprinter); + System.out.println("\nRemaining Constraints:"); + printConstraints.printSubTypeConstraints(solvedClass.getConstraints()); + + System.out.println("____________________________________________________________________________"); + } + } +} diff --git a/test/strucType/TestSolve.java b/test/strucType/TestSolve.java new file mode 100644 index 00000000..53813713 --- /dev/null +++ b/test/strucType/TestSolve.java @@ -0,0 +1,88 @@ +package strucType; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.strucTypes.ClassOrInterfaceWithConstraints; +import de.dhbwstuttgart.strucTypes.Construct; +import de.dhbwstuttgart.strucTypes.InferredTypes; +import de.dhbwstuttgart.strucTypes.Solve; +import de.dhbwstuttgart.strucTypes.StrucTYPE; +import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet; +import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint; +import de.dhbwstuttgart.strucTypes.exception.ImpossibleSubTypeException; +import de.dhbwstuttgart.strucTypes.exception.InconsistentConstraintsException; +import de.dhbwstuttgart.strucTypes.printutils.PrintConstraints; +import de.dhbwstuttgart.strucTypes.printutils.PrintInferredTypes; +import de.dhbwstuttgart.strucTypes.printutils.SyntaxTreePrinter; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; +import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; + +public class TestSolve { + public static final String rootDirectory = System.getProperty("user.dir") + "/test/strucType/javFiles/"; + public final PrintConstraints printConstraints = new PrintConstraints(); + + @org.junit.Test + public void test() throws ClassNotFoundException, IOException, ImpossibleSubTypeException, InconsistentConstraintsException { + ArrayList files = new ArrayList<>(); + files.add(new File(rootDirectory + "testLocalVar.jav")); + files.add(new File(rootDirectory + "testCast.jav")); + files.add(new File(rootDirectory + "testNew.jav")); + files.add(new File(rootDirectory + "testFieldVar.jav")); + files.add(new File(rootDirectory + "testFieldMethod.jav")); + files.add(new File(rootDirectory + "testMethod.jav")); + files.add(new File(rootDirectory + "testPaperExample.jav")); + JavaTXCompiler compiler = new JavaTXCompiler(files); + for (File f : compiler.sourceFiles.keySet()) { + String name = f.getName(); + System.out.println("Filename: " + name); + SourceFile sourceFile = compiler.sourceFiles.get(f); + // Print SourceFile Infos + SyntaxTreePrinter syntaxtreeprinter = new SyntaxTreePrinter(); + sourceFile.accept(syntaxtreeprinter); + + StrucTYPE strucTYPE = new StrucTYPE(sourceFile); + + final ConstraintsSet constraints = strucTYPE.getConstraints(); + final InferredTypes inferredTypesType = strucTYPE.getInferredTypes(); + + System.out.println("\n--StrucTYPE--"); + printConstraints.print(constraints); + PrintInferredTypes.print(inferredTypesType); + + Construct construct = new Construct(constraints, inferredTypesType); + + final List constructedInterfaces = construct.getConstructedInterfaces(); + final Set subTypeConstraints = construct.getSubTypeConstraints(); +// Set subTypeConstraints = construct.getSubTypeConstraintsAsUnifyPairs(); + final InferredTypes inferredTypesConstruct = construct.getInferredTypes(); + + System.out.println("\n--Construct--"); + System.out.println("\nConstructed Interfaces:"); + constructedInterfaces.forEach(i -> i.accept(syntaxtreeprinter)); + printConstraints.printSubTypeConstraints(subTypeConstraints); + PrintInferredTypes.print(inferredTypesConstruct); + + System.out.println("\n--Solve--"); + // Alle Klassen aus allen SourceFiles + List availableClasses = compiler.getAvailableClasses(sourceFile); + FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(availableClasses); + System.out.println("\nFinite Closure:"); + System.out.println(finiteClosure); + Solve solve = new Solve(subTypeConstraints, sourceFile.getClasses().get(0), finiteClosure, inferredTypesConstruct); + ClassOrInterfaceWithConstraints solvedClass = solve.getSolvedClass(); + System.out.println("\nSolved Class:"); + solvedClass.accept(syntaxtreeprinter); + System.out.println("\nRemaining Constraints:"); + printConstraints.printSubTypeConstraints(solvedClass.getConstraints()); + + System.out.println("____________________________________________________________________________"); + } + } +} diff --git a/test/strucType/constructed/A.java b/test/strucType/constructed/A.java new file mode 100644 index 00000000..1d36e64e --- /dev/null +++ b/test/strucType/constructed/A.java @@ -0,0 +1,12 @@ +package strucType.constructed; + +public class A, U>, M extends U, P, N extends V, V, AD extends O, U> { + + public A() { + } + + public P mt(Z x, M y, N z) { + return x.sub(y).add(z); + } + +} diff --git a/test/strucType/constructed/CO.java b/test/strucType/constructed/CO.java new file mode 100644 index 00000000..974c7e9b --- /dev/null +++ b/test/strucType/constructed/CO.java @@ -0,0 +1,6 @@ +package strucType.constructed; + +public abstract class CO { + + public OX f; +} \ No newline at end of file diff --git a/test/strucType/constructed/DE.java b/test/strucType/constructed/DE.java new file mode 100644 index 00000000..39db1813 --- /dev/null +++ b/test/strucType/constructed/DE.java @@ -0,0 +1,7 @@ +package strucType.constructed; + +public abstract class DE { + + public abstract NG mF(NI x, NJ y); + +} \ No newline at end of file diff --git a/test/strucType/constructed/DG.java b/test/strucType/constructed/DG.java new file mode 100644 index 00000000..44db0371 --- /dev/null +++ b/test/strucType/constructed/DG.java @@ -0,0 +1,8 @@ +package strucType.constructed; + +public abstract class DG { + + public NL g; + public abstract NM m2(NO x); + +} \ No newline at end of file diff --git a/test/strucType/constructed/EK.java b/test/strucType/constructed/EK.java new file mode 100644 index 00000000..5ed0c930 --- /dev/null +++ b/test/strucType/constructed/EK.java @@ -0,0 +1,7 @@ +package strucType.constructed; + +public abstract class EK { + + public abstract OA getA(); + +} \ No newline at end of file diff --git a/test/strucType/constructed/FF.java b/test/strucType/constructed/FF.java new file mode 100644 index 00000000..5b8d0062 --- /dev/null +++ b/test/strucType/constructed/FF.java @@ -0,0 +1,7 @@ +package strucType.constructed; + +public interface FF { + + public PP sub(PR x); + +} \ No newline at end of file diff --git a/test/strucType/constructed/FI.java b/test/strucType/constructed/FI.java new file mode 100644 index 00000000..e9b8bc2a --- /dev/null +++ b/test/strucType/constructed/FI.java @@ -0,0 +1,7 @@ +package strucType.constructed; + +public interface FI { + + public PL add(PN x); + +} \ No newline at end of file diff --git a/test/strucType/constructed/L.java b/test/strucType/constructed/L.java new file mode 100644 index 00000000..e4f7b9d2 --- /dev/null +++ b/test/strucType/constructed/L.java @@ -0,0 +1,7 @@ +package strucType.constructed; + +public interface L { + + public R sub(T x); + +} \ No newline at end of file diff --git a/test/strucType/constructed/O.java b/test/strucType/constructed/O.java new file mode 100644 index 00000000..35e781ff --- /dev/null +++ b/test/strucType/constructed/O.java @@ -0,0 +1,7 @@ +package strucType.constructed; + +public interface O { + + public R add(T x); + +} \ No newline at end of file diff --git a/test/strucType/javFiles/testMain.jav b/test/strucType/javFiles/testMain.jav new file mode 100644 index 00000000..d0bb6d40 --- /dev/null +++ b/test/strucType/javFiles/testMain.jav @@ -0,0 +1,8 @@ +package strucType.input; + +import strucType.typedtestclasses.MyInteger; +import strucType.constructed.A; + +class Main { + main() { return new A().mt(new MyInteger(2), new MyInteger(1), new MyInteger(3)); } +} \ No newline at end of file diff --git a/test/strucType/typedtestclasses/MyInteger.java b/test/strucType/typedtestclasses/MyInteger.java new file mode 100644 index 00000000..ed8ce447 --- /dev/null +++ b/test/strucType/typedtestclasses/MyInteger.java @@ -0,0 +1,24 @@ +package strucType.typedtestclasses; + +import strucType.constructed.L; +import strucType.constructed.O; + +public class MyInteger implements L, O { + + public MyInteger(Integer i){ + this.i = i; + } + + Integer i; + + @Override + public MyInteger sub(MyInteger x) { + return new MyInteger(i - x.i); + } + + @Override + public MyInteger add(MyInteger x) { + return new MyInteger(i + x.i); + } + +}