diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 6b114e74..7de0e938 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -39,6 +39,8 @@ public class JavaTXCompiler { for(Constraint constraint : xCons){ xConsSet.addAll(constraint); } + + System.out.println(xConsSet); Set> result = unify.unify(xConsSet, finiteClosure); System.out.println(result); } diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/GenericContext.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/GenericContext.java new file mode 100644 index 00000000..88f94ee8 --- /dev/null +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/GenericContext.java @@ -0,0 +1,14 @@ +package de.dhbwstuttgart.parser.SyntaxTreeGenerator; + +import de.dhbwstuttgart.typecheck.JavaClassName; + +public class GenericContext { + public final String parentMethod; + public final JavaClassName parentClass; + + public GenericContext(JavaClassName parentClass, String parentMethod) { + if(parentMethod == null)parentMethod = ""; + this.parentClass = parentClass; + this.parentMethod = parentMethod; + } +} diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/GenericsRegistry.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/GenericsRegistry.java index 8684ae1c..08212fc9 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/GenericsRegistry.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/GenericsRegistry.java @@ -1,7 +1,7 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; -import java.util.HashSet; +import java.util.HashMap; -public class GenericsRegistry extends HashSet { +public class GenericsRegistry extends HashMap { -} +} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 90b21d0b..68b6afda 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -10,6 +10,7 @@ import de.dhbwstuttgart.syntaxtree.statement.literal.*; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.typecheck.JavaClassName; import de.dhbwstuttgart.typecheck.JavaClassRegistry; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.tree.TerminalNode; @@ -33,12 +34,12 @@ public class StatementGenerator { } - public Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext) { + public Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext, JavaClassName parentClass) { Java8Parser.MethodHeaderContext header = methodDeclarationContext.methodHeader(); String name = header.methodDeclarator().Identifier().getText(); GenericDeclarationList gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), methodDeclarationContext.methodHeader().getStart()); if(methodDeclarationContext.methodHeader().typeParameters() != null){ - gtvDeclarations = TypeGenerator.convert(methodDeclarationContext.methodHeader().typeParameters(), reg, generics); + gtvDeclarations = TypeGenerator.convert(methodDeclarationContext.methodHeader().typeParameters(), parentClass, name,reg, generics); } RefTypeOrTPHOrWildcardOrGeneric retType; if(header.result() != null){ @@ -128,7 +129,7 @@ public class StatementGenerator { if(stmt.block() != null){ return convert(stmt.block()); }else if(stmt.emptyStatement() != null){ - return new EmptyStmt(); + return new EmptyStmt(stmt.getStart()); }else if(stmt.expressionStatement() != null){ return convert(stmt.expressionStatement()); }else if(stmt.assertStatement() != null){ @@ -159,7 +160,7 @@ public class StatementGenerator { Statement stmt = convert(statementContext); statements.add(stmt); } - + statements = SyntacticSugar.addTrailingReturn(statements); return new Block(statements, block.getStart()); } @@ -228,6 +229,8 @@ public class StatementGenerator { receiver = convert(methodInvocationContext.primary()); }else if(methodInvocationContext.toString().startsWith("super")){ receiver = new Super(methodInvocationContext.getStart()); + }else if(methodInvocationContext.methodName() != null){ + receiver = new This(methodInvocationContext.getStart()); }else throw new NotImplementedException(); ArgumentList argumentList = convert(methodInvocationContext.argumentList()); @@ -631,7 +634,15 @@ public class StatementGenerator { } private Expression convert(Java8Parser.CastExpressionContext expressionContext) { - throw new NotImplementedException(); + Expression expr = null; + if(expressionContext.unaryExpression() != null){ + throw new NotImplementedException(); + }else if(expressionContext.unaryExpressionNotPlusMinus() != null){ + expr = convert(expressionContext.unaryExpressionNotPlusMinus()); + }else if(expressionContext.lambdaExpression() != null){ + expr = convert(expressionContext.lambdaExpression()); + } + return new CastExpr(TypeGenerator.convert(expressionContext.referenceType(), reg, generics),expr, expressionContext.getStart()); } private Expression convert(Java8Parser.PostfixExpressionContext expression) { diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java new file mode 100644 index 00000000..07b2d1fb --- /dev/null +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java @@ -0,0 +1,34 @@ +package de.dhbwstuttgart.parser.SyntaxTreeGenerator; + +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.syntaxtree.statement.*; + +import java.util.List; + +public class SyntacticSugar { + + public static List addTrailingReturn(List statements){ + if(statements.size()!=0) { + Statement lastStmt = statements.get(statements.size() - 1); + if (lastStmt instanceof Return) return statements; + if (lastStmt instanceof WhileStmt) { + if (hasReturn(((WhileStmt) lastStmt).loop_block)) return statements; + } else if (lastStmt instanceof IfStmt) { + if (hasReturn(((IfStmt) lastStmt).then_block) + && hasReturn(((IfStmt) lastStmt).else_block)) return statements; + } else if (lastStmt instanceof ForStmt) { + if (hasReturn(((ForStmt) lastStmt).body_Loop_block)) return statements; + } else { + } + } + statements.add(new ReturnVoid(new NullToken())); + return statements; + } + + private static boolean hasReturn(Block block){ + for(Statement s : block.getStatements()) + if(s instanceof Return)return true; + return false; + } + +} diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index cc512bd8..23ad5028 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -200,10 +200,9 @@ public class SyntaxTreeGenerator{ } } JavaClassName name = reg.getName(ctx.Identifier().getText()); - GenericDeclarationList genericClassParameters = TypeGenerator.convert(ctx.typeParameters(), reg, generics); - Block class_block = null; + GenericDeclarationList genericClassParameters = TypeGenerator.convert(ctx.typeParameters(), name, "",reg, generics); List fielddecl = convertFields(ctx.classBody()); - List methods = convertMethods(ctx.classBody()); + List methods = convertMethods(ctx.classBody(), name); Token offset = ctx.getStart(); RefType superClass ; @@ -221,7 +220,7 @@ public class SyntaxTreeGenerator{ throw new NotImplementedException(); } - private List convertMethods(Java8Parser.ClassBodyContext classBodyContext) { + private List convertMethods(Java8Parser.ClassBodyContext classBodyContext, JavaClassName parentClass) { List ret = new ArrayList<>(); for(Java8Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){ if(classMember.classMemberDeclaration() != null){ @@ -230,7 +229,7 @@ public class SyntaxTreeGenerator{ //Do nothing! }else if(classMemberDeclarationContext.methodDeclaration()!= null){ StatementGenerator stmtGen = new StatementGenerator(reg, generics); - ret.add(stmtGen.convert(classMemberDeclarationContext.methodDeclaration())); + ret.add(stmtGen.convert(classMemberDeclarationContext.methodDeclaration(), parentClass)); } } } diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java index 8c2ad269..dc0d71af 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java @@ -24,14 +24,7 @@ public class TypeGenerator { public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.UnannClassOrInterfaceTypeContext unannClassOrInterfaceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) { String name = unannClassOrInterfaceTypeContext.getText(); - if(!reg.contains(name)){ //Dann könnte es ein Generische Type sein: - if(generics.contains(name)){ - return new GenericRefType(new GenericTypeName(name), unannClassOrInterfaceTypeContext.getStart()); - }else{ - throw new TypeinferenceException("Der Typ "+ name + " ist nicht vorhanden",unannClassOrInterfaceTypeContext.getStart()); - } - } - return new RefType(reg.getName(name), unannClassOrInterfaceTypeContext.getStart()); + return convertTypeName(name,unannClassOrInterfaceTypeContext.getStart(), reg, generics); } public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.UnannTypeContext unannTypeContext, JavaClassRegistry reg, GenericsRegistry genericsRegistry) { @@ -48,25 +41,25 @@ public class TypeGenerator { return TypeGenerator.convert(unannTypeContext.unannReferenceType().unannClassOrInterfaceType(), reg, genericsRegistry); } - public static GenericDeclarationList convert(Java8Parser.TypeParametersContext typeParametersContext, JavaClassRegistry reg, GenericsRegistry generics) { + public static GenericDeclarationList convert(Java8Parser.TypeParametersContext typeParametersContext, JavaClassName parentClass, String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) { if(typeParametersContext == null){ return new GenericDeclarationList(new ArrayList<>(), new NullToken()); } Token endOffset = typeParametersContext.getStop(); List typeVars = new ArrayList<>(); for(Java8Parser.TypeParameterContext typeParameter : typeParametersContext.typeParameterList().typeParameter()){ - typeVars.add(convert(typeParameter, reg, generics)); + typeVars.add(convert(typeParameter, parentClass, parentMethod, reg, generics)); endOffset = typeParameter.getStop(); } return new GenericDeclarationList(typeVars, endOffset); } - public static GenericTypeVar convert(Java8Parser.TypeParameterContext typeParameter, JavaClassRegistry reg, GenericsRegistry generics) { + public static GenericTypeVar convert(Java8Parser.TypeParameterContext typeParameter, JavaClassName parentClass, String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) { String name = typeParameter.Identifier().getText(); List bounds = TypeGenerator.convert(typeParameter.typeBound(),reg, generics); - GenericTypeVar ret = new GenericTypeVar(name, bounds, typeParameter.getStart(), typeParameter.getStop()); - generics.add(name); + GenericTypeVar ret = new GenericTypeVar(new GenericTypeName(new GenericContext(parentClass, parentMethod), name), bounds, typeParameter.getStart(), typeParameter.getStop()); + generics.put(name, new GenericContext(parentClass, parentMethod)); return ret; } @@ -79,4 +72,19 @@ public class TypeGenerator { throw new de.dhbwstuttgart.exceptions.NotImplementedException(); } + + public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.ReferenceTypeContext referenceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) { + return convertTypeName(referenceTypeContext.getText(), referenceTypeContext.getStart(), reg, generics); + } + + private static RefTypeOrTPHOrWildcardOrGeneric convertTypeName(String name, Token offset, JavaClassRegistry reg, GenericsRegistry generics){ + if(!reg.contains(name)){ //Dann könnte es ein Generische Type sein: + if(generics.keySet().contains(name)){ + return new GenericRefType(new GenericTypeName(generics.get(name),name), offset); + }else{ + throw new TypeinferenceException("Der Typ "+ name + " ist nicht vorhanden",offset); + } + } + return new RefType(reg.getName(name), offset); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java index 66eb84cd..14422351 100755 --- a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java +++ b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java @@ -85,4 +85,8 @@ public class ClassOrInterface extends GTVDeclarationContext implements IItemWith public RefTypeOrTPHOrWildcardOrGeneric getSuperClass() { return superClass; } + + public Iterable getGenerics() { + return this.genericClassParameters; + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java b/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java index 464f5aaa..ceea42b1 100644 --- a/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java +++ b/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java @@ -24,6 +24,6 @@ public class GenericDeclarationList extends SyntaxTreeNode implements Iterable iterator() { - return null; + return gtvs.iterator(); } } diff --git a/src/de/dhbwstuttgart/syntaxtree/GenericTypeVar.java b/src/de/dhbwstuttgart/syntaxtree/GenericTypeVar.java index 4c30e6d8..5d151046 100755 --- a/src/de/dhbwstuttgart/syntaxtree/GenericTypeVar.java +++ b/src/de/dhbwstuttgart/syntaxtree/GenericTypeVar.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.typecheck.GenericTypeName; import org.antlr.v4.runtime.Token; import java.util.ArrayList; @@ -23,9 +24,9 @@ public class GenericTypeVar extends SyntaxTreeNode */ List bounds=new ArrayList(); private Token endOffset; - private String name; + private GenericTypeName name; - public GenericTypeVar(String s, List bounds, Token offset, Token endOffset) + public GenericTypeVar(GenericTypeName s, List bounds, Token offset, Token endOffset) { super(offset); name = s; @@ -47,4 +48,7 @@ public class GenericTypeVar extends SyntaxTreeNode return "BoGTV " + this.name; } + public GenericTypeName getName() { + return name; + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index ce8c95a5..f8ed9be1 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -5,8 +5,11 @@ import java.util.ArrayList; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; +import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; +import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory; +import de.dhbwstuttgart.typeinference.constraints.Pair; import org.antlr.v4.runtime.Token; import de.dhbwstuttgart.core.IItemWithOffset; @@ -25,17 +28,24 @@ public class Method extends Field implements IItemWithOffset private Block block; private ParameterList parameterlist = new ParameterList(new ArrayList<>(), new NullToken()); private ExceptionList exceptionlist; + private GenericDeclarationList generics; public Method(String name, RefTypeOrTPHOrWildcardOrGeneric returnType, int modifiers, ParameterList parameterList, Block block, GenericDeclarationList gtvDeclarations, Token offset) { super(name, returnType, modifiers, offset); this.parameterlist = parameterList; this.block = block; + this.generics = gtvDeclarations; } public ConstraintSet getConstraints(TypeInferenceInformation info, ClassOrInterface currentClass) { ConstraintSet ret = new ConstraintSet(); - ret.addAll(block.getConstraints(new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, this))); + TypeInferenceBlockInformation blockInfo = new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, this); + ret.addAll(block.getConstraints(blockInfo)); + /* + ret.addUndConstraint( + ConstraintsFactory.createPair(block.getType(), this.getType(), blockInfo)); + */ return ret; } @@ -43,11 +53,13 @@ public class Method extends Field implements IItemWithOffset return parameterlist; } - //erweitert Sebastian Ritt public Block get_Block() { return block; } - - + + + public Iterable getGenerics() { + return generics; + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index 1345edb6..c12dbd46 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -6,10 +6,10 @@ import java.util.ArrayList; import java.util.List; import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext; import de.dhbwstuttgart.syntaxtree.Field; import de.dhbwstuttgart.syntaxtree.Method; -import de.dhbwstuttgart.syntaxtree.type.GenericRefType; -import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.syntaxtree.type.Void; import de.dhbwstuttgart.typecheck.GenericTypeName; import de.dhbwstuttgart.typecheck.JavaClassName; @@ -17,7 +17,6 @@ import de.dhbwstuttgart.typecheck.JavaClassRegistry; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.statement.Block; import de.dhbwstuttgart.syntaxtree.statement.Statement; -import de.dhbwstuttgart.syntaxtree.type.RefType; import org.antlr.v4.runtime.Token; /** @@ -48,13 +47,13 @@ public class ASTFactory { java.lang.Class superjreClass = jreClass.getSuperclass(); RefType superClass; if(superjreClass != null){ - superClass = (RefType) createType(superjreClass); + superClass = (RefType) createType(superjreClass, name, ""); }else{//Jede Klasse und jedes Interface erbt von Object: (auch Object selbst!) - superClass = (RefType) createType(java.lang.Object.class); + superClass = (RefType) createType(java.lang.Object.class, name, ""); } List implementedInterfaces = new ArrayList<>(); for(java.lang.Class jreInterface : jreClass.getInterfaces()){ - implementedInterfaces.add((RefType) createType(jreInterface)); + implementedInterfaces.add((RefType) createType(jreInterface, name, "")); } GenericDeclarationList genericDeclarationList = createGenerics(jreClass.getTypeParameters(), jreClass, null); @@ -64,11 +63,11 @@ public class ASTFactory { private de.dhbwstuttgart.syntaxtree.Constructor createConstructor(Constructor constructor, Class inClass) { String name = constructor.getName(); - RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass); + RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass, names.getName(inClass.getName()), name); Parameter[] jreParams = constructor.getParameters(); List params = new ArrayList<>(); for(Parameter jreParam : jreParams){ - RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType()); + RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType(),names.getName(inClass.getName()), name); params.add(new FormalParameter(jreParam.getName(),paramType, new NullToken())); } ParameterList parameterList = new ParameterList(params, new NullToken()); @@ -83,11 +82,11 @@ public class ASTFactory { public Method createMethod(java.lang.reflect.Method jreMethod, java.lang.Class inClass){ String name = jreMethod.getName(); RefTypeOrTPHOrWildcardOrGeneric returnType; - returnType = createType(jreMethod.getReturnType()); + returnType = createType(jreMethod.getReturnType(),names.getName(inClass.getName()), name); Parameter[] jreParams = jreMethod.getParameters(); List params = new ArrayList<>(); for(Parameter jreParam : jreParams){ - RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType()); + RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType(),names.getName(inClass.getName()), name); params.add(new FormalParameter(jreParam.getName(),paramType, new NullToken())); } ParameterList parameterList = new ParameterList(params, new NullToken()); @@ -99,14 +98,14 @@ public class ASTFactory { return new Method(name,returnType, modifier, parameterList, block, gtvDeclarations, offset); } - public GenericDeclarationList createGenerics(TypeVariable[] typeParameters, Class context, String methodName){ - List gtvs = new ArrayList<>(); - for(TypeVariable jreTV : typeParameters){ - de.dhbwstuttgart.syntaxtree.GenericTypeVar gtv = createGeneric(jreTV, jreTV.getName()); - gtvs.add(gtv); - } - return new GenericDeclarationList(gtvs,new NullToken()); - } + public GenericDeclarationList createGenerics(TypeVariable[] typeParameters, Class context, String methodName){ + List gtvs = new ArrayList<>(); + for(TypeVariable jreTV : typeParameters){ + de.dhbwstuttgart.syntaxtree.GenericTypeVar gtv = createGeneric(jreTV, jreTV.getName(), context, methodName); + gtvs.add(gtv); + } + return new GenericDeclarationList(gtvs,new NullToken()); + } /* public RefType createType(java.lang.Class jreClass){ @@ -119,7 +118,7 @@ public class ASTFactory { } */ - public RefTypeOrTPHOrWildcardOrGeneric createType(java.lang.reflect.Type type){ + public RefTypeOrTPHOrWildcardOrGeneric createType(java.lang.reflect.Type type, JavaClassName parentClass, String parentMethod){ if(type.getTypeName().equals("void")){ return new Void(new NullToken()); }else if(type.getTypeName().equals("int")){ @@ -139,12 +138,14 @@ public class ASTFactory { }else{ if(type instanceof TypeVariable){ //GTVDeclarationContext via "(TypeVariable) type).getGenericDeclaration()" - return new GenericRefType(new GenericTypeName(type.getTypeName()), new NullToken()); + return new GenericRefType( + new GenericTypeName(new GenericContext(parentClass, parentMethod),type.getTypeName()), + new NullToken()); } List params = new ArrayList<>(); if(type instanceof ParameterizedType){ for(Type t : ((ParameterizedType)type).getActualTypeArguments()){ - params.add(createType(t)); + params.add(createType(t, parentClass, parentMethod)); } } RefType ret = new RefType(this.names.getName(type.getTypeName()), params, new NullToken()); @@ -152,15 +153,18 @@ public class ASTFactory { } } - public de.dhbwstuttgart.syntaxtree.GenericTypeVar createGeneric(TypeVariable jreTypeVar, String name){ + public de.dhbwstuttgart.syntaxtree.GenericTypeVar createGeneric(TypeVariable jreTypeVar, String jreTVName, Class context, String parentMethod){ + JavaClassName parentClass = names.getName(context.getName()); List genericBounds = new ArrayList<>(); java.lang.reflect.Type[] bounds = jreTypeVar.getBounds(); if(bounds.length > 0){ for(java.lang.reflect.Type bound : bounds){ - genericBounds.add((RefType) createType(bound)); + genericBounds.add((RefType) createType(bound, parentClass, parentMethod)); } } - return new de.dhbwstuttgart.syntaxtree.GenericTypeVar(name, genericBounds, new NullToken(), new NullToken()); + return new de.dhbwstuttgart.syntaxtree.GenericTypeVar( + new GenericTypeName(new GenericContext(parentClass, parentMethod), jreTVName) + , genericBounds, new NullToken(), new NullToken()); } public ClassOrInterface createObjectClass() { @@ -212,4 +216,4 @@ public class ASTFactory { return createObjectClass().getType(); } */ -} +} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java index a0b65973..ae536e81 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java @@ -3,8 +3,8 @@ package de.dhbwstuttgart.syntaxtree.statement; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.constraints.Pair; -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; +import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import org.antlr.v4.runtime.Token; @@ -23,7 +23,8 @@ public class Assign extends Statement public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { ConstraintSet ret = lefSide.getConstraints(info); ret.addAll(rightSide.getConstraints(info)); - ret.addUndConstraint(new Pair(rightSide.getType(), lefSide.getType())); + ret.addUndConstraint(ConstraintsFactory.createPair( + rightSide.getType(), lefSide.getType(), PairOperator.SMALLERDOT, info)); return ret; } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Block.java b/src/de/dhbwstuttgart/syntaxtree/statement/Block.java index 56b9a1f7..04e4a106 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Block.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Block.java @@ -31,8 +31,6 @@ public class Block extends Statement } return ret; } - - } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/CastExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/CastExpr.java index 6145a826..a8294daf 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/CastExpr.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/CastExpr.java @@ -3,14 +3,16 @@ package de.dhbwstuttgart.syntaxtree.statement; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import org.antlr.v4.runtime.Token; import sun.reflect.generics.reflectiveObjects.NotImplementedException; public class CastExpr extends Expression { - public CastExpr(RefTypeOrTPHOrWildcardOrGeneric castType, Expression expr, int offset) + public CastExpr(RefTypeOrTPHOrWildcardOrGeneric castType, Expression expr, Token offset) { - super(null, null); + super(castType, offset); + this.expr = expr; } public Expression expr; diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/EmptyStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/EmptyStmt.java index 164ff6d6..1ae2df74 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/EmptyStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/EmptyStmt.java @@ -1,15 +1,17 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.syntaxtree.type.Void; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; +import org.antlr.v4.runtime.Token; public class EmptyStmt extends Statement { - public EmptyStmt() + public EmptyStmt(Token offset) { - super(null,null); + super(new Void(offset),offset); } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/FieldVar.java b/src/de/dhbwstuttgart/syntaxtree/statement/FieldVar.java index 8423e38c..8a0124d0 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/FieldVar.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/FieldVar.java @@ -6,9 +6,9 @@ import de.dhbwstuttgart.typeinference.assumptions.FieldAssumption; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import org.antlr.v4.runtime.Token; -import sun.reflect.generics.reflectiveObjects.NotImplementedException; import java.util.HashSet; import java.util.Set; @@ -31,8 +31,10 @@ public class FieldVar extends Expression { ret.addAll(receiver.getConstraints(info)); for(FieldAssumption fieldAssumption : info.getFields(fieldVarName)){ Constraint constraint = new Constraint(); - constraint.add(new Pair(receiver.getType(),fieldAssumption.getReceiverType())); - constraint.add(new Pair(this.getType(),fieldAssumption.getType())); + constraint.add(ConstraintsFactory.createPair( + receiver.getType(),fieldAssumption.getReceiverType(), info)); + constraint.add(ConstraintsFactory.createPair( + this.getType(),fieldAssumption.getType(), info)); oderConstraints.add(constraint); } if(oderConstraints.size() == 0) diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java index 086427d9..8e8791ab 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java @@ -15,7 +15,7 @@ public class ForStmt extends Statement private Expression head_Initializer; private Expression head_Condition; private Expression head_Loop_expr; - private Statement body_Loop_block; + public Block body_Loop_block; public ForStmt(int offset, int variableLength) { diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java index d253172f..ec4145b9 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java @@ -14,8 +14,8 @@ public class IfStmt extends Statement public boolean hamaDebug = true; //hama: Debug Ausgaben von mir ein- bzw. ausschalten public Expression expr; - public Statement then_block; - public Statement else_block; + public Block then_block; + public Block else_block; @Override public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java b/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java index 9207e071..49031201 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java @@ -4,8 +4,9 @@ import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory; import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import org.antlr.v4.runtime.Token; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; @@ -56,11 +57,11 @@ public class MethodCall extends Statement Set methodConstraints = new HashSet<>(); for(MethodAssumption m : info.getMethods(name, arglist)){ Constraint methodConstraint = new Constraint(); - methodConstraint.add(new Pair(receiver.getType(), m.getReceiverType())); - methodConstraint.add(new Pair(m.getReturnType(), this.getType())); + methodConstraint.add(ConstraintsFactory.createPair(receiver.getType(), m.getReceiverType(), PairOperator.SMALLERDOT, info)); + methodConstraint.add(ConstraintsFactory.createPair(m.getReturnType(), this.getType(), PairOperator.SMALLERDOT, info)); for(int i = 0;iAuthor: Martin Pl�micke diff --git a/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java b/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java index 746dac84..8d36c0bb 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java @@ -12,5 +12,10 @@ public class GenericRefType extends RefTypeOrTPHOrWildcardOrGeneric super(offset); this.name = name; } + + public GenericTypeName getName(){ + return name; + } + } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 4d7f1692..40c92ebb 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -32,6 +32,11 @@ public class RefType extends RefTypeOrTPHOrWildcardOrGeneric this.name = (fullyQualifiedName); } + @Override + public String toString(){ + return this.name.toString(); + } + @Override public int hashCode() { int hash = 0; diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefTypeOrTPHOrWildcardOrGeneric.java b/src/de/dhbwstuttgart/syntaxtree/type/RefTypeOrTPHOrWildcardOrGeneric.java old mode 100755 new mode 100644 index 058fb944..79813f56 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefTypeOrTPHOrWildcardOrGeneric.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefTypeOrTPHOrWildcardOrGeneric.java @@ -1,16 +1,10 @@ package de.dhbwstuttgart.syntaxtree.type; -import java.util.ArrayList; -import java.util.List; -import de.dhbwstuttgart.typecheck.JavaClassName; import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; import org.antlr.v4.runtime.Token; - -public abstract class RefTypeOrTPHOrWildcardOrGeneric extends SyntaxTreeNode -{ +public class RefTypeOrTPHOrWildcardOrGeneric extends SyntaxTreeNode{ public RefTypeOrTPHOrWildcardOrGeneric(Token offset) { super(offset); } } - diff --git a/src/de/dhbwstuttgart/typecheck/GenericTypeName.java b/src/de/dhbwstuttgart/typecheck/GenericTypeName.java index 4191bdd5..d6321fa7 100644 --- a/src/de/dhbwstuttgart/typecheck/GenericTypeName.java +++ b/src/de/dhbwstuttgart/typecheck/GenericTypeName.java @@ -1,9 +1,22 @@ package de.dhbwstuttgart.typecheck; -import de.dhbwstuttgart.syntaxtree.GTVDeclarationContext; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext; public class GenericTypeName extends JavaClassName { - public GenericTypeName(String name) { + private final static String DELIMITER = "%"; + + private final JavaClassName parentClass; + private final String methodName; + + public GenericTypeName(GenericContext parentClass, String name) { super(name); + this.parentClass = parentClass.parentClass; + this.methodName = parentClass.parentMethod; + } + + public String toString(){ + return parentClass.toString() + + DELIMITER + methodName + + DELIMITER + super.toString(); } } diff --git a/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java b/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java index e31d2ec2..383b3509 100644 --- a/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java +++ b/src/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.typeinference.assumptions; +import com.sun.istack.internal.NotNull; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.FormalParameter; import de.dhbwstuttgart.syntaxtree.Method; @@ -10,7 +11,7 @@ public class TypeInferenceBlockInformation extends TypeInferenceInformation { private Method methodContext; private ClassOrInterface currentClass; - public TypeInferenceBlockInformation(Set availableClasses, ClassOrInterface currentClass, Method methodContext) { + public TypeInferenceBlockInformation(Set availableClasses, @NotNull ClassOrInterface currentClass, Method methodContext) { super(availableClasses); this.methodContext = methodContext; this.currentClass = currentClass; @@ -19,4 +20,7 @@ public class TypeInferenceBlockInformation extends TypeInferenceInformation { public ClassOrInterface getCurrentClass() { return currentClass; } + public Method getCurrentMethod() { + return methodContext; + } } diff --git a/src/de/dhbwstuttgart/typeinference/constraints/ConstraintsFactory.java b/src/de/dhbwstuttgart/typeinference/constraints/ConstraintsFactory.java new file mode 100644 index 00000000..6a13da40 --- /dev/null +++ b/src/de/dhbwstuttgart/typeinference/constraints/ConstraintsFactory.java @@ -0,0 +1,47 @@ +package de.dhbwstuttgart.typeinference.constraints; + +import de.dhbwstuttgart.syntaxtree.GenericTypeVar; +import de.dhbwstuttgart.syntaxtree.type.GenericRefType; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class ConstraintsFactory { + public static Pair createPair(RefTypeOrTPHOrWildcardOrGeneric t1, RefTypeOrTPHOrWildcardOrGeneric t2, PairOperator equalsdot, TypeInferenceBlockInformation info){ + //Check whether Generics are in the same class: + return new Pair(checkGeneric(t1, info), checkGeneric(t2, info)); + } + public static Pair createPair(RefTypeOrTPHOrWildcardOrGeneric t1, + RefTypeOrTPHOrWildcardOrGeneric t2, TypeInferenceBlockInformation info){ + return createPair(t1,t2,PairOperator.SMALLERDOT, info); + } + + private static RefTypeOrTPHOrWildcardOrGeneric checkGeneric(RefTypeOrTPHOrWildcardOrGeneric type, TypeInferenceBlockInformation info){ + if(type instanceof GenericRefType){ + List genericsInContext = new ArrayList<>(); + for(GenericTypeVar genericTypeVar : info.getCurrentClass().getGenerics()){ + genericsInContext.add(genericTypeVar); + } + if(info.getCurrentMethod() != null)for(GenericTypeVar genericTypeVar : info.getCurrentMethod().getGenerics()){ + genericsInContext.add(genericTypeVar); + } + + for(GenericTypeVar genericTypeVar : genericsInContext){ + if(genericTypeVar.getName().equals(((GenericRefType)type).getName())){ + return new RefType(((GenericRefType)type).getName(),type.getOffset()); + } + } + //Nicht in den Generics in diesem Kontext enthalten: + return TypePlaceholder.fresh(type.getOffset()); + }else{ + return type; + } + } + +} diff --git a/src/de/dhbwstuttgart/typeinference/constraints/Pair.java b/src/de/dhbwstuttgart/typeinference/constraints/Pair.java old mode 100755 new mode 100644 index 08611bb3..72f98326 --- a/src/de/dhbwstuttgart/typeinference/constraints/Pair.java +++ b/src/de/dhbwstuttgart/typeinference/constraints/Pair.java @@ -4,6 +4,7 @@ import java.io.Serializable; import com.sun.istack.internal.NotNull; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; public class Pair implements Serializable @@ -13,7 +14,7 @@ public class Pair implements Serializable private PairOperator eOperator = PairOperator.SMALLER; - public Pair(@NotNull RefTypeOrTPHOrWildcardOrGeneric TA1, @NotNull RefTypeOrTPHOrWildcardOrGeneric TA2 ) + Pair(@NotNull RefTypeOrTPHOrWildcardOrGeneric TA1, @NotNull RefTypeOrTPHOrWildcardOrGeneric TA2 ) { this.TA1 = TA1; this.TA2 = TA2; @@ -22,7 +23,7 @@ public class Pair implements Serializable eOperator = PairOperator.SMALLER; } - public Pair(RefTypeOrTPHOrWildcardOrGeneric TA1, RefTypeOrTPHOrWildcardOrGeneric TA2, PairOperator eOp) + Pair(@NotNull RefTypeOrTPHOrWildcardOrGeneric TA1, @NotNull RefTypeOrTPHOrWildcardOrGeneric TA2, PairOperator eOp) { // Konstruktor this(TA1,TA2); diff --git a/test/parser/GeneralParserTest.java b/test/parser/GeneralParserTest.java index c26640b3..9cb20ac6 100644 --- a/test/parser/GeneralParserTest.java +++ b/test/parser/GeneralParserTest.java @@ -27,9 +27,11 @@ public class GeneralParserTest{ List filenames = new ArrayList(); filenames.add("FieldInitializationTest.jav"); filenames.add("ImportTest.jav"); + filenames.add("CastTest.jav"); filenames.add("StatementsTest.jav"); filenames.add("Methods.jav"); filenames.add("ImportTestGeneric.jav"); + filenames.add("CastTest.jav"); //filenames.add("BoundedParameter.jav"); //filenames.add("GenericFieldVarTest.jav"); filenames.add("FieldVarTest.jav"); diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index 1de2cc87..8760e321 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -16,7 +16,7 @@ public class JavaTXCompilerTest { @Test public void test() throws IOException, ClassNotFoundException { JavaTXCompiler compiler = new JavaTXCompiler(); - compiler.parse(new File(rootDirectory+"Methods.jav")); + //compiler.parse(new File(rootDirectory+"Methods.jav")); compiler.parse(new File(rootDirectory+"Generics.jav")); compiler.typeInference(); }