diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index adc160fc..19c996ba 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -53,7 +53,7 @@ public class JavaTXCompiler { for(File forSourceFile : sourceFiles.keySet()) for(JavaClassName name : sourceFiles.get(forSourceFile).getImports()){ ClassOrInterface importedClass = ASTFactory.createClass( - ClassLoader.getSystemClassLoader().loadClass(name.toString())); + ClassLoader.getSystemClassLoader().loadClass(name.toString()));// Konvertiert class -> ClassOrInterface importedClasses.add(importedClass); } allClasses.addAll(importedClasses); @@ -78,6 +78,12 @@ public class JavaTXCompiler { return results.stream().map((unifyPairs -> new ResultSet(UnifyTypeFactory.convert(unifyPairs, generateTPHMap(cons))))).collect(Collectors.toList()); } + + public Object strucTypeInference(){ + //TODO mvr + return null; + + } private Map generateTPHMap(ConstraintSet constraints){ HashMap ret = new HashMap<>(); diff --git a/src/de/dhbwstuttgart/strucTypes/CheckTypeVisitor.java b/src/de/dhbwstuttgart/strucTypes/CheckTypeVisitor.java new file mode 100644 index 00000000..45b05013 --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/CheckTypeVisitor.java @@ -0,0 +1,32 @@ +package de.dhbwstuttgart.strucTypes; + +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; + +public class CheckTypeVisitor extends DefaultASTVisitor { + + private boolean isTypeVariable; + private String name; + + @Override + public void visit(RefType refType) { + this.isTypeVariable = true; + this.name = refType.getName().toString(); + } + + @Override + public void visit(TypePlaceholder typePlaceholder) { + this.isTypeVariable = false; + this.name = typePlaceholder.getName(); + } + + public boolean isTypeVariable() { + return isTypeVariable; + } + + public String getName() { + return name; + } + + +} diff --git a/src/de/dhbwstuttgart/strucTypes/ConstraintsSet.java b/src/de/dhbwstuttgart/strucTypes/ConstraintsSet.java new file mode 100644 index 00000000..710329ad --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/ConstraintsSet.java @@ -0,0 +1,44 @@ +package de.dhbwstuttgart.strucTypes; + +import java.util.ArrayList; +import java.util.List; + +public class ConstraintsSet { + + private List subTypeConstraints; + private List fieldConstraints; + private List methodConstraints; + + public ConstraintsSet() { + this.subTypeConstraints = new ArrayList<>(); + this.fieldConstraints = new ArrayList<>(); + this.methodConstraints = new ArrayList<>(); + } + + public List getSubTypeConstraints() { + return subTypeConstraints; + } + + public List getFieldConstraints() { + return fieldConstraints; + } + + public List getMethodConstraints() { + return methodConstraints; + } + + public void addConstraint(SubTypeConstraint constraint){ + this.subTypeConstraints.add(constraint); + } + + public void addConstraint(FieldConstraint constraint){ + this.fieldConstraints.add(constraint); + } + + public void addConstraint(MethodConstraint constraint){ + this.methodConstraints.add(constraint); + } + + + +} diff --git a/src/de/dhbwstuttgart/strucTypes/DefaultASTVisitor.java b/src/de/dhbwstuttgart/strucTypes/DefaultASTVisitor.java new file mode 100644 index 00000000..6514f882 --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/DefaultASTVisitor.java @@ -0,0 +1,314 @@ +package de.dhbwstuttgart.strucTypes; + +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; +import de.dhbwstuttgart.syntaxtree.ASTVisitor; +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.Binary; +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.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.UnaryPlus; +import de.dhbwstuttgart.syntaxtree.statement.WhileStmt; +import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; +import de.dhbwstuttgart.syntaxtree.statement.literal.Null; +import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; +import de.dhbwstuttgart.syntaxtree.type.GenericRefType; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; + +public class DefaultASTVisitor implements ASTVisitor { + + @Override + public void visit(ArgumentList argumentList) { + throw new NotImplementedException(); + } + + @Override + public void visit(LambdaExpression lambdaExpression) { + throw new NotImplementedException(); + } + + @Override + public void visit(Assign assign) { + throw new NotImplementedException(); + + } + + @Override + public void visit(Binary binary) { + throw new NotImplementedException(); + + } + + @Override + public void visit(Block block) { + throw new NotImplementedException(); + + } + + @Override + public void visit(CastExpr castExpr) { + throw new NotImplementedException(); + + } + + @Override + public void visit(EmptyStmt emptyStmt) { + throw new NotImplementedException(); + + } + + @Override + public void visit(FieldVar fieldVar) { + throw new NotImplementedException(); + + } + + @Override + public void visit(ForStmt forStmt) { + throw new NotImplementedException(); + + } + + @Override + public void visit(IfStmt ifStmt) { + throw new NotImplementedException(); + + } + + @Override + public void visit(InstanceOf instanceOf) { + throw new NotImplementedException(); + + } + + @Override + public void visit(LocalVar localVar) { + throw new NotImplementedException(); + + } + + @Override + public void visit(LocalVarDecl localVarDecl) { + throw new NotImplementedException(); + + } + + @Override + public void visit(MethodCall methodCall) { + throw new NotImplementedException(); + + } + + @Override + public void visit(NewClass methodCall) { + throw new NotImplementedException(); + + } + + @Override + public void visit(NewArray newArray) { + throw new NotImplementedException(); + + } + + @Override + public void visit(Return aReturn) { + throw new NotImplementedException(); + + } + + @Override + public void visit(ReturnVoid aReturn) { + throw new NotImplementedException(); + + } + + @Override + public void visit(StaticClassName staticClassName) { + throw new NotImplementedException(); + + } + + @Override + public void visit(Super aSuper) { + throw new NotImplementedException(); + + } + + @Override + public void visit(This aThis) { + throw new NotImplementedException(); + + } + + @Override + public void visit(UnaryPlus unaryPlus) { + throw new NotImplementedException(); + + } + + @Override + public void visit(WhileStmt whileStmt) { + throw new NotImplementedException(); + + } + + @Override + public void visit(DoStmt whileStmt) { + throw new NotImplementedException(); + + } + + @Override + public void visit(Null aNull) { + throw new NotImplementedException(); + + } + + @Override + public void visit(Literal literal) { + throw new NotImplementedException(); + + } + + @Override + public void visit(AssignToField assignLeftSide) { + throw new NotImplementedException(); + + } + + @Override + public void visit(AssignToLocal assignLeftSide) { + throw new NotImplementedException(); + + } + + @Override + public void visit(SuperCall superCall) { + throw new NotImplementedException(); + + } + + @Override + public void visit(ExpressionReceiver expressionReceiver) { + throw new NotImplementedException(); + + } + + @Override + public void visit(SourceFile sourceFile) { + throw new NotImplementedException(); + + } + + @Override + public void visit(GenericTypeVar genericTypeVar) { + throw new NotImplementedException(); + + } + + @Override + public void visit(FormalParameter formalParameter) { + throw new NotImplementedException(); + + } + + @Override + public void visit(GenericDeclarationList genericTypeVars) { + throw new NotImplementedException(); + + } + + @Override + public void visit(Field field) { + throw new NotImplementedException(); + + } + + @Override + public void visit(Method field) { + throw new NotImplementedException(); + + } + + @Override + public void visit(Constructor field) { + throw new NotImplementedException(); + + } + + @Override + public void visit(ParameterList formalParameters) { + throw new NotImplementedException(); + + } + + @Override + public void visit(ClassOrInterface classOrInterface) { + throw new NotImplementedException(); + + } + + @Override + public void visit(RefType refType) { + throw new NotImplementedException(); + + } + + @Override + public void visit(SuperWildcardType superWildcardType) { + throw new NotImplementedException(); + + } + + @Override + public void visit(TypePlaceholder typePlaceholder) { + throw new NotImplementedException(); + + } + + @Override + public void visit(ExtendsWildcardType extendsWildcardType) { + throw new NotImplementedException(); + + } + + @Override + public void visit(GenericRefType genericRefType) { + throw new NotImplementedException(); + + } + +} diff --git a/src/de/dhbwstuttgart/strucTypes/FieldConstraint.java b/src/de/dhbwstuttgart/strucTypes/FieldConstraint.java new file mode 100644 index 00000000..e569b83a --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/FieldConstraint.java @@ -0,0 +1,33 @@ +package de.dhbwstuttgart.strucTypes; + +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; + +public class FieldConstraint { + + private RefTypeOrTPHOrWildcardOrGeneric classType; + private RefTypeOrTPHOrWildcardOrGeneric fieldType; + private String fieldName; + + public FieldConstraint(RefTypeOrTPHOrWildcardOrGeneric classType, RefTypeOrTPHOrWildcardOrGeneric fieldType, + String fieldName) { + this.classType = classType; + this.fieldType = fieldType; + this.fieldName = fieldName; + } + + public RefTypeOrTPHOrWildcardOrGeneric getClassType() { + return classType; + } + + public RefTypeOrTPHOrWildcardOrGeneric getFieldType() { + return fieldType; + } + + public String getFieldName() { + return fieldName; + } + + + + +} diff --git a/src/de/dhbwstuttgart/strucTypes/MethodConstraint.java b/src/de/dhbwstuttgart/strucTypes/MethodConstraint.java new file mode 100644 index 00000000..07d04037 --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/MethodConstraint.java @@ -0,0 +1,57 @@ +package de.dhbwstuttgart.strucTypes; + +import java.util.List; + +import org.antlr.v4.runtime.Token; + +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; + +public class MethodConstraint { + + private RefTypeOrTPHOrWildcardOrGeneric classType; + private RefTypeOrTPHOrWildcardOrGeneric returnType; + private String methodName; + private List arguments; + + public MethodConstraint(RefTypeOrTPHOrWildcardOrGeneric classType, RefTypeOrTPHOrWildcardOrGeneric returnType, + String methodName, List arguments) { + this.classType = classType; + this.returnType = returnType; + this.methodName = methodName; + this.arguments = arguments; + } + + public RefTypeOrTPHOrWildcardOrGeneric getClassType() { + return classType; + } + + public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { + return returnType; + } + + public String getMethodName() { + return methodName; + } + + public List getArguments() { + return arguments; + } + + public void addArgument(SubTypeConstraint... subTypeConstraints){ + for (SubTypeConstraint subTypeConstraint : subTypeConstraints) { + this.arguments.add(subTypeConstraint); + } + } + + public void addArgument(RefTypeOrTPHOrWildcardOrGeneric... parameters){ + for (RefTypeOrTPHOrWildcardOrGeneric parameter : parameters) { + Token offset = parameter.getOffset(); + TypePlaceholder tph = TypePlaceholder.fresh(offset); + SubTypeConstraint subTypeConstraint = new SubTypeConstraint(parameter, tph); + this.arguments.add(subTypeConstraint); + } + } + + +} diff --git a/src/de/dhbwstuttgart/strucTypes/StrucTYPE.java b/src/de/dhbwstuttgart/strucTypes/StrucTYPE.java new file mode 100644 index 00000000..e5a07769 --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/StrucTYPE.java @@ -0,0 +1,16 @@ +package de.dhbwstuttgart.strucTypes; + +import de.dhbwstuttgart.syntaxtree.SourceFile; + +public class StrucTYPE { + + private TypeAssumptions assumptions; + private SourceFile sourceFile; + + public StrucTYPE(TypeAssumptions assumptions, SourceFile sourceFile) { + this.assumptions = assumptions; + this.sourceFile = sourceFile; + } + + +} diff --git a/src/de/dhbwstuttgart/strucTypes/StrucTypeVisitor.java b/src/de/dhbwstuttgart/strucTypes/StrucTypeVisitor.java new file mode 100644 index 00000000..1c3689e8 --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/StrucTypeVisitor.java @@ -0,0 +1,187 @@ +package de.dhbwstuttgart.strucTypes; + +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; +import de.dhbwstuttgart.syntaxtree.ASTVisitor; +import de.dhbwstuttgart.syntaxtree.Constructor; +import de.dhbwstuttgart.syntaxtree.GenericDeclarationList; +import de.dhbwstuttgart.syntaxtree.GenericTypeVar; +import de.dhbwstuttgart.syntaxtree.statement.Assign; +import de.dhbwstuttgart.syntaxtree.statement.AssignToField; +import de.dhbwstuttgart.syntaxtree.statement.Binary; +import de.dhbwstuttgart.syntaxtree.statement.DoStmt; +import de.dhbwstuttgart.syntaxtree.statement.EmptyStmt; +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.LocalVarDecl; +import de.dhbwstuttgart.syntaxtree.statement.NewArray; +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.UnaryPlus; +import de.dhbwstuttgart.syntaxtree.statement.WhileStmt; +import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; +import de.dhbwstuttgart.syntaxtree.statement.literal.Null; +import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; +import de.dhbwstuttgart.syntaxtree.type.GenericRefType; +import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; + +public abstract class StrucTypeVisitor implements ASTVisitor { + @Override + public void visit(LambdaExpression lambdaExpression) { + throw new NotImplementedException(); + } + + @Override + public void visit(Assign assign) { + throw new NotImplementedException(); + + } + + @Override + public void visit(Binary binary) { + throw new NotImplementedException(); + + } + + @Override + public void visit(EmptyStmt emptyStmt) { + throw new NotImplementedException(); + + } + + @Override + public void visit(ForStmt forStmt) { + throw new NotImplementedException(); + + } + + @Override + public void visit(IfStmt ifStmt) { + throw new NotImplementedException(); + + } + + @Override + public void visit(InstanceOf instanceOf) { + throw new NotImplementedException(); + + } + + @Override + public void visit(LocalVarDecl localVarDecl) { + throw new NotImplementedException(); + + } + + @Override + public void visit(NewArray newArray) { + throw new NotImplementedException(); + + } + + @Override + public void visit(ReturnVoid aReturn) { + throw new NotImplementedException(); + + } + + @Override + public void visit(StaticClassName staticClassName) { + throw new NotImplementedException(); + + } + + @Override + public void visit(Super aSuper) { + throw new NotImplementedException(); + + } + + @Override + public void visit(UnaryPlus unaryPlus) { + throw new NotImplementedException(); + + } + + @Override + public void visit(WhileStmt whileStmt) { + throw new NotImplementedException(); + + } + + @Override + public void visit(DoStmt whileStmt) { + throw new NotImplementedException(); + + } + + @Override + public void visit(Null aNull) { + throw new NotImplementedException(); + + } + + @Override + public void visit(Literal literal) { + throw new NotImplementedException(); + + } + + @Override + public void visit(AssignToField assignLeftSide) { + throw new NotImplementedException(); + + } + + @Override + public void visit(AssignToLocal assignLeftSide) { + throw new NotImplementedException(); + + } + + @Override + public void visit(SuperCall superCall) { + throw new NotImplementedException(); + + } + + @Override + public void visit(GenericTypeVar genericTypeVar) { + throw new NotImplementedException(); + + } + + @Override + public void visit(GenericDeclarationList genericTypeVars) { + throw new NotImplementedException(); + + } + + @Override + public void visit(Constructor field) { + throw new NotImplementedException(); + + } + + @Override + public void visit(SuperWildcardType superWildcardType) { + throw new NotImplementedException(); + + } + + @Override + public void visit(ExtendsWildcardType extendsWildcardType) { + throw new NotImplementedException(); + + } + + @Override + public void visit(GenericRefType genericRefType) { + throw new NotImplementedException(); + + } +} diff --git a/src/de/dhbwstuttgart/strucTypes/SubTypeConstraint.java b/src/de/dhbwstuttgart/strucTypes/SubTypeConstraint.java new file mode 100644 index 00000000..8102ca02 --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/SubTypeConstraint.java @@ -0,0 +1,22 @@ +package de.dhbwstuttgart.strucTypes; + +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; + +public class SubTypeConstraint { + + private RefTypeOrTPHOrWildcardOrGeneric subtype; + private RefTypeOrTPHOrWildcardOrGeneric supertype; + + public SubTypeConstraint(RefTypeOrTPHOrWildcardOrGeneric subtype, RefTypeOrTPHOrWildcardOrGeneric supertype) { + this.subtype = subtype; + this.supertype = supertype; + } + public RefTypeOrTPHOrWildcardOrGeneric getSubtype() { + return subtype; + } + public RefTypeOrTPHOrWildcardOrGeneric getSupertype() { + return supertype; + } + + +} diff --git a/src/de/dhbwstuttgart/strucTypes/SyntaxTreePrinter.java b/src/de/dhbwstuttgart/strucTypes/SyntaxTreePrinter.java new file mode 100644 index 00000000..d35112a8 --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/SyntaxTreePrinter.java @@ -0,0 +1,361 @@ +package de.dhbwstuttgart.strucTypes; + +import java.util.List; + +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; +import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.syntaxtree.ASTVisitor; +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.Binary; +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.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.Receiver; +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.UnaryPlus; +import de.dhbwstuttgart.syntaxtree.statement.WhileStmt; +import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; +import de.dhbwstuttgart.syntaxtree.statement.literal.Null; +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 SyntaxTreePrinter implements ASTVisitor { + + + + + @Override + public void visit(SourceFile sourceFile) { + sourceFile.KlassenVektor.forEach(cl -> cl.accept(this)); + } + + @Override + public void visit(ClassOrInterface classOrInterface) { + JavaClassName className = classOrInterface.getClassName(); + List fields = classOrInterface.getFieldDecl(); + List methods = classOrInterface.getMethods(); + RefType superClass = classOrInterface.getSuperClass(); + List implementedInterfaces = classOrInterface.implementedInterfaces; + System.out.println("class: " + className); + fields.forEach(f->f.accept(this)); + methods.forEach(m->m.accept(this)); + System.out.print("superClass: "); + superClass.accept(this); + System.out.println("implemented Interfaces: "); + implementedInterfaces.forEach(i -> i.accept(this)); + + } + + @Override + public void visit(Field field) { + String name = field.getName(); + RefTypeOrTPHOrWildcardOrGeneric type = field.getType(); + System.out.print("field: " + name + " : "); + type.accept(this); + + } + + @Override + public void visit(Method method) { + String name = method.getName(); + RefTypeOrTPHOrWildcardOrGeneric returnType = method.getReturnType(); + ParameterList parameterList = method.getParameterList(); + System.out.print("method: " + name + " : "); + returnType.accept(this); + parameterList.accept(this); + method.block.accept(this); + + } + + @Override + public void visit(Block block) { + block.statements.forEach(s->s.accept(this)); + } + + @Override + public void visit(ArgumentList argumentList) { + List arguments = argumentList.getArguments(); + System.out.println("arguments: "); + arguments.forEach(a->a.accept(this)); + } + + @Override + public void visit(ParameterList formalParameters) { + List formalparalist = formalParameters.getFormalparalist(); + formalparalist.forEach(p->p.accept(this)); + + } + + @Override + public void visit(FormalParameter formalParameter) { + String name = formalParameter.getName(); + RefTypeOrTPHOrWildcardOrGeneric type = formalParameter.getType(); + System.out.print("parameter: " + name + " : "); + type.accept(this); + } + + @Override + public void visit(MethodCall methodCall) { + String name = methodCall.name; + RefTypeOrTPHOrWildcardOrGeneric type = methodCall.getType(); + ArgumentList argumentList = methodCall.getArgumentList(); + Receiver receiver = methodCall.receiver; + System.out.print("methodCall: " + name + " : "); + type.accept(this); + argumentList.accept(this); + System.out.print("receiver: "); + receiver.accept(this); + + } + + @Override + public void visit(Return aReturn) { + RefTypeOrTPHOrWildcardOrGeneric type = aReturn.getType(); + System.out.println("returnType: " + type); + aReturn.retexpr.accept(this); + } + + @Override + public void visit(LocalVar localVar) { + String name = localVar.name; + RefTypeOrTPHOrWildcardOrGeneric type = localVar.getType(); + System.out.print("localVar: " + name + " : "); + type.accept(this); + } + + @Override + public void visit(LocalVarDecl localVarDecl) { + throw new NotImplementedException(); + } + + @Override + public void visit(FieldVar fieldVar) { + String name = fieldVar.fieldVarName; + RefTypeOrTPHOrWildcardOrGeneric type = fieldVar.getType(); + Expression receiver = fieldVar.receiver; + System.out.print("fieldVar: " + name + " : "); + type.accept(this); + System.out.println("receiver: "); + receiver.accept(this); + } + + @Override + public void visit(CastExpr castExpr) { + Expression expr = castExpr.expr; + RefTypeOrTPHOrWildcardOrGeneric type = castExpr.getType(); + System.out.print("castExpr: "); + expr.accept(this); + System.out.print("typeCastExpr: "); + type.accept(this); + + } + + @Override + public void visit(NewClass newClass) { + String name = newClass.name; + RefTypeOrTPHOrWildcardOrGeneric type = newClass.getType(); + ArgumentList argumentList = newClass.getArgumentList(); + System.out.print("new: " + name + " : "); + type.accept(this); + argumentList.accept(this); + + } + + @Override + public void visit(This aThis) { + ArgumentList arglist = aThis.arglist; + RefTypeOrTPHOrWildcardOrGeneric type = aThis.getType(); + System.out.print("this: " + " : "); + type.accept(this); + if( arglist != null) arglist.accept(this); + } + + @Override + public void visit(RefType refType) { + JavaClassName name = refType.getName(); + System.out.println(name); + + } + + @Override + public void visit(TypePlaceholder typePlaceholder) { + String name = typePlaceholder.getName(); + System.out.println("TPH " + name); + + + } + + @Override + public void visit(NewArray newArray) { + throw new NotImplementedException(); + } + + @Override + public void visit(ReturnVoid aReturn) { + throw new NotImplementedException(); + } + + @Override + public void visit(StaticClassName staticClassName) { + throw new NotImplementedException(); + } + + @Override + public void visit(Super aSuper) { + throw new NotImplementedException(); + } + + @Override + public void visit(UnaryPlus unaryPlus) { + throw new NotImplementedException(); + } + + @Override + public void visit(LambdaExpression lambdaExpression) { + throw new NotImplementedException(); + + } + + @Override + public void visit(WhileStmt whileStmt) { + throw new NotImplementedException(); + } + + @Override + public void visit(DoStmt whileStmt) { + throw new NotImplementedException(); + } + + @Override + public void visit(Assign assign) { + throw new NotImplementedException(); + + } + + @Override + public void visit(ForStmt forStmt) { + throw new NotImplementedException(); + } + + @Override + public void visit(Binary binary) { + throw new NotImplementedException(); + } + + @Override + public void visit(IfStmt ifStmt) { + throw new NotImplementedException(); + } + + @Override + public void visit(EmptyStmt emptyStmt) { + throw new NotImplementedException(); + } + + @Override + public void visit(Null aNull) { + throw new NotImplementedException(); + } + + @Override + public void visit(Literal literal) { + RefTypeOrTPHOrWildcardOrGeneric type = literal.getType(); + System.out.print("literal: "); + type.accept(this); + } + + @Override + public void visit(InstanceOf instanceOf) { + throw new NotImplementedException(); + } + + @Override + public void visit(AssignToField assignLeftSide) { + throw new NotImplementedException(); + } + + @Override + public void visit(AssignToLocal assignLeftSide) { + throw new NotImplementedException(); + } + + @Override + public void visit(SuperCall superCall) { + throw new NotImplementedException(); + } + + @Override + public void visit(ExpressionReceiver expressionReceiver) { + Expression expr = expressionReceiver.expr; + RefTypeOrTPHOrWildcardOrGeneric type = expressionReceiver.getType(); + System.out.print("expressionReceiverType: " + type); + System.out.print(" expressionReceiver: "); + expr.accept(this); + + } + + @Override + public void visit(GenericTypeVar genericTypeVar) { + throw new NotImplementedException(); + } + + @Override + public void visit(GenericDeclarationList genericTypeVars) { + throw new NotImplementedException(); + } + + @Override + public void visit(Constructor field) { + throw new NotImplementedException(); + } + + @Override + public void visit(SuperWildcardType superWildcardType) { + throw new NotImplementedException(); + } + + @Override + public void visit(ExtendsWildcardType extendsWildcardType) { + throw new NotImplementedException(); + } + + @Override + public void visit(GenericRefType genericRefType) { + throw new NotImplementedException(); + } + +} diff --git a/src/de/dhbwstuttgart/strucTypes/TYPEExpr.java b/src/de/dhbwstuttgart/strucTypes/TYPEExpr.java new file mode 100644 index 00000000..aee55872 --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/TYPEExpr.java @@ -0,0 +1,188 @@ +package de.dhbwstuttgart.strucTypes; + +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; +import de.dhbwstuttgart.syntaxtree.ASTVisitor; +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.Binary; +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.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.UnaryPlus; +import de.dhbwstuttgart.syntaxtree.statement.WhileStmt; +import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; +import de.dhbwstuttgart.syntaxtree.statement.literal.Null; +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 TYPEExpr extends StrucTypeVisitor { + + private TypeAssumptions assumptions; + private ConstraintsSet constraints; + + public TYPEExpr(TypeAssumptions assumptions) { + this.assumptions = assumptions; + } + + public ConstraintsSet getConstraints(){ + return this.constraints; + } + + @Override + public void visit(SourceFile sourceFile) { + // TODO Auto-generated method stub + throw new NotImplementedException(); + } + + @Override + public void visit(ClassOrInterface classOrInterface) { + // TODO Auto-generated method stub + throw new NotImplementedException(); + + } + + @Override + public void visit(Field field) { + // TODO Auto-generated method stub + throw new NotImplementedException(); + + } + + @Override + public void visit(Method method) { + // TODO Auto-generated method stub + throw new NotImplementedException(); + + } + + @Override + public void visit(ParameterList formalParameters) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(FormalParameter formalParameter) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(Block block) { + // TODO Auto-generated method stub + throw new NotImplementedException(); + + } + + @Override + public void visit(Return aReturn) { + // TODO Auto-generated method stub + throw new NotImplementedException(); + + } + + @Override + public void visit(ExpressionReceiver expressionReceiver) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(LocalVar localVar) { + // keine neuen Constraints + + } + + @Override + public void visit(FieldVar fieldVar) { + fieldVar.receiver.accept(this); + CheckTypeVisitor fieldTypeVisitor = new CheckTypeVisitor(); + fieldVar.getType().accept(fieldTypeVisitor); + //TODO check ty in Ass && rty f in fields(ty) + if(!fieldTypeVisitor.isTypeVariable()){} // keine neuen Constraints + else{ + FieldConstraint fieldConstraint = new FieldConstraint(fieldVar.receiver.getType(), fieldVar.getType(), fieldVar.fieldVarName); + this.constraints.addConstraint(fieldConstraint); + } + + } + + @Override + public void visit(MethodCall methodCall) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(ArgumentList argumentList) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(CastExpr castExpr) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(NewClass newClass) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(RefType refType) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(TypePlaceholder typePlaceholder) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(This aThis) { + // TODO Auto-generated method stub + + } + + +} + diff --git a/src/de/dhbwstuttgart/strucTypes/TypeAssumptions.java b/src/de/dhbwstuttgart/strucTypes/TypeAssumptions.java new file mode 100644 index 00000000..3397fd29 --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/TypeAssumptions.java @@ -0,0 +1,5 @@ +package de.dhbwstuttgart.strucTypes; + +public class TypeAssumptions { + +} diff --git a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java index 540bc993..1c34ad43 100644 --- a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java +++ b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java @@ -26,7 +26,7 @@ public class ClassOrInterface extends SyntaxTreeNode implements TypeScope{ private GenericDeclarationList genericClassParameters; private RefType superClass; protected boolean isInterface; - private List implementedInterfaces; + public final List implementedInterfaces; private List constructors; public ClassOrInterface(int modifiers, JavaClassName name, List fielddecl, List methods, List constructors, GenericDeclarationList genericClassParameters, diff --git a/test/strucType/Test.java b/test/strucType/Test.java deleted file mode 100644 index fc856247..00000000 --- a/test/strucType/Test.java +++ /dev/null @@ -1,20 +0,0 @@ -package strucType; - -import static org.junit.Assert.*; - -import java.io.File; -import java.io.IOException; - -import de.dhbwstuttgart.core.JavaTXCompiler; - -public class Test { - public static final String rootDirectory = System.getProperty("user.dir")+"/test/strucType/javFiles/"; - - - @org.junit.Test - public void test() throws ClassNotFoundException, IOException { - JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory + "test.jav")); - System.out.println("test ende"); - } - -} diff --git a/test/strucType/TestSyntaxTreePrinter.java b/test/strucType/TestSyntaxTreePrinter.java new file mode 100644 index 00000000..d9d15916 --- /dev/null +++ b/test/strucType/TestSyntaxTreePrinter.java @@ -0,0 +1,73 @@ +package strucType; + + +import java.io.File; +import java.io.IOException; +import java.io.Serializable; +import java.util.ArrayList; + + +import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.strucTypes.SyntaxTreePrinter; + +public class TestSyntaxTreePrinter { + public static final String rootDirectory = System.getProperty("user.dir")+"/test/strucType/javFiles/"; + + + @org.junit.Test + 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 + "testPaperExample.jav")); + JavaTXCompiler compiler = new JavaTXCompiler(files); + compiler.sourceFiles.keySet().forEach(f->{ + String name = f.getName(); + System.out.println("Filename: " + name); + compiler.sourceFiles.get(f).accept(new SyntaxTreePrinter()); + System.out.println(); + }); +// this.printSyntaxTree(compiler); + System.out.println("test end"); + } + +// private void printSyntaxTree(JavaTXCompiler compiler){ +// Collection sourceFiles= compiler.sourceFiles.values(); +// for (SourceFile sourceFile : sourceFiles) { +// List klassenVektor = sourceFile.KlassenVektor; +// for (ClassOrInterface classOrInterface : klassenVektor) { +// JavaClassName className = classOrInterface.getClassName(); +// System.out.println("class: "+ className); +// List fields = classOrInterface.getFieldDecl(); +// List methods = classOrInterface.getMethods(); +// for (Field field : fields) { +// String fName = field.getName(); +// RefTypeOrTPHOrWildcardOrGeneric fType = field.getType(); +// System.out.println("field: "+ fName + " : " + fType); +// } +// for (Method method : methods) { +// String mName = method.getName(); +// RefTypeOrTPHOrWildcardOrGeneric mReturnType = method.getReturnType(); +// System.out.println("method: " + mName + " : " + mReturnType); +// ParameterList mParameterList = method.getParameterList(); +// for (FormalParameter formalParameter : mParameterList) { +// String paraName = formalParameter.getName(); +// RefTypeOrTPHOrWildcardOrGeneric paraType = formalParameter.getType(); +// System.out.println("parameter: " + paraName + " : " + paraType); +// } +// RefTypeOrTPHOrWildcardOrGeneric blockType = method.block.getType(); +// System.out.println("blockType: " + blockType); +// List blockStatements = method.block.getStatements(); +// for (Statement statement : blockStatements) { +// RefTypeOrTPHOrWildcardOrGeneric statementType = statement.getType(); +// System.out.println("statementType: " + statementType); +// } +// } +// } +// } +// } + +} diff --git a/test/strucType/javFiles/test.jav b/test/strucType/javFiles/test.jav deleted file mode 100644 index c3e6bede..00000000 --- a/test/strucType/javFiles/test.jav +++ /dev/null @@ -1,4 +0,0 @@ -class C1 extends Object -{ - m(para) { return para; } -} \ No newline at end of file diff --git a/test/strucType/javFiles/testCast.jav b/test/strucType/javFiles/testCast.jav new file mode 100644 index 00000000..757d5a27 --- /dev/null +++ b/test/strucType/javFiles/testCast.jav @@ -0,0 +1,4 @@ +class C +{ + mC(x){return (Integer)x; } +} \ No newline at end of file diff --git a/test/strucType/javFiles/testFieldMethod.jav b/test/strucType/javFiles/testFieldMethod.jav new file mode 100644 index 00000000..06356bdd --- /dev/null +++ b/test/strucType/javFiles/testFieldMethod.jav @@ -0,0 +1,5 @@ +class M +{ + f; + mM(x, y) { return f.mF(x, y); } +} \ No newline at end of file diff --git a/test/strucType/javFiles/testFieldVar.jav b/test/strucType/javFiles/testFieldVar.jav new file mode 100644 index 00000000..50466e04 --- /dev/null +++ b/test/strucType/javFiles/testFieldVar.jav @@ -0,0 +1,8 @@ +class F +{ + f; + + mF() { return f; } + + mX(x) { return x.f; } +} \ No newline at end of file diff --git a/test/strucType/javFiles/testLocalVar.jav b/test/strucType/javFiles/testLocalVar.jav new file mode 100644 index 00000000..ff887250 --- /dev/null +++ b/test/strucType/javFiles/testLocalVar.jav @@ -0,0 +1,6 @@ +import java.util.List; + +class C1 implements List +{ + m(x) { return x; } +} diff --git a/test/strucType/javFiles/testNew.jav b/test/strucType/javFiles/testNew.jav new file mode 100644 index 00000000..6633683f --- /dev/null +++ b/test/strucType/javFiles/testNew.jav @@ -0,0 +1,4 @@ +class N +{ + mN(s) {return new String(s); } +} \ No newline at end of file diff --git a/test/strucType/javFiles/testPaperExample.jav b/test/strucType/javFiles/testPaperExample.jav new file mode 100644 index 00000000..5b52508b --- /dev/null +++ b/test/strucType/javFiles/testPaperExample.jav @@ -0,0 +1,4 @@ +class A +{ + mt(x, y, z) { return x.sub(y).add(z); } +} \ No newline at end of file